blob: f8c98556e2db5a2ba4709fd788fbe8e0e1b34165 [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':
Akron910828a2025-06-27 15:38:48 +0200482 let j = d.job;
483 if (
484 ((j == 'del-after' || j == 'add-after') &&
485 KorAP.ResponsePipe == undefined) ||
486 KorAP.Pipe == undefined) {
487
488 KorAP.log(0,"No Pipe established");
489 break;
490 };
491
492 if (j == 'del') {
493 KorAP.Pipe.remove(d.service);
494 } else if (j == 'del-after') {
495 KorAP.ResponsePipe.remove(d.service);
496 } else if (j == 'add-after') {
497 KorAP.ResponsePipe.append(d.service);
498 } else {
499 KorAP.Pipe.append(d.service);
Akron51ee6232019-12-17 21:00:05 +0100500 };
501 break;
Akronc3003642020-03-30 10:19:14 +0200502
503 // Get information from the embedding platform
504 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200505
506 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200507 if (d.key == 'KQ') {
508 if (KorAP.koralQuery !== undefined) {
509 d["value"] = KorAP.koralQuery;
510 };
Akron45308ce2020-08-28 14:10:23 +0200511 }
512
Akron26d57f22021-09-10 16:48:57 +0200513 // Get Query information from form
Akron45308ce2020-08-28 14:10:23 +0200514 else if (d.key == 'QueryForm') {
515 let doc = document;
516 let v = d["value"] = {};
517
518 var el;
519 if (el = doc.getElementById('q-field')) {
520 v["q"] = el.value;
521 };
522 if (el = doc.getElementById('ql-field')) {
523 v["ql"] = el.value;
524 };
525 if (el = KorAP.vc) {
526 v["cq"] = el.toQuery();
527 };
Akronb759ee92024-11-19 18:02:56 +0100528 if (el = KorAP.Hint) {
529 v["selection"] = KorAP.Hint.selectionRange();
530 };
Akron432972b2020-09-18 17:05:53 +0200531 }
532
Akron338b4d42022-12-20 13:59:22 +0100533 // Get text sigle from match
534 else if (d.key == 'textSigle') {
535 if (service.panel.type != "match") {
536 KorAP.log(0, "Service can only be called on matches", service.src);
537 return;
538 };
539 let v = d["value"] = {};
540 v["value"] = service.panel._match.textSigle;
541 }
542
Akron432972b2020-09-18 17:05:53 +0200543 // Get Query information from parameters
544 else if (d.key == 'QueryParam') {
545
546 // Supported in all modern browsers
547 var p = new URLSearchParams(window.location.search);
548 let v = d["value"] = {};
Akron4de759f2021-10-13 10:46:45 +0200549 v["search"] = window.location.search; // readonly
Akron432972b2020-09-18 17:05:53 +0200550 v["q"] = p.get('q');
551 v["ql"] = p.get('ql');
552 v["cq"] = p.get('cq');
Akron26d57f22021-09-10 16:48:57 +0200553 }
554
555 // Get pagination information
556 else if (d.key == 'Pagination') {
557 const pi = pageInfoClass.create();
558 let v = d["value"] = {};
559 v["page"] = pi.page();
560 v["total"] = pi.total();
561 v["count"] = pi.count();
Akronec4bbfa2021-09-15 15:00:59 +0200562 };
Akronc3003642020-03-30 10:19:14 +0200563
Akronec4bbfa2021-09-15 15:00:59 +0200564 // data needs to be mirrored
565 if (d._id) {
566 service.sendMsg(d);
567 };
568
569 break;
570
Akron7d18d8e2024-11-08 11:08:48 +0100571 case 'set':
Akrone03faf62024-11-14 11:51:04 +0100572
573 // Get Query information from data
Akron7d18d8e2024-11-08 11:08:48 +0100574 if (d.key == 'QueryForm') {
575 let v = d["value"];
Akrone03faf62024-11-14 11:51:04 +0100576
Akron7d18d8e2024-11-08 11:08:48 +0100577 if (v["q"] != undefined && this._q) {
578 this._q.value = v["q"];
579 };
580
581 // Set query language field
582 // Identical to tutorial.js
Akrone03faf62024-11-14 11:51:04 +0100583 if (v["ql"] != undefined) {
584 if (KorAP.QLmenu) {
585 KorAP.QLmenu.selectValue(v["ql"]);
586 }
Akron7d18d8e2024-11-08 11:08:48 +0100587
Akrone03faf62024-11-14 11:51:04 +0100588 else if (this._ql) {
589 let found = Array.from(this._ql.options).find(o => o.value === v["ql"]);
590 if (found)
591 found.selected = true;
592 };
Akron7d18d8e2024-11-08 11:08:48 +0100593 };
594
595 window.scrollTo(0, 0);
596 // if (v["cq"] != undefined) {};
597 }
598
599 break;
600
Akronec4bbfa2021-09-15 15:00:59 +0200601 // Redirect to a different page relative to the current
602 case 'redirect':
603 const url = new URL(window.location);
604
605 // Currently this only accepts search parameters
606 if (d["queryParam"]) {
607 url.search = new URLSearchParams(d["queryParam"]);
608 };
609
610 window.location = url.toString();
611 break;
Akrona6c32b92018-07-02 18:39:42 +0200612 };
613
614 // TODO:
615 // Close
Akron479994e2018-07-02 13:21:44 +0200616 },
617
Akron22598cd2019-12-09 14:59:03 +0100618 // Close the service
619 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200620 delete limits[id];
Akronda32e7a2021-11-16 17:28:57 +0100621
Akron22598cd2019-12-09 14:59:03 +0100622 // Close the iframe
623 if (services[id] && services[id]._closeIframe) {
Akronda32e7a2021-11-16 17:28:57 +0100624
Akron22598cd2019-12-09 14:59:03 +0100625 services[id]._closeIframe();
626
627 // Remove from list
628 delete services[id];
629 };
630
Akron76dd8d32018-07-06 09:30:22 +0200631
632 // Remove listeners in case no widget
633 // is available any longer
634 if (Object.keys(limits).length == 0)
635 this._removeListener();
636 },
637
Akrona6c32b92018-07-02 18:39:42 +0200638 // Get a random identifier
639 _randomID : function () {
640 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200641 },
642
Akron76dd8d32018-07-06 09:30:22 +0200643 // Remove the listener
644 _removeListener : function () {
645 window.clearInterval(this._timer);
646 this._timer = undefined;
647 window.removeEventListener("message", this._listener);
648 this._listener = undefined;
649 },
650
Akron22598cd2019-12-09 14:59:03 +0100651 /**
652 * Return the service element.
653 */
654 element : function () {
Akron24aa0052020-11-10 11:00:34 +0100655 if (!this._el) {
656 this._el = document.createElement('div');
657 this._el.setAttribute("id", "services");
Akron22598cd2019-12-09 14:59:03 +0100658 }
Akron24aa0052020-11-10 11:00:34 +0100659 return this._el;
Akron22598cd2019-12-09 14:59:03 +0100660 },
Akronda32e7a2021-11-16 17:28:57 +0100661
662
663 // Return states object
664 states : function () {
Akroned223be2024-12-10 13:01:46 +0100665 return this._states;
Akronda32e7a2021-11-16 17:28:57 +0100666 },
667
Akronb43c8c62018-07-04 18:27:28 +0200668 // Destructor, just for testing scenarios
669 destroy : function () {
670 limits = {};
Akron678c26f2020-10-09 08:52:50 +0200671 Object.keys(services).forEach(
672 s => services[s].close()
673 );
Akron22598cd2019-12-09 14:59:03 +0100674 services = {};
Akron678c26f2020-10-09 08:52:50 +0200675 Object.keys(buttons).forEach(
676 b => buttons[b] = []
677 );
678 Object.keys(buttonsSingle).forEach(
679 b => buttonsSingle[b] = []
680 );
Akron22598cd2019-12-09 14:59:03 +0100681
Akron24aa0052020-11-10 11:00:34 +0100682 if (this._el) {
683 let e = this._el;
Akron22598cd2019-12-09 14:59:03 +0100684 if (e.parentNode) {
685 e.parentNode.removeChild(e);
686 };
Akron24aa0052020-11-10 11:00:34 +0100687 this._el = null;
Akron22598cd2019-12-09 14:59:03 +0100688 };
Akron76dd8d32018-07-06 09:30:22 +0200689 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200690 }
Akrone1c27f62018-07-20 11:42:59 +0200691 };
Akron479994e2018-07-02 13:21:44 +0200692});