| 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 | */ |
| Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 10 | "use strict"; |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 11 | |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 12 | define(['plugin/widget', 'plugin/service', 'state', 'state/manager', 'pageInfo', 'util'], function (widgetClass, serviceClass, stateClass, stateManagerClass, pageInfoClass) { |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 13 | |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 14 | KorAP.Panel = KorAP.Panel || {}; |
| 15 | |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 16 | const d = document; |
| Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame] | 17 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 18 | // Contains all servicess to address with |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 19 | // messages to them |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 20 | var services = {}; |
| 21 | var plugins = {}; |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 22 | |
| 23 | // TODO: |
| 24 | // These should better be panels and every panel |
| 25 | // has a buttonGroup |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 26 | |
| 27 | // List of panels with dynamic buttons, i.e. |
| 28 | // panels that may occur multiple times. |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 29 | var buttons = { |
| 30 | match : [] |
| 31 | }; |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 32 | |
| 33 | // List of panels with static buttons, i.e. |
| 34 | // panels that occur only once. |
| 35 | var buttonsSingle = { |
| hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 36 | query : [], |
| 37 | result : [] |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 38 | } |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 39 | |
| Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 40 | // This is a counter to limit acceptable incoming messages |
| 41 | // to a certain amount. For every message, this counter will |
| 42 | // be decreased (down to 0), for every second this will be |
| 43 | // increased (up to 100). |
| 44 | // Once a widget surpasses the limit, it will be killed |
| 45 | // and called suspicious. |
| 46 | var maxMessages = 100; |
| 47 | var limits = {}; |
| 48 | |
| 49 | // TODO: |
| 50 | // It may be useful to establish a watcher that pings |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 51 | // all widgets every second to see if it is still alive, |
| 52 | // otherwise kill |
| Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 53 | |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 54 | // Load Plugin server |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 55 | return { |
| 56 | |
| 57 | /** |
| 58 | * Create new plugin management system |
| 59 | */ |
| 60 | create : function () { |
| 61 | return Object.create(this)._init(); |
| 62 | }, |
| 63 | |
| 64 | /* |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 65 | * Initialize the plugin manager |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 66 | */ |
| 67 | _init : function () { |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 68 | this._ql = d.getElementById("ql-field"); |
| 69 | this._q = d.getElementById("q-field") |
| 70 | this._cutoff = d.getElementById("q-cutoff-field"); |
| 71 | |
| Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame] | 72 | // State manager undefined |
| 73 | this._states = KorAP.States ? KorAP.States : |
| 74 | // Not serialized state manager |
| 75 | stateManagerClass.create(document.createElement('input')); |
| 76 | |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 77 | return this; |
| 78 | }, |
| 79 | |
| 80 | /** |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 81 | * Register a plugin described as a JSON object. |
| 82 | * |
| 83 | * This is work in progress. |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 84 | * |
| 85 | * Example: |
| 86 | * |
| 87 | * KorAP.Plugin.register({ |
| 88 | * 'name' : 'CatContent', |
| 89 | * 'desc' : 'Some content about cats', |
| 90 | * 'about' : 'https://localhost:5678/', |
| 91 | * 'embed' : [{ |
| 92 | * 'panel' : 'match', |
| 93 | * 'title' : loc.TRANSLATE, |
| 94 | * 'classes' : ['translate'] |
| 95 | * 'onClick' : { |
| 96 | * 'action' : 'addWidget', |
| 97 | * 'template' : 'https://localhost:5678/?match={matchid}', |
| 98 | * } |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 99 | * },{ |
| 100 | * 'title' : 'glemm', |
| 101 | * 'panel' : 'query', |
| 102 | * 'onClick' : { |
| 103 | * 'action' : 'toggle', |
| 104 | * 'state' : 'glemm', |
| 105 | * 'service' : { |
| 106 | * 'id' : 'glemm', |
| 107 | * 'template' : 'https://localhost:5678/' |
| 108 | * } |
| 109 | * } |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 110 | * }] |
| 111 | * }); |
| 112 | * |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 113 | */ |
| 114 | register : function (obj) { |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 115 | // TODO: |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 116 | // These fields need to be localized for display by a structure like |
| 117 | // { de : { name : '..' }, en : { .. } } |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 118 | const name = obj["name"]; |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 119 | |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 120 | if (!name) |
| 121 | throw new Error("Missing name of plugin"); |
| 122 | |
| Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 123 | var desc = obj["desc"]; |
| 124 | |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 125 | // Register plugin by name |
| 126 | var plugin = plugins[name] = { |
| 127 | name : name, |
| Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 128 | desc : desc, |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 129 | about : obj["about"], |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 130 | widgets : [], |
| 131 | services : [] |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 132 | }; |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 133 | |
| 134 | if (typeof obj["embed"] !== 'object') |
| 135 | throw new Error("Embedding of plugin is no list"); |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 136 | |
| 137 | // Embed all embeddings of the plugin |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 138 | var that = this; |
| Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 139 | obj["embed"].forEach(function(embed) { |
| Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 140 | |
| 141 | if (typeof embed !== 'object') |
| 142 | throw new Error("Embedding of plugin is no object"); |
| 143 | |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 144 | // Needs to be localized as well |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 145 | let title = embed["title"]; |
| 146 | let panel = embed["panel"]; |
| 147 | let onClick = embed["onClick"]; |
| hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame] | 148 | let icon = embed["icon"]; |
| 149 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 150 | if (!panel || !(buttons[panel] || buttonsSingle[panel])) |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 151 | throw new Error("Panel for plugin is invalid"); |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 152 | |
| 153 | // The embedding will open a widget |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 154 | if (!onClick["action"] || |
| 155 | onClick["action"] == "addWidget" || |
| 156 | onClick["action"] == "setWidget") { |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 157 | |
| 158 | let cb = function (e) { |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 159 | |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 160 | // "this" is bind to the panel |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 161 | // "this".button is the button |
| 162 | // "that" is the server object |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 163 | |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 164 | // Active-check click: load iframe as a background service, |
| 165 | // not as a visible widget. |
| 166 | if (this.button._activeClick && 'active' in this.button) { |
| 167 | |
| 168 | // Service already running - just return |
| 169 | // (active.roll() was already called in buttongroup) |
| 170 | if (this.button['widgetID'] && |
| 171 | services[this.button['widgetID']]) { |
| 172 | return; |
| 173 | }; |
| 174 | |
| 175 | // For addWidget mode (no state), just toggle - no service |
| 176 | if (!('state' in this.button)) { |
| 177 | return; |
| 178 | }; |
| 179 | |
| 180 | // Create a background service (iframe in services container) |
| 181 | let id = that.addService({ |
| 182 | "name": name, |
| 183 | "src": onClick["template"], |
| 184 | "permissions": onClick["permissions"] |
| 185 | }); |
| 186 | plugin["widgets"].push(id); |
| 187 | |
| 188 | this.button['widgetID'] = id; |
| 189 | |
| 190 | // Store panel reference so 'get Active' works |
| 191 | services[id].panel = this; |
| 192 | |
| 193 | let activeState = this.button.active; |
| 194 | let iframe = services[id].load(); |
| 195 | |
| 196 | iframe.onload = function () { |
| 197 | activeState.associate({ |
| 198 | setState : function (value) { |
| 199 | if (services[id]) { |
| 200 | services[id].sendMsg({ |
| 201 | action: 'state', |
| 202 | key : 'active', |
| 203 | value : value |
| 204 | }); |
| 205 | }; |
| 206 | } |
| 207 | }); |
| 208 | }; |
| 209 | |
| 210 | return; |
| 211 | }; |
| 212 | |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 213 | // The button has a state and the state is associated to the |
| 214 | // a intermediate object to toggle the view |
| 215 | if ('state' in this.button && this.button.state.associates() > 0) { |
| 216 | |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 217 | const s = this.button.state; |
| Akron | fcf89db | 2020-10-01 17:40:20 +0200 | [diff] [blame] | 218 | |
| 219 | // The associated service is existent |
| 220 | if (services[this.button['widgetID']]) { |
| Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 221 | s.roll(); |
| Akron | fcf89db | 2020-10-01 17:40:20 +0200 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
| 225 | // The service is not existent |
| 226 | else { |
| 227 | |
| 228 | // Remove broken state associations |
| 229 | s.clear(); |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 230 | s.set(true); |
| Akron | fcf89db | 2020-10-01 17:40:20 +0200 | [diff] [blame] | 231 | } |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 232 | }; |
| 233 | |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 234 | // If a background service exists (from active-check), |
| 235 | // close it before creating the widget |
| 236 | if ('active' in this.button && |
| 237 | this.button['widgetID'] && |
| 238 | services[this.button['widgetID']] && |
| 239 | !services[this.button['widgetID']].isWidget) { |
| 240 | that._closeService(this.button['widgetID']); |
| 241 | this.button['widgetID'] = undefined; |
| 242 | }; |
| 243 | |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 244 | // Add the widget to the panel |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 245 | let id = that.addWidget(this, { |
| 246 | "name": name, |
| 247 | "src": onClick["template"], // that._interpolateURI(onClick["template"], this.match); |
| Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 248 | "permissions": onClick["permissions"], |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 249 | "desc":desc, |
| 250 | "panel":panel |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 251 | }); |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 252 | plugin["widgets"].push(id); |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 253 | |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 254 | // If a state exists, associate with a mediator object |
| 255 | if ('state' in this.button) { |
| Akron | fcf89db | 2020-10-01 17:40:20 +0200 | [diff] [blame] | 256 | this.button['widgetID'] = id; |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 257 | this.button.state.associate({ |
| 258 | setState : function (value) { |
| 259 | // Minimize the widget |
| 260 | if (value == false) { |
| 261 | services[id].minimize(); |
| 262 | } |
| 263 | else { |
| 264 | services[id].show(); |
| 265 | }; |
| 266 | } |
| 267 | }); |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 268 | }; |
| 269 | |
| 270 | // If an active state exists, associate it with the widget |
| 271 | // so active-state changes can be reflected in the iframe. |
| 272 | if ('active' in this.button) { |
| 273 | this.button['widgetID'] = id; |
| 274 | let first = true; |
| 275 | this.button.active.associate({ |
| 276 | setState : function (value) { |
| 277 | if (first) { |
| 278 | first = false; |
| 279 | return; |
| 280 | }; |
| 281 | if (services[id]) { |
| 282 | services[id].sendMsg({ |
| 283 | action: 'state', |
| 284 | key : 'active', |
| 285 | value : value |
| 286 | }); |
| 287 | }; |
| 288 | } |
| 289 | }); |
| 290 | }; |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 291 | }; |
| 292 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 293 | |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 294 | // Button object |
| Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 295 | let obj = {'cls':embed["classes"], 'icon': icon }; |
| 296 | |
| 297 | if (embed['desc'] != undefined) |
| 298 | obj['desc'] = embed['desc']; |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 299 | |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 300 | if (onClick['active'] !== undefined) { |
| 301 | obj['active'] = stateClass.create([true, false]); |
| 302 | obj['active'].setIfNotYet(onClick['active'] ? true : false); |
| 303 | }; |
| 304 | |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 305 | if (onClick["action"] && onClick["action"] == "setWidget") { |
| 306 | |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 307 | // Create a boolean state value, |
| 308 | // that initializes to true == opened |
| Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 309 | obj['state'] = stateClass.create([true, false]); |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 310 | obj['state'].setIfNotYet(true); |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 311 | }; |
| 312 | |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 313 | // Add to dynamic button list (e.g. for matches) |
| 314 | if (buttons[panel]) { |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 315 | buttons[panel].push([title, obj, cb]); |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Add to static button list (e.g. for query) already loaded |
| 319 | else if (KorAP.Panel[panel]) { |
| Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 320 | KorAP.Panel[panel].actions().add(title, obj, cb); |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Add to static button list (e.g. for query) not yet loaded |
| 324 | else { |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 325 | buttonsSingle[panel].push([title, obj, cb]); |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 326 | } |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 327 | } |
| Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 328 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 329 | else if (onClick["action"] == "toggle") { |
| 330 | |
| Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 331 | // TODO: |
| 332 | // Accept a "value" list here for toggling, which should |
| 333 | // also allow for "rolling" through states via CSS classes |
| 334 | // as 'toggle-true', 'toggle-false' etc. |
| Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame] | 335 | let state = that._states.newState( |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 336 | (onClick["state"] ? onClick["state"] : name), |
| 337 | [true, false], |
| 338 | onClick["default"] |
| 339 | ); |
| Akron | a70b689 | 2021-11-04 14:23:24 +0100 | [diff] [blame] | 340 | |
| Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 341 | let obj = {'cls':["title"], 'icon': icon}; |
| 342 | |
| 343 | if (embed['desc'] != undefined) |
| 344 | obj['desc'] = embed['desc']; |
| 345 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 346 | // TODO: |
| 347 | // Lazy registration (see above!) |
| Akron | 83a58bc | 2024-11-08 09:55:19 +0100 | [diff] [blame] | 348 | KorAP.Panel[panel].actions().addToggle(title, obj, state); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 349 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 350 | // Add the service |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 351 | let id = this.addService({ |
| 352 | "name" : name, |
| 353 | // TODO: |
| 354 | // Use the "service" keyword |
| 355 | "src" : onClick["template"], |
| 356 | "permissions" : onClick["permissions"] |
| 357 | }); |
| Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 358 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 359 | // TODO: |
| 360 | // This is a bit stupid to get the service window |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 361 | let service = services[id]; |
| 362 | let iframe = service.load(); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 363 | |
| 364 | // Create object to communicate the toggle state |
| 365 | // once the iframe is loaded. |
| 366 | iframe.onload = function () { |
| 367 | let sendToggle = { |
| 368 | setState : function (val) { |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 369 | service.sendMsg({ |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 370 | action: 'state', |
| 371 | key : onClick['state'], |
| 372 | value : val |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 373 | }); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 374 | } |
| 375 | }; |
| 376 | |
| 377 | // Associate object with the state |
| 378 | state.associate(sendToggle); |
| 379 | }; |
| 380 | |
| 381 | plugin["services"].push(id); |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 382 | }; |
| Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 383 | }, this); |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 384 | }, |
| 385 | |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 386 | // TODO: |
| 387 | // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570 |
| 388 | // but as simple as possible |
| 389 | _interpolateURI : function (uri, obj) { |
| 390 | // ... |
| 391 | }, |
| 392 | |
| 393 | |
| 394 | /** |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 395 | * Get named button group - better rename to "action" |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 396 | */ |
| 397 | buttonGroup : function (name) { |
| Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 398 | if (buttons[name] != undefined) { |
| 399 | return buttons[name]; |
| 400 | } else if (buttonsSingle[name] != undefined) { |
| 401 | return buttonsSingle[name]; |
| 402 | }; |
| 403 | return []; |
| 404 | }, |
| 405 | |
| 406 | /** |
| 407 | * Clear named button group - better rename to "action" |
| 408 | */ |
| 409 | clearButtonGroup : function (name) { |
| 410 | if (buttons[name] != undefined) { |
| 411 | buttons[name] = []; |
| 412 | } else if (buttonsSingle[name] != undefined) { |
| 413 | buttonsSingle[name] = []; |
| 414 | } |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 415 | }, |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 416 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 417 | // Optionally initialize the service mechanism and get an ID |
| 418 | _getServiceID : function () { |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 419 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 420 | // Is it the first service? |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 421 | if (!this._listener) { |
| 422 | |
| 423 | /* |
| 424 | * Establish the global 'message' hook. |
| 425 | */ |
| 426 | this._listener = this._receiveMsg.bind(this); |
| 427 | window.addEventListener("message", this._listener); |
| 428 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 429 | // Every second increase the limits of all registered services |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 430 | this._timer = window.setInterval(function () { |
| Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 431 | for (let i = 0; i < limits.length; i++) { |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 432 | if (limits[i]++ >= maxMessages) { |
| 433 | limits[i] = maxMessages; |
| 434 | } |
| 435 | } |
| 436 | }, 1000); |
| 437 | }; |
| 438 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 439 | // Create a unique random ID per service |
| 440 | return 'id-' + this._randomID(); |
| 441 | }, |
| 442 | |
| 443 | /** |
| 444 | * Add a service in a certain panel and return the id. |
| 445 | */ |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 446 | addService : function (data) { |
| 447 | if (!data["src"]) |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 448 | return; |
| 449 | |
| 450 | let id = this._getServiceID(); |
| 451 | |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 452 | data["id"] = id; |
| 453 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 454 | // Create a new service |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 455 | let service = serviceClass.create(data); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 456 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 457 | services[id] = service; |
| 458 | limits[id] = maxMessages; |
| 459 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 460 | // Add service to panel |
| 461 | this.element().appendChild( |
| 462 | service.load() |
| 463 | ); |
| 464 | |
| 465 | return id; |
| 466 | }, |
| 467 | |
| 468 | |
| 469 | /** |
| 470 | * Open a new widget view in a certain panel and return |
| 471 | * the id. |
| 472 | */ |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 473 | addWidget : function (panel, data) { |
| 474 | // panel, name, src, permissions |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 475 | |
| 476 | let id = this._getServiceID(); |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 477 | |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 478 | data["id"] = id; |
| 479 | |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 480 | // Create a new widget |
| Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 481 | var widget = widgetClass.create(data); |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 482 | |
| 483 | // Store the widget based on the identifier |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 484 | services[id] = widget; |
| Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 485 | limits[id] = maxMessages; |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 486 | |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 487 | widget._mgr = this; |
| 488 | |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 489 | // Add widget to panel |
| 490 | panel.add(widget); |
| Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 491 | |
| 492 | return id; |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 493 | }, |
| 494 | |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 495 | |
| 496 | /** |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 497 | * Get service by identifier |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 498 | */ |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 499 | service : function (id) { |
| 500 | return services[id]; |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 501 | }, |
| 502 | |
| 503 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 504 | // Receive a call from an embedded service. |
| Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 505 | // The handling needs to be very careful, |
| 506 | // as this can easily become a security nightmare. |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 507 | _receiveMsg : function (e) { |
| Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 508 | |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 509 | // Get event data |
| 510 | var d = e.data; |
| 511 | |
| Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 512 | // If no data given - fail |
| 513 | // (probably check that it's an assoc array) |
| 514 | if (!d) |
| 515 | return; |
| 516 | |
| 517 | // e.origin is probably set and okay - CHECK! |
| Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 518 | // TODO: Check e.origin is in the list of registered participants |
| 519 | // if (e.origin !== "http://example.com:8080") |
| 520 | // return; |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 521 | |
| Akron | bc94a9c | 2021-04-15 00:07:35 +0200 | [diff] [blame] | 522 | |
| Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 523 | // Get origin ID |
| 524 | var id = d["originID"]; |
| 525 | |
| 526 | // If no origin ID given - fail |
| 527 | if (!id) |
| 528 | return; |
| 529 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 530 | // Get the service |
| 531 | let service = services[id]; |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 532 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 533 | // If the addressed service does not exist - fail |
| 534 | if (!service) |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 535 | return; |
| 536 | |
| Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 537 | // Check for message limits |
| 538 | if (limits[id]-- < 0) { |
| Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 539 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 540 | // Kill service |
| 541 | KorAP.log(0, 'Suspicious action by service', service.src); |
| Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 542 | |
| 543 | // TODO: |
| 544 | // Potentially kill the whole plugin! |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 545 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 546 | // This removes all connections before closing the service |
| 547 | this._closeService(service.id); |
| 548 | |
| 549 | // if (service.isWidget) |
| 550 | service.close(); |
| 551 | |
| Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 552 | return; |
| 553 | }; |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 554 | |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 555 | // Resize the iframe |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 556 | switch (d.action) { |
| 557 | case 'resize': |
| 558 | if (service.isWidget) |
| 559 | service.resize(d); |
| 560 | break; |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 561 | |
| 562 | // Log message from iframe |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 563 | case 'log': |
| 564 | KorAP.log(d.code, d.msg, service.src); |
| 565 | break; |
| Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 566 | |
| 567 | // Modify pipes |
| 568 | case 'pipe': |
| Akron | 910828a | 2025-06-27 15:38:48 +0200 | [diff] [blame] | 569 | let j = d.job; |
| 570 | if ( |
| 571 | ((j == 'del-after' || j == 'add-after') && |
| 572 | KorAP.ResponsePipe == undefined) || |
| 573 | KorAP.Pipe == undefined) { |
| 574 | |
| 575 | KorAP.log(0,"No Pipe established"); |
| 576 | break; |
| 577 | }; |
| 578 | |
| 579 | if (j == 'del') { |
| 580 | KorAP.Pipe.remove(d.service); |
| 581 | } else if (j == 'del-after') { |
| 582 | KorAP.ResponsePipe.remove(d.service); |
| 583 | } else if (j == 'add-after') { |
| 584 | KorAP.ResponsePipe.append(d.service); |
| 585 | } else { |
| 586 | KorAP.Pipe.append(d.service); |
| Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 587 | }; |
| 588 | break; |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 589 | |
| 590 | // Get information from the embedding platform |
| 591 | case 'get': |
| Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 592 | |
| 593 | // Get KoralQuery |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 594 | if (d.key == 'KQ') { |
| 595 | if (KorAP.koralQuery !== undefined) { |
| 596 | d["value"] = KorAP.koralQuery; |
| 597 | }; |
| Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 600 | // Get Query information from form |
| Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 601 | else if (d.key == 'QueryForm') { |
| 602 | let doc = document; |
| 603 | let v = d["value"] = {}; |
| 604 | |
| 605 | var el; |
| 606 | if (el = doc.getElementById('q-field')) { |
| 607 | v["q"] = el.value; |
| 608 | }; |
| 609 | if (el = doc.getElementById('ql-field')) { |
| 610 | v["ql"] = el.value; |
| 611 | }; |
| 612 | if (el = KorAP.vc) { |
| 613 | v["cq"] = el.toQuery(); |
| 614 | }; |
| Akron | b759ee9 | 2024-11-19 18:02:56 +0100 | [diff] [blame] | 615 | if (el = KorAP.Hint) { |
| 616 | v["selection"] = KorAP.Hint.selectionRange(); |
| 617 | }; |
| Akron | 432972b | 2020-09-18 17:05:53 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| Akron | 338b4d4 | 2022-12-20 13:59:22 +0100 | [diff] [blame] | 620 | // Get text sigle from match |
| 621 | else if (d.key == 'textSigle') { |
| 622 | if (service.panel.type != "match") { |
| 623 | KorAP.log(0, "Service can only be called on matches", service.src); |
| 624 | return; |
| 625 | }; |
| 626 | let v = d["value"] = {}; |
| 627 | v["value"] = service.panel._match.textSigle; |
| 628 | } |
| 629 | |
| Akron | 432972b | 2020-09-18 17:05:53 +0200 | [diff] [blame] | 630 | // Get Query information from parameters |
| 631 | else if (d.key == 'QueryParam') { |
| 632 | |
| 633 | // Supported in all modern browsers |
| 634 | var p = new URLSearchParams(window.location.search); |
| 635 | let v = d["value"] = {}; |
| Akron | 4de759f | 2021-10-13 10:46:45 +0200 | [diff] [blame] | 636 | v["search"] = window.location.search; // readonly |
| Akron | 432972b | 2020-09-18 17:05:53 +0200 | [diff] [blame] | 637 | v["q"] = p.get('q'); |
| 638 | v["ql"] = p.get('ql'); |
| 639 | v["cq"] = p.get('cq'); |
| Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | // Get pagination information |
| 643 | else if (d.key == 'Pagination') { |
| 644 | const pi = pageInfoClass.create(); |
| 645 | let v = d["value"] = {}; |
| 646 | v["page"] = pi.page(); |
| 647 | v["total"] = pi.total(); |
| 648 | v["count"] = pi.count(); |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 649 | } |
| 650 | |
| 651 | // Get active toggle state of the widget button |
| 652 | else if (d.key == 'Active') { |
| 653 | let button = services[d["originID"]].panel.button; |
| 654 | if (button && button.active) { |
| 655 | d["value"] = button.active.get(); |
| 656 | }; |
| Akron | ec4bbfa | 2021-09-15 15:00:59 +0200 | [diff] [blame] | 657 | }; |
| Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 658 | |
| Akron | ec4bbfa | 2021-09-15 15:00:59 +0200 | [diff] [blame] | 659 | // data needs to be mirrored |
| 660 | if (d._id) { |
| 661 | service.sendMsg(d); |
| 662 | }; |
| 663 | |
| 664 | break; |
| 665 | |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 666 | case 'set': |
| Akron | e03faf6 | 2024-11-14 11:51:04 +0100 | [diff] [blame] | 667 | |
| 668 | // Get Query information from data |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 669 | if (d.key == 'QueryForm') { |
| 670 | let v = d["value"]; |
| Akron | e03faf6 | 2024-11-14 11:51:04 +0100 | [diff] [blame] | 671 | |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 672 | if (v["q"] != undefined && this._q) { |
| 673 | this._q.value = v["q"]; |
| 674 | }; |
| 675 | |
| 676 | // Set query language field |
| 677 | // Identical to tutorial.js |
| Akron | e03faf6 | 2024-11-14 11:51:04 +0100 | [diff] [blame] | 678 | if (v["ql"] != undefined) { |
| 679 | if (KorAP.QLmenu) { |
| 680 | KorAP.QLmenu.selectValue(v["ql"]); |
| 681 | } |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 682 | |
| Akron | e03faf6 | 2024-11-14 11:51:04 +0100 | [diff] [blame] | 683 | else if (this._ql) { |
| 684 | let found = Array.from(this._ql.options).find(o => o.value === v["ql"]); |
| 685 | if (found) |
| 686 | found.selected = true; |
| 687 | }; |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | window.scrollTo(0, 0); |
| 691 | // if (v["cq"] != undefined) {}; |
| 692 | } |
| 693 | |
| Akron | b0ae841 | 2026-02-24 11:47:52 +0100 | [diff] [blame^] | 694 | else if (d.key == "Title") { |
| 695 | // TODO: Only support "Add title"! |
| 696 | let v = d["value"]; |
| 697 | services[d["originID"]].panel.button.changeTitle(v); |
| 698 | } |
| 699 | |
| 700 | else if (d.key == "Active") { |
| 701 | let v = d["value"]; |
| 702 | let button = services[d["originID"]].panel.button; |
| 703 | if (button && button.active) { |
| 704 | if (v !== undefined) { |
| 705 | button.active.set(v); |
| 706 | } else { |
| 707 | button.active.roll(); |
| 708 | }; |
| 709 | }; |
| 710 | }; |
| 711 | |
| Akron | 7d18d8e | 2024-11-08 11:08:48 +0100 | [diff] [blame] | 712 | break; |
| 713 | |
| Akron | ec4bbfa | 2021-09-15 15:00:59 +0200 | [diff] [blame] | 714 | // Redirect to a different page relative to the current |
| 715 | case 'redirect': |
| 716 | const url = new URL(window.location); |
| 717 | |
| 718 | // Currently this only accepts search parameters |
| 719 | if (d["queryParam"]) { |
| 720 | url.search = new URLSearchParams(d["queryParam"]); |
| 721 | }; |
| 722 | |
| 723 | window.location = url.toString(); |
| 724 | break; |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | // TODO: |
| 728 | // Close |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 729 | }, |
| 730 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 731 | // Close the service |
| 732 | _closeService : function (id) { |
| Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 733 | delete limits[id]; |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 734 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 735 | // Close the iframe |
| 736 | if (services[id] && services[id]._closeIframe) { |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 737 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 738 | services[id]._closeIframe(); |
| 739 | |
| 740 | // Remove from list |
| 741 | delete services[id]; |
| 742 | }; |
| 743 | |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 744 | |
| 745 | // Remove listeners in case no widget |
| 746 | // is available any longer |
| 747 | if (Object.keys(limits).length == 0) |
| 748 | this._removeListener(); |
| 749 | }, |
| 750 | |
| Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 751 | // Get a random identifier |
| 752 | _randomID : function () { |
| 753 | return randomID(20); |
| Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 754 | }, |
| 755 | |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 756 | // Remove the listener |
| 757 | _removeListener : function () { |
| 758 | window.clearInterval(this._timer); |
| 759 | this._timer = undefined; |
| 760 | window.removeEventListener("message", this._listener); |
| 761 | this._listener = undefined; |
| 762 | }, |
| 763 | |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 764 | /** |
| 765 | * Return the service element. |
| 766 | */ |
| 767 | element : function () { |
| Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 768 | if (!this._el) { |
| 769 | this._el = document.createElement('div'); |
| 770 | this._el.setAttribute("id", "services"); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 771 | } |
| Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 772 | return this._el; |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 773 | }, |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 774 | |
| 775 | |
| 776 | // Return states object |
| 777 | states : function () { |
| Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame] | 778 | return this._states; |
| Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 779 | }, |
| 780 | |
| Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 781 | // Destructor, just for testing scenarios |
| 782 | destroy : function () { |
| 783 | limits = {}; |
| Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 784 | Object.keys(services).forEach( |
| 785 | s => services[s].close() |
| 786 | ); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 787 | services = {}; |
| Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 788 | Object.keys(buttons).forEach( |
| 789 | b => buttons[b] = [] |
| 790 | ); |
| 791 | Object.keys(buttonsSingle).forEach( |
| 792 | b => buttonsSingle[b] = [] |
| 793 | ); |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 794 | |
| Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 795 | if (this._el) { |
| 796 | let e = this._el; |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 797 | if (e.parentNode) { |
| 798 | e.parentNode.removeChild(e); |
| 799 | }; |
| Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 800 | this._el = null; |
| Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 801 | }; |
| Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 802 | this._removeListener(); |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 803 | } |
| Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 804 | }; |
| Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 805 | }); |