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 | */ |
| 10 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 11 | define(["plugin/widget", 'plugin/service', 'state', "util"], function (widgetClass, serviceClass, stateClass) { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 12 | "use strict"; |
| 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 | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 112 | // Register plugin by name |
| 113 | var plugin = plugins[name] = { |
| 114 | name : name, |
| 115 | desc : obj["desc"], |
| 116 | about : obj["about"], |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 117 | widgets : [], |
| 118 | services : [] |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 119 | }; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 120 | |
| 121 | if (typeof obj["embed"] !== 'object') |
| 122 | throw new Error("Embedding of plugin is no list"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 123 | |
| 124 | // Embed all embeddings of the plugin |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 125 | var that = this; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 126 | for (let i in obj["embed"]) { |
| 127 | let embed = obj["embed"][i]; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 128 | |
| 129 | if (typeof embed !== 'object') |
| 130 | throw new Error("Embedding of plugin is no object"); |
| 131 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 132 | // Needs to be localized as well |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 133 | let title = embed["title"]; |
| 134 | let panel = embed["panel"]; |
| 135 | let onClick = embed["onClick"]; |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame^] | 136 | let icon = embed["icon"]; |
| 137 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 138 | if (!panel || !(buttons[panel] || buttonsSingle[panel])) |
| 139 | throw new Error("Panel for plugin is invalid"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 140 | |
| 141 | // The embedding will open a widget |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 142 | if (!onClick["action"] || onClick["action"] == "addWidget") { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 143 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 144 | |
| 145 | let cb = function (e) { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 146 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 147 | // "this" is bind to the panel |
| 148 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 149 | // Get the URL of the widget |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 150 | let url = onClick["template"]; |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 151 | // that._interpolateURI(onClick["template"], this.match); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 152 | |
| 153 | // Add the widget to the panel |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 154 | let id = that.addWidget(this, name, url); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 155 | plugin["widgets"].push(id); |
| 156 | }; |
| 157 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 158 | // TODO: |
| 159 | // Create button class to be stored and loaded in button groups! |
| 160 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 161 | // Add to dynamic button list (e.g. for matches) |
| 162 | if (buttons[panel]) { |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame^] | 163 | buttons[panel].push([title, embed["classes"], cb, icon]); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Add to static button list (e.g. for query) already loaded |
| 167 | else if (KorAP.Panel[panel]) { |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame^] | 168 | KorAP.Panel[panel].actions.add(title, embed["classes"], cb, icon); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Add to static button list (e.g. for query) not yet loaded |
| 172 | else { |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame^] | 173 | buttonsSingle[panel].push([title, embed["classes"], cb, icon]); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 174 | } |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 175 | } |
hebasta | 40a85cf | 2020-07-15 18:10:08 +0200 | [diff] [blame^] | 176 | //TODO There is no possibility to add icons to an plugin toggle button right now. |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 177 | else if (onClick["action"] == "toggle") { |
| 178 | |
| 179 | // Todo: Initially false |
| 180 | let state = stateClass.create(false); |
| 181 | |
| 182 | // TODO: |
| 183 | // Lazy registration (see above!) |
Akron | 16bd164 | 2020-06-25 08:30:28 +0200 | [diff] [blame] | 184 | KorAP.Panel[panel].actions.addToggle(title, ["title"], state); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 185 | |
| 186 | // Get the URL of the service |
| 187 | |
| 188 | // TODO: |
| 189 | // Use the "service" keyword |
| 190 | let url = onClick["template"]; |
| 191 | |
| 192 | // Add the service |
| 193 | let id = this.addService(name, url); |
| 194 | |
| 195 | // TODO: |
| 196 | // This is a bit stupid to get the service window |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 197 | let service = services[id]; |
| 198 | let iframe = service.load(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 199 | |
| 200 | // Create object to communicate the toggle state |
| 201 | // once the iframe is loaded. |
| 202 | iframe.onload = function () { |
| 203 | let sendToggle = { |
| 204 | setState : function (val) { |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 205 | service.sendMsg({ |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 206 | action: 'state', |
| 207 | key : onClick['state'], |
| 208 | value : val |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 209 | }); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | |
| 213 | // Associate object with the state |
| 214 | state.associate(sendToggle); |
| 215 | }; |
| 216 | |
| 217 | plugin["services"].push(id); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 218 | }; |
| 219 | }; |
| 220 | }, |
| 221 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 222 | // TODO: |
| 223 | // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570 |
| 224 | // but as simple as possible |
| 225 | _interpolateURI : function (uri, obj) { |
| 226 | // ... |
| 227 | }, |
| 228 | |
| 229 | |
| 230 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 231 | * Get named button group - better rename to "action" |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 232 | */ |
| 233 | buttonGroup : function (name) { |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 234 | if (buttons[name] != undefined) { |
| 235 | return buttons[name]; |
| 236 | } else if (buttonsSingle[name] != undefined) { |
| 237 | return buttonsSingle[name]; |
| 238 | }; |
| 239 | return []; |
| 240 | }, |
| 241 | |
| 242 | /** |
| 243 | * Clear named button group - better rename to "action" |
| 244 | */ |
| 245 | clearButtonGroup : function (name) { |
| 246 | if (buttons[name] != undefined) { |
| 247 | buttons[name] = []; |
| 248 | } else if (buttonsSingle[name] != undefined) { |
| 249 | buttonsSingle[name] = []; |
| 250 | } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 251 | }, |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 252 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 253 | // Optionally initialize the service mechanism and get an ID |
| 254 | _getServiceID : function () { |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 255 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 256 | // Is it the first service? |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 257 | if (!this._listener) { |
| 258 | |
| 259 | /* |
| 260 | * Establish the global 'message' hook. |
| 261 | */ |
| 262 | this._listener = this._receiveMsg.bind(this); |
| 263 | window.addEventListener("message", this._listener); |
| 264 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 265 | // Every second increase the limits of all registered services |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 266 | this._timer = window.setInterval(function () { |
| 267 | for (var i in limits) { |
| 268 | if (limits[i]++ >= maxMessages) { |
| 269 | limits[i] = maxMessages; |
| 270 | } |
| 271 | } |
| 272 | }, 1000); |
| 273 | }; |
| 274 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 275 | // Create a unique random ID per service |
| 276 | return 'id-' + this._randomID(); |
| 277 | }, |
| 278 | |
| 279 | /** |
| 280 | * Add a service in a certain panel and return the id. |
| 281 | */ |
| 282 | addService : function (name, src) { |
| 283 | if (!src) |
| 284 | return; |
| 285 | |
| 286 | let id = this._getServiceID(); |
| 287 | |
| 288 | // Create a new service |
| 289 | let service = serviceClass.create(name, src, id); |
| 290 | |
| 291 | // TODO! |
| 292 | // Store the service based on the identifier |
| 293 | services[id] = service; |
| 294 | limits[id] = maxMessages; |
| 295 | |
| 296 | // widget._mgr = this; |
| 297 | |
| 298 | // Add service to panel |
| 299 | this.element().appendChild( |
| 300 | service.load() |
| 301 | ); |
| 302 | |
| 303 | return id; |
| 304 | }, |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Open a new widget view in a certain panel and return |
| 309 | * the id. |
| 310 | */ |
| 311 | addWidget : function (panel, name, src) { |
| 312 | |
| 313 | let id = this._getServiceID(); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 314 | |
| 315 | // Create a new widget |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 316 | var widget = widgetClass.create(name, src, id); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 317 | |
| 318 | // Store the widget based on the identifier |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 319 | services[id] = widget; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 320 | limits[id] = maxMessages; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 321 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 322 | widget._mgr = this; |
| 323 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 324 | // Add widget to panel |
| 325 | panel.add(widget); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 326 | |
| 327 | return id; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 328 | }, |
| 329 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 330 | |
| 331 | /** |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 332 | * Get service by identifier |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 333 | */ |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 334 | service : function (id) { |
| 335 | return services[id]; |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 336 | }, |
| 337 | |
| 338 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 339 | // Receive a call from an embedded service. |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 340 | // The handling needs to be very careful, |
| 341 | // as this can easily become a security nightmare. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 342 | _receiveMsg : function (e) { |
| 343 | // Get event data |
| 344 | var d = e.data; |
| 345 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 346 | // If no data given - fail |
| 347 | // (probably check that it's an assoc array) |
| 348 | if (!d) |
| 349 | return; |
| 350 | |
| 351 | // e.origin is probably set and okay - CHECK! |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 352 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 353 | // Get origin ID |
| 354 | var id = d["originID"]; |
| 355 | |
| 356 | // If no origin ID given - fail |
| 357 | if (!id) |
| 358 | return; |
| 359 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 360 | // Get the service |
| 361 | let service = services[id]; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 362 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 363 | // If the addressed service does not exist - fail |
| 364 | if (!service) |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 365 | return; |
| 366 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 367 | // Check for message limits |
| 368 | if (limits[id]-- < 0) { |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 369 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 370 | // Kill service |
| 371 | KorAP.log(0, 'Suspicious action by service', service.src); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 372 | |
| 373 | // TODO: |
| 374 | // Potentially kill the whole plugin! |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 375 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 376 | // This removes all connections before closing the service |
| 377 | this._closeService(service.id); |
| 378 | |
| 379 | // if (service.isWidget) |
| 380 | service.close(); |
| 381 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 382 | return; |
| 383 | }; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 384 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 385 | // Resize the iframe |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 386 | switch (d.action) { |
| 387 | case 'resize': |
| 388 | if (service.isWidget) |
| 389 | service.resize(d); |
| 390 | break; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 391 | |
| 392 | // Log message from iframe |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 393 | case 'log': |
| 394 | KorAP.log(d.code, d.msg, service.src); |
| 395 | break; |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 396 | |
| 397 | // Modify pipes |
| 398 | case 'pipe': |
| 399 | if (KorAP.Pipe != undefined) { |
| 400 | if (d.job == 'del') { |
| 401 | KorAP.Pipe.remove(d.service); |
| 402 | } else { |
| 403 | KorAP.Pipe.append(d.service); |
| 404 | }; |
| 405 | }; |
| 406 | break; |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 407 | |
| 408 | // Get information from the embedding platform |
| 409 | case 'get': |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 410 | |
| 411 | // Get KoralQuery |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 412 | if (d.key == 'KQ') { |
| 413 | if (KorAP.koralQuery !== undefined) { |
| 414 | d["value"] = KorAP.koralQuery; |
| 415 | }; |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Get Query form information |
| 419 | else if (d.key == 'QueryForm') { |
| 420 | let doc = document; |
| 421 | let v = d["value"] = {}; |
| 422 | |
| 423 | var el; |
| 424 | if (el = doc.getElementById('q-field')) { |
| 425 | v["q"] = el.value; |
| 426 | }; |
| 427 | if (el = doc.getElementById('ql-field')) { |
| 428 | v["ql"] = el.value; |
| 429 | }; |
| 430 | if (el = KorAP.vc) { |
| 431 | v["cq"] = el.toQuery(); |
| 432 | }; |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 433 | }; |
| 434 | }; |
| 435 | |
| 436 | // data needs to be mirrored |
| 437 | if (d._id) { |
| 438 | service.sendMsg(d); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | // TODO: |
| 442 | // Close |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 443 | }, |
| 444 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 445 | // Close the service |
| 446 | _closeService : function (id) { |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 447 | delete limits[id]; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 448 | |
| 449 | // Close the iframe |
| 450 | if (services[id] && services[id]._closeIframe) { |
| 451 | services[id]._closeIframe(); |
| 452 | |
| 453 | // Remove from list |
| 454 | delete services[id]; |
| 455 | }; |
| 456 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 457 | |
| 458 | // Remove listeners in case no widget |
| 459 | // is available any longer |
| 460 | if (Object.keys(limits).length == 0) |
| 461 | this._removeListener(); |
| 462 | }, |
| 463 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 464 | // Get a random identifier |
| 465 | _randomID : function () { |
| 466 | return randomID(20); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 467 | }, |
| 468 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 469 | // Remove the listener |
| 470 | _removeListener : function () { |
| 471 | window.clearInterval(this._timer); |
| 472 | this._timer = undefined; |
| 473 | window.removeEventListener("message", this._listener); |
| 474 | this._listener = undefined; |
| 475 | }, |
| 476 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 477 | /** |
| 478 | * Return the service element. |
| 479 | */ |
| 480 | element : function () { |
| 481 | if (!this._element) { |
| 482 | this._element = document.createElement('div'); |
| 483 | this._element.setAttribute("id", "services"); |
| 484 | } |
| 485 | return this._element; |
| 486 | }, |
| 487 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 488 | // Destructor, just for testing scenarios |
| 489 | destroy : function () { |
| 490 | limits = {}; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 491 | for (let s in services) { |
| 492 | services[s].close(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 493 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 494 | services = {}; |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 495 | for (let b in buttons) { |
| 496 | buttons[b] = []; |
| 497 | }; |
| 498 | for (let b in buttonsSingle) { |
| 499 | buttonsSingle[b] = []; |
| 500 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 501 | |
| 502 | if (this._element) { |
| 503 | let e = this._element; |
| 504 | if (e.parentNode) { |
| 505 | e.parentNode.removeChild(e); |
| 506 | }; |
| 507 | this._element = null; |
| 508 | }; |
| 509 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 510 | this._removeListener(); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 511 | } |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 512 | }; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 513 | }); |