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