blob: bdc48735bdc1fe043a78837a9445878d2fc22fc5 [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
Akron7d18d8e2024-11-08 11:08:48 +010016 const d = document;
Akroned223be2024-12-10 13:01:46 +010017
Akron22598cd2019-12-09 14:59:03 +010018 // Contains all servicess to address with
Akrona6c32b92018-07-02 18:39:42 +020019 // messages to them
Akron22598cd2019-12-09 14:59:03 +010020 var services = {};
21 var plugins = {};
Akron10a47962018-07-12 21:17:10 +020022
23 // TODO:
24 // These should better be panels and every panel
25 // has a buttonGroup
Akron2d0d96d2019-11-18 19:49:50 +010026
27 // List of panels with dynamic buttons, i.e.
28 // panels that may occur multiple times.
Akron7c6e05f2018-07-12 19:08:13 +020029 var buttons = {
30 match : []
31 };
Akron2d0d96d2019-11-18 19:49:50 +010032
33 // List of panels with static buttons, i.e.
34 // panels that occur only once.
35 var buttonsSingle = {
hebasta043e96f2019-11-28 12:33:00 +010036 query : [],
37 result : []
Akron22598cd2019-12-09 14:59:03 +010038 }
Akron10a47962018-07-12 21:17:10 +020039
Akrone8e2c952018-07-04 13:43:12 +020040 // This is a counter to limit acceptable incoming messages
41 // to a certain amount. For every message, this counter will
42 // be decreased (down to 0), for every second this will be
43 // increased (up to 100).
44 // Once a widget surpasses the limit, it will be killed
45 // and called suspicious.
46 var maxMessages = 100;
47 var limits = {};
48
49 // TODO:
50 // It may be useful to establish a watcher that pings
Akrone1c27f62018-07-20 11:42:59 +020051 // all widgets every second to see if it is still alive,
52 // otherwise kill
Akrone8e2c952018-07-04 13:43:12 +020053
Akrone1c27f62018-07-20 11:42:59 +020054 // Load Plugin server
Akron479994e2018-07-02 13:21:44 +020055 return {
56
57 /**
58 * Create new plugin management system
59 */
60 create : function () {
61 return Object.create(this)._init();
62 },
63
64 /*
Akron76dd8d32018-07-06 09:30:22 +020065 * Initialize the plugin manager
Akron479994e2018-07-02 13:21:44 +020066 */
67 _init : function () {
Akron7d18d8e2024-11-08 11:08:48 +010068 this._ql = d.getElementById("ql-field");
69 this._q = d.getElementById("q-field")
70 this._cutoff = d.getElementById("q-cutoff-field");
71
Akroned223be2024-12-10 13:01:46 +010072 // State manager undefined
73 this._states = KorAP.States ? KorAP.States :
74 // Not serialized state manager
75 stateManagerClass.create(document.createElement('input'));
76
Akron479994e2018-07-02 13:21:44 +020077 return this;
78 },
79
80 /**
Akron7c6e05f2018-07-12 19:08:13 +020081 * Register a plugin described as a JSON object.
82 *
83 * This is work in progress.
Akron10a47962018-07-12 21:17:10 +020084 *
85 * Example:
86 *
87 * KorAP.Plugin.register({
88 * 'name' : 'CatContent',
89 * 'desc' : 'Some content about cats',
90 * 'about' : 'https://localhost:5678/',
91 * 'embed' : [{
92 * 'panel' : 'match',
93 * 'title' : loc.TRANSLATE,
94 * 'classes' : ['translate']
95 * 'onClick' : {
96 * 'action' : 'addWidget',
97 * 'template' : 'https://localhost:5678/?match={matchid}',
98 * }
Akron22598cd2019-12-09 14:59:03 +010099 * },{
100 * 'title' : 'glemm',
101 * 'panel' : 'query',
102 * 'onClick' : {
103 * 'action' : 'toggle',
104 * 'state' : 'glemm',
105 * 'service' : {
106 * 'id' : 'glemm',
107 * 'template' : 'https://localhost:5678/'
108 * }
109 * }
Akron10a47962018-07-12 21:17:10 +0200110 * }]
111 * });
112 *
Akron7c6e05f2018-07-12 19:08:13 +0200113 */
114 register : function (obj) {
Akron7c6e05f2018-07-12 19:08:13 +0200115 // TODO:
Akron10a47962018-07-12 21:17:10 +0200116 // These fields need to be localized for display by a structure like
117 // { de : { name : '..' }, en : { .. } }
Akronda32e7a2021-11-16 17:28:57 +0100118 const name = obj["name"];
Akron7c6e05f2018-07-12 19:08:13 +0200119
Akron10a47962018-07-12 21:17:10 +0200120 if (!name)
121 throw new Error("Missing name of plugin");
122
Akron3d013802020-10-07 15:03:38 +0200123 var desc = obj["desc"];
124
Akron7c6e05f2018-07-12 19:08:13 +0200125 // Register plugin by name
126 var plugin = plugins[name] = {
127 name : name,
Akron3d013802020-10-07 15:03:38 +0200128 desc : desc,
Akron7c6e05f2018-07-12 19:08:13 +0200129 about : obj["about"],
Akron22598cd2019-12-09 14:59:03 +0100130 widgets : [],
131 services : []
Akron7c6e05f2018-07-12 19:08:13 +0200132 };
Akron10a47962018-07-12 21:17:10 +0200133
134 if (typeof obj["embed"] !== 'object')
135 throw new Error("Embedding of plugin is no list");
Akron7c6e05f2018-07-12 19:08:13 +0200136
137 // Embed all embeddings of the plugin
Akrone1c27f62018-07-20 11:42:59 +0200138 var that = this;
Akron678c26f2020-10-09 08:52:50 +0200139 obj["embed"].forEach(function(embed) {
Akron10a47962018-07-12 21:17:10 +0200140
141 if (typeof embed !== 'object')
142 throw new Error("Embedding of plugin is no object");
143
Akron7c6e05f2018-07-12 19:08:13 +0200144 // Needs to be localized as well
Akron22598cd2019-12-09 14:59:03 +0100145 let title = embed["title"];
146 let panel = embed["panel"];
147 let onClick = embed["onClick"];
hebasta40a85cf2020-07-15 18:10:08 +0200148 let icon = embed["icon"];
149
Akron22598cd2019-12-09 14:59:03 +0100150 if (!panel || !(buttons[panel] || buttonsSingle[panel]))
Akronba09ed22020-10-01 16:01:45 +0200151 throw new Error("Panel for plugin is invalid");
Akron7c6e05f2018-07-12 19:08:13 +0200152
153 // The embedding will open a widget
Akronba09ed22020-10-01 16:01:45 +0200154 if (!onClick["action"] ||
155 onClick["action"] == "addWidget" ||
156 onClick["action"] == "setWidget") {
Akron22598cd2019-12-09 14:59:03 +0100157
158 let cb = function (e) {
Akron7c6e05f2018-07-12 19:08:13 +0200159
Akrone1c27f62018-07-20 11:42:59 +0200160 // "this" is bind to the panel
Akronba09ed22020-10-01 16:01:45 +0200161 // "this".button is the button
162 // "that" is the server object
Akrone1c27f62018-07-20 11:42:59 +0200163
Akronba09ed22020-10-01 16:01:45 +0200164 // The button has a state and the state is associated to the
165 // a intermediate object to toggle the view
166 if ('state' in this.button && this.button.state.associates() > 0) {
167
Akronda32e7a2021-11-16 17:28:57 +0100168 const s = this.button.state;
Akronfcf89db2020-10-01 17:40:20 +0200169
170 // The associated service is existent
171 if (services[this.button['widgetID']]) {
Akron237abc42020-10-07 14:14:52 +0200172 s.roll();
Akronfcf89db2020-10-01 17:40:20 +0200173 return;
174 }
175
176 // The service is not existent
177 else {
178
179 // Remove broken state associations
180 s.clear();
Akronba09ed22020-10-01 16:01:45 +0200181 s.set(true);
Akronfcf89db2020-10-01 17:40:20 +0200182 }
Akronba09ed22020-10-01 16:01:45 +0200183 };
184
Akron7c6e05f2018-07-12 19:08:13 +0200185 // Add the widget to the panel
Akronbb891982020-10-05 16:07:18 +0200186 let id = that.addWidget(this, {
187 "name": name,
188 "src": onClick["template"], // that._interpolateURI(onClick["template"], this.match);
Akron3d013802020-10-07 15:03:38 +0200189 "permissions": onClick["permissions"],
190 "desc":desc
Akronbb891982020-10-05 16:07:18 +0200191 });
Akron7c6e05f2018-07-12 19:08:13 +0200192 plugin["widgets"].push(id);
Akronba09ed22020-10-01 16:01:45 +0200193
194 // If a state exists, associate with a mediator object
195 if ('state' in this.button) {
Akronfcf89db2020-10-01 17:40:20 +0200196 this.button['widgetID'] = id;
Akronba09ed22020-10-01 16:01:45 +0200197 this.button.state.associate({
198 setState : function (value) {
199 // Minimize the widget
200 if (value == false) {
201 services[id].minimize();
202 }
203 else {
204 services[id].show();
205 };
206 }
207 });
208 }
Akron7c6e05f2018-07-12 19:08:13 +0200209 };
210
Akron22598cd2019-12-09 14:59:03 +0100211
Akronba09ed22020-10-01 16:01:45 +0200212 // Button object
Akron83a58bc2024-11-08 09:55:19 +0100213 let obj = {'cls':embed["classes"], 'icon': icon };
214
215 if (embed['desc'] != undefined)
216 obj['desc'] = embed['desc'];
Akronba09ed22020-10-01 16:01:45 +0200217
218 if (onClick["action"] && onClick["action"] == "setWidget") {
219
Akronda32e7a2021-11-16 17:28:57 +0100220 // Create a boolean state value,
221 // that initializes to true == opened
Akron237abc42020-10-07 14:14:52 +0200222 obj['state'] = stateClass.create([true, false]);
Akronda32e7a2021-11-16 17:28:57 +0100223 obj['state'].setIfNotYet(true);
Akronba09ed22020-10-01 16:01:45 +0200224 };
225
Akron2d0d96d2019-11-18 19:49:50 +0100226 // Add to dynamic button list (e.g. for matches)
227 if (buttons[panel]) {
Akronba09ed22020-10-01 16:01:45 +0200228 buttons[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100229 }
230
231 // Add to static button list (e.g. for query) already loaded
232 else if (KorAP.Panel[panel]) {
Akron37ea1192021-07-28 10:40:14 +0200233 KorAP.Panel[panel].actions().add(title, obj, cb);
Akron2d0d96d2019-11-18 19:49:50 +0100234 }
235
236 // Add to static button list (e.g. for query) not yet loaded
237 else {
Akronba09ed22020-10-01 16:01:45 +0200238 buttonsSingle[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100239 }
Akron22598cd2019-12-09 14:59:03 +0100240 }
Akronba09ed22020-10-01 16:01:45 +0200241
Akron22598cd2019-12-09 14:59:03 +0100242 else if (onClick["action"] == "toggle") {
243
Akron237abc42020-10-07 14:14:52 +0200244 // TODO:
245 // Accept a "value" list here for toggling, which should
246 // also allow for "rolling" through states via CSS classes
247 // as 'toggle-true', 'toggle-false' etc.
Akroned223be2024-12-10 13:01:46 +0100248 let state = that._states.newState(
Akronda32e7a2021-11-16 17:28:57 +0100249 (onClick["state"] ? onClick["state"] : name),
250 [true, false],
251 onClick["default"]
252 );
Akrona70b6892021-11-04 14:23:24 +0100253
Akron83a58bc2024-11-08 09:55:19 +0100254 let obj = {'cls':["title"], 'icon': icon};
255
256 if (embed['desc'] != undefined)
257 obj['desc'] = embed['desc'];
258
Akron22598cd2019-12-09 14:59:03 +0100259 // TODO:
260 // Lazy registration (see above!)
Akron83a58bc2024-11-08 09:55:19 +0100261 KorAP.Panel[panel].actions().addToggle(title, obj, state);
Akron22598cd2019-12-09 14:59:03 +0100262
Akron22598cd2019-12-09 14:59:03 +0100263 // Add the service
Akronbb891982020-10-05 16:07:18 +0200264 let id = this.addService({
265 "name" : name,
266 // TODO:
267 // Use the "service" keyword
268 "src" : onClick["template"],
269 "permissions" : onClick["permissions"]
270 });
Akronfb11a962020-10-05 12:12:55 +0200271
Akron22598cd2019-12-09 14:59:03 +0100272 // TODO:
273 // This is a bit stupid to get the service window
Akronc3003642020-03-30 10:19:14 +0200274 let service = services[id];
275 let iframe = service.load();
Akron22598cd2019-12-09 14:59:03 +0100276
277 // Create object to communicate the toggle state
278 // once the iframe is loaded.
279 iframe.onload = function () {
280 let sendToggle = {
281 setState : function (val) {
Akronc3003642020-03-30 10:19:14 +0200282 service.sendMsg({
Akron22598cd2019-12-09 14:59:03 +0100283 action: 'state',
284 key : onClick['state'],
285 value : val
Akronc3003642020-03-30 10:19:14 +0200286 });
Akron22598cd2019-12-09 14:59:03 +0100287 }
288 };
289
290 // Associate object with the state
291 state.associate(sendToggle);
292 };
293
294 plugin["services"].push(id);
Akron7c6e05f2018-07-12 19:08:13 +0200295 };
Akron678c26f2020-10-09 08:52:50 +0200296 }, this);
Akron7c6e05f2018-07-12 19:08:13 +0200297 },
298
Akron7c6e05f2018-07-12 19:08:13 +0200299 // TODO:
300 // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570
301 // but as simple as possible
302 _interpolateURI : function (uri, obj) {
303 // ...
304 },
305
306
307 /**
Akron4a703872018-07-26 10:59:41 +0200308 * Get named button group - better rename to "action"
Akron7c6e05f2018-07-12 19:08:13 +0200309 */
310 buttonGroup : function (name) {
Akron2d0d96d2019-11-18 19:49:50 +0100311 if (buttons[name] != undefined) {
312 return buttons[name];
313 } else if (buttonsSingle[name] != undefined) {
314 return buttonsSingle[name];
315 };
316 return [];
317 },
318
319 /**
320 * Clear named button group - better rename to "action"
321 */
322 clearButtonGroup : function (name) {
323 if (buttons[name] != undefined) {
324 buttons[name] = [];
325 } else if (buttonsSingle[name] != undefined) {
326 buttonsSingle[name] = [];
327 }
Akron7c6e05f2018-07-12 19:08:13 +0200328 },
Akron479994e2018-07-02 13:21:44 +0200329
Akron22598cd2019-12-09 14:59:03 +0100330 // Optionally initialize the service mechanism and get an ID
331 _getServiceID : function () {
Akron4a703872018-07-26 10:59:41 +0200332
Akron22598cd2019-12-09 14:59:03 +0100333 // Is it the first service?
Akron76dd8d32018-07-06 09:30:22 +0200334 if (!this._listener) {
335
336 /*
337 * Establish the global 'message' hook.
338 */
339 this._listener = this._receiveMsg.bind(this);
340 window.addEventListener("message", this._listener);
341
Akron22598cd2019-12-09 14:59:03 +0100342 // Every second increase the limits of all registered services
Akron76dd8d32018-07-06 09:30:22 +0200343 this._timer = window.setInterval(function () {
Akron678c26f2020-10-09 08:52:50 +0200344 for (let i = 0; i < limits.length; i++) {
Akron76dd8d32018-07-06 09:30:22 +0200345 if (limits[i]++ >= maxMessages) {
346 limits[i] = maxMessages;
347 }
348 }
349 }, 1000);
350 };
351
Akron22598cd2019-12-09 14:59:03 +0100352 // Create a unique random ID per service
353 return 'id-' + this._randomID();
354 },
355
356 /**
357 * Add a service in a certain panel and return the id.
358 */
Akronbb891982020-10-05 16:07:18 +0200359 addService : function (data) {
360 if (!data["src"])
Akron22598cd2019-12-09 14:59:03 +0100361 return;
362
363 let id = this._getServiceID();
364
Akronbb891982020-10-05 16:07:18 +0200365 data["id"] = id;
366
Akron22598cd2019-12-09 14:59:03 +0100367 // Create a new service
Akronbb891982020-10-05 16:07:18 +0200368 let service = serviceClass.create(data);
Akron22598cd2019-12-09 14:59:03 +0100369
Akron22598cd2019-12-09 14:59:03 +0100370 services[id] = service;
371 limits[id] = maxMessages;
372
Akron22598cd2019-12-09 14:59:03 +0100373 // Add service to panel
374 this.element().appendChild(
375 service.load()
376 );
377
378 return id;
379 },
380
381
382 /**
383 * Open a new widget view in a certain panel and return
384 * the id.
385 */
Akronbb891982020-10-05 16:07:18 +0200386 addWidget : function (panel, data) {
387 // panel, name, src, permissions
Akron22598cd2019-12-09 14:59:03 +0100388
389 let id = this._getServiceID();
Akrona6c32b92018-07-02 18:39:42 +0200390
Akronbb891982020-10-05 16:07:18 +0200391 data["id"] = id;
392
Akrona6c32b92018-07-02 18:39:42 +0200393 // Create a new widget
Akronbb891982020-10-05 16:07:18 +0200394 var widget = widgetClass.create(data);
Akrona6c32b92018-07-02 18:39:42 +0200395
396 // Store the widget based on the identifier
Akron22598cd2019-12-09 14:59:03 +0100397 services[id] = widget;
Akrona99315e2018-07-03 22:56:45 +0200398 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +0200399
Akron4a703872018-07-26 10:59:41 +0200400 widget._mgr = this;
401
Akrone1c27f62018-07-20 11:42:59 +0200402 // Add widget to panel
403 panel.add(widget);
Akronb43c8c62018-07-04 18:27:28 +0200404
405 return id;
Akron479994e2018-07-02 13:21:44 +0200406 },
407
Akron4a703872018-07-26 10:59:41 +0200408
409 /**
Akron22598cd2019-12-09 14:59:03 +0100410 * Get service by identifier
Akron4a703872018-07-26 10:59:41 +0200411 */
Akron22598cd2019-12-09 14:59:03 +0100412 service : function (id) {
413 return services[id];
Akron4a703872018-07-26 10:59:41 +0200414 },
415
416
Akron22598cd2019-12-09 14:59:03 +0100417 // Receive a call from an embedded service.
Akrone8e2c952018-07-04 13:43:12 +0200418 // The handling needs to be very careful,
419 // as this can easily become a security nightmare.
Akron479994e2018-07-02 13:21:44 +0200420 _receiveMsg : function (e) {
Akronbc94a9c2021-04-15 00:07:35 +0200421
Akron479994e2018-07-02 13:21:44 +0200422 // Get event data
423 var d = e.data;
424
Akrona99315e2018-07-03 22:56:45 +0200425 // If no data given - fail
426 // (probably check that it's an assoc array)
427 if (!d)
428 return;
429
430 // e.origin is probably set and okay - CHECK!
Akronbc94a9c2021-04-15 00:07:35 +0200431 // TODO: Check e.origin is in the list of registered participants
432 // if (e.origin !== "http://example.com:8080")
433 // return;
Akron479994e2018-07-02 13:21:44 +0200434
Akronbc94a9c2021-04-15 00:07:35 +0200435
Akrona99315e2018-07-03 22:56:45 +0200436 // Get origin ID
437 var id = d["originID"];
438
439 // If no origin ID given - fail
440 if (!id)
441 return;
442
Akron22598cd2019-12-09 14:59:03 +0100443 // Get the service
444 let service = services[id];
Akrona6c32b92018-07-02 18:39:42 +0200445
Akron22598cd2019-12-09 14:59:03 +0100446 // If the addressed service does not exist - fail
447 if (!service)
Akrona6c32b92018-07-02 18:39:42 +0200448 return;
449
Akrona99315e2018-07-03 22:56:45 +0200450 // Check for message limits
451 if (limits[id]-- < 0) {
Akrone8e2c952018-07-04 13:43:12 +0200452
Akron22598cd2019-12-09 14:59:03 +0100453 // Kill service
454 KorAP.log(0, 'Suspicious action by service', service.src);
Akron7c6e05f2018-07-12 19:08:13 +0200455
456 // TODO:
457 // Potentially kill the whole plugin!
Akron4a703872018-07-26 10:59:41 +0200458
Akron22598cd2019-12-09 14:59:03 +0100459 // This removes all connections before closing the service
460 this._closeService(service.id);
461
462 // if (service.isWidget)
463 service.close();
464
Akrona99315e2018-07-03 22:56:45 +0200465 return;
466 };
Akrona6c32b92018-07-02 18:39:42 +0200467
Akron479994e2018-07-02 13:21:44 +0200468 // Resize the iframe
Akron22598cd2019-12-09 14:59:03 +0100469 switch (d.action) {
470 case 'resize':
471 if (service.isWidget)
472 service.resize(d);
473 break;
Akron479994e2018-07-02 13:21:44 +0200474
475 // Log message from iframe
Akron22598cd2019-12-09 14:59:03 +0100476 case 'log':
477 KorAP.log(d.code, d.msg, service.src);
478 break;
Akron51ee6232019-12-17 21:00:05 +0100479
480 // Modify pipes
481 case 'pipe':
482 if (KorAP.Pipe != undefined) {
483 if (d.job == 'del') {
484 KorAP.Pipe.remove(d.service);
485 } else {
486 KorAP.Pipe.append(d.service);
487 };
488 };
489 break;
Akronc3003642020-03-30 10:19:14 +0200490
491 // Get information from the embedding platform
492 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200493
494 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200495 if (d.key == 'KQ') {
496 if (KorAP.koralQuery !== undefined) {
497 d["value"] = KorAP.koralQuery;
498 };
Akron45308ce2020-08-28 14:10:23 +0200499 }
500
Akron26d57f22021-09-10 16:48:57 +0200501 // Get Query information from form
Akron45308ce2020-08-28 14:10:23 +0200502 else if (d.key == 'QueryForm') {
503 let doc = document;
504 let v = d["value"] = {};
505
506 var el;
507 if (el = doc.getElementById('q-field')) {
508 v["q"] = el.value;
509 };
510 if (el = doc.getElementById('ql-field')) {
511 v["ql"] = el.value;
512 };
513 if (el = KorAP.vc) {
514 v["cq"] = el.toQuery();
515 };
Akronb759ee92024-11-19 18:02:56 +0100516 if (el = KorAP.Hint) {
517 v["selection"] = KorAP.Hint.selectionRange();
518 };
Akron432972b2020-09-18 17:05:53 +0200519 }
520
Akron338b4d42022-12-20 13:59:22 +0100521 // Get text sigle from match
522 else if (d.key == 'textSigle') {
523 if (service.panel.type != "match") {
524 KorAP.log(0, "Service can only be called on matches", service.src);
525 return;
526 };
527 let v = d["value"] = {};
528 v["value"] = service.panel._match.textSigle;
529 }
530
Akron432972b2020-09-18 17:05:53 +0200531 // Get Query information from parameters
532 else if (d.key == 'QueryParam') {
533
534 // Supported in all modern browsers
535 var p = new URLSearchParams(window.location.search);
536 let v = d["value"] = {};
Akron4de759f2021-10-13 10:46:45 +0200537 v["search"] = window.location.search; // readonly
Akron432972b2020-09-18 17:05:53 +0200538 v["q"] = p.get('q');
539 v["ql"] = p.get('ql');
540 v["cq"] = p.get('cq');
Akron26d57f22021-09-10 16:48:57 +0200541 }
542
543 // Get pagination information
544 else if (d.key == 'Pagination') {
545 const pi = pageInfoClass.create();
546 let v = d["value"] = {};
547 v["page"] = pi.page();
548 v["total"] = pi.total();
549 v["count"] = pi.count();
Akronec4bbfa2021-09-15 15:00:59 +0200550 };
Akronc3003642020-03-30 10:19:14 +0200551
Akronec4bbfa2021-09-15 15:00:59 +0200552 // data needs to be mirrored
553 if (d._id) {
554 service.sendMsg(d);
555 };
556
557 break;
558
Akron7d18d8e2024-11-08 11:08:48 +0100559 case 'set':
Akrone03faf62024-11-14 11:51:04 +0100560
561 // Get Query information from data
Akron7d18d8e2024-11-08 11:08:48 +0100562 if (d.key == 'QueryForm') {
563 let v = d["value"];
Akrone03faf62024-11-14 11:51:04 +0100564
Akron7d18d8e2024-11-08 11:08:48 +0100565 if (v["q"] != undefined && this._q) {
566 this._q.value = v["q"];
567 };
568
569 // Set query language field
570 // Identical to tutorial.js
Akrone03faf62024-11-14 11:51:04 +0100571 if (v["ql"] != undefined) {
572 if (KorAP.QLmenu) {
573 KorAP.QLmenu.selectValue(v["ql"]);
574 }
Akron7d18d8e2024-11-08 11:08:48 +0100575
Akrone03faf62024-11-14 11:51:04 +0100576 else if (this._ql) {
577 let found = Array.from(this._ql.options).find(o => o.value === v["ql"]);
578 if (found)
579 found.selected = true;
580 };
Akron7d18d8e2024-11-08 11:08:48 +0100581 };
582
583 window.scrollTo(0, 0);
584 // if (v["cq"] != undefined) {};
585 }
586
587 break;
588
Akronec4bbfa2021-09-15 15:00:59 +0200589 // Redirect to a different page relative to the current
590 case 'redirect':
591 const url = new URL(window.location);
592
593 // Currently this only accepts search parameters
594 if (d["queryParam"]) {
595 url.search = new URLSearchParams(d["queryParam"]);
596 };
597
598 window.location = url.toString();
599 break;
Akrona6c32b92018-07-02 18:39:42 +0200600 };
601
602 // TODO:
603 // Close
Akron479994e2018-07-02 13:21:44 +0200604 },
605
Akron22598cd2019-12-09 14:59:03 +0100606 // Close the service
607 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200608 delete limits[id];
Akronda32e7a2021-11-16 17:28:57 +0100609
Akron22598cd2019-12-09 14:59:03 +0100610 // Close the iframe
611 if (services[id] && services[id]._closeIframe) {
Akronda32e7a2021-11-16 17:28:57 +0100612
Akron22598cd2019-12-09 14:59:03 +0100613 services[id]._closeIframe();
614
615 // Remove from list
616 delete services[id];
617 };
618
Akron76dd8d32018-07-06 09:30:22 +0200619
620 // Remove listeners in case no widget
621 // is available any longer
622 if (Object.keys(limits).length == 0)
623 this._removeListener();
624 },
625
Akrona6c32b92018-07-02 18:39:42 +0200626 // Get a random identifier
627 _randomID : function () {
628 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200629 },
630
Akron76dd8d32018-07-06 09:30:22 +0200631 // Remove the listener
632 _removeListener : function () {
633 window.clearInterval(this._timer);
634 this._timer = undefined;
635 window.removeEventListener("message", this._listener);
636 this._listener = undefined;
637 },
638
Akron22598cd2019-12-09 14:59:03 +0100639 /**
640 * Return the service element.
641 */
642 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100643 if (!this._el) {
644 this._el = document.createElement('div');
645 this._el.setAttribute("id", "services");
Akron22598cd2019-12-09 14:59:03 +0100646 }
Akron24aa0052020-11-10 11:00:34 +0100647 return this._el;
Akron22598cd2019-12-09 14:59:03 +0100648 },
Akronda32e7a2021-11-16 17:28:57 +0100649
650
651 // Return states object
652 states : function () {
Akroned223be2024-12-10 13:01:46 +0100653 return this._states;
Akronda32e7a2021-11-16 17:28:57 +0100654 },
655
Akronb43c8c62018-07-04 18:27:28 +0200656 // Destructor, just for testing scenarios
657 destroy : function () {
658 limits = {};
Akron678c26f2020-10-09 08:52:50 +0200659 Object.keys(services).forEach(
660 s => services[s].close()
661 );
Akron22598cd2019-12-09 14:59:03 +0100662 services = {};
Akron678c26f2020-10-09 08:52:50 +0200663 Object.keys(buttons).forEach(
664 b => buttons[b] = []
665 );
666 Object.keys(buttonsSingle).forEach(
667 b => buttonsSingle[b] = []
668 );
Akron22598cd2019-12-09 14:59:03 +0100669
Akron24aa0052020-11-10 11:00:34 +0100670 if (this._el) {
671 let e = this._el;
Akron22598cd2019-12-09 14:59:03 +0100672 if (e.parentNode) {
673 e.parentNode.removeChild(e);
674 };
Akron24aa0052020-11-10 11:00:34 +0100675 this._el = null;
Akron22598cd2019-12-09 14:59:03 +0100676 };
Akron76dd8d32018-07-06 09:30:22 +0200677 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200678 }
Akrone1c27f62018-07-20 11:42:59 +0200679 };
Akron479994e2018-07-02 13:21:44 +0200680});