blob: 1fe417a060793b541c8b52dfdacd981a470236a8 [file] [log] [blame]
Akrona6c32b92018-07-02 18:39:42 +02001/**
2 * The plugin system is based
3 * on registered widgets (iframes) from
4 * foreign services.
5 * The widget component represents a single iframe.
6 *
7 * @author Nils Diewald
8 */
9
10define(["util"], function () {
11 "use strict";
12
Akron8d646d72018-07-08 13:45:53 +020013 // Localization values
14 const loc = KorAP.Locale;
15 loc.CLOSE = loc.CLOSE || 'Close';
16
Akrona6c32b92018-07-02 18:39:42 +020017 return {
18
19 /**
20 * Create new widget
21 */
22 create : function (src, id) {
23 return Object.create(this)._init(src, id);
24 },
25
Akrone8e2c952018-07-04 13:43:12 +020026 // Initialize widget
Akrona6c32b92018-07-02 18:39:42 +020027 _init : function (src, id) {
28 this.src = src;
29 this.id = id;
30 return this;
31 },
32
33 /**
34 * The element of the widget
35 */
36 element : function () {
37
38 if (this._element)
39 return this._element;
40
Akron8d646d72018-07-08 13:45:53 +020041 var div = document.createElement('div');
42 div.classList.add('widget');
43
Akrona6c32b92018-07-02 18:39:42 +020044 // Spawn new iframe
Akron8d646d72018-07-08 13:45:53 +020045 var i = div.addE('iframe');
Akrona6c32b92018-07-02 18:39:42 +020046 i.setAttribute('allowTransparency',"true");
47 i.setAttribute('frameborder', 0);
48 i.setAttribute('sandbox','allow-scripts');
Akrone8e2c952018-07-04 13:43:12 +020049 i.style.height = '0px';
Akrona6c32b92018-07-02 18:39:42 +020050 i.setAttribute('name', this.id);
51 i.setAttribute('src', this.src);
Akron8d646d72018-07-08 13:45:53 +020052 this._iframe = i;
Akrona6c32b92018-07-02 18:39:42 +020053
Akron8d646d72018-07-08 13:45:53 +020054 var ul = div.addE('ul');
55 ul.classList.add('action','right');
56
57 // Add close button
58 var close = ul.addE('li');
59 close.addE('span').addT(loc.CLOSE);
60 close.classList.add('close');
61 close.setAttribute('title', loc.CLOSE);
62
63 // Close match
64 close.addEventListener('click', function (e) {
65 e.halt();
66 this.shutdown()
67 }.bind(this));
68
69 this._element = div;
70
71 return div;
72 },
73
74 // Return iframe of widget
75 iframe : function () {
76 if (this._iframe)
77 return this._iframe;
78 this.element();
79 return this._iframe;
Akrona6c32b92018-07-02 18:39:42 +020080 },
81
82 // Resize iframe
83 resize : function (data) {
Akron8d646d72018-07-08 13:45:53 +020084 this.iframe().style.height = data.height + 'px';
Akrona99315e2018-07-03 22:56:45 +020085 },
86
87 // Shutdown suspicious iframe
88 shutdown : function () {
Akrona99315e2018-07-03 22:56:45 +020089 this._element.parentNode.removeChild(this._element);
Akrona6c32b92018-07-02 18:39:42 +020090 }
91 }
92});