blob: ecf436cfdde485429300d439323102d799efeadc [file] [log] [blame]
Akron479994e2018-07-02 13:21:44 +02001/**
2 * The plugin system is based
Akron22598cd2019-12-09 14:59:03 +01003 * on registered services (iframes) from
Akron479994e2018-07-02 13:21:44 +02004 * foreign services.
5 * The server component spawns new iframes and
6 * listens to them.
7 *
8 * @author Nils Diewald
9 */
Akrone51eaa32020-11-10 09:35:53 +010010"use strict";
Akron479994e2018-07-02 13:21:44 +020011
Akronda32e7a2021-11-16 17:28:57 +010012define(['plugin/widget', 'plugin/service', 'state', 'state/manager', 'pageInfo', 'util'], function (widgetClass, serviceClass, stateClass, stateManagerClass, pageInfoClass) {
Akron479994e2018-07-02 13:21:44 +020013
Akron2d0d96d2019-11-18 19:49:50 +010014 KorAP.Panel = KorAP.Panel || {};
15
Akronda32e7a2021-11-16 17:28:57 +010016 // State manager undefined
17 const states = KorAP.States ? KorAP.States :
18
19 // Not serialized state manager
20 stateManagerClass.create(document.createElement('input'));
21
Akron22598cd2019-12-09 14:59:03 +010022 // Contains all servicess to address with
Akrona6c32b92018-07-02 18:39:42 +020023 // messages to them
Akron22598cd2019-12-09 14:59:03 +010024 var services = {};
25 var plugins = {};
Akron10a47962018-07-12 21:17:10 +020026
27 // TODO:
28 // These should better be panels and every panel
29 // has a buttonGroup
Akron2d0d96d2019-11-18 19:49:50 +010030
31 // List of panels with dynamic buttons, i.e.
32 // panels that may occur multiple times.
Akron7c6e05f2018-07-12 19:08:13 +020033 var buttons = {
34 match : []
35 };
Akron2d0d96d2019-11-18 19:49:50 +010036
37 // List of panels with static buttons, i.e.
38 // panels that occur only once.
39 var buttonsSingle = {
hebasta043e96f2019-11-28 12:33:00 +010040 query : [],
41 result : []
Akron22598cd2019-12-09 14:59:03 +010042 }
Akron10a47962018-07-12 21:17:10 +020043
Akrone8e2c952018-07-04 13:43:12 +020044 // 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
Akrone1c27f62018-07-20 11:42:59 +020055 // all widgets every second to see if it is still alive,
56 // otherwise kill
Akrone8e2c952018-07-04 13:43:12 +020057
Akrone1c27f62018-07-20 11:42:59 +020058 // Load Plugin server
Akron479994e2018-07-02 13:21:44 +020059 return {
60
61 /**
62 * Create new plugin management system
63 */
64 create : function () {
65 return Object.create(this)._init();
66 },
67
68 /*
Akron76dd8d32018-07-06 09:30:22 +020069 * Initialize the plugin manager
Akron479994e2018-07-02 13:21:44 +020070 */
71 _init : function () {
Akron479994e2018-07-02 13:21:44 +020072 return this;
73 },
74
75 /**
Akron7c6e05f2018-07-12 19:08:13 +020076 * Register a plugin described as a JSON object.
77 *
78 * This is work in progress.
Akron10a47962018-07-12 21:17:10 +020079 *
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 * }
Akron22598cd2019-12-09 14:59:03 +010094 * },{
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 * }
Akron10a47962018-07-12 21:17:10 +0200105 * }]
106 * });
107 *
Akron7c6e05f2018-07-12 19:08:13 +0200108 */
109 register : function (obj) {
Akron7c6e05f2018-07-12 19:08:13 +0200110 // TODO:
Akron10a47962018-07-12 21:17:10 +0200111 // These fields need to be localized for display by a structure like
112 // { de : { name : '..' }, en : { .. } }
Akronda32e7a2021-11-16 17:28:57 +0100113 const name = obj["name"];
Akron7c6e05f2018-07-12 19:08:13 +0200114
Akron10a47962018-07-12 21:17:10 +0200115 if (!name)
116 throw new Error("Missing name of plugin");
117
Akron3d013802020-10-07 15:03:38 +0200118 var desc = obj["desc"];
119
Akron7c6e05f2018-07-12 19:08:13 +0200120 // Register plugin by name
121 var plugin = plugins[name] = {
122 name : name,
Akron3d013802020-10-07 15:03:38 +0200123 desc : desc,
Akron7c6e05f2018-07-12 19:08:13 +0200124 about : obj["about"],
Akron22598cd2019-12-09 14:59:03 +0100125 widgets : [],
126 services : []
Akron7c6e05f2018-07-12 19:08:13 +0200127 };
Akron10a47962018-07-12 21:17:10 +0200128
129 if (typeof obj["embed"] !== 'object')
130 throw new Error("Embedding of plugin is no list");
Akron7c6e05f2018-07-12 19:08:13 +0200131
132 // Embed all embeddings of the plugin
Akrone1c27f62018-07-20 11:42:59 +0200133 var that = this;
Akron678c26f2020-10-09 08:52:50 +0200134 obj["embed"].forEach(function(embed) {
Akron10a47962018-07-12 21:17:10 +0200135
136 if (typeof embed !== 'object')
137 throw new Error("Embedding of plugin is no object");
138
Akron7c6e05f2018-07-12 19:08:13 +0200139 // Needs to be localized as well
Akron22598cd2019-12-09 14:59:03 +0100140 let title = embed["title"];
141 let panel = embed["panel"];
142 let onClick = embed["onClick"];
hebasta40a85cf2020-07-15 18:10:08 +0200143 let icon = embed["icon"];
144
Akron22598cd2019-12-09 14:59:03 +0100145 if (!panel || !(buttons[panel] || buttonsSingle[panel]))
Akronba09ed22020-10-01 16:01:45 +0200146 throw new Error("Panel for plugin is invalid");
Akron7c6e05f2018-07-12 19:08:13 +0200147
148 // The embedding will open a widget
Akronba09ed22020-10-01 16:01:45 +0200149 if (!onClick["action"] ||
150 onClick["action"] == "addWidget" ||
151 onClick["action"] == "setWidget") {
Akron22598cd2019-12-09 14:59:03 +0100152
153 let cb = function (e) {
Akron7c6e05f2018-07-12 19:08:13 +0200154
Akrone1c27f62018-07-20 11:42:59 +0200155 // "this" is bind to the panel
Akronba09ed22020-10-01 16:01:45 +0200156 // "this".button is the button
157 // "that" is the server object
Akrone1c27f62018-07-20 11:42:59 +0200158
Akronba09ed22020-10-01 16:01:45 +0200159 // 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
Akronda32e7a2021-11-16 17:28:57 +0100163 const s = this.button.state;
Akronfcf89db2020-10-01 17:40:20 +0200164
165 // The associated service is existent
166 if (services[this.button['widgetID']]) {
Akron237abc42020-10-07 14:14:52 +0200167 s.roll();
Akronfcf89db2020-10-01 17:40:20 +0200168 return;
169 }
170
171 // The service is not existent
172 else {
173
174 // Remove broken state associations
175 s.clear();
Akronba09ed22020-10-01 16:01:45 +0200176 s.set(true);
Akronfcf89db2020-10-01 17:40:20 +0200177 }
Akronba09ed22020-10-01 16:01:45 +0200178 };
179
Akron7c6e05f2018-07-12 19:08:13 +0200180 // Add the widget to the panel
Akronbb891982020-10-05 16:07:18 +0200181 let id = that.addWidget(this, {
182 "name": name,
183 "src": onClick["template"], // that._interpolateURI(onClick["template"], this.match);
Akron3d013802020-10-07 15:03:38 +0200184 "permissions": onClick["permissions"],
185 "desc":desc
Akronbb891982020-10-05 16:07:18 +0200186 });
Akron7c6e05f2018-07-12 19:08:13 +0200187 plugin["widgets"].push(id);
Akronba09ed22020-10-01 16:01:45 +0200188
189 // If a state exists, associate with a mediator object
190 if ('state' in this.button) {
Akronfcf89db2020-10-01 17:40:20 +0200191 this.button['widgetID'] = id;
Akronba09ed22020-10-01 16:01:45 +0200192 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 }
Akron7c6e05f2018-07-12 19:08:13 +0200204 };
205
Akron22598cd2019-12-09 14:59:03 +0100206
Akronba09ed22020-10-01 16:01:45 +0200207 // Button object
208 let obj = {'cls':embed["classes"], 'icon': icon }
209
210 if (onClick["action"] && onClick["action"] == "setWidget") {
211
Akronda32e7a2021-11-16 17:28:57 +0100212 // Create a boolean state value,
213 // that initializes to true == opened
Akron237abc42020-10-07 14:14:52 +0200214 obj['state'] = stateClass.create([true, false]);
Akronda32e7a2021-11-16 17:28:57 +0100215 obj['state'].setIfNotYet(true);
Akronba09ed22020-10-01 16:01:45 +0200216 };
217
Akron2d0d96d2019-11-18 19:49:50 +0100218 // Add to dynamic button list (e.g. for matches)
219 if (buttons[panel]) {
Akronba09ed22020-10-01 16:01:45 +0200220 buttons[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100221 }
222
223 // Add to static button list (e.g. for query) already loaded
224 else if (KorAP.Panel[panel]) {
Akron37ea1192021-07-28 10:40:14 +0200225 KorAP.Panel[panel].actions().add(title, obj, cb);
Akron2d0d96d2019-11-18 19:49:50 +0100226 }
227
228 // Add to static button list (e.g. for query) not yet loaded
229 else {
Akronba09ed22020-10-01 16:01:45 +0200230 buttonsSingle[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100231 }
Akron22598cd2019-12-09 14:59:03 +0100232 }
Akronba09ed22020-10-01 16:01:45 +0200233
Akronec4bbfa2021-09-15 15:00:59 +0200234 // TODO There is no possibility to add icons to a plugin toggle button right now.
Akron22598cd2019-12-09 14:59:03 +0100235 else if (onClick["action"] == "toggle") {
236
Akron237abc42020-10-07 14:14:52 +0200237 // 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.
Akronda32e7a2021-11-16 17:28:57 +0100241 let state = states.newState(
242 (onClick["state"] ? onClick["state"] : name),
243 [true, false],
244 onClick["default"]
245 );
Akrona70b6892021-11-04 14:23:24 +0100246
Akron22598cd2019-12-09 14:59:03 +0100247 // TODO:
248 // Lazy registration (see above!)
Akron37ea1192021-07-28 10:40:14 +0200249 KorAP.Panel[panel].actions().addToggle(title, {'cls':["title"]}, state);
Akron22598cd2019-12-09 14:59:03 +0100250
Akron22598cd2019-12-09 14:59:03 +0100251 // Add the service
Akronbb891982020-10-05 16:07:18 +0200252 let id = this.addService({
253 "name" : name,
254 // TODO:
255 // Use the "service" keyword
256 "src" : onClick["template"],
257 "permissions" : onClick["permissions"]
258 });
Akronfb11a962020-10-05 12:12:55 +0200259
Akron22598cd2019-12-09 14:59:03 +0100260 // TODO:
261 // This is a bit stupid to get the service window
Akronc3003642020-03-30 10:19:14 +0200262 let service = services[id];
263 let iframe = service.load();
Akron22598cd2019-12-09 14:59:03 +0100264
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) {
Akronc3003642020-03-30 10:19:14 +0200270 service.sendMsg({
Akron22598cd2019-12-09 14:59:03 +0100271 action: 'state',
272 key : onClick['state'],
273 value : val
Akronc3003642020-03-30 10:19:14 +0200274 });
Akron22598cd2019-12-09 14:59:03 +0100275 }
276 };
277
278 // Associate object with the state
279 state.associate(sendToggle);
280 };
281
282 plugin["services"].push(id);
Akron7c6e05f2018-07-12 19:08:13 +0200283 };
Akron678c26f2020-10-09 08:52:50 +0200284 }, this);
Akron7c6e05f2018-07-12 19:08:13 +0200285 },
286
Akron7c6e05f2018-07-12 19:08:13 +0200287 // 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 /**
Akron4a703872018-07-26 10:59:41 +0200296 * Get named button group - better rename to "action"
Akron7c6e05f2018-07-12 19:08:13 +0200297 */
298 buttonGroup : function (name) {
Akron2d0d96d2019-11-18 19:49:50 +0100299 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 }
Akron7c6e05f2018-07-12 19:08:13 +0200316 },
Akron479994e2018-07-02 13:21:44 +0200317
Akron22598cd2019-12-09 14:59:03 +0100318 // Optionally initialize the service mechanism and get an ID
319 _getServiceID : function () {
Akron4a703872018-07-26 10:59:41 +0200320
Akron22598cd2019-12-09 14:59:03 +0100321 // Is it the first service?
Akron76dd8d32018-07-06 09:30:22 +0200322 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
Akron22598cd2019-12-09 14:59:03 +0100330 // Every second increase the limits of all registered services
Akron76dd8d32018-07-06 09:30:22 +0200331 this._timer = window.setInterval(function () {
Akron678c26f2020-10-09 08:52:50 +0200332 for (let i = 0; i < limits.length; i++) {
Akron76dd8d32018-07-06 09:30:22 +0200333 if (limits[i]++ >= maxMessages) {
334 limits[i] = maxMessages;
335 }
336 }
337 }, 1000);
338 };
339
Akron22598cd2019-12-09 14:59:03 +0100340 // 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 */
Akronbb891982020-10-05 16:07:18 +0200347 addService : function (data) {
348 if (!data["src"])
Akron22598cd2019-12-09 14:59:03 +0100349 return;
350
351 let id = this._getServiceID();
352
Akronbb891982020-10-05 16:07:18 +0200353 data["id"] = id;
354
Akron22598cd2019-12-09 14:59:03 +0100355 // Create a new service
Akronbb891982020-10-05 16:07:18 +0200356 let service = serviceClass.create(data);
Akron22598cd2019-12-09 14:59:03 +0100357
Akron22598cd2019-12-09 14:59:03 +0100358 services[id] = service;
359 limits[id] = maxMessages;
360
Akron22598cd2019-12-09 14:59:03 +0100361 // 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 */
Akronbb891982020-10-05 16:07:18 +0200374 addWidget : function (panel, data) {
375 // panel, name, src, permissions
Akron22598cd2019-12-09 14:59:03 +0100376
377 let id = this._getServiceID();
Akrona6c32b92018-07-02 18:39:42 +0200378
Akronbb891982020-10-05 16:07:18 +0200379 data["id"] = id;
380
Akrona6c32b92018-07-02 18:39:42 +0200381 // Create a new widget
Akronbb891982020-10-05 16:07:18 +0200382 var widget = widgetClass.create(data);
Akrona6c32b92018-07-02 18:39:42 +0200383
384 // Store the widget based on the identifier
Akron22598cd2019-12-09 14:59:03 +0100385 services[id] = widget;
Akrona99315e2018-07-03 22:56:45 +0200386 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +0200387
Akron4a703872018-07-26 10:59:41 +0200388 widget._mgr = this;
389
Akrone1c27f62018-07-20 11:42:59 +0200390 // Add widget to panel
391 panel.add(widget);
Akronb43c8c62018-07-04 18:27:28 +0200392
393 return id;
Akron479994e2018-07-02 13:21:44 +0200394 },
395
Akron4a703872018-07-26 10:59:41 +0200396
397 /**
Akron22598cd2019-12-09 14:59:03 +0100398 * Get service by identifier
Akron4a703872018-07-26 10:59:41 +0200399 */
Akron22598cd2019-12-09 14:59:03 +0100400 service : function (id) {
401 return services[id];
Akron4a703872018-07-26 10:59:41 +0200402 },
403
404
Akron22598cd2019-12-09 14:59:03 +0100405 // Receive a call from an embedded service.
Akrone8e2c952018-07-04 13:43:12 +0200406 // The handling needs to be very careful,
407 // as this can easily become a security nightmare.
Akron479994e2018-07-02 13:21:44 +0200408 _receiveMsg : function (e) {
Akronbc94a9c2021-04-15 00:07:35 +0200409
Akron479994e2018-07-02 13:21:44 +0200410 // Get event data
411 var d = e.data;
412
Akrona99315e2018-07-03 22:56:45 +0200413 // 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!
Akronbc94a9c2021-04-15 00:07:35 +0200419 // TODO: Check e.origin is in the list of registered participants
420 // if (e.origin !== "http://example.com:8080")
421 // return;
Akron479994e2018-07-02 13:21:44 +0200422
Akronbc94a9c2021-04-15 00:07:35 +0200423
Akrona99315e2018-07-03 22:56:45 +0200424 // Get origin ID
425 var id = d["originID"];
426
427 // If no origin ID given - fail
428 if (!id)
429 return;
430
Akron22598cd2019-12-09 14:59:03 +0100431 // Get the service
432 let service = services[id];
Akrona6c32b92018-07-02 18:39:42 +0200433
Akron22598cd2019-12-09 14:59:03 +0100434 // If the addressed service does not exist - fail
435 if (!service)
Akrona6c32b92018-07-02 18:39:42 +0200436 return;
437
Akrona99315e2018-07-03 22:56:45 +0200438 // Check for message limits
439 if (limits[id]-- < 0) {
Akrone8e2c952018-07-04 13:43:12 +0200440
Akron22598cd2019-12-09 14:59:03 +0100441 // Kill service
442 KorAP.log(0, 'Suspicious action by service', service.src);
Akron7c6e05f2018-07-12 19:08:13 +0200443
444 // TODO:
445 // Potentially kill the whole plugin!
Akron4a703872018-07-26 10:59:41 +0200446
Akron22598cd2019-12-09 14:59:03 +0100447 // This removes all connections before closing the service
448 this._closeService(service.id);
449
450 // if (service.isWidget)
451 service.close();
452
Akrona99315e2018-07-03 22:56:45 +0200453 return;
454 };
Akrona6c32b92018-07-02 18:39:42 +0200455
Akron479994e2018-07-02 13:21:44 +0200456 // Resize the iframe
Akron22598cd2019-12-09 14:59:03 +0100457 switch (d.action) {
458 case 'resize':
459 if (service.isWidget)
460 service.resize(d);
461 break;
Akron479994e2018-07-02 13:21:44 +0200462
463 // Log message from iframe
Akron22598cd2019-12-09 14:59:03 +0100464 case 'log':
465 KorAP.log(d.code, d.msg, service.src);
466 break;
Akron51ee6232019-12-17 21:00:05 +0100467
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;
Akronc3003642020-03-30 10:19:14 +0200478
479 // Get information from the embedding platform
480 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200481
482 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200483 if (d.key == 'KQ') {
484 if (KorAP.koralQuery !== undefined) {
485 d["value"] = KorAP.koralQuery;
486 };
Akron45308ce2020-08-28 14:10:23 +0200487 }
488
Akron26d57f22021-09-10 16:48:57 +0200489 // Get Query information from form
Akron45308ce2020-08-28 14:10:23 +0200490 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 };
Akron432972b2020-09-18 17:05:53 +0200504 }
505
Akron338b4d42022-12-20 13:59:22 +0100506 // Get text sigle from match
507 else if (d.key == 'textSigle') {
508 if (service.panel.type != "match") {
509 KorAP.log(0, "Service can only be called on matches", service.src);
510 return;
511 };
512 let v = d["value"] = {};
513 v["value"] = service.panel._match.textSigle;
514 }
515
Akron432972b2020-09-18 17:05:53 +0200516 // Get Query information from parameters
517 else if (d.key == 'QueryParam') {
518
519 // Supported in all modern browsers
520 var p = new URLSearchParams(window.location.search);
521 let v = d["value"] = {};
Akron4de759f2021-10-13 10:46:45 +0200522 v["search"] = window.location.search; // readonly
Akron432972b2020-09-18 17:05:53 +0200523 v["q"] = p.get('q');
524 v["ql"] = p.get('ql');
525 v["cq"] = p.get('cq');
Akron26d57f22021-09-10 16:48:57 +0200526 }
527
528 // Get pagination information
529 else if (d.key == 'Pagination') {
530 const pi = pageInfoClass.create();
531 let v = d["value"] = {};
532 v["page"] = pi.page();
533 v["total"] = pi.total();
534 v["count"] = pi.count();
Akronec4bbfa2021-09-15 15:00:59 +0200535 };
Akronc3003642020-03-30 10:19:14 +0200536
Akronec4bbfa2021-09-15 15:00:59 +0200537 // data needs to be mirrored
538 if (d._id) {
539 service.sendMsg(d);
540 };
541
542 break;
543
544 // Redirect to a different page relative to the current
545 case 'redirect':
546 const url = new URL(window.location);
547
548 // Currently this only accepts search parameters
549 if (d["queryParam"]) {
550 url.search = new URLSearchParams(d["queryParam"]);
551 };
552
553 window.location = url.toString();
554 break;
Akrona6c32b92018-07-02 18:39:42 +0200555 };
556
557 // TODO:
558 // Close
Akron479994e2018-07-02 13:21:44 +0200559 },
560
Akron22598cd2019-12-09 14:59:03 +0100561 // Close the service
562 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200563 delete limits[id];
Akronda32e7a2021-11-16 17:28:57 +0100564
Akron22598cd2019-12-09 14:59:03 +0100565 // Close the iframe
566 if (services[id] && services[id]._closeIframe) {
Akronda32e7a2021-11-16 17:28:57 +0100567
Akron22598cd2019-12-09 14:59:03 +0100568 services[id]._closeIframe();
569
570 // Remove from list
571 delete services[id];
572 };
573
Akron76dd8d32018-07-06 09:30:22 +0200574
575 // Remove listeners in case no widget
576 // is available any longer
577 if (Object.keys(limits).length == 0)
578 this._removeListener();
579 },
580
Akrona6c32b92018-07-02 18:39:42 +0200581 // Get a random identifier
582 _randomID : function () {
583 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200584 },
585
Akron76dd8d32018-07-06 09:30:22 +0200586 // Remove the listener
587 _removeListener : function () {
588 window.clearInterval(this._timer);
589 this._timer = undefined;
590 window.removeEventListener("message", this._listener);
591 this._listener = undefined;
592 },
593
Akron22598cd2019-12-09 14:59:03 +0100594 /**
595 * Return the service element.
596 */
597 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100598 if (!this._el) {
599 this._el = document.createElement('div');
600 this._el.setAttribute("id", "services");
Akron22598cd2019-12-09 14:59:03 +0100601 }
Akron24aa0052020-11-10 11:00:34 +0100602 return this._el;
Akron22598cd2019-12-09 14:59:03 +0100603 },
Akronda32e7a2021-11-16 17:28:57 +0100604
605
606 // Return states object
607 states : function () {
608 return states;
609 },
610
Akronb43c8c62018-07-04 18:27:28 +0200611 // Destructor, just for testing scenarios
612 destroy : function () {
613 limits = {};
Akron678c26f2020-10-09 08:52:50 +0200614 Object.keys(services).forEach(
615 s => services[s].close()
616 );
Akron22598cd2019-12-09 14:59:03 +0100617 services = {};
Akron678c26f2020-10-09 08:52:50 +0200618 Object.keys(buttons).forEach(
619 b => buttons[b] = []
620 );
621 Object.keys(buttonsSingle).forEach(
622 b => buttonsSingle[b] = []
623 );
Akron22598cd2019-12-09 14:59:03 +0100624
Akron24aa0052020-11-10 11:00:34 +0100625 if (this._el) {
626 let e = this._el;
Akron22598cd2019-12-09 14:59:03 +0100627 if (e.parentNode) {
628 e.parentNode.removeChild(e);
629 };
Akron24aa0052020-11-10 11:00:34 +0100630 this._el = null;
Akron22598cd2019-12-09 14:59:03 +0100631 };
Akron76dd8d32018-07-06 09:30:22 +0200632 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200633 }
Akrone1c27f62018-07-20 11:42:59 +0200634 };
Akron479994e2018-07-02 13:21:44 +0200635});