blob: 338faa31c20439facdf2ca7535d2dcbf691d6359 [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 */
Akron7991b192018-07-09 17:28:43 +020022 create : function (name, src, id) {
23 return Object.create(this)._init(name, src, id);
Akrona6c32b92018-07-02 18:39:42 +020024 },
25
Akrone8e2c952018-07-04 13:43:12 +020026 // Initialize widget
Akron7991b192018-07-09 17:28:43 +020027 _init : function (name, src, id) {
28 this.name = name;
Akrona6c32b92018-07-02 18:39:42 +020029 this.src = src;
30 this.id = id;
31 return this;
32 },
33
34 /**
35 * The element of the widget
36 */
37 element : function () {
38
39 if (this._element)
40 return this._element;
41
Akron8d646d72018-07-08 13:45:53 +020042 var div = document.createElement('div');
43 div.classList.add('widget');
44
Akrona6c32b92018-07-02 18:39:42 +020045 // Spawn new iframe
Akron8d646d72018-07-08 13:45:53 +020046 var i = div.addE('iframe');
Akrona6c32b92018-07-02 18:39:42 +020047 i.setAttribute('allowTransparency',"true");
48 i.setAttribute('frameborder', 0);
49 i.setAttribute('sandbox','allow-scripts');
Akrone8e2c952018-07-04 13:43:12 +020050 i.style.height = '0px';
Akrona6c32b92018-07-02 18:39:42 +020051 i.setAttribute('name', this.id);
52 i.setAttribute('src', this.src);
Akron8d646d72018-07-08 13:45:53 +020053 this._iframe = i;
Akrona6c32b92018-07-02 18:39:42 +020054
Akron8d646d72018-07-08 13:45:53 +020055 var ul = div.addE('ul');
56 ul.classList.add('action','right');
57
58 // Add close button
59 var close = ul.addE('li');
60 close.addE('span').addT(loc.CLOSE);
61 close.classList.add('close');
62 close.setAttribute('title', loc.CLOSE);
63
Akron7991b192018-07-09 17:28:43 +020064 // Add info button on plugin
65 var plugin = ul.addE('li');
66 plugin.addE('span').addT(this.name);
67 plugin.classList.add('plugin');
68 plugin.setAttribute('title', this.name);
69
Akron8d646d72018-07-08 13:45:53 +020070 // Close match
71 close.addEventListener('click', function (e) {
72 e.halt();
73 this.shutdown()
74 }.bind(this));
75
76 this._element = div;
77
78 return div;
79 },
80
81 // Return iframe of widget
82 iframe : function () {
83 if (this._iframe)
84 return this._iframe;
85 this.element();
86 return this._iframe;
Akrona6c32b92018-07-02 18:39:42 +020087 },
88
89 // Resize iframe
90 resize : function (data) {
Akron8d646d72018-07-08 13:45:53 +020091 this.iframe().style.height = data.height + 'px';
Akrona99315e2018-07-03 22:56:45 +020092 },
93
94 // Shutdown suspicious iframe
95 shutdown : function () {
Akrona99315e2018-07-03 22:56:45 +020096 this._element.parentNode.removeChild(this._element);
Akrona6c32b92018-07-02 18:39:42 +020097 }
98 }
99});