blob: 01dddac635ef6df38e780ef20437c5c917a02e6a [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
11define(["util"], function () {
12 "use strict";
13
14 return {
15
16 /**
17 * Create new plugin management system
18 */
19 create : function () {
20 return Object.create(this)._init();
21 },
22
23 /*
24 * Initialize the plugin manager by establishing
25 * the global 'message' hook.
26 */
27 _init : function () {
28
29 var that = this;
30 window.addEventListener("message", function (e) {
31 that._receiveMsg(e);
32 });
33 return this;
34 },
35
36 /**
37 * Open a new widget on a certain element
38 * TODO: and register
39 */
40 addWidget : function (element, src) {
41
42 // Spawn new iframe
43 var iframe = element.addE('iframe');
44 iframe.setAttribute('allowTransparency',"true");
45 iframe.setAttribute('frameborder',0);
46 iframe.setAttribute('sandbox','allow-scripts');
47 iframe.classList.add('widget');
48 iframe.setAttribute('src', src);
49 },
50
51 // Receive a call from an embedded iframe
52 _receiveMsg : function (e) {
53 // Get event data
54 var d = e.data;
55
56 // TODO: Check for e.origin!
57
58 // TODO: Deal with mad iframes
59
60 // Resize the iframe
61 if (d.action === 'resize') {
62
63 // TODO: Check which iframe it was
64 // var iframe = document.getElementById('?');
65
66 // this.resize(iframe, d);
67 console.log('Resizing not yet implemented');
68 }
69
70 // Log message from iframe
71 else if (d.action === 'log') {
72 KorAP.log(d.code, d.msg);
73 }
74 },
75
76
77 // Resize the calling iframe
78 resize : function (iframe, d) {
79 iframe.style.height = d.height + 'px';
80 }
81 }
82});