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"]; |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 136 | let icon = embed["icon"]; |
| 137 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 138 | if (!panel || !(buttons[panel] || buttonsSingle[panel])) |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 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 | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 142 | if (!onClick["action"] || |
| 143 | onClick["action"] == "addWidget" || |
| 144 | onClick["action"] == "setWidget") { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 145 | |
| 146 | let cb = function (e) { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 147 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 148 | // "this" is bind to the panel |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 149 | // "this".button is the button |
| 150 | // "that" is the server object |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 151 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 152 | // Get the URL of the widget |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 153 | let url = onClick["template"]; |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 154 | // that._interpolateURI(onClick["template"], this.match); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 155 | |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 156 | // The button has a state and the state is associated to the |
| 157 | // a intermediate object to toggle the view |
| 158 | if ('state' in this.button && this.button.state.associates() > 0) { |
| 159 | |
| 160 | // TODO: |
| 161 | // Use roll() when existing |
| 162 | let s = this.button.state; |
| 163 | if (s.get()) { |
| 164 | s.set(false); |
| 165 | } else { |
| 166 | s.set(true); |
| 167 | }; |
| 168 | return; |
| 169 | }; |
| 170 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 171 | // Add the widget to the panel |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 172 | let id = that.addWidget(this, name, url); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 173 | plugin["widgets"].push(id); |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 174 | |
| 175 | // If a state exists, associate with a mediator object |
| 176 | if ('state' in this.button) { |
| 177 | this.button.state.associate({ |
| 178 | setState : function (value) { |
| 179 | // Minimize the widget |
| 180 | if (value == false) { |
| 181 | services[id].minimize(); |
| 182 | } |
| 183 | else { |
| 184 | services[id].show(); |
| 185 | }; |
| 186 | } |
| 187 | }); |
| 188 | } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 189 | }; |
| 190 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 191 | |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 192 | // Button object |
| 193 | let obj = {'cls':embed["classes"], 'icon': icon } |
| 194 | |
| 195 | if (onClick["action"] && onClick["action"] == "setWidget") { |
| 196 | |
| 197 | // Create a boolean state value, that initializes to true == opened |
| 198 | obj['state'] = stateClass.create(true); |
| 199 | }; |
| 200 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 201 | // Add to dynamic button list (e.g. for matches) |
| 202 | if (buttons[panel]) { |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 203 | buttons[panel].push([title, obj, cb]); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Add to static button list (e.g. for query) already loaded |
| 207 | else if (KorAP.Panel[panel]) { |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 208 | KorAP.Panel[panel].actions.add(title, obj, cb); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Add to static button list (e.g. for query) not yet loaded |
| 212 | else { |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 213 | buttonsSingle[panel].push([title, obj, cb]); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 214 | } |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 215 | } |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 216 | |
| 217 | // TODO There is no possibility to add icons to an plugin toggle button right now. |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 218 | else if (onClick["action"] == "toggle") { |
| 219 | |
| 220 | // Todo: Initially false |
| 221 | let state = stateClass.create(false); |
| 222 | |
| 223 | // TODO: |
| 224 | // Lazy registration (see above!) |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 225 | KorAP.Panel[panel].actions.addToggle(title, {'cls':["title"]}, state); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 226 | |
| 227 | // Get the URL of the service |
| 228 | |
| 229 | // TODO: |
| 230 | // Use the "service" keyword |
| 231 | let url = onClick["template"]; |
| 232 | |
| 233 | // Add the service |
| 234 | let id = this.addService(name, url); |
| 235 | |
| 236 | // TODO: |
| 237 | // This is a bit stupid to get the service window |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 238 | let service = services[id]; |
| 239 | let iframe = service.load(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 240 | |
| 241 | // Create object to communicate the toggle state |
| 242 | // once the iframe is loaded. |
| 243 | iframe.onload = function () { |
| 244 | let sendToggle = { |
| 245 | setState : function (val) { |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 246 | service.sendMsg({ |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 247 | action: 'state', |
| 248 | key : onClick['state'], |
| 249 | value : val |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 250 | }); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 251 | } |
| 252 | }; |
| 253 | |
| 254 | // Associate object with the state |
| 255 | state.associate(sendToggle); |
| 256 | }; |
| 257 | |
| 258 | plugin["services"].push(id); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 259 | }; |
| 260 | }; |
| 261 | }, |
| 262 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 263 | // TODO: |
| 264 | // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570 |
| 265 | // but as simple as possible |
| 266 | _interpolateURI : function (uri, obj) { |
| 267 | // ... |
| 268 | }, |
| 269 | |
| 270 | |
| 271 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 272 | * Get named button group - better rename to "action" |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 273 | */ |
| 274 | buttonGroup : function (name) { |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 275 | if (buttons[name] != undefined) { |
| 276 | return buttons[name]; |
| 277 | } else if (buttonsSingle[name] != undefined) { |
| 278 | return buttonsSingle[name]; |
| 279 | }; |
| 280 | return []; |
| 281 | }, |
| 282 | |
| 283 | /** |
| 284 | * Clear named button group - better rename to "action" |
| 285 | */ |
| 286 | clearButtonGroup : function (name) { |
| 287 | if (buttons[name] != undefined) { |
| 288 | buttons[name] = []; |
| 289 | } else if (buttonsSingle[name] != undefined) { |
| 290 | buttonsSingle[name] = []; |
| 291 | } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 292 | }, |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 293 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 294 | // Optionally initialize the service mechanism and get an ID |
| 295 | _getServiceID : function () { |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 296 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 297 | // Is it the first service? |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 298 | if (!this._listener) { |
| 299 | |
| 300 | /* |
| 301 | * Establish the global 'message' hook. |
| 302 | */ |
| 303 | this._listener = this._receiveMsg.bind(this); |
| 304 | window.addEventListener("message", this._listener); |
| 305 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 306 | // Every second increase the limits of all registered services |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 307 | this._timer = window.setInterval(function () { |
| 308 | for (var i in limits) { |
| 309 | if (limits[i]++ >= maxMessages) { |
| 310 | limits[i] = maxMessages; |
| 311 | } |
| 312 | } |
| 313 | }, 1000); |
| 314 | }; |
| 315 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 316 | // Create a unique random ID per service |
| 317 | return 'id-' + this._randomID(); |
| 318 | }, |
| 319 | |
| 320 | /** |
| 321 | * Add a service in a certain panel and return the id. |
| 322 | */ |
| 323 | addService : function (name, src) { |
| 324 | if (!src) |
| 325 | return; |
| 326 | |
| 327 | let id = this._getServiceID(); |
| 328 | |
| 329 | // Create a new service |
| 330 | let service = serviceClass.create(name, src, id); |
| 331 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 332 | services[id] = service; |
| 333 | limits[id] = maxMessages; |
| 334 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 335 | // Add service to panel |
| 336 | this.element().appendChild( |
| 337 | service.load() |
| 338 | ); |
| 339 | |
| 340 | return id; |
| 341 | }, |
| 342 | |
| 343 | |
| 344 | /** |
| 345 | * Open a new widget view in a certain panel and return |
| 346 | * the id. |
| 347 | */ |
| 348 | addWidget : function (panel, name, src) { |
| 349 | |
| 350 | let id = this._getServiceID(); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 351 | |
| 352 | // Create a new widget |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 353 | var widget = widgetClass.create(name, src, id); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 354 | |
| 355 | // Store the widget based on the identifier |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 356 | services[id] = widget; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 357 | limits[id] = maxMessages; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 358 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 359 | widget._mgr = this; |
| 360 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 361 | // Add widget to panel |
| 362 | panel.add(widget); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 363 | |
| 364 | return id; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 365 | }, |
| 366 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 367 | |
| 368 | /** |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 369 | * Get service by identifier |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 370 | */ |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 371 | service : function (id) { |
| 372 | return services[id]; |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 373 | }, |
| 374 | |
| 375 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 376 | // Receive a call from an embedded service. |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 377 | // The handling needs to be very careful, |
| 378 | // as this can easily become a security nightmare. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 379 | _receiveMsg : function (e) { |
| 380 | // Get event data |
| 381 | var d = e.data; |
| 382 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 383 | // If no data given - fail |
| 384 | // (probably check that it's an assoc array) |
| 385 | if (!d) |
| 386 | return; |
| 387 | |
| 388 | // e.origin is probably set and okay - CHECK! |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 389 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 390 | // Get origin ID |
| 391 | var id = d["originID"]; |
| 392 | |
| 393 | // If no origin ID given - fail |
| 394 | if (!id) |
| 395 | return; |
| 396 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 397 | // Get the service |
| 398 | let service = services[id]; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 399 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 400 | // If the addressed service does not exist - fail |
| 401 | if (!service) |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 402 | return; |
| 403 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 404 | // Check for message limits |
| 405 | if (limits[id]-- < 0) { |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 406 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 407 | // Kill service |
| 408 | KorAP.log(0, 'Suspicious action by service', service.src); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 409 | |
| 410 | // TODO: |
| 411 | // Potentially kill the whole plugin! |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 412 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 413 | // This removes all connections before closing the service |
| 414 | this._closeService(service.id); |
| 415 | |
| 416 | // if (service.isWidget) |
| 417 | service.close(); |
| 418 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 419 | return; |
| 420 | }; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 421 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 422 | // Resize the iframe |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 423 | switch (d.action) { |
| 424 | case 'resize': |
| 425 | if (service.isWidget) |
| 426 | service.resize(d); |
| 427 | break; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 428 | |
| 429 | // Log message from iframe |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 430 | case 'log': |
| 431 | KorAP.log(d.code, d.msg, service.src); |
| 432 | break; |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 433 | |
| 434 | // Modify pipes |
| 435 | case 'pipe': |
| 436 | if (KorAP.Pipe != undefined) { |
| 437 | if (d.job == 'del') { |
| 438 | KorAP.Pipe.remove(d.service); |
| 439 | } else { |
| 440 | KorAP.Pipe.append(d.service); |
| 441 | }; |
| 442 | }; |
| 443 | break; |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 444 | |
| 445 | // Get information from the embedding platform |
| 446 | case 'get': |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 447 | |
| 448 | // Get KoralQuery |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 449 | if (d.key == 'KQ') { |
| 450 | if (KorAP.koralQuery !== undefined) { |
| 451 | d["value"] = KorAP.koralQuery; |
| 452 | }; |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Akron | 432972b | 2020-09-18 17:05:53 +0200 | [diff] [blame] | 455 | // Get Query information from from |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 456 | else if (d.key == 'QueryForm') { |
| 457 | let doc = document; |
| 458 | let v = d["value"] = {}; |
| 459 | |
| 460 | var el; |
| 461 | if (el = doc.getElementById('q-field')) { |
| 462 | v["q"] = el.value; |
| 463 | }; |
| 464 | if (el = doc.getElementById('ql-field')) { |
| 465 | v["ql"] = el.value; |
| 466 | }; |
| 467 | if (el = KorAP.vc) { |
| 468 | v["cq"] = el.toQuery(); |
| 469 | }; |
Akron | 432972b | 2020-09-18 17:05:53 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // Get Query information from parameters |
| 473 | else if (d.key == 'QueryParam') { |
| 474 | |
| 475 | // Supported in all modern browsers |
| 476 | var p = new URLSearchParams(window.location.search); |
| 477 | let v = d["value"] = {}; |
| 478 | v["q"] = p.get('q'); |
| 479 | v["ql"] = p.get('ql'); |
| 480 | v["cq"] = p.get('cq'); |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 481 | }; |
| 482 | }; |
| 483 | |
| 484 | // data needs to be mirrored |
| 485 | if (d._id) { |
| 486 | service.sendMsg(d); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | // TODO: |
| 490 | // Close |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 491 | }, |
| 492 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 493 | // Close the service |
| 494 | _closeService : function (id) { |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 495 | delete limits[id]; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 496 | |
| 497 | // Close the iframe |
| 498 | if (services[id] && services[id]._closeIframe) { |
| 499 | services[id]._closeIframe(); |
| 500 | |
| 501 | // Remove from list |
| 502 | delete services[id]; |
| 503 | }; |
| 504 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 505 | |
| 506 | // Remove listeners in case no widget |
| 507 | // is available any longer |
| 508 | if (Object.keys(limits).length == 0) |
| 509 | this._removeListener(); |
| 510 | }, |
| 511 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 512 | // Get a random identifier |
| 513 | _randomID : function () { |
| 514 | return randomID(20); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 515 | }, |
| 516 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 517 | // Remove the listener |
| 518 | _removeListener : function () { |
| 519 | window.clearInterval(this._timer); |
| 520 | this._timer = undefined; |
| 521 | window.removeEventListener("message", this._listener); |
| 522 | this._listener = undefined; |
| 523 | }, |
| 524 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 525 | /** |
| 526 | * Return the service element. |
| 527 | */ |
| 528 | element : function () { |
| 529 | if (!this._element) { |
| 530 | this._element = document.createElement('div'); |
| 531 | this._element.setAttribute("id", "services"); |
| 532 | } |
| 533 | return this._element; |
| 534 | }, |
| 535 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 536 | // Destructor, just for testing scenarios |
| 537 | destroy : function () { |
| 538 | limits = {}; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 539 | for (let s in services) { |
| 540 | services[s].close(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 541 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 542 | services = {}; |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 543 | for (let b in buttons) { |
| 544 | buttons[b] = []; |
| 545 | }; |
| 546 | for (let b in buttonsSingle) { |
| 547 | buttonsSingle[b] = []; |
| 548 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 549 | |
| 550 | if (this._element) { |
| 551 | let e = this._element; |
| 552 | if (e.parentNode) { |
| 553 | e.parentNode.removeChild(e); |
| 554 | }; |
| 555 | this._element = null; |
| 556 | }; |
| 557 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 558 | this._removeListener(); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 559 | } |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 560 | }; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 561 | }); |