blob: 24228926797172c35ce76055e92cdf65f20704eb [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
Akron22598cd2019-12-09 14:59:03 +010011define(["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
Akron7c6e05f2018-07-12 19:08:13 +0200112 // Register plugin by name
113 var plugin = plugins[name] = {
114 name : name,
115 desc : obj["desc"],
116 about : obj["about"],
Akron22598cd2019-12-09 14:59:03 +0100117 widgets : [],
118 services : []
Akron7c6e05f2018-07-12 19:08:13 +0200119 };
Akron10a47962018-07-12 21:17:10 +0200120
121 if (typeof obj["embed"] !== 'object')
122 throw new Error("Embedding of plugin is no list");
Akron7c6e05f2018-07-12 19:08:13 +0200123
124 // Embed all embeddings of the plugin
Akrone1c27f62018-07-20 11:42:59 +0200125 var that = this;
Akron22598cd2019-12-09 14:59:03 +0100126 for (let i in obj["embed"]) {
127 let embed = obj["embed"][i];
Akron10a47962018-07-12 21:17:10 +0200128
129 if (typeof embed !== 'object')
130 throw new Error("Embedding of plugin is no object");
131
Akron7c6e05f2018-07-12 19:08:13 +0200132 // Needs to be localized as well
Akron22598cd2019-12-09 14:59:03 +0100133 let title = embed["title"];
134 let panel = embed["panel"];
135 let onClick = embed["onClick"];
hebasta40a85cf2020-07-15 18:10:08 +0200136 let icon = embed["icon"];
137
Akron22598cd2019-12-09 14:59:03 +0100138 if (!panel || !(buttons[panel] || buttonsSingle[panel]))
Akronba09ed22020-10-01 16:01:45 +0200139 throw new Error("Panel for plugin is invalid");
Akron7c6e05f2018-07-12 19:08:13 +0200140
141 // The embedding will open a widget
Akronba09ed22020-10-01 16:01:45 +0200142 if (!onClick["action"] ||
143 onClick["action"] == "addWidget" ||
144 onClick["action"] == "setWidget") {
Akron22598cd2019-12-09 14:59:03 +0100145
146 let cb = function (e) {
Akron7c6e05f2018-07-12 19:08:13 +0200147
Akrone1c27f62018-07-20 11:42:59 +0200148 // "this" is bind to the panel
Akronba09ed22020-10-01 16:01:45 +0200149 // "this".button is the button
150 // "that" is the server object
Akrone1c27f62018-07-20 11:42:59 +0200151
Akron7c6e05f2018-07-12 19:08:13 +0200152 // Get the URL of the widget
Akron22598cd2019-12-09 14:59:03 +0100153 let url = onClick["template"];
Akrone1c27f62018-07-20 11:42:59 +0200154 // that._interpolateURI(onClick["template"], this.match);
Akron7c6e05f2018-07-12 19:08:13 +0200155
Akronfb11a962020-10-05 12:12:55 +0200156 let perm = onClick["permissions"];
157
Akronba09ed22020-10-01 16:01:45 +0200158 // The button has a state and the state is associated to the
159 // a intermediate object to toggle the view
160 if ('state' in this.button && this.button.state.associates() > 0) {
161
Akronba09ed22020-10-01 16:01:45 +0200162 let s = this.button.state;
Akronfcf89db2020-10-01 17:40:20 +0200163
164 // The associated service is existent
165 if (services[this.button['widgetID']]) {
166
167 // TODO:
168 // Use roll() when existing
169 if (s.get()) {
170 s.set(false);
171 } else {
172 s.set(true);
173 };
174 return;
175 }
176
177 // The service is not existent
178 else {
179
180 // Remove broken state associations
181 s.clear();
Akronba09ed22020-10-01 16:01:45 +0200182 s.set(true);
Akronfcf89db2020-10-01 17:40:20 +0200183 }
Akronba09ed22020-10-01 16:01:45 +0200184 };
185
Akron7c6e05f2018-07-12 19:08:13 +0200186 // Add the widget to the panel
Akronfb11a962020-10-05 12:12:55 +0200187 let id = that.addWidget(this, name, url, perm);
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
243 // Get the URL of the service
244
245 // TODO:
246 // Use the "service" keyword
247 let url = onClick["template"];
248
249 // Add the service
250 let id = this.addService(name, url);
251
Akronfb11a962020-10-05 12:12:55 +0200252 let perm = onClick["permissions"];
253
Akron22598cd2019-12-09 14:59:03 +0100254 // TODO:
255 // This is a bit stupid to get the service window
Akronc3003642020-03-30 10:19:14 +0200256 let service = services[id];
257 let iframe = service.load();
Akron22598cd2019-12-09 14:59:03 +0100258
259 // Create object to communicate the toggle state
260 // once the iframe is loaded.
261 iframe.onload = function () {
262 let sendToggle = {
263 setState : function (val) {
Akronc3003642020-03-30 10:19:14 +0200264 service.sendMsg({
Akron22598cd2019-12-09 14:59:03 +0100265 action: 'state',
266 key : onClick['state'],
267 value : val
Akronc3003642020-03-30 10:19:14 +0200268 });
Akron22598cd2019-12-09 14:59:03 +0100269 }
270 };
271
272 // Associate object with the state
273 state.associate(sendToggle);
274 };
275
276 plugin["services"].push(id);
Akron7c6e05f2018-07-12 19:08:13 +0200277 };
278 };
279 },
280
Akron7c6e05f2018-07-12 19:08:13 +0200281 // TODO:
282 // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570
283 // but as simple as possible
284 _interpolateURI : function (uri, obj) {
285 // ...
286 },
287
288
289 /**
Akron4a703872018-07-26 10:59:41 +0200290 * Get named button group - better rename to "action"
Akron7c6e05f2018-07-12 19:08:13 +0200291 */
292 buttonGroup : function (name) {
Akron2d0d96d2019-11-18 19:49:50 +0100293 if (buttons[name] != undefined) {
294 return buttons[name];
295 } else if (buttonsSingle[name] != undefined) {
296 return buttonsSingle[name];
297 };
298 return [];
299 },
300
301 /**
302 * Clear named button group - better rename to "action"
303 */
304 clearButtonGroup : function (name) {
305 if (buttons[name] != undefined) {
306 buttons[name] = [];
307 } else if (buttonsSingle[name] != undefined) {
308 buttonsSingle[name] = [];
309 }
Akron7c6e05f2018-07-12 19:08:13 +0200310 },
Akron479994e2018-07-02 13:21:44 +0200311
Akron22598cd2019-12-09 14:59:03 +0100312 // Optionally initialize the service mechanism and get an ID
313 _getServiceID : function () {
Akron4a703872018-07-26 10:59:41 +0200314
Akron22598cd2019-12-09 14:59:03 +0100315 // Is it the first service?
Akron76dd8d32018-07-06 09:30:22 +0200316 if (!this._listener) {
317
318 /*
319 * Establish the global 'message' hook.
320 */
321 this._listener = this._receiveMsg.bind(this);
322 window.addEventListener("message", this._listener);
323
Akron22598cd2019-12-09 14:59:03 +0100324 // Every second increase the limits of all registered services
Akron76dd8d32018-07-06 09:30:22 +0200325 this._timer = window.setInterval(function () {
326 for (var i in limits) {
327 if (limits[i]++ >= maxMessages) {
328 limits[i] = maxMessages;
329 }
330 }
331 }, 1000);
332 };
333
Akron22598cd2019-12-09 14:59:03 +0100334 // Create a unique random ID per service
335 return 'id-' + this._randomID();
336 },
337
338 /**
339 * Add a service in a certain panel and return the id.
340 */
Akronfb11a962020-10-05 12:12:55 +0200341 addService : function (name, src, permissions) {
Akron22598cd2019-12-09 14:59:03 +0100342 if (!src)
343 return;
344
345 let id = this._getServiceID();
346
347 // Create a new service
348 let service = serviceClass.create(name, src, id);
Akronfb11a962020-10-05 12:12:55 +0200349 if (permissions != undefined) {
350 service.allow(permissions);
351 };
Akron22598cd2019-12-09 14:59:03 +0100352
Akron22598cd2019-12-09 14:59:03 +0100353 services[id] = service;
354 limits[id] = maxMessages;
355
Akron22598cd2019-12-09 14:59:03 +0100356 // Add service to panel
357 this.element().appendChild(
358 service.load()
359 );
360
361 return id;
362 },
363
364
365 /**
366 * Open a new widget view in a certain panel and return
367 * the id.
368 */
Akronfb11a962020-10-05 12:12:55 +0200369 addWidget : function (panel, name, src, permissions) {
Akron22598cd2019-12-09 14:59:03 +0100370
371 let id = this._getServiceID();
Akrona6c32b92018-07-02 18:39:42 +0200372
373 // Create a new widget
Akron7991b192018-07-09 17:28:43 +0200374 var widget = widgetClass.create(name, src, id);
Akronfb11a962020-10-05 12:12:55 +0200375 if (permissions != undefined) {
376 widget.allow(permissions);
377 };
Akrona6c32b92018-07-02 18:39:42 +0200378
379 // Store the widget based on the identifier
Akron22598cd2019-12-09 14:59:03 +0100380 services[id] = widget;
Akrona99315e2018-07-03 22:56:45 +0200381 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +0200382
Akron4a703872018-07-26 10:59:41 +0200383 widget._mgr = this;
384
Akrone1c27f62018-07-20 11:42:59 +0200385 // Add widget to panel
386 panel.add(widget);
Akronb43c8c62018-07-04 18:27:28 +0200387
388 return id;
Akron479994e2018-07-02 13:21:44 +0200389 },
390
Akron4a703872018-07-26 10:59:41 +0200391
392 /**
Akron22598cd2019-12-09 14:59:03 +0100393 * Get service by identifier
Akron4a703872018-07-26 10:59:41 +0200394 */
Akron22598cd2019-12-09 14:59:03 +0100395 service : function (id) {
396 return services[id];
Akron4a703872018-07-26 10:59:41 +0200397 },
398
399
Akron22598cd2019-12-09 14:59:03 +0100400 // Receive a call from an embedded service.
Akrone8e2c952018-07-04 13:43:12 +0200401 // The handling needs to be very careful,
402 // as this can easily become a security nightmare.
Akron479994e2018-07-02 13:21:44 +0200403 _receiveMsg : function (e) {
404 // Get event data
405 var d = e.data;
406
Akrona99315e2018-07-03 22:56:45 +0200407 // If no data given - fail
408 // (probably check that it's an assoc array)
409 if (!d)
410 return;
411
412 // e.origin is probably set and okay - CHECK!
Akron479994e2018-07-02 13:21:44 +0200413
Akrona99315e2018-07-03 22:56:45 +0200414 // Get origin ID
415 var id = d["originID"];
416
417 // If no origin ID given - fail
418 if (!id)
419 return;
420
Akron22598cd2019-12-09 14:59:03 +0100421 // Get the service
422 let service = services[id];
Akrona6c32b92018-07-02 18:39:42 +0200423
Akron22598cd2019-12-09 14:59:03 +0100424 // If the addressed service does not exist - fail
425 if (!service)
Akrona6c32b92018-07-02 18:39:42 +0200426 return;
427
Akrona99315e2018-07-03 22:56:45 +0200428 // Check for message limits
429 if (limits[id]-- < 0) {
Akrone8e2c952018-07-04 13:43:12 +0200430
Akron22598cd2019-12-09 14:59:03 +0100431 // Kill service
432 KorAP.log(0, 'Suspicious action by service', service.src);
Akron7c6e05f2018-07-12 19:08:13 +0200433
434 // TODO:
435 // Potentially kill the whole plugin!
Akron4a703872018-07-26 10:59:41 +0200436
Akron22598cd2019-12-09 14:59:03 +0100437 // This removes all connections before closing the service
438 this._closeService(service.id);
439
440 // if (service.isWidget)
441 service.close();
442
Akrona99315e2018-07-03 22:56:45 +0200443 return;
444 };
Akrona6c32b92018-07-02 18:39:42 +0200445
Akron479994e2018-07-02 13:21:44 +0200446 // Resize the iframe
Akron22598cd2019-12-09 14:59:03 +0100447 switch (d.action) {
448 case 'resize':
449 if (service.isWidget)
450 service.resize(d);
451 break;
Akron479994e2018-07-02 13:21:44 +0200452
453 // Log message from iframe
Akron22598cd2019-12-09 14:59:03 +0100454 case 'log':
455 KorAP.log(d.code, d.msg, service.src);
456 break;
Akron51ee6232019-12-17 21:00:05 +0100457
458 // Modify pipes
459 case 'pipe':
460 if (KorAP.Pipe != undefined) {
461 if (d.job == 'del') {
462 KorAP.Pipe.remove(d.service);
463 } else {
464 KorAP.Pipe.append(d.service);
465 };
466 };
467 break;
Akronc3003642020-03-30 10:19:14 +0200468
469 // Get information from the embedding platform
470 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200471
472 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200473 if (d.key == 'KQ') {
474 if (KorAP.koralQuery !== undefined) {
475 d["value"] = KorAP.koralQuery;
476 };
Akron45308ce2020-08-28 14:10:23 +0200477 }
478
Akron432972b2020-09-18 17:05:53 +0200479 // Get Query information from from
Akron45308ce2020-08-28 14:10:23 +0200480 else if (d.key == 'QueryForm') {
481 let doc = document;
482 let v = d["value"] = {};
483
484 var el;
485 if (el = doc.getElementById('q-field')) {
486 v["q"] = el.value;
487 };
488 if (el = doc.getElementById('ql-field')) {
489 v["ql"] = el.value;
490 };
491 if (el = KorAP.vc) {
492 v["cq"] = el.toQuery();
493 };
Akron432972b2020-09-18 17:05:53 +0200494 }
495
496 // Get Query information from parameters
497 else if (d.key == 'QueryParam') {
498
499 // Supported in all modern browsers
500 var p = new URLSearchParams(window.location.search);
501 let v = d["value"] = {};
502 v["q"] = p.get('q');
503 v["ql"] = p.get('ql');
504 v["cq"] = p.get('cq');
Akronc3003642020-03-30 10:19:14 +0200505 };
506 };
507
508 // data needs to be mirrored
509 if (d._id) {
510 service.sendMsg(d);
Akrona6c32b92018-07-02 18:39:42 +0200511 };
512
513 // TODO:
514 // Close
Akron479994e2018-07-02 13:21:44 +0200515 },
516
Akron22598cd2019-12-09 14:59:03 +0100517 // Close the service
518 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200519 delete limits[id];
Akronfcf89db2020-10-01 17:40:20 +0200520
Akron22598cd2019-12-09 14:59:03 +0100521 // Close the iframe
522 if (services[id] && services[id]._closeIframe) {
523 services[id]._closeIframe();
524
525 // Remove from list
526 delete services[id];
527 };
528
Akron76dd8d32018-07-06 09:30:22 +0200529
530 // Remove listeners in case no widget
531 // is available any longer
532 if (Object.keys(limits).length == 0)
533 this._removeListener();
534 },
535
Akrona6c32b92018-07-02 18:39:42 +0200536 // Get a random identifier
537 _randomID : function () {
538 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200539 },
540
Akron76dd8d32018-07-06 09:30:22 +0200541 // Remove the listener
542 _removeListener : function () {
543 window.clearInterval(this._timer);
544 this._timer = undefined;
545 window.removeEventListener("message", this._listener);
546 this._listener = undefined;
547 },
548
Akron22598cd2019-12-09 14:59:03 +0100549 /**
550 * Return the service element.
551 */
552 element : function () {
553 if (!this._element) {
554 this._element = document.createElement('div');
555 this._element.setAttribute("id", "services");
556 }
557 return this._element;
558 },
559
Akronb43c8c62018-07-04 18:27:28 +0200560 // Destructor, just for testing scenarios
561 destroy : function () {
562 limits = {};
Akron22598cd2019-12-09 14:59:03 +0100563 for (let s in services) {
564 services[s].close();
Akron4a703872018-07-26 10:59:41 +0200565 };
Akron22598cd2019-12-09 14:59:03 +0100566 services = {};
Akron2d0d96d2019-11-18 19:49:50 +0100567 for (let b in buttons) {
568 buttons[b] = [];
569 };
570 for (let b in buttonsSingle) {
571 buttonsSingle[b] = [];
572 };
Akron22598cd2019-12-09 14:59:03 +0100573
574 if (this._element) {
575 let e = this._element;
576 if (e.parentNode) {
577 e.parentNode.removeChild(e);
578 };
579 this._element = null;
580 };
581
Akron76dd8d32018-07-06 09:30:22 +0200582 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200583 }
Akrone1c27f62018-07-20 11:42:59 +0200584 };
Akron479994e2018-07-02 13:21:44 +0200585});