Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 11 | define(["plugin/widget", "util"], function (widgetClass) { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 12 | "use strict"; |
| 13 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 14 | // 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). |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 19 | var maxMessages = 100; |
| 20 | var limits = {}; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 21 | |
| 22 | // Contains all widgets to address with |
| 23 | // messages to them |
| 24 | var widgets = {}; |
| 25 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 26 | 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 | }); |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 45 | |
| 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); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 54 | return this; |
| 55 | }, |
| 56 | |
| 57 | /** |
| 58 | * Open a new widget on a certain element |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 59 | */ |
| 60 | addWidget : function (element, src) { |
| 61 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 62 | // 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; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 70 | limits[id] = maxMessages; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 71 | |
| 72 | // Open widget in frontend |
| 73 | element.appendChild( |
| 74 | widget.element() |
| 75 | ); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 76 | }, |
| 77 | |
| 78 | // Receive a call from an embedded iframe |
| 79 | _receiveMsg : function (e) { |
| 80 | // Get event data |
| 81 | var d = e.data; |
| 82 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 83 | // 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! |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 89 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 90 | // TODO: |
| 91 | // Deal with mad iframes |
| 92 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 93 | // Get origin ID |
| 94 | var id = d["originID"]; |
| 95 | |
| 96 | // If no origin ID given - fail |
| 97 | if (!id) |
| 98 | return; |
| 99 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 100 | // Get the widget |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 101 | var widget = widgets[id]; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 102 | |
| 103 | // If the addressed widget does not exist - fail |
| 104 | if (!widget) |
| 105 | return; |
| 106 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame^] | 107 | // Check for message limits |
| 108 | if (limits[id]-- < 0) { |
| 109 | widget.shutdown(); |
| 110 | delete limits[id]; |
| 111 | delete widgets[id]; |
| 112 | return; |
| 113 | }; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 114 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 115 | // Resize the iframe |
| 116 | if (d.action === 'resize') { |
| 117 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 118 | widget.resize(d); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Log message from iframe |
| 122 | else if (d.action === 'log') { |
| 123 | KorAP.log(d.code, d.msg); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | // TODO: |
| 127 | // Close |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 128 | }, |
| 129 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 130 | // Get a random identifier |
| 131 | _randomID : function () { |
| 132 | return randomID(20); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | }); |