blob: 3152f3828c0db0f3d01d01c2dd50842f6b006447 [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;
16
17 // There is no close method defined yet
18 if (!this.close) {
19 this.close = function () {
20 this._closeIframe();
21 }
22 }
23
24 return this;
25 },
26
27 /**
28 * The element of the service as embedded in the panel
29 */
30 load : function () {
31 if (this._load)
32 return this._load;
Akron24f48ea2020-07-01 09:37:19 +020033
34 if (window.location.protocol == 'https:' &&
35 this.src.toLowerCase().indexOf('https:') != 0) {
36 KorAP.log(0, "Service endpoint is insecure");
37 return;
38 };
39
Akron22598cd2019-12-09 14:59:03 +010040 // Spawn new iframe
41 let e = document.createElement('iframe');
42 e.setAttribute('allowTransparency',"true");
43 e.setAttribute('frameborder', 0);
hebasta78913242020-03-30 13:39:20 +020044 // Allow forms in Plugins
45 e.setAttribute('sandbox','allow-scripts allow-forms');
Akron22598cd2019-12-09 14:59:03 +010046 e.style.height = '0px';
47 e.setAttribute('name', this.id);
48 e.setAttribute('src', this.src);
49
50 this._load = e;
51 return e;
52 },
53
Akronc3003642020-03-30 10:19:14 +020054 /**
55 * Send a message to the embedded service.
56 */
57 sendMsg : function (d) {
58 let iframe = this.load();
59 iframe.contentWindow.postMessage(
60 d,
61 '*'
62 ); // TODO: Fix origin
63 },
64
Akron22598cd2019-12-09 14:59:03 +010065 // onClose : function () {},
66
67 /**
68 * Close the service iframe.
69 */
70 _closeIframe : function () {
71 var e = this._load;
72 if (e && e.parentNode) {
73 e.parentNode.removeChild(e);
74 };
75 this._load = null;
76 }
77 };
78});