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