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