blob: 4504e560a766852411a94e4f5a375a7e32def817 [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
Akronba09ed22020-10-01 16:01:45 +0200152 // The button has a state and the state is associated to the
153 // a intermediate object to toggle the view
154 if ('state' in this.button && this.button.state.associates() > 0) {
155
Akronba09ed22020-10-01 16:01:45 +0200156 let s = this.button.state;
Akronfcf89db2020-10-01 17:40:20 +0200157
158 // The associated service is existent
159 if (services[this.button['widgetID']]) {
160
161 // TODO:
162 // Use roll() when existing
163 if (s.get()) {
164 s.set(false);
165 } else {
166 s.set(true);
167 };
168 return;
169 }
170
171 // The service is not existent
172 else {
173
174 // Remove broken state associations
175 s.clear();
Akronba09ed22020-10-01 16:01:45 +0200176 s.set(true);
Akronfcf89db2020-10-01 17:40:20 +0200177 }
Akronba09ed22020-10-01 16:01:45 +0200178 };
179
Akron7c6e05f2018-07-12 19:08:13 +0200180 // Add the widget to the panel
Akronbb891982020-10-05 16:07:18 +0200181 let id = that.addWidget(this, {
182 "name": name,
183 "src": onClick["template"], // that._interpolateURI(onClick["template"], this.match);
184 "permissions": onClick["permissions"]
185 });
Akron7c6e05f2018-07-12 19:08:13 +0200186 plugin["widgets"].push(id);
Akronba09ed22020-10-01 16:01:45 +0200187
188 // If a state exists, associate with a mediator object
189 if ('state' in this.button) {
Akronfcf89db2020-10-01 17:40:20 +0200190 this.button['widgetID'] = id;
Akronba09ed22020-10-01 16:01:45 +0200191 this.button.state.associate({
192 setState : function (value) {
193 // Minimize the widget
194 if (value == false) {
195 services[id].minimize();
196 }
197 else {
198 services[id].show();
199 };
200 }
201 });
202 }
Akron7c6e05f2018-07-12 19:08:13 +0200203 };
204
Akron22598cd2019-12-09 14:59:03 +0100205
Akronba09ed22020-10-01 16:01:45 +0200206 // Button object
207 let obj = {'cls':embed["classes"], 'icon': icon }
208
209 if (onClick["action"] && onClick["action"] == "setWidget") {
210
211 // Create a boolean state value, that initializes to true == opened
212 obj['state'] = stateClass.create(true);
213 };
214
Akron2d0d96d2019-11-18 19:49:50 +0100215 // Add to dynamic button list (e.g. for matches)
216 if (buttons[panel]) {
Akronba09ed22020-10-01 16:01:45 +0200217 buttons[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100218 }
219
220 // Add to static button list (e.g. for query) already loaded
221 else if (KorAP.Panel[panel]) {
Akronba09ed22020-10-01 16:01:45 +0200222 KorAP.Panel[panel].actions.add(title, obj, cb);
Akron2d0d96d2019-11-18 19:49:50 +0100223 }
224
225 // Add to static button list (e.g. for query) not yet loaded
226 else {
Akronba09ed22020-10-01 16:01:45 +0200227 buttonsSingle[panel].push([title, obj, cb]);
Akron2d0d96d2019-11-18 19:49:50 +0100228 }
Akron22598cd2019-12-09 14:59:03 +0100229 }
Akronba09ed22020-10-01 16:01:45 +0200230
231 // TODO There is no possibility to add icons to an plugin toggle button right now.
Akron22598cd2019-12-09 14:59:03 +0100232 else if (onClick["action"] == "toggle") {
233
234 // Todo: Initially false
235 let state = stateClass.create(false);
236
237 // TODO:
238 // Lazy registration (see above!)
Akron792b1a42020-09-14 18:56:38 +0200239 KorAP.Panel[panel].actions.addToggle(title, {'cls':["title"]}, state);
Akron22598cd2019-12-09 14:59:03 +0100240
Akron22598cd2019-12-09 14:59:03 +0100241 // Add the service
Akronbb891982020-10-05 16:07:18 +0200242 let id = this.addService({
243 "name" : name,
244 // TODO:
245 // Use the "service" keyword
246 "src" : onClick["template"],
247 "permissions" : onClick["permissions"]
248 });
Akronfb11a962020-10-05 12:12:55 +0200249
Akron22598cd2019-12-09 14:59:03 +0100250 // TODO:
251 // This is a bit stupid to get the service window
Akronc3003642020-03-30 10:19:14 +0200252 let service = services[id];
253 let iframe = service.load();
Akron22598cd2019-12-09 14:59:03 +0100254
255 // Create object to communicate the toggle state
256 // once the iframe is loaded.
257 iframe.onload = function () {
258 let sendToggle = {
259 setState : function (val) {
Akronc3003642020-03-30 10:19:14 +0200260 service.sendMsg({
Akron22598cd2019-12-09 14:59:03 +0100261 action: 'state',
262 key : onClick['state'],
263 value : val
Akronc3003642020-03-30 10:19:14 +0200264 });
Akron22598cd2019-12-09 14:59:03 +0100265 }
266 };
267
268 // Associate object with the state
269 state.associate(sendToggle);
270 };
271
272 plugin["services"].push(id);
Akron7c6e05f2018-07-12 19:08:13 +0200273 };
274 };
275 },
276
Akron7c6e05f2018-07-12 19:08:13 +0200277 // TODO:
278 // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570
279 // but as simple as possible
280 _interpolateURI : function (uri, obj) {
281 // ...
282 },
283
284
285 /**
Akron4a703872018-07-26 10:59:41 +0200286 * Get named button group - better rename to "action"
Akron7c6e05f2018-07-12 19:08:13 +0200287 */
288 buttonGroup : function (name) {
Akron2d0d96d2019-11-18 19:49:50 +0100289 if (buttons[name] != undefined) {
290 return buttons[name];
291 } else if (buttonsSingle[name] != undefined) {
292 return buttonsSingle[name];
293 };
294 return [];
295 },
296
297 /**
298 * Clear named button group - better rename to "action"
299 */
300 clearButtonGroup : function (name) {
301 if (buttons[name] != undefined) {
302 buttons[name] = [];
303 } else if (buttonsSingle[name] != undefined) {
304 buttonsSingle[name] = [];
305 }
Akron7c6e05f2018-07-12 19:08:13 +0200306 },
Akron479994e2018-07-02 13:21:44 +0200307
Akron22598cd2019-12-09 14:59:03 +0100308 // Optionally initialize the service mechanism and get an ID
309 _getServiceID : function () {
Akron4a703872018-07-26 10:59:41 +0200310
Akron22598cd2019-12-09 14:59:03 +0100311 // Is it the first service?
Akron76dd8d32018-07-06 09:30:22 +0200312 if (!this._listener) {
313
314 /*
315 * Establish the global 'message' hook.
316 */
317 this._listener = this._receiveMsg.bind(this);
318 window.addEventListener("message", this._listener);
319
Akron22598cd2019-12-09 14:59:03 +0100320 // Every second increase the limits of all registered services
Akron76dd8d32018-07-06 09:30:22 +0200321 this._timer = window.setInterval(function () {
322 for (var i in limits) {
323 if (limits[i]++ >= maxMessages) {
324 limits[i] = maxMessages;
325 }
326 }
327 }, 1000);
328 };
329
Akron22598cd2019-12-09 14:59:03 +0100330 // Create a unique random ID per service
331 return 'id-' + this._randomID();
332 },
333
334 /**
335 * Add a service in a certain panel and return the id.
336 */
Akronbb891982020-10-05 16:07:18 +0200337 addService : function (data) {
338 if (!data["src"])
Akron22598cd2019-12-09 14:59:03 +0100339 return;
340
341 let id = this._getServiceID();
342
Akronbb891982020-10-05 16:07:18 +0200343 data["id"] = id;
344
Akron22598cd2019-12-09 14:59:03 +0100345 // Create a new service
Akronbb891982020-10-05 16:07:18 +0200346 let service = serviceClass.create(data);
Akron22598cd2019-12-09 14:59:03 +0100347
Akron22598cd2019-12-09 14:59:03 +0100348 services[id] = service;
349 limits[id] = maxMessages;
350
Akron22598cd2019-12-09 14:59:03 +0100351 // Add service to panel
352 this.element().appendChild(
353 service.load()
354 );
355
356 return id;
357 },
358
359
360 /**
361 * Open a new widget view in a certain panel and return
362 * the id.
363 */
Akronbb891982020-10-05 16:07:18 +0200364 addWidget : function (panel, data) {
365 // panel, name, src, permissions
Akron22598cd2019-12-09 14:59:03 +0100366
367 let id = this._getServiceID();
Akrona6c32b92018-07-02 18:39:42 +0200368
Akronbb891982020-10-05 16:07:18 +0200369 data["id"] = id;
370
Akrona6c32b92018-07-02 18:39:42 +0200371 // Create a new widget
Akronbb891982020-10-05 16:07:18 +0200372 var widget = widgetClass.create(data);
Akrona6c32b92018-07-02 18:39:42 +0200373
374 // Store the widget based on the identifier
Akron22598cd2019-12-09 14:59:03 +0100375 services[id] = widget;
Akrona99315e2018-07-03 22:56:45 +0200376 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +0200377
Akron4a703872018-07-26 10:59:41 +0200378 widget._mgr = this;
379
Akrone1c27f62018-07-20 11:42:59 +0200380 // Add widget to panel
381 panel.add(widget);
Akronb43c8c62018-07-04 18:27:28 +0200382
383 return id;
Akron479994e2018-07-02 13:21:44 +0200384 },
385
Akron4a703872018-07-26 10:59:41 +0200386
387 /**
Akron22598cd2019-12-09 14:59:03 +0100388 * Get service by identifier
Akron4a703872018-07-26 10:59:41 +0200389 */
Akron22598cd2019-12-09 14:59:03 +0100390 service : function (id) {
391 return services[id];
Akron4a703872018-07-26 10:59:41 +0200392 },
393
394
Akron22598cd2019-12-09 14:59:03 +0100395 // Receive a call from an embedded service.
Akrone8e2c952018-07-04 13:43:12 +0200396 // The handling needs to be very careful,
397 // as this can easily become a security nightmare.
Akron479994e2018-07-02 13:21:44 +0200398 _receiveMsg : function (e) {
399 // Get event data
400 var d = e.data;
401
Akrona99315e2018-07-03 22:56:45 +0200402 // If no data given - fail
403 // (probably check that it's an assoc array)
404 if (!d)
405 return;
406
407 // e.origin is probably set and okay - CHECK!
Akron479994e2018-07-02 13:21:44 +0200408
Akrona99315e2018-07-03 22:56:45 +0200409 // Get origin ID
410 var id = d["originID"];
411
412 // If no origin ID given - fail
413 if (!id)
414 return;
415
Akron22598cd2019-12-09 14:59:03 +0100416 // Get the service
417 let service = services[id];
Akrona6c32b92018-07-02 18:39:42 +0200418
Akron22598cd2019-12-09 14:59:03 +0100419 // If the addressed service does not exist - fail
420 if (!service)
Akrona6c32b92018-07-02 18:39:42 +0200421 return;
422
Akrona99315e2018-07-03 22:56:45 +0200423 // Check for message limits
424 if (limits[id]-- < 0) {
Akrone8e2c952018-07-04 13:43:12 +0200425
Akron22598cd2019-12-09 14:59:03 +0100426 // Kill service
427 KorAP.log(0, 'Suspicious action by service', service.src);
Akron7c6e05f2018-07-12 19:08:13 +0200428
429 // TODO:
430 // Potentially kill the whole plugin!
Akron4a703872018-07-26 10:59:41 +0200431
Akron22598cd2019-12-09 14:59:03 +0100432 // This removes all connections before closing the service
433 this._closeService(service.id);
434
435 // if (service.isWidget)
436 service.close();
437
Akrona99315e2018-07-03 22:56:45 +0200438 return;
439 };
Akrona6c32b92018-07-02 18:39:42 +0200440
Akron479994e2018-07-02 13:21:44 +0200441 // Resize the iframe
Akron22598cd2019-12-09 14:59:03 +0100442 switch (d.action) {
443 case 'resize':
444 if (service.isWidget)
445 service.resize(d);
446 break;
Akron479994e2018-07-02 13:21:44 +0200447
448 // Log message from iframe
Akron22598cd2019-12-09 14:59:03 +0100449 case 'log':
450 KorAP.log(d.code, d.msg, service.src);
451 break;
Akron51ee6232019-12-17 21:00:05 +0100452
453 // Modify pipes
454 case 'pipe':
455 if (KorAP.Pipe != undefined) {
456 if (d.job == 'del') {
457 KorAP.Pipe.remove(d.service);
458 } else {
459 KorAP.Pipe.append(d.service);
460 };
461 };
462 break;
Akronc3003642020-03-30 10:19:14 +0200463
464 // Get information from the embedding platform
465 case 'get':
Akron45308ce2020-08-28 14:10:23 +0200466
467 // Get KoralQuery
Akronc3003642020-03-30 10:19:14 +0200468 if (d.key == 'KQ') {
469 if (KorAP.koralQuery !== undefined) {
470 d["value"] = KorAP.koralQuery;
471 };
Akron45308ce2020-08-28 14:10:23 +0200472 }
473
Akron432972b2020-09-18 17:05:53 +0200474 // Get Query information from from
Akron45308ce2020-08-28 14:10:23 +0200475 else if (d.key == 'QueryForm') {
476 let doc = document;
477 let v = d["value"] = {};
478
479 var el;
480 if (el = doc.getElementById('q-field')) {
481 v["q"] = el.value;
482 };
483 if (el = doc.getElementById('ql-field')) {
484 v["ql"] = el.value;
485 };
486 if (el = KorAP.vc) {
487 v["cq"] = el.toQuery();
488 };
Akron432972b2020-09-18 17:05:53 +0200489 }
490
491 // Get Query information from parameters
492 else if (d.key == 'QueryParam') {
493
494 // Supported in all modern browsers
495 var p = new URLSearchParams(window.location.search);
496 let v = d["value"] = {};
497 v["q"] = p.get('q');
498 v["ql"] = p.get('ql');
499 v["cq"] = p.get('cq');
Akronc3003642020-03-30 10:19:14 +0200500 };
501 };
502
503 // data needs to be mirrored
504 if (d._id) {
505 service.sendMsg(d);
Akrona6c32b92018-07-02 18:39:42 +0200506 };
507
508 // TODO:
509 // Close
Akron479994e2018-07-02 13:21:44 +0200510 },
511
Akron22598cd2019-12-09 14:59:03 +0100512 // Close the service
513 _closeService : function (id) {
Akron4a703872018-07-26 10:59:41 +0200514 delete limits[id];
Akronfcf89db2020-10-01 17:40:20 +0200515
Akron22598cd2019-12-09 14:59:03 +0100516 // Close the iframe
517 if (services[id] && services[id]._closeIframe) {
518 services[id]._closeIframe();
519
520 // Remove from list
521 delete services[id];
522 };
523
Akron76dd8d32018-07-06 09:30:22 +0200524
525 // Remove listeners in case no widget
526 // is available any longer
527 if (Object.keys(limits).length == 0)
528 this._removeListener();
529 },
530
Akrona6c32b92018-07-02 18:39:42 +0200531 // Get a random identifier
532 _randomID : function () {
533 return randomID(20);
Akronb43c8c62018-07-04 18:27:28 +0200534 },
535
Akron76dd8d32018-07-06 09:30:22 +0200536 // Remove the listener
537 _removeListener : function () {
538 window.clearInterval(this._timer);
539 this._timer = undefined;
540 window.removeEventListener("message", this._listener);
541 this._listener = undefined;
542 },
543
Akron22598cd2019-12-09 14:59:03 +0100544 /**
545 * Return the service element.
546 */
547 element : function () {
548 if (!this._element) {
549 this._element = document.createElement('div');
550 this._element.setAttribute("id", "services");
551 }
552 return this._element;
553 },
554
Akronb43c8c62018-07-04 18:27:28 +0200555 // Destructor, just for testing scenarios
556 destroy : function () {
557 limits = {};
Akron22598cd2019-12-09 14:59:03 +0100558 for (let s in services) {
559 services[s].close();
Akron4a703872018-07-26 10:59:41 +0200560 };
Akron22598cd2019-12-09 14:59:03 +0100561 services = {};
Akron2d0d96d2019-11-18 19:49:50 +0100562 for (let b in buttons) {
563 buttons[b] = [];
564 };
565 for (let b in buttonsSingle) {
566 buttonsSingle[b] = [];
567 };
Akron22598cd2019-12-09 14:59:03 +0100568
569 if (this._element) {
570 let e = this._element;
571 if (e.parentNode) {
572 e.parentNode.removeChild(e);
573 };
574 this._element = null;
575 };
576
Akron76dd8d32018-07-06 09:30:22 +0200577 this._removeListener();
Akron479994e2018-07-02 13:21:44 +0200578 }
Akrone1c27f62018-07-20 11:42:59 +0200579 };
Akron479994e2018-07-02 13:21:44 +0200580});