blob: d26de27451c23af6ad696429d3b12602ee1416cd [file] [log] [blame]
Akron479994e2018-07-02 13:21:44 +02001/**
2 * The plugin system is based
3 * on registered widgets (iframes) from
4 * foreign services.
5 * The server component spawns new iframes and
6 * listens to them.
7 *
8 * @author Nils Diewald
9 */
10
Akrona6c32b92018-07-02 18:39:42 +020011define(["plugin/widget", "util"], function (widgetClass) {
Akron479994e2018-07-02 13:21:44 +020012 "use strict";
13
Akrona6c32b92018-07-02 18:39:42 +020014 // TODO:
15 // This is a counter to limit acceptable incoming messages
16 // to hundred. For every message, this will be decreased
17 // (down to 0), for every second this will be increased
18 // (up to 100).
Akrona99315e2018-07-03 22:56:45 +020019 var maxMessages = 100;
20 var limits = {};
Akrona6c32b92018-07-02 18:39:42 +020021
22 // Contains all widgets to address with
23 // messages to them
24 var widgets = {};
25
Akron479994e2018-07-02 13:21:44 +020026 return {
27
28 /**
29 * Create new plugin management system
30 */
31 create : function () {
32 return Object.create(this)._init();
33 },
34
35 /*
36 * Initialize the plugin manager by establishing
37 * the global 'message' hook.
38 */
39 _init : function () {
40
41 var that = this;
42 window.addEventListener("message", function (e) {
43 that._receiveMsg(e);
44 });
Akrona99315e2018-07-03 22:56:45 +020045
46 // Every second increase the limits of all registered widgets
47 var myTimer = setInterval(function () {
48 for (var i in limits) {
49 if (limits[i]++ >= maxMessages) {
50 limits[i] = maxMessages;
51 }
52 }
53 }, 1000);
Akron479994e2018-07-02 13:21:44 +020054 return this;
55 },
56
57 /**
58 * Open a new widget on a certain element
Akron479994e2018-07-02 13:21:44 +020059 */
60 addWidget : function (element, src) {
61
Akrona6c32b92018-07-02 18:39:42 +020062 // Create a unique random ID per widget
63 var id = 'id-' + this._randomID();
64
65 // Create a new widget
66 var widget = widgetClass.create(src, id);
67
68 // Store the widget based on the identifier
69 widgets[id] = widget;
Akrona99315e2018-07-03 22:56:45 +020070 limits[id] = maxMessages;
Akrona6c32b92018-07-02 18:39:42 +020071
72 // Open widget in frontend
73 element.appendChild(
74 widget.element()
75 );
Akron479994e2018-07-02 13:21:44 +020076 },
77
78 // Receive a call from an embedded iframe
79 _receiveMsg : function (e) {
80 // Get event data
81 var d = e.data;
82
Akrona99315e2018-07-03 22:56:45 +020083 // If no data given - fail
84 // (probably check that it's an assoc array)
85 if (!d)
86 return;
87
88 // e.origin is probably set and okay - CHECK!
Akron479994e2018-07-02 13:21:44 +020089
Akrona6c32b92018-07-02 18:39:42 +020090 // TODO:
91 // Deal with mad iframes
92
Akrona99315e2018-07-03 22:56:45 +020093 // Get origin ID
94 var id = d["originID"];
95
96 // If no origin ID given - fail
97 if (!id)
98 return;
99
Akrona6c32b92018-07-02 18:39:42 +0200100 // Get the widget
Akrona99315e2018-07-03 22:56:45 +0200101 var widget = widgets[id];
Akrona6c32b92018-07-02 18:39:42 +0200102
103 // If the addressed widget does not exist - fail
104 if (!widget)
105 return;
106
Akrona99315e2018-07-03 22:56:45 +0200107 // Check for message limits
108 if (limits[id]-- < 0) {
109 widget.shutdown();
110 delete limits[id];
111 delete widgets[id];
112 return;
113 };
Akrona6c32b92018-07-02 18:39:42 +0200114
Akron479994e2018-07-02 13:21:44 +0200115 // Resize the iframe
116 if (d.action === 'resize') {
117
Akrona6c32b92018-07-02 18:39:42 +0200118 widget.resize(d);
Akron479994e2018-07-02 13:21:44 +0200119 }
120
121 // Log message from iframe
122 else if (d.action === 'log') {
123 KorAP.log(d.code, d.msg);
Akrona6c32b92018-07-02 18:39:42 +0200124 };
125
126 // TODO:
127 // Close
Akron479994e2018-07-02 13:21:44 +0200128 },
129
Akrona6c32b92018-07-02 18:39:42 +0200130 // Get a random identifier
131 _randomID : function () {
132 return randomID(20);
Akron479994e2018-07-02 13:21:44 +0200133 }
134 }
135});