blob: e47cfbfb237e3e311a52837674b8a4adf92e6679 [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 */
10
Akron3d013802020-10-07 15:03:38 +020011define(['plugin/widget', 'plugin/service', 'state', 'util'], function (widgetClass, serviceClass, stateClass) {
Akron479994e2018-07-02 13:21:44 +020012 "use strict";
13
Akron2d0d96d2019-11-18 19:49:50 +010014 KorAP.Panel = KorAP.Panel || {};
15
Akron22598cd2019-12-09 14:59:03 +010016 // Contains all servicess to address with
Akrona6c32b92018-07-02 18:39:42 +020017 // messages to them
Akron22598cd2019-12-09 14:59:03 +010018 var services = {};
19 var plugins = {};
Akron10a47962018-07-12 21:17:10 +020020
21 // TODO:
22 // These should better be panels and every panel
23 // has a buttonGroup
Akron2d0d96d2019-11-18 19:49:50 +010024
25 // List of panels with dynamic buttons, i.e.
26 // panels that may occur multiple times.
Akron7c6e05f2018-07-12 19:08:13 +020027 var buttons = {
28 match : []
29 };
Akron2d0d96d2019-11-18 19:49:50 +010030
31 // List of panels with static buttons, i.e.
32 // panels that occur only once.
33 var buttonsSingle = {
hebasta043e96f2019-11-28 12:33:00 +010034 query : [],
35 result : []
Akron22598cd2019-12-09 14:59:03 +010036 }
Akron10a47962018-07-12 21:17:10 +020037
Akrone8e2c952018-07-04 13:43:12 +020038 // 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
Akrone1c27f62018-07-20 11:42:59 +020049 // all widgets every second to see if it is still alive,
50 // otherwise kill
Akrone8e2c952018-07-04 13:43:12 +020051
Akrone1c27f62018-07-20 11:42:59 +020052 // Load Plugin server
Akron479994e2018-07-02 13:21:44 +020053 return {
54
55 /**
56 * Create new plugin management system
57 */
58 create : function () {
59 return Object.create(this)._init();
60 },
61
62 /*
Akron76dd8d32018-07-06 09:30:22 +020063 * Initialize the plugin manager
Akron479994e2018-07-02 13:21:44 +020064 */
65 _init : function () {
Akron479994e2018-07-02 13:21:44 +020066 return this;
67 },
68
69 /**
Akron7c6e05f2018-07-12 19:08:13 +020070 * Register a plugin described as a JSON object.
71 *
72 * This is work in progress.
Akron10a47962018-07-12 21:17:10 +020073 *
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 * }
Akron22598cd2019-12-09 14:59:03 +010088 * },{
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 * }
Akron10a47962018-07-12 21:17:10 +020099 * }]
100 * });
101 *
Akron7c6e05f2018-07-12 19:08:13 +0200102 */
103 register : function (obj) {
Akron7c6e05f2018-07-12 19:08:13 +0200104 // TODO:
Akron10a47962018-07-12 21:17:10 +0200105 // These fields need to be localized for display by a structure like
106 // { de : { name : '..' }, en : { .. } }
Akron7c6e05f2018-07-12 19:08:13 +0200107 var name = obj["name"];
108
Akron10a47962018-07-12 21:17:10 +0200109 if (!name)
110 throw new Error("Missing name of plugin");
111
Akron3d013802020-10-07 15:03:38 +0200112 var desc = obj["desc"];
113
Akron7c6e05f2018-07-12 19:08:13 +0200114 // Register plugin by name
115 var plugin = plugins[name] = {
116 name : name,
Akron3d013802020-10-07 15:03:38 +0200117 desc : desc,
Akron7c6e05f2018-07-12 19:08:13 +0200118 about : obj["about"],
Akron22598cd2019-12-09 14:59:03 +0100119 widgets : [],
120 services : []
Akron7c6e05f2018-07-12 19:08:13 +0200121 };
Akron10a47962018-07-12 21:17:10 +0200122
123 if (typeof obj["embed"] !== 'object')
124 throw new Error("Embedding of plugin is no list");
Akron7c6e05f2018-07-12 19:08:13 +0200125
126 // Embed all embeddings of the plugin
Akrone1c27f62018-07-20 11:42:59 +0200127 var that = this;
Akron678c26f2020-10-09 08:52:50 +0200128 obj["embed"].forEach(function(embed) {
Akron10a47962018-07-12 21:17:10 +0200129
130 if (typeof embed !== 'object')
131 throw new Error("Embedding of plugin is no object");
132
Akron7c6e05f2018-07-12 19:08:13 +0200133 // Needs to be localized as well
Akron22598cd2019-12-09 14:59:03 +0100134 let title = embed["title"];
135 let panel = embed["panel"];
136 let onClick = embed["onClick"];
hebasta40a85cf2020-07-15 18:10:08 +0200137 let icon = embed["icon"];
138
Akron22598cd2019-12-09 14:59:03 +0100139 if (!panel || !(buttons[panel] || buttonsSingle[panel]))
Akronba09ed22020-10-01 16:01:45 +0200140 throw new Error("Panel for plugin is invalid");
Akron7c6e05f2018-07-12 19:08:13 +0200141
142 // The embedding will open a widget
Akronba09ed22020-10-01 16:01:45 +0200143 if (!onClick["action"] ||
144 onClick["action"] == "addWidget" ||
145 onClick["action"] == "setWidget") {
Akron22598cd2019-12-09 14:59:03 +0100146
147 let cb = function (e) {
Akron7c6e05f2018-07-12 19:08:13 +0200148
Akrone1c27f62018-07-20 11:42:59 +0200149 // "this" is bind to the panel
Akronba09ed22020-10-01 16:01:45 +0200150 // "this".button is the button
151 // "that" is the server object
Akrone1c27f62018-07-20 11:42:59 +0200152
Akronba09ed22020-10-01 16:01:45 +0200153 // The button has a state and the state is associated to the
154 // a intermediate object to toggle the view
155 if ('state' in this.button && this.button.state.associates() > 0) {
156
Akronba09ed22020-10-01 16:01:45 +0200157 let s = this.button.state;
Akronfcf89db2020-10-01 17:40:20 +0200158
159 // The associated service is existent
160 if (services[this.button['widgetID']]) {
161
162 // TODO:
163 // Use roll() when existing
164 if (s.get()) {
165 s.set(false);
166 } else {
167 s.set(true);
168 };
169 return;
170 }
171
172 // The service is not existent
173 else {
174
175 // Remove broken state associations
176 s.clear();
Akronba09ed22020-10-01 16:01:45 +0200177 s.set(true);
Akronfcf89db2020-10-01 17:40:20 +0200178 }
Akronba09ed22020-10-01 16:01:45 +0200179 };
180
Akron7c6e05f2018-07-12 19:08:13 +0200181 // Add the widget to the panel
Akronbb891982020-10-05 16:07:18 +0200182 let id = that.addWidget(this, {
183 "name": name,
184 "src": onClick["template"], // that._interpolateURI(onClick["template"], this.match);
Akron3d013802020-10-07 15:03:38 +0200185 "permissions": onClick["permissions"],
186 "desc":desc
Akronbb891982020-10-05 16:07:18 +0200187 });
Akron7c6e05f2018-07-12 19:08:13 +0200188 plugin["widgets"].push(id);
Akronba09ed22020-10-01 16:01:45 +0200189
190 // If a state exists, associate with a mediator object
191 if ('state' in this.button) {
Akronfcf89db2020-10-01 17:40:20 +0200192 this.button['widgetID'] = id;
Akronba09ed22020-10-01 16:01:45 +0200193 this.button.state.associate({
194 setState : function (value) {
195 // Minimize the widget
196 if (value == false) {
197 services[id].minimize();
198 }
199 else {
200 services[id].show();
201 };
202 }
203 });
204 }
Akron7c6e05f2018-07-12 19:08:13 +0200205 };
206
Akron22598cd2019-12-09 14:59:03 +0100207
Akronba09ed22020-10-01 16:01:45 +0200208 // Button object
209 let obj = {'cls':embed["classes"], 'icon': icon }
210
211 if (onClick["action"] && onClick["action"] == "setWidget") {
212
213 // Create a boolean state value, that initializes to true == opened
214 obj['state'] = stateClass.create(true);
215 };
216
Akron2d0d96d2019-11-18 19:49:50 +0100217 // Add to dynamic button list (e.g. for matches)
218 if (buttons[panel]) {
Akronba09ed22020-10-01 16:01:45 +0200219 buttons[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100220 }
221
222 // Add to static button list (e.g. for query) already loaded
223 else if (KorAP.Panel[panel]) {
Akronba09ed22020-10-01 16:01:45 +0200224 KorAP.Panel[panel].actions.add(title, obj, cb);
Akron2d0d96d2019-11-18 19:49:50 +0100225 }
226
227 // Add to static button list (e.g. for query) not yet loaded
228 else {
Akronba09ed22020-10-01 16:01:45 +0200229 buttonsSingle[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100230 }
Akron22598cd2019-12-09 14:59:03 +0100231 }
Akronba09ed22020-10-01 16:01:45 +0200232
233 // TODO There is no possibility to add icons to an plugin toggle button right now.
Akron22598cd2019-12-09 14:59:03 +0100234 else if (onClick["action"] == "toggle") {
235
236 // Todo: Initially false
237 let state = stateClass.create(false);
238
239 // TODO:
240 // Lazy registration (see above!)
Akron792b1a42020-09-14 18:56:38 +0200241 KorAP.Panel[panel].actions.addToggle(title, {'cls':["title"]}, state);
Akron22598cd2019-12-09 14:59:03 +0100242
Akron22598cd2019-12-09 14:59:03 +0100243 // Add the service
Akronbb891982020-10-05 16:07:18 +0200244 let id = this.addService({
245 "name" : name,
246 // TODO:
247 // Use the "service" keyword
248 "src" : onClick["template"],
249 "permissions" : onClick["permissions"]
250 });
Akronfb11a962020-10-05 12:12:55 +0200251
Akron22598cd2019-12-09 14:59:03 +0100252 // TODO:
253 // This is a bit stupid to get the service window
Akronc3003642020-03-30 10:19:14 +0200254 let service = services[id];
255 let iframe = service.load();
Akron22598cd2019-12-09 14:59:03 +0100256
257 // Create object to communicate the toggle state
258 // once the iframe is loaded.
259 iframe.onload = function () {
260 let sendToggle = {
261 setState : function (val) {
Akronc3003642020-03-30 10:19:14 +0200262 service.sendMsg({
Akron22598cd2019-12-09 14:59:03 +0100263 action: 'state',
264 key : onClick['state'],
265 value : val
Akronc3003642020-03-30 10:19:14 +0200266 });
Akron22598cd2019-12-09 14:59:03 +0100267 }
268 };
269
270 // Associate object with the state
271 state.associate(sendToggle);
272 };
273
274 plugin["services"].push(id);
Akron7c6e05f2018-07-12 19:08:13 +0200275 };
Akron678c26f2020-10-09 08:52:50 +0200276 }, this);
Akron7c6e05f2018-07-12 19:08:13 +0200277 },
278
Akron7c6e05f2018-07-12 19:08:13 +0200279 // TODO:
280 // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570
281 // but as simple as possible
282 _interpolateURI : function (uri, obj) {
283 // ...
284 },
285
286
287 /**
Akron4a703872018-07-26 10:59:41 +0200288 * Get named button group - better rename to "action"
Akron7c6e05f2018-07-12 19:08:13 +0200289 */
290 buttonGroup : function (name) {
Akron2d0d96d2019-11-18 19:49:50 +0100291 if (buttons[name] != undefined) {
292 return buttons[name];
293 } else if (buttonsSingle[name] != undefined) {
294 return buttonsSingle[name];
295 };
296 return [];
297 },
298
299 /**
300 * Clear named button group - better rename to "action"
301 */
302 clearButtonGroup : function (name) {
303 if (buttons[name] != undefined) {
304 buttons[name] = [];
305 } else if (buttonsSingle[name] != undefined) {
306 buttonsSingle[name] = [];
307 }
Akron7c6e05f2018-07-12 19:08:13 +0200308 },
Akron479994e2018-07-02 13:21:44 +0200309
Akron22598cd2019-12-09 14:59:03 +0100310 // Optionally initialize the service mechanism and get an ID
311 _getServiceID : function () {
Akron4a703872018-07-26 10:59:41 +0200312
Akron22598cd2019-12-09 14:59:03 +0100313 // Is it the first service?
Akron76dd8d32018-07-06 09:30:22 +0200314 if (!this._listener) {
315
316 /*
317 * Establish the global 'message' hook.
318 */
319 this._listener = this._receiveMsg.bind(this);
320 window.addEventListener("message", this._listener);
321
Akron22598cd2019-12-09 14:59:03 +0100322 // Every second increase the limits of all registered services
Akron76dd8d32018-07-06 09:30:22 +0200323 this._timer = window.setInterval(function () {
Akron678c26f2020-10-09 08:52:50 +0200324 for (let i = 0; i < limits.length; i++) {
Akron76dd8d32018-07-06 09:30:22 +0200325 if (limits[i]++ >= maxMessages) {
326 limits[i] = maxMessages;
327 }
328 }
329 }, 1000);
330 };
331
Akron22598cd2019-12-09 14:59:03 +0100332 // Create a unique random ID per service
333 return 'id-' + this._randomID();
334 },
335
336 /**
337 * Add a service in a certain panel and return the id.
338 */
Akronbb891982020-10-05 16:07:18 +0200339 addService : function (data) {
340 if (!data["src"])
Akron22598cd2019-12-09 14:59:03 +0100341 return;
342
343 let id = this._getServiceID();
344
Akronbb891982020-10-05 16:07:18 +0200345 data["id"] = id;
346
Akron22598cd2019-12-09 14:59:03 +0100347 // Create a new service
Akronbb891982020-10-05 16:07:18 +0200348 let service = serviceClass.create(data);
Akron22598cd2019-12-09 14:59:03 +0100349
Akron22598cd2019-12-09 14:59:03 +0100350 services[id] = service;
351 limits[id] = maxMessages;
352
Akron22598cd2019-12-09 14:59:03 +0100353 // Add service to panel
354 this.element().appendChild(
355 service.load()
356 );
357
358 return id;
359 },
360
361
362 /**
363 * Open a new widget view in a certain panel and return
364 * the id.
365 */
Akronbb891982020-10-05 16:07:18 +0200366 addWidget : function (panel, data) {
367 // panel, name, src, permissions
Akron22598cd2019-12-09 14:59:03 +0100368
369 let id = this._getServiceID();
Akrona6c32b92018-07-02 18:39:42 +0200370
Akronbb891982020-10-05 16:07:18 +0200371 data["id"] = id;
372
Akrona6c32b92018-07-02 18:39:42 +0200373 // Create a new widget
Akronbb891982020-10-05 16:07:18 +0200374 var widget = widgetClass.create(data);
Akrona6c32b92018-07-02 18:39:42 +0200375
376 // Store the widget based on the identifier
Akron22598cd2019-12-09 14:59:03 +0100377 services[id] = widget;
Akrona99315e2018-07-03 22:56:45 +0200378 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +0200379
Akron4a703872018-07-26 10:59:41 +0200380 widget._mgr = this;
381
Akrone1c27f62018-07-20 11:42:59 +0200382 // Add widget to panel
383 panel.add(widget);
Akronb43c8c62018-07-04 18:27:28 +0200384
385 return id;
Akron479994e2018-07-02 13:21:44 +0200386 },
387
Akron4a703872018-07-26 10:59:41 +0200388
389 /**
Akron22598cd2019-12-09 14:59:03 +0100390 * Get service by identifier
Akron4a703872018-07-26 10:59:41 +0200391 */
Akron22598cd2019-12-09 14:59:03 +0100392 service : function (id) {
393 return services[id];
Akron4a703872018-07-26 10:59:41 +0200394 },
395
396
Akron22598cd2019-12-09 14:59:03 +0100397 // Receive a call from an embedded service.
Akrone8e2c952018-07-04 13:43:12 +0200398 // The handling needs to be very careful,
399 // as this can easily become a security nightmare.
Akron479994e2018-07-02 13:21:44 +0200400 _receiveMsg : function (e) {
401 // Get event data
402 var d = e.data;
403
Akrona99315e2018-07-03 22:56:45 +0200404 // If no data given - fail
405 // (probably check that it's an assoc array)
406 if (!d)
407 return;
408
409 // e.origin is probably set and okay - CHECK!
Akron479994e2018-07-02 13:21:44 +0200410
Akrona99315e2018-07-03 22:56:45 +0200411 // Get origin ID
412 var id = d["originID"];
413
414 // If no origin ID given - fail
415 if (!id)
416 return;
417
Akron22598cd2019-12-09 14:59:03 +0100418 // Get the service
419 let service = services[id];
Akrona6c32b92018-07-02 18:39:42 +0200420
Akron22598cd2019-12-09 14:59:03 +0100421 // If the addressed service does not exist - fail
422 if (!service)
Akrona6c32b92018-07-02 18:39:42 +0200423 return;
424
Akrona99315e2018-07-03 22:56:45 +0200425 // Check for message limits
426 if (limits[id]-- < 0) {
Akrone8e2c952018-07-04 13:43:12 +0200427
Akron22598cd2019-12-09 14:59:03 +0100428 // Kill service
429 KorAP.log(0, 'Suspicious action by service', service.src);
Akron7c6e05f2018-07-12 19:08:13 +0200430
431 // TODO:
432 // Potentially kill the whole plugin!
Akron4a703872018-07-26 10:59:41 +0200433
Akron22598cd2019-12-09 14:59:03 +0100434 // This removes all connections before closing the service
435 this._closeService(service.id);
436
437 // if (service.isWidget)
438 service.close();
439
Akrona99315e2018-07-03 22:56:45 +0200440 return;
441 };
Akrona6c32b92018-07-02 18:39:42 +0200442
Akron479994e2018-07-02 13:21:44 +0200443 // Resize the iframe
Akron22598cd2019-12-09 14:59:03 +0100444 switch (d.action) {
445 case 'resize':
446 if (service.isWidget)
447 service.resize(d);
448 break;
Akron479994e2018-07-02 13:21:44 +0200449
450 // Log message from iframe
Akron22598cd2019-12-09 14:59:03 +0100451 case 'log':
452 KorAP.log(d.code, d.msg, service.src);
453 break;
Akron51ee6232019-12-17 21:00:05 +0100454
455 // Modify pipes
456 case 'pipe':
457 if (KorAP.Pipe != undefined) {
458 if (d.job == 'del') {
459 KorAP.Pipe.remove(d.service);
460 } else {
461 KorAP.Pipe.append(d.service);
462 };
463 };
464 break;
Akronc3003642020-03-30 10:19:14 +0200465
466 // Get information from the embedding platform
467 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200468
469 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200470 if (d.key == 'KQ') {
471 if (KorAP.koralQuery !== undefined) {
472 d["value"] = KorAP.koralQuery;
473 };
Akron45308ce2020-08-28 14:10:23 +0200474 }
475
Akron432972b2020-09-18 17:05:53 +0200476 // Get Query information from from
Akron45308ce2020-08-28 14:10:23 +0200477 else if (d.key == 'QueryForm') {
478 let doc = document;
479 let v = d["value"] = {};
480
481 var el;
482 if (el = doc.getElementById('q-field')) {
483 v["q"] = el.value;
484 };
485 if (el = doc.getElementById('ql-field')) {
486 v["ql"] = el.value;
487 };
488 if (el = KorAP.vc) {
489 v["cq"] = el.toQuery();
490 };
Akron432972b2020-09-18 17:05:53 +0200491 }
492
493 // Get Query information from parameters
494 else if (d.key == 'QueryParam') {
495
496 // Supported in all modern browsers
497 var p = new URLSearchParams(window.location.search);
498 let v = d["value"] = {};
499 v["q"] = p.get('q');
500 v["ql"] = p.get('ql');
501 v["cq"] = p.get('cq');
Akronc3003642020-03-30 10:19:14 +0200502 };
503 };
504
505 // data needs to be mirrored
506 if (d._id) {
507 service.sendMsg(d);
Akrona6c32b92018-07-02 18:39:42 +0200508 };
509
510 // TODO:
511 // Close
Akron479994e2018-07-02 13:21:44 +0200512 },
513
Akron22598cd2019-12-09 14:59:03 +0100514 // Close the service
515 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200516 delete limits[id];
Akronfcf89db2020-10-01 17:40:20 +0200517
Akron22598cd2019-12-09 14:59:03 +0100518 // Close the iframe
519 if (services[id] && services[id]._closeIframe) {
520 services[id]._closeIframe();
521
522 // Remove from list
523 delete services[id];
524 };
525
Akron76dd8d32018-07-06 09:30:22 +0200526
527 // Remove listeners in case no widget
528 // is available any longer
529 if (Object.keys(limits).length == 0)
530 this._removeListener();
531 },
532
Akrona6c32b92018-07-02 18:39:42 +0200533 // Get a random identifier
534 _randomID : function () {
535 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200536 },
537
Akron76dd8d32018-07-06 09:30:22 +0200538 // Remove the listener
539 _removeListener : function () {
540 window.clearInterval(this._timer);
541 this._timer = undefined;
542 window.removeEventListener("message", this._listener);
543 this._listener = undefined;
544 },
545
Akron22598cd2019-12-09 14:59:03 +0100546 /**
547 * Return the service element.
548 */
549 element : function () {
550 if (!this._element) {
551 this._element = document.createElement('div');
552 this._element.setAttribute("id", "services");
553 }
554 return this._element;
555 },
556
Akronb43c8c62018-07-04 18:27:28 +0200557 // Destructor, just for testing scenarios
558 destroy : function () {
559 limits = {};
Akron678c26f2020-10-09 08:52:50 +0200560 Object.keys(services).forEach(
561 s => services[s].close()
562 );
Akron22598cd2019-12-09 14:59:03 +0100563 services = {};
Akron678c26f2020-10-09 08:52:50 +0200564 Object.keys(buttons).forEach(
565 b => buttons[b] = []
566 );
567 Object.keys(buttonsSingle).forEach(
568 b => buttonsSingle[b] = []
569 );
Akron22598cd2019-12-09 14:59:03 +0100570
571 if (this._element) {
572 let e = this._element;
573 if (e.parentNode) {
574 e.parentNode.removeChild(e);
575 };
576 this._element = null;
577 };
578
Akron76dd8d32018-07-06 09:30:22 +0200579 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200580 }
Akrone1c27f62018-07-20 11:42:59 +0200581 };
Akron479994e2018-07-02 13:21:44 +0200582});