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