Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 1 | /** |
| 2 | * The plugin system is based |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 3 | * on registered services (iframes) from |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 4 | * foreign services. |
| 5 | * The server component spawns new iframes and |
| 6 | * listens to them. |
| 7 | * |
| 8 | * @author Nils Diewald |
| 9 | */ |
| 10 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 11 | define(["plugin/widget", 'plugin/service', 'state', "util"], function (widgetClass, serviceClass, stateClass) { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 12 | "use strict"; |
| 13 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 14 | KorAP.Panel = KorAP.Panel || {}; |
| 15 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 16 | // Contains all servicess to address with |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 17 | // messages to them |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 18 | var services = {}; |
| 19 | var plugins = {}; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 20 | |
| 21 | // TODO: |
| 22 | // These should better be panels and every panel |
| 23 | // has a buttonGroup |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 24 | |
| 25 | // List of panels with dynamic buttons, i.e. |
| 26 | // panels that may occur multiple times. |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 27 | var buttons = { |
| 28 | match : [] |
| 29 | }; |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 30 | |
| 31 | // List of panels with static buttons, i.e. |
| 32 | // panels that occur only once. |
| 33 | var buttonsSingle = { |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 34 | query : [], |
| 35 | result : [] |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 36 | } |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 37 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 38 | // This is a counter to limit acceptable incoming messages |
| 39 | // to a certain amount. For every message, this counter will |
| 40 | // be decreased (down to 0), for every second this will be |
| 41 | // increased (up to 100). |
| 42 | // Once a widget surpasses the limit, it will be killed |
| 43 | // and called suspicious. |
| 44 | var maxMessages = 100; |
| 45 | var limits = {}; |
| 46 | |
| 47 | // TODO: |
| 48 | // It may be useful to establish a watcher that pings |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 49 | // all widgets every second to see if it is still alive, |
| 50 | // otherwise kill |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 51 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 52 | // Load Plugin server |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 53 | return { |
| 54 | |
| 55 | /** |
| 56 | * Create new plugin management system |
| 57 | */ |
| 58 | create : function () { |
| 59 | return Object.create(this)._init(); |
| 60 | }, |
| 61 | |
| 62 | /* |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 63 | * Initialize the plugin manager |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 64 | */ |
| 65 | _init : function () { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 66 | return this; |
| 67 | }, |
| 68 | |
| 69 | /** |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 70 | * Register a plugin described as a JSON object. |
| 71 | * |
| 72 | * This is work in progress. |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 73 | * |
| 74 | * Example: |
| 75 | * |
| 76 | * KorAP.Plugin.register({ |
| 77 | * 'name' : 'CatContent', |
| 78 | * 'desc' : 'Some content about cats', |
| 79 | * 'about' : 'https://localhost:5678/', |
| 80 | * 'embed' : [{ |
| 81 | * 'panel' : 'match', |
| 82 | * 'title' : loc.TRANSLATE, |
| 83 | * 'classes' : ['translate'] |
| 84 | * 'onClick' : { |
| 85 | * 'action' : 'addWidget', |
| 86 | * 'template' : 'https://localhost:5678/?match={matchid}', |
| 87 | * } |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 88 | * },{ |
| 89 | * 'title' : 'glemm', |
| 90 | * 'panel' : 'query', |
| 91 | * 'onClick' : { |
| 92 | * 'action' : 'toggle', |
| 93 | * 'state' : 'glemm', |
| 94 | * 'service' : { |
| 95 | * 'id' : 'glemm', |
| 96 | * 'template' : 'https://localhost:5678/' |
| 97 | * } |
| 98 | * } |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 99 | * }] |
| 100 | * }); |
| 101 | * |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 102 | */ |
| 103 | register : function (obj) { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 104 | // TODO: |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 105 | // These fields need to be localized for display by a structure like |
| 106 | // { de : { name : '..' }, en : { .. } } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 107 | var name = obj["name"]; |
| 108 | |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 109 | if (!name) |
| 110 | throw new Error("Missing name of plugin"); |
| 111 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 112 | // Register plugin by name |
| 113 | var plugin = plugins[name] = { |
| 114 | name : name, |
| 115 | desc : obj["desc"], |
| 116 | about : obj["about"], |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 117 | widgets : [], |
| 118 | services : [] |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 119 | }; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 120 | |
| 121 | if (typeof obj["embed"] !== 'object') |
| 122 | throw new Error("Embedding of plugin is no list"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 123 | |
| 124 | // Embed all embeddings of the plugin |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 125 | var that = this; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 126 | for (let i in obj["embed"]) { |
| 127 | let embed = obj["embed"][i]; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 128 | |
| 129 | if (typeof embed !== 'object') |
| 130 | throw new Error("Embedding of plugin is no object"); |
| 131 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 132 | // Needs to be localized as well |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 133 | let title = embed["title"]; |
| 134 | let panel = embed["panel"]; |
| 135 | let onClick = embed["onClick"]; |
| 136 | |
| 137 | |
| 138 | if (!panel || !(buttons[panel] || buttonsSingle[panel])) |
| 139 | throw new Error("Panel for plugin is invalid"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 140 | |
| 141 | // The embedding will open a widget |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 142 | if (!onClick["action"] || onClick["action"] == "addWidget") { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 143 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 144 | |
| 145 | let cb = function (e) { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 146 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 147 | // "this" is bind to the panel |
| 148 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 149 | // Get the URL of the widget |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 150 | let url = onClick["template"]; |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 151 | // that._interpolateURI(onClick["template"], this.match); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 152 | |
| 153 | // Add the widget to the panel |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 154 | let id = that.addWidget(this, name, url); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 155 | plugin["widgets"].push(id); |
| 156 | }; |
| 157 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 158 | // TODO: |
| 159 | // Create button class to be stored and loaded in button groups! |
| 160 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 161 | // Add to dynamic button list (e.g. for matches) |
| 162 | if (buttons[panel]) { |
| 163 | buttons[panel].push([title, embed["classes"], cb]); |
| 164 | } |
| 165 | |
| 166 | // Add to static button list (e.g. for query) already loaded |
| 167 | else if (KorAP.Panel[panel]) { |
| 168 | KorAP.Panel[panel].actions.add(title, embed["classes"], cb); |
| 169 | } |
| 170 | |
| 171 | // Add to static button list (e.g. for query) not yet loaded |
| 172 | else { |
| 173 | buttonsSingle[panel].push([title, embed["classes"], cb]); |
| 174 | } |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | else if (onClick["action"] == "toggle") { |
| 178 | |
| 179 | // Todo: Initially false |
| 180 | let state = stateClass.create(false); |
| 181 | |
| 182 | // TODO: |
| 183 | // Lazy registration (see above!) |
Akron | 16bd164 | 2020-06-25 08:30:28 +0200 | [diff] [blame] | 184 | KorAP.Panel[panel].actions.addToggle(title, ["title"], state); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 185 | |
| 186 | // Get the URL of the service |
| 187 | |
| 188 | // TODO: |
| 189 | // Use the "service" keyword |
| 190 | let url = onClick["template"]; |
| 191 | |
| 192 | // Add the service |
| 193 | let id = this.addService(name, url); |
| 194 | |
| 195 | // TODO: |
| 196 | // This is a bit stupid to get the service window |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 197 | let service = services[id]; |
| 198 | let iframe = service.load(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 199 | |
| 200 | // Create object to communicate the toggle state |
| 201 | // once the iframe is loaded. |
| 202 | iframe.onload = function () { |
| 203 | let sendToggle = { |
| 204 | setState : function (val) { |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 205 | service.sendMsg({ |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 206 | action: 'state', |
| 207 | key : onClick['state'], |
| 208 | value : val |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 209 | }); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | |
| 213 | // Associate object with the state |
| 214 | state.associate(sendToggle); |
| 215 | }; |
| 216 | |
| 217 | plugin["services"].push(id); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 218 | }; |
| 219 | }; |
| 220 | }, |
| 221 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 222 | // TODO: |
| 223 | // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570 |
| 224 | // but as simple as possible |
| 225 | _interpolateURI : function (uri, obj) { |
| 226 | // ... |
| 227 | }, |
| 228 | |
| 229 | |
| 230 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 231 | * Get named button group - better rename to "action" |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 232 | */ |
| 233 | buttonGroup : function (name) { |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 234 | if (buttons[name] != undefined) { |
| 235 | return buttons[name]; |
| 236 | } else if (buttonsSingle[name] != undefined) { |
| 237 | return buttonsSingle[name]; |
| 238 | }; |
| 239 | return []; |
| 240 | }, |
| 241 | |
| 242 | /** |
| 243 | * Clear named button group - better rename to "action" |
| 244 | */ |
| 245 | clearButtonGroup : function (name) { |
| 246 | if (buttons[name] != undefined) { |
| 247 | buttons[name] = []; |
| 248 | } else if (buttonsSingle[name] != undefined) { |
| 249 | buttonsSingle[name] = []; |
| 250 | } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 251 | }, |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 252 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 253 | // Optionally initialize the service mechanism and get an ID |
| 254 | _getServiceID : function () { |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 255 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 256 | // Is it the first service? |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 257 | if (!this._listener) { |
| 258 | |
| 259 | /* |
| 260 | * Establish the global 'message' hook. |
| 261 | */ |
| 262 | this._listener = this._receiveMsg.bind(this); |
| 263 | window.addEventListener("message", this._listener); |
| 264 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 265 | // Every second increase the limits of all registered services |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 266 | this._timer = window.setInterval(function () { |
| 267 | for (var i in limits) { |
| 268 | if (limits[i]++ >= maxMessages) { |
| 269 | limits[i] = maxMessages; |
| 270 | } |
| 271 | } |
| 272 | }, 1000); |
| 273 | }; |
| 274 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 275 | // Create a unique random ID per service |
| 276 | return 'id-' + this._randomID(); |
| 277 | }, |
| 278 | |
| 279 | /** |
| 280 | * Add a service in a certain panel and return the id. |
| 281 | */ |
| 282 | addService : function (name, src) { |
| 283 | if (!src) |
| 284 | return; |
| 285 | |
| 286 | let id = this._getServiceID(); |
| 287 | |
| 288 | // Create a new service |
| 289 | let service = serviceClass.create(name, src, id); |
| 290 | |
| 291 | // TODO! |
| 292 | // Store the service based on the identifier |
| 293 | services[id] = service; |
| 294 | limits[id] = maxMessages; |
| 295 | |
| 296 | // widget._mgr = this; |
| 297 | |
| 298 | // Add service to panel |
| 299 | this.element().appendChild( |
| 300 | service.load() |
| 301 | ); |
| 302 | |
| 303 | return id; |
| 304 | }, |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Open a new widget view in a certain panel and return |
| 309 | * the id. |
| 310 | */ |
| 311 | addWidget : function (panel, name, src) { |
| 312 | |
| 313 | let id = this._getServiceID(); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 314 | |
| 315 | // Create a new widget |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 316 | var widget = widgetClass.create(name, src, id); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 317 | |
| 318 | // Store the widget based on the identifier |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 319 | services[id] = widget; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 320 | limits[id] = maxMessages; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 321 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 322 | widget._mgr = this; |
| 323 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 324 | // Add widget to panel |
| 325 | panel.add(widget); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 326 | |
| 327 | return id; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 328 | }, |
| 329 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 330 | |
| 331 | /** |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 332 | * Get service by identifier |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 333 | */ |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 334 | service : function (id) { |
| 335 | return services[id]; |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 336 | }, |
| 337 | |
| 338 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 339 | // Receive a call from an embedded service. |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 340 | // The handling needs to be very careful, |
| 341 | // as this can easily become a security nightmare. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 342 | _receiveMsg : function (e) { |
| 343 | // Get event data |
| 344 | var d = e.data; |
| 345 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 346 | // If no data given - fail |
| 347 | // (probably check that it's an assoc array) |
| 348 | if (!d) |
| 349 | return; |
| 350 | |
| 351 | // e.origin is probably set and okay - CHECK! |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 352 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 353 | // Get origin ID |
| 354 | var id = d["originID"]; |
| 355 | |
| 356 | // If no origin ID given - fail |
| 357 | if (!id) |
| 358 | return; |
| 359 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 360 | // Get the service |
| 361 | let service = services[id]; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 362 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 363 | // If the addressed service does not exist - fail |
| 364 | if (!service) |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 365 | return; |
| 366 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 367 | // Check for message limits |
| 368 | if (limits[id]-- < 0) { |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 369 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 370 | // Kill service |
| 371 | KorAP.log(0, 'Suspicious action by service', service.src); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 372 | |
| 373 | // TODO: |
| 374 | // Potentially kill the whole plugin! |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 375 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 376 | // This removes all connections before closing the service |
| 377 | this._closeService(service.id); |
| 378 | |
| 379 | // if (service.isWidget) |
| 380 | service.close(); |
| 381 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 382 | return; |
| 383 | }; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 384 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 385 | // Resize the iframe |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 386 | switch (d.action) { |
| 387 | case 'resize': |
| 388 | if (service.isWidget) |
| 389 | service.resize(d); |
| 390 | break; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 391 | |
| 392 | // Log message from iframe |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 393 | case 'log': |
| 394 | KorAP.log(d.code, d.msg, service.src); |
| 395 | break; |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 396 | |
| 397 | // Modify pipes |
| 398 | case 'pipe': |
| 399 | if (KorAP.Pipe != undefined) { |
| 400 | if (d.job == 'del') { |
| 401 | KorAP.Pipe.remove(d.service); |
| 402 | } else { |
| 403 | KorAP.Pipe.append(d.service); |
| 404 | }; |
| 405 | }; |
| 406 | break; |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 407 | |
| 408 | // Get information from the embedding platform |
| 409 | case 'get': |
| 410 | if (d.key == 'KQ') { |
| 411 | if (KorAP.koralQuery !== undefined) { |
| 412 | d["value"] = KorAP.koralQuery; |
| 413 | }; |
| 414 | }; |
| 415 | }; |
| 416 | |
| 417 | // data needs to be mirrored |
| 418 | if (d._id) { |
| 419 | service.sendMsg(d); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | // TODO: |
| 423 | // Close |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 424 | }, |
| 425 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 426 | // Close the service |
| 427 | _closeService : function (id) { |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 428 | delete limits[id]; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 429 | |
| 430 | // Close the iframe |
| 431 | if (services[id] && services[id]._closeIframe) { |
| 432 | services[id]._closeIframe(); |
| 433 | |
| 434 | // Remove from list |
| 435 | delete services[id]; |
| 436 | }; |
| 437 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 438 | |
| 439 | // Remove listeners in case no widget |
| 440 | // is available any longer |
| 441 | if (Object.keys(limits).length == 0) |
| 442 | this._removeListener(); |
| 443 | }, |
| 444 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 445 | // Get a random identifier |
| 446 | _randomID : function () { |
| 447 | return randomID(20); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 448 | }, |
| 449 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 450 | // Remove the listener |
| 451 | _removeListener : function () { |
| 452 | window.clearInterval(this._timer); |
| 453 | this._timer = undefined; |
| 454 | window.removeEventListener("message", this._listener); |
| 455 | this._listener = undefined; |
| 456 | }, |
| 457 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 458 | /** |
| 459 | * Return the service element. |
| 460 | */ |
| 461 | element : function () { |
| 462 | if (!this._element) { |
| 463 | this._element = document.createElement('div'); |
| 464 | this._element.setAttribute("id", "services"); |
| 465 | } |
| 466 | return this._element; |
| 467 | }, |
| 468 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 469 | // Destructor, just for testing scenarios |
| 470 | destroy : function () { |
| 471 | limits = {}; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 472 | for (let s in services) { |
| 473 | services[s].close(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 474 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 475 | services = {}; |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 476 | for (let b in buttons) { |
| 477 | buttons[b] = []; |
| 478 | }; |
| 479 | for (let b in buttonsSingle) { |
| 480 | buttonsSingle[b] = []; |
| 481 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 482 | |
| 483 | if (this._element) { |
| 484 | let e = this._element; |
| 485 | if (e.parentNode) { |
| 486 | e.parentNode.removeChild(e); |
| 487 | }; |
| 488 | this._element = null; |
| 489 | }; |
| 490 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 491 | this._removeListener(); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 492 | } |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 493 | }; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 494 | }); |