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; |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame^] | 16 | this._perm = new Set(); |
| 17 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 18 | // There is no close method defined yet |
| 19 | if (!this.close) { |
| 20 | this.close = function () { |
| 21 | this._closeIframe(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return this; |
| 26 | }, |
| 27 | |
| 28 | /** |
| 29 | * The element of the service as embedded in the panel |
| 30 | */ |
| 31 | load : function () { |
| 32 | if (this._load) |
| 33 | return this._load; |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 34 | |
| 35 | if (window.location.protocol == 'https:' && |
| 36 | this.src.toLowerCase().indexOf('https:') != 0) { |
| 37 | KorAP.log(0, "Service endpoint is insecure"); |
| 38 | return; |
| 39 | }; |
| 40 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 41 | // Spawn new iframe |
| 42 | let e = document.createElement('iframe'); |
| 43 | e.setAttribute('allowTransparency',"true"); |
| 44 | e.setAttribute('frameborder', 0); |
hebasta | 7891324 | 2020-03-30 13:39:20 +0200 | [diff] [blame] | 45 | // Allow forms in Plugins |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame^] | 46 | e.setAttribute('sandbox', this._permString()); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 47 | e.style.height = '0px'; |
| 48 | e.setAttribute('name', this.id); |
| 49 | e.setAttribute('src', this.src); |
| 50 | |
| 51 | this._load = e; |
| 52 | return e; |
| 53 | }, |
| 54 | |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame^] | 55 | allow : function (permission) { |
| 56 | if (Array.isArray(permission)) { |
| 57 | permission.forEach( |
| 58 | p => this._perm.add(p) |
| 59 | ); |
| 60 | } |
| 61 | else { |
| 62 | this._perm.add(permission); |
| 63 | }; |
| 64 | |
| 65 | if (this._load) { |
| 66 | this._load.setAttribute('sandbox', this._permString()); |
| 67 | } |
| 68 | }, |
| 69 | |
| 70 | _permString : function () { |
| 71 | return Array.from(this._perm).sort().join(" "); |
| 72 | }, |
| 73 | |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 74 | /** |
| 75 | * Send a message to the embedded service. |
| 76 | */ |
| 77 | sendMsg : function (d) { |
| 78 | let iframe = this.load(); |
| 79 | iframe.contentWindow.postMessage( |
| 80 | d, |
| 81 | '*' |
| 82 | ); // TODO: Fix origin |
| 83 | }, |
| 84 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 85 | // onClose : function () {}, |
| 86 | |
| 87 | /** |
| 88 | * Close the service iframe. |
| 89 | */ |
| 90 | _closeIframe : function () { |
| 91 | var e = this._load; |
| 92 | if (e && e.parentNode) { |
| 93 | e.parentNode.removeChild(e); |
| 94 | }; |
| 95 | this._load = null; |
| 96 | } |
| 97 | }; |
| 98 | }); |