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); |
hebasta | 7891324 | 2020-03-30 13:39:20 +0200 | [diff] [blame] | 38 | // Allow forms in Plugins |
| 39 | e.setAttribute('sandbox','allow-scripts allow-forms'); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 40 | e.style.height = '0px'; |
| 41 | e.setAttribute('name', this.id); |
| 42 | e.setAttribute('src', this.src); |
| 43 | |
| 44 | this._load = e; |
| 45 | return e; |
| 46 | }, |
| 47 | |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 48 | /** |
| 49 | * Send a message to the embedded service. |
| 50 | */ |
| 51 | sendMsg : function (d) { |
| 52 | let iframe = this.load(); |
| 53 | iframe.contentWindow.postMessage( |
| 54 | d, |
| 55 | '*' |
| 56 | ); // TODO: Fix origin |
| 57 | }, |
| 58 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 59 | // onClose : function () {}, |
| 60 | |
| 61 | /** |
| 62 | * Close the service iframe. |
| 63 | */ |
| 64 | _closeIframe : function () { |
| 65 | var e = this._load; |
| 66 | if (e && e.parentNode) { |
| 67 | e.parentNode.removeChild(e); |
| 68 | }; |
| 69 | this._load = null; |
| 70 | } |
| 71 | }; |
| 72 | }); |