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 | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 14 | KorAP.Panel = KorAP.Panel || {}; |
| 15 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 16 | // Contains all widgets to address with |
| 17 | // messages to them |
| 18 | var widgets = {}; |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 19 | var plugins = {}; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 20 | |
| 21 | // TODO: |
| 22 | // These should better be panels and every panel |
| 23 | // has a buttonGroup |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 24 | |
| 25 | // List of panels with dynamic buttons, i.e. |
| 26 | // panels that may occur multiple times. |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 27 | var buttons = { |
| 28 | match : [] |
| 29 | }; |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 30 | |
| 31 | // List of panels with static buttons, i.e. |
| 32 | // panels that occur only once. |
| 33 | var buttonsSingle = { |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 34 | query : [], |
| 35 | result : [] |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 36 | } |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 37 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 38 | // 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 |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 49 | // all widgets every second to see if it is still alive, |
| 50 | // otherwise kill |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 51 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 52 | // Load Plugin server |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 53 | return { |
| 54 | |
| 55 | /** |
| 56 | * Create new plugin management system |
| 57 | */ |
| 58 | create : function () { |
| 59 | return Object.create(this)._init(); |
| 60 | }, |
| 61 | |
| 62 | /* |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 63 | * Initialize the plugin manager |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 64 | */ |
| 65 | _init : function () { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 66 | return this; |
| 67 | }, |
| 68 | |
| 69 | /** |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 70 | * Register a plugin described as a JSON object. |
| 71 | * |
| 72 | * This is work in progress. |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 73 | * |
| 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 | * } |
| 88 | * }] |
| 89 | * }); |
| 90 | * |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 91 | */ |
| 92 | register : function (obj) { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 93 | // TODO: |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 94 | // These fields need to be localized for display by a structure like |
| 95 | // { de : { name : '..' }, en : { .. } } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 96 | var name = obj["name"]; |
| 97 | |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 98 | if (!name) |
| 99 | throw new Error("Missing name of plugin"); |
| 100 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 101 | // Register plugin by name |
| 102 | var plugin = plugins[name] = { |
| 103 | name : name, |
| 104 | desc : obj["desc"], |
| 105 | about : obj["about"], |
| 106 | widgets : [] |
| 107 | }; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 108 | |
| 109 | if (typeof obj["embed"] !== 'object') |
| 110 | throw new Error("Embedding of plugin is no list"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 111 | |
| 112 | // Embed all embeddings of the plugin |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 113 | var that = this; |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 114 | for (var i in obj["embed"]) { |
| 115 | var embed = obj["embed"][i]; |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 116 | |
| 117 | if (typeof embed !== 'object') |
| 118 | throw new Error("Embedding of plugin is no object"); |
| 119 | |
| 120 | var panel = embed["panel"]; |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 121 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 122 | if (!panel || !(buttons[panel] || buttonsSingle[panel])) |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 123 | throw new Error("Panel for plugin is invalid"); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 124 | var onClick = embed["onClick"]; |
| 125 | |
| 126 | // Needs to be localized as well |
| 127 | var title = embed["title"]; |
| 128 | |
| 129 | // The embedding will open a widget |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 130 | if (!onClick["action"] || onClick["action"] == "addWidget") { |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 131 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 132 | var cb = function (e) { |
| 133 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 134 | // "this" is bind to the panel |
| 135 | |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 136 | // Get the URL of the widget |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 137 | var url = onClick["template"]; |
| 138 | // that._interpolateURI(onClick["template"], this.match); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 139 | |
| 140 | // Add the widget to the panel |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 141 | var id = that.addWidget(this, name, url); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 142 | plugin["widgets"].push(id); |
| 143 | }; |
| 144 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 145 | // Add to dynamic button list (e.g. for matches) |
| 146 | if (buttons[panel]) { |
| 147 | buttons[panel].push([title, embed["classes"], cb]); |
| 148 | } |
| 149 | |
| 150 | // Add to static button list (e.g. for query) already loaded |
| 151 | else if (KorAP.Panel[panel]) { |
| 152 | KorAP.Panel[panel].actions.add(title, embed["classes"], cb); |
| 153 | } |
| 154 | |
| 155 | // Add to static button list (e.g. for query) not yet loaded |
| 156 | else { |
| 157 | buttonsSingle[panel].push([title, embed["classes"], cb]); |
| 158 | } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 159 | }; |
| 160 | }; |
| 161 | }, |
| 162 | |
| 163 | |
| 164 | // TODO: |
| 165 | // Interpolate URIs similar to https://tools.ietf.org/html/rfc6570 |
| 166 | // but as simple as possible |
| 167 | _interpolateURI : function (uri, obj) { |
| 168 | // ... |
| 169 | }, |
| 170 | |
| 171 | |
| 172 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 173 | * Get named button group - better rename to "action" |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 174 | */ |
| 175 | buttonGroup : function (name) { |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 176 | if (buttons[name] != undefined) { |
| 177 | return buttons[name]; |
| 178 | } else if (buttonsSingle[name] != undefined) { |
| 179 | return buttonsSingle[name]; |
| 180 | }; |
| 181 | return []; |
| 182 | }, |
| 183 | |
| 184 | /** |
| 185 | * Clear named button group - better rename to "action" |
| 186 | */ |
| 187 | clearButtonGroup : function (name) { |
| 188 | if (buttons[name] != undefined) { |
| 189 | buttons[name] = []; |
| 190 | } else if (buttonsSingle[name] != undefined) { |
| 191 | buttonsSingle[name] = []; |
| 192 | } |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 193 | }, |
| 194 | |
| 195 | /** |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 196 | * Open a new widget view in a certain panel and return |
| 197 | * the id. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 198 | */ |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 199 | addWidget : function (panel, name, src) { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 200 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 201 | if (!src) |
| 202 | return; |
| 203 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 204 | // Is it the first widget? |
| 205 | if (!this._listener) { |
| 206 | |
| 207 | /* |
| 208 | * Establish the global 'message' hook. |
| 209 | */ |
| 210 | this._listener = this._receiveMsg.bind(this); |
| 211 | window.addEventListener("message", this._listener); |
| 212 | |
| 213 | // Every second increase the limits of all registered widgets |
| 214 | this._timer = window.setInterval(function () { |
| 215 | for (var i in limits) { |
| 216 | if (limits[i]++ >= maxMessages) { |
| 217 | limits[i] = maxMessages; |
| 218 | } |
| 219 | } |
| 220 | }, 1000); |
| 221 | }; |
| 222 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 223 | // Create a unique random ID per widget |
| 224 | var id = 'id-' + this._randomID(); |
| 225 | |
| 226 | // Create a new widget |
Akron | 7991b19 | 2018-07-09 17:28:43 +0200 | [diff] [blame] | 227 | var widget = widgetClass.create(name, src, id); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 228 | |
| 229 | // Store the widget based on the identifier |
| 230 | widgets[id] = widget; |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 231 | limits[id] = maxMessages; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 232 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 233 | widget._mgr = this; |
| 234 | |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 235 | // Add widget to panel |
| 236 | panel.add(widget); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 237 | |
| 238 | return id; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 239 | }, |
| 240 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Get widget by identifier |
| 244 | */ |
| 245 | widget : function (id) { |
| 246 | return widgets[id]; |
| 247 | }, |
| 248 | |
| 249 | |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 250 | // Receive a call from an embedded iframe. |
| 251 | // The handling needs to be very careful, |
| 252 | // as this can easily become a security nightmare. |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 253 | _receiveMsg : function (e) { |
| 254 | // Get event data |
| 255 | var d = e.data; |
| 256 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 257 | // If no data given - fail |
| 258 | // (probably check that it's an assoc array) |
| 259 | if (!d) |
| 260 | return; |
| 261 | |
| 262 | // e.origin is probably set and okay - CHECK! |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 263 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 264 | // Get origin ID |
| 265 | var id = d["originID"]; |
| 266 | |
| 267 | // If no origin ID given - fail |
| 268 | if (!id) |
| 269 | return; |
| 270 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 271 | // Get the widget |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 272 | var widget = widgets[id]; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 273 | |
| 274 | // If the addressed widget does not exist - fail |
| 275 | if (!widget) |
| 276 | return; |
| 277 | |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 278 | // Check for message limits |
| 279 | if (limits[id]-- < 0) { |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 280 | |
| 281 | // Kill widget |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 282 | KorAP.log(0, 'Suspicious action by widget', widget.src); |
Akron | 7c6e05f | 2018-07-12 19:08:13 +0200 | [diff] [blame] | 283 | |
| 284 | // TODO: |
| 285 | // Potentially kill the whole plugin! |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 286 | |
| 287 | // This removes all connections before closing the widget |
| 288 | this._closeWidget(widget.id); |
| 289 | widget.close(); |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 290 | return; |
| 291 | }; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 292 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 293 | // Resize the iframe |
| 294 | if (d.action === 'resize') { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 295 | widget.resize(d); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // Log message from iframe |
| 299 | else if (d.action === 'log') { |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 300 | KorAP.log(d.code, d.msg, widget.src); |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | // TODO: |
| 304 | // Close |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 305 | }, |
| 306 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 307 | // Close the widget |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 308 | _closeWidget : function (id) { |
| 309 | delete limits[id]; |
| 310 | delete widgets[id]; |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 311 | |
| 312 | // Remove listeners in case no widget |
| 313 | // is available any longer |
| 314 | if (Object.keys(limits).length == 0) |
| 315 | this._removeListener(); |
| 316 | }, |
| 317 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 318 | // Get a random identifier |
| 319 | _randomID : function () { |
| 320 | return randomID(20); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 321 | }, |
| 322 | |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 323 | // Remove the listener |
| 324 | _removeListener : function () { |
| 325 | window.clearInterval(this._timer); |
| 326 | this._timer = undefined; |
| 327 | window.removeEventListener("message", this._listener); |
| 328 | this._listener = undefined; |
| 329 | }, |
| 330 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 331 | // Destructor, just for testing scenarios |
| 332 | destroy : function () { |
| 333 | limits = {}; |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 334 | for (let w in widgets) { |
| 335 | widgets[w].close(); |
| 336 | }; |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 337 | widgets = {}; |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 338 | for (let b in buttons) { |
| 339 | buttons[b] = []; |
| 340 | }; |
| 341 | for (let b in buttonsSingle) { |
| 342 | buttonsSingle[b] = []; |
| 343 | }; |
Akron | 76dd8d3 | 2018-07-06 09:30:22 +0200 | [diff] [blame] | 344 | this._removeListener(); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 345 | } |
Akron | e1c27f6 | 2018-07-20 11:42:59 +0200 | [diff] [blame] | 346 | }; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 347 | }); |