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