Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 1 | define(function () { |
| 2 | "use strict"; |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 3 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 4 | 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 | |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 47 | /** |
| 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 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 58 | // 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 | }); |