blob: c8b42dd5186735835414ae7bb021bdc86f41cd8c [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;
33
34 // Spawn new iframe
35 let e = document.createElement('iframe');
36 e.setAttribute('allowTransparency',"true");
37 e.setAttribute('frameborder', 0);
38 e.setAttribute('sandbox','allow-scripts');
39 e.style.height = '0px';
40 e.setAttribute('name', this.id);
41 e.setAttribute('src', this.src);
42
43 this._load = e;
44 return e;
45 },
46
Akronc3003642020-03-30 10:19:14 +020047 /**
48 * Send a message to the embedded service.
49 */
50 sendMsg : function (d) {
51 let iframe = this.load();
52 iframe.contentWindow.postMessage(
53 d,
54 '*'
55 ); // TODO: Fix origin
56 },
57
Akron22598cd2019-12-09 14:59:03 +010058 // onClose : function () {},
59
60 /**
61 * Close the service iframe.
62 */
63 _closeIframe : function () {
64 var e = this._load;
65 if (e && e.parentNode) {
66 e.parentNode.removeChild(e);
67 };
68 this._load = null;
69 }
70 };
71});