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