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