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