blob: 23271e5bd5817fd0cb180cad17780d9e04a75fdb [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 {
Akronbb891982020-10-05 16:07:18 +02005 create : function (data) {
6 return Object.create(this)._init(data);
Akron22598cd2019-12-09 14:59:03 +01007 },
8
9 // Initialize service
Akronbb891982020-10-05 16:07:18 +020010 _init : function (data) {
11 if (!data || !data["name"] || !data["src"] || !data["id"])
Akron22598cd2019-12-09 14:59:03 +010012 throw Error("Service not well defined");
Akronbb891982020-10-05 16:07:18 +020013
14 this.name = data["name"];
15 this.src = data["src"];
16 this.id = data["id"];
Akronfb11a962020-10-05 12:12:55 +020017 this._perm = new Set();
Akronbb891982020-10-05 16:07:18 +020018
19 let perm = data["permissions"];
20 if (perm && Array.isArray(perm)) {
21 perm.forEach(
22 p => this._perm.add(p)
23 );
24 };
Akronfb11a962020-10-05 12:12:55 +020025
Akron22598cd2019-12-09 14:59:03 +010026 // There is no close method defined yet
27 if (!this.close) {
28 this.close = function () {
29 this._closeIframe();
30 }
31 }
32
33 return this;
34 },
35
36 /**
37 * The element of the service as embedded in the panel
38 */
39 load : function () {
40 if (this._load)
41 return this._load;
Akron24f48ea2020-07-01 09:37:19 +020042
43 if (window.location.protocol == 'https:' &&
44 this.src.toLowerCase().indexOf('https:') != 0) {
45 KorAP.log(0, "Service endpoint is insecure");
46 return;
47 };
48
Akron22598cd2019-12-09 14:59:03 +010049 // Spawn new iframe
50 let e = document.createElement('iframe');
51 e.setAttribute('allowTransparency',"true");
52 e.setAttribute('frameborder', 0);
hebasta78913242020-03-30 13:39:20 +020053 // Allow forms in Plugins
Akronbb891982020-10-05 16:07:18 +020054 e.setAttribute('sandbox', Array.from(this._perm).sort().join(" "));
Akron22598cd2019-12-09 14:59:03 +010055 e.style.height = '0px';
56 e.setAttribute('name', this.id);
57 e.setAttribute('src', this.src);
58
59 this._load = e;
60 return e;
61 },
62
Akronc3003642020-03-30 10:19:14 +020063 /**
64 * Send a message to the embedded service.
65 */
66 sendMsg : function (d) {
67 let iframe = this.load();
68 iframe.contentWindow.postMessage(
69 d,
70 '*'
71 ); // TODO: Fix origin
72 },
73
Akron22598cd2019-12-09 14:59:03 +010074 // onClose : function () {},
75
76 /**
77 * Close the service iframe.
78 */
79 _closeIframe : function () {
80 var e = this._load;
81 if (e && e.parentNode) {
82 e.parentNode.removeChild(e);
83 };
84 this._load = null;
85 }
86 };
87});