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