blob: f86277086ee40d8162ccb0035bd4c17857629bda [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]))
139 throw new Error("Panel for plugin is invalid");
Akron7c6e05f2018-07-12 19:08:13 +0200140
141 // The embedding will open a widget
Akron10a47962018-07-12 21:17:10 +0200142 if (!onClick["action"] || onClick["action"] == "addWidget") {
Akron7c6e05f2018-07-12 19:08:13 +0200143
Akron22598cd2019-12-09 14:59:03 +0100144
145 let cb = function (e) {
Akron7c6e05f2018-07-12 19:08:13 +0200146
Akrone1c27f62018-07-20 11:42:59 +0200147 // "this" is bind to the panel
148
Akron7c6e05f2018-07-12 19:08:13 +0200149 // Get the URL of the widget
Akron22598cd2019-12-09 14:59:03 +0100150 let url = onClick["template"];
Akrone1c27f62018-07-20 11:42:59 +0200151 // that._interpolateURI(onClick["template"], this.match);
Akron7c6e05f2018-07-12 19:08:13 +0200152
153 // Add the widget to the panel
Akron22598cd2019-12-09 14:59:03 +0100154 let id = that.addWidget(this, name, url);
Akron7c6e05f2018-07-12 19:08:13 +0200155 plugin["widgets"].push(id);
156 };
157
Akron22598cd2019-12-09 14:59:03 +0100158 // TODO:
159 // Create button class to be stored and loaded in button groups!
160
Akron2d0d96d2019-11-18 19:49:50 +0100161 // Add to dynamic button list (e.g. for matches)
162 if (buttons[panel]) {
Akron792b1a42020-09-14 18:56:38 +0200163 buttons[panel].push([title, {'cls':embed["classes"], 'icon': icon }, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100164 }
165
166 // Add to static button list (e.g. for query) already loaded
167 else if (KorAP.Panel[panel]) {
Akron792b1a42020-09-14 18:56:38 +0200168 KorAP.Panel[panel].actions.add(title, {'cls':embed["classes"], 'icon':icon}, cb);
Akron2d0d96d2019-11-18 19:49:50 +0100169 }
170
171 // Add to static button list (e.g. for query) not yet loaded
172 else {
Akron792b1a42020-09-14 18:56:38 +0200173 buttonsSingle[panel].push([title, {'cls':embed["classes"], 'icon':icon}, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100174 }
Akron22598cd2019-12-09 14:59:03 +0100175 }
hebasta40a85cf2020-07-15 18:10:08 +0200176 //TODO There is no possibility to add icons to an plugin toggle button right now.
Akron22598cd2019-12-09 14:59:03 +0100177 else if (onClick["action"] == "toggle") {
178
179 // Todo: Initially false
180 let state = stateClass.create(false);
181
182 // TODO:
183 // Lazy registration (see above!)
Akron792b1a42020-09-14 18:56:38 +0200184 KorAP.Panel[panel].actions.addToggle(title, {'cls':["title"]}, state);
Akron22598cd2019-12-09 14:59:03 +0100185
186 // Get the URL of the service
187
188 // TODO:
189 // Use the "service" keyword
190 let url = onClick["template"];
191
192 // Add the service
193 let id = this.addService(name, url);
194
195 // TODO:
196 // This is a bit stupid to get the service window
Akronc3003642020-03-30 10:19:14 +0200197 let service = services[id];
198 let iframe = service.load();
Akron22598cd2019-12-09 14:59:03 +0100199
200 // Create object to communicate the toggle state
201 // once the iframe is loaded.
202 iframe.onload = function () {
203 let sendToggle = {
204 setState : function (val) {
Akronc3003642020-03-30 10:19:14 +0200205 service.sendMsg({
Akron22598cd2019-12-09 14:59:03 +0100206 action: 'state',
207 key : onClick['state'],
208 value : val
Akronc3003642020-03-30 10:19:14 +0200209 });
Akron22598cd2019-12-09 14:59:03 +0100210 }
211 };
212
213 // Associate object with the state
214 state.associate(sendToggle);
215 };
216
217 plugin["services"].push(id);
Akron7c6e05f2018-07-12 19:08:13 +0200218 };
219 };
220 },
221
Akron7c6e05f2018-07-12 19:08:13 +0200222 // TODO:
223 // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570
224 // but as simple as possible
225 _interpolateURI : function (uri, obj) {
226 // ...
227 },
228
229
230 /**
Akron4a703872018-07-26 10:59:41 +0200231 * Get named button group - better rename to "action"
Akron7c6e05f2018-07-12 19:08:13 +0200232 */
233 buttonGroup : function (name) {
Akron2d0d96d2019-11-18 19:49:50 +0100234 if (buttons[name] != undefined) {
235 return buttons[name];
236 } else if (buttonsSingle[name] != undefined) {
237 return buttonsSingle[name];
238 };
239 return [];
240 },
241
242 /**
243 * Clear named button group - better rename to "action"
244 */
245 clearButtonGroup : function (name) {
246 if (buttons[name] != undefined) {
247 buttons[name] = [];
248 } else if (buttonsSingle[name] != undefined) {
249 buttonsSingle[name] = [];
250 }
Akron7c6e05f2018-07-12 19:08:13 +0200251 },
Akron479994e2018-07-02 13:21:44 +0200252
Akron22598cd2019-12-09 14:59:03 +0100253 // Optionally initialize the service mechanism and get an ID
254 _getServiceID : function () {
Akron4a703872018-07-26 10:59:41 +0200255
Akron22598cd2019-12-09 14:59:03 +0100256 // Is it the first service?
Akron76dd8d32018-07-06 09:30:22 +0200257 if (!this._listener) {
258
259 /*
260 * Establish the global 'message' hook.
261 */
262 this._listener = this._receiveMsg.bind(this);
263 window.addEventListener("message", this._listener);
264
Akron22598cd2019-12-09 14:59:03 +0100265 // Every second increase the limits of all registered services
Akron76dd8d32018-07-06 09:30:22 +0200266 this._timer = window.setInterval(function () {
267 for (var i in limits) {
268 if (limits[i]++ >= maxMessages) {
269 limits[i] = maxMessages;
270 }
271 }
272 }, 1000);
273 };
274
Akron22598cd2019-12-09 14:59:03 +0100275 // Create a unique random ID per service
276 return 'id-' + this._randomID();
277 },
278
279 /**
280 * Add a service in a certain panel and return the id.
281 */
282 addService : function (name, src) {
283 if (!src)
284 return;
285
286 let id = this._getServiceID();
287
288 // Create a new service
289 let service = serviceClass.create(name, src, id);
290
291 // TODO!
292 // Store the service based on the identifier
293 services[id] = service;
294 limits[id] = maxMessages;
295
296 // widget._mgr = this;
297
298 // Add service to panel
299 this.element().appendChild(
300 service.load()
301 );
302
303 return id;
304 },
305
306
307 /**
308 * Open a new widget view in a certain panel and return
309 * the id.
310 */
311 addWidget : function (panel, name, src) {
312
313 let id = this._getServiceID();
Akrona6c32b92018-07-02 18:39:42 +0200314
315 // Create a new widget
Akron7991b192018-07-09 17:28:43 +0200316 var widget = widgetClass.create(name, src, id);
Akrona6c32b92018-07-02 18:39:42 +0200317
318 // Store the widget based on the identifier
Akron22598cd2019-12-09 14:59:03 +0100319 services[id] = widget;
Akrona99315e2018-07-03 22:56:45 +0200320 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +0200321
Akron4a703872018-07-26 10:59:41 +0200322 widget._mgr = this;
323
Akrone1c27f62018-07-20 11:42:59 +0200324 // Add widget to panel
325 panel.add(widget);
Akronb43c8c62018-07-04 18:27:28 +0200326
327 return id;
Akron479994e2018-07-02 13:21:44 +0200328 },
329
Akron4a703872018-07-26 10:59:41 +0200330
331 /**
Akron22598cd2019-12-09 14:59:03 +0100332 * Get service by identifier
Akron4a703872018-07-26 10:59:41 +0200333 */
Akron22598cd2019-12-09 14:59:03 +0100334 service : function (id) {
335 return services[id];
Akron4a703872018-07-26 10:59:41 +0200336 },
337
338
Akron22598cd2019-12-09 14:59:03 +0100339 // Receive a call from an embedded service.
Akrone8e2c952018-07-04 13:43:12 +0200340 // The handling needs to be very careful,
341 // as this can easily become a security nightmare.
Akron479994e2018-07-02 13:21:44 +0200342 _receiveMsg : function (e) {
343 // Get event data
344 var d = e.data;
345
Akrona99315e2018-07-03 22:56:45 +0200346 // If no data given - fail
347 // (probably check that it's an assoc array)
348 if (!d)
349 return;
350
351 // e.origin is probably set and okay - CHECK!
Akron479994e2018-07-02 13:21:44 +0200352
Akrona99315e2018-07-03 22:56:45 +0200353 // Get origin ID
354 var id = d["originID"];
355
356 // If no origin ID given - fail
357 if (!id)
358 return;
359
Akron22598cd2019-12-09 14:59:03 +0100360 // Get the service
361 let service = services[id];
Akrona6c32b92018-07-02 18:39:42 +0200362
Akron22598cd2019-12-09 14:59:03 +0100363 // If the addressed service does not exist - fail
364 if (!service)
Akrona6c32b92018-07-02 18:39:42 +0200365 return;
366
Akrona99315e2018-07-03 22:56:45 +0200367 // Check for message limits
368 if (limits[id]-- < 0) {
Akrone8e2c952018-07-04 13:43:12 +0200369
Akron22598cd2019-12-09 14:59:03 +0100370 // Kill service
371 KorAP.log(0, 'Suspicious action by service', service.src);
Akron7c6e05f2018-07-12 19:08:13 +0200372
373 // TODO:
374 // Potentially kill the whole plugin!
Akron4a703872018-07-26 10:59:41 +0200375
Akron22598cd2019-12-09 14:59:03 +0100376 // This removes all connections before closing the service
377 this._closeService(service.id);
378
379 // if (service.isWidget)
380 service.close();
381
Akrona99315e2018-07-03 22:56:45 +0200382 return;
383 };
Akrona6c32b92018-07-02 18:39:42 +0200384
Akron479994e2018-07-02 13:21:44 +0200385 // Resize the iframe
Akron22598cd2019-12-09 14:59:03 +0100386 switch (d.action) {
387 case 'resize':
388 if (service.isWidget)
389 service.resize(d);
390 break;
Akron479994e2018-07-02 13:21:44 +0200391
392 // Log message from iframe
Akron22598cd2019-12-09 14:59:03 +0100393 case 'log':
394 KorAP.log(d.code, d.msg, service.src);
395 break;
Akron51ee6232019-12-17 21:00:05 +0100396
397 // Modify pipes
398 case 'pipe':
399 if (KorAP.Pipe != undefined) {
400 if (d.job == 'del') {
401 KorAP.Pipe.remove(d.service);
402 } else {
403 KorAP.Pipe.append(d.service);
404 };
405 };
406 break;
Akronc3003642020-03-30 10:19:14 +0200407
408 // Get information from the embedding platform
409 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200410
411 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200412 if (d.key == 'KQ') {
413 if (KorAP.koralQuery !== undefined) {
414 d["value"] = KorAP.koralQuery;
415 };
Akron45308ce2020-08-28 14:10:23 +0200416 }
417
Akron432972b2020-09-18 17:05:53 +0200418 // Get Query information from from
Akron45308ce2020-08-28 14:10:23 +0200419 else if (d.key == 'QueryForm') {
420 let doc = document;
421 let v = d["value"] = {};
422
423 var el;
424 if (el = doc.getElementById('q-field')) {
425 v["q"] = el.value;
426 };
427 if (el = doc.getElementById('ql-field')) {
428 v["ql"] = el.value;
429 };
430 if (el = KorAP.vc) {
431 v["cq"] = el.toQuery();
432 };
Akron432972b2020-09-18 17:05:53 +0200433 }
434
435 // Get Query information from parameters
436 else if (d.key == 'QueryParam') {
437
438 // Supported in all modern browsers
439 var p = new URLSearchParams(window.location.search);
440 let v = d["value"] = {};
441 v["q"] = p.get('q');
442 v["ql"] = p.get('ql');
443 v["cq"] = p.get('cq');
Akronc3003642020-03-30 10:19:14 +0200444 };
445 };
446
447 // data needs to be mirrored
448 if (d._id) {
449 service.sendMsg(d);
Akrona6c32b92018-07-02 18:39:42 +0200450 };
451
452 // TODO:
453 // Close
Akron479994e2018-07-02 13:21:44 +0200454 },
455
Akron22598cd2019-12-09 14:59:03 +0100456 // Close the service
457 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200458 delete limits[id];
Akron22598cd2019-12-09 14:59:03 +0100459
460 // Close the iframe
461 if (services[id] && services[id]._closeIframe) {
462 services[id]._closeIframe();
463
464 // Remove from list
465 delete services[id];
466 };
467
Akron76dd8d32018-07-06 09:30:22 +0200468
469 // Remove listeners in case no widget
470 // is available any longer
471 if (Object.keys(limits).length == 0)
472 this._removeListener();
473 },
474
Akrona6c32b92018-07-02 18:39:42 +0200475 // Get a random identifier
476 _randomID : function () {
477 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200478 },
479
Akron76dd8d32018-07-06 09:30:22 +0200480 // Remove the listener
481 _removeListener : function () {
482 window.clearInterval(this._timer);
483 this._timer = undefined;
484 window.removeEventListener("message", this._listener);
485 this._listener = undefined;
486 },
487
Akron22598cd2019-12-09 14:59:03 +0100488 /**
489 * Return the service element.
490 */
491 element : function () {
492 if (!this._element) {
493 this._element = document.createElement('div');
494 this._element.setAttribute("id", "services");
495 }
496 return this._element;
497 },
498
Akronb43c8c62018-07-04 18:27:28 +0200499 // Destructor, just for testing scenarios
500 destroy : function () {
501 limits = {};
Akron22598cd2019-12-09 14:59:03 +0100502 for (let s in services) {
503 services[s].close();
Akron4a703872018-07-26 10:59:41 +0200504 };
Akron22598cd2019-12-09 14:59:03 +0100505 services = {};
Akron2d0d96d2019-11-18 19:49:50 +0100506 for (let b in buttons) {
507 buttons[b] = [];
508 };
509 for (let b in buttonsSingle) {
510 buttonsSingle[b] = [];
511 };
Akron22598cd2019-12-09 14:59:03 +0100512
513 if (this._element) {
514 let e = this._element;
515 if (e.parentNode) {
516 e.parentNode.removeChild(e);
517 };
518 this._element = null;
519 };
520
Akron76dd8d32018-07-06 09:30:22 +0200521 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200522 }
Akrone1c27f62018-07-20 11:42:59 +0200523 };
Akron479994e2018-07-02 13:21:44 +0200524});