blob: 402934995169db9b42d076b93544acd6acf3d437 [file] [log] [blame]
Akron22598cd2019-12-09 14:59:03 +01001define(function () {
2 "use strict";
Akronc3003642020-03-30 10:19:14 +02003
Akron22598cd2019-12-09 14:59:03 +01004 return {
5 create : function (name, src, id) {
6 return Object.create(this)._init(name, src, id);
7 },
8
9 // Initialize service
10 _init : function (name, src, id) {
11 if (!name || !src || !id)
12 throw Error("Service not well defined");
13 this.name = name;
14 this.src = src;
15 this.id = id;
Akronfb11a962020-10-05 12:12:55 +020016 this._perm = new Set();
17
Akron22598cd2019-12-09 14:59:03 +010018 // There is no close method defined yet
19 if (!this.close) {
20 this.close = function () {
21 this._closeIframe();
22 }
23 }
24
25 return this;
26 },
27
28 /**
29 * The element of the service as embedded in the panel
30 */
31 load : function () {
32 if (this._load)
33 return this._load;
Akron24f48ea2020-07-01 09:37:19 +020034
35 if (window.location.protocol == 'https:' &&
36 this.src.toLowerCase().indexOf('https:') != 0) {
37 KorAP.log(0, "Service endpoint is insecure");
38 return;
39 };
40
Akron22598cd2019-12-09 14:59:03 +010041 // Spawn new iframe
42 let e = document.createElement('iframe');
43 e.setAttribute('allowTransparency',"true");
44 e.setAttribute('frameborder', 0);
hebasta78913242020-03-30 13:39:20 +020045 // Allow forms in Plugins
Akronfb11a962020-10-05 12:12:55 +020046 e.setAttribute('sandbox', this._permString());
Akron22598cd2019-12-09 14:59:03 +010047 e.style.height = '0px';
48 e.setAttribute('name', this.id);
49 e.setAttribute('src', this.src);
50
51 this._load = e;
52 return e;
53 },
54
Akronfb11a962020-10-05 12:12:55 +020055 allow : function (permission) {
56 if (Array.isArray(permission)) {
57 permission.forEach(
58 p => this._perm.add(p)
59 );
60 }
61 else {
62 this._perm.add(permission);
63 };
64
65 if (this._load) {
66 this._load.setAttribute('sandbox', this._permString());
67 }
68 },
69
70 _permString : function () {
71 return Array.from(this._perm).sort().join(" ");
72 },
73
Akronc3003642020-03-30 10:19:14 +020074 /**
75 * Send a message to the embedded service.
76 */
77 sendMsg : function (d) {
78 let iframe = this.load();
79 iframe.contentWindow.postMessage(
80 d,
81 '*'
82 ); // TODO: Fix origin
83 },
84
Akron22598cd2019-12-09 14:59:03 +010085 // onClose : function () {},
86
87 /**
88 * Close the service iframe.
89 */
90 _closeIframe : function () {
91 var e = this._load;
92 if (e && e.parentNode) {
93 e.parentNode.removeChild(e);
94 };
95 this._load = null;
96 }
97 };
98});