Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 1 | /** |
| 2 | * The plugin system is based |
| 3 | * on registered widgets (iframes) from foreign services. |
| 4 | * The client component is loaded independently |
| 5 | * in a plugin and communicates with the embedding |
| 6 | * KorAP service. |
| 7 | * |
| 8 | * @author Nils Diewald |
| 9 | */ |
| 10 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 11 | /* |
| 12 | * TODO: |
| 13 | * Some methods require bidirectional |
| 14 | * calling, like |
| 15 | * - getKoralQuery() |
| 16 | * this probably should be done using a callback, |
| 17 | * like fetch({data}, function () {}, '*'), that will |
| 18 | * add a unique ID to the message and will call on the cb |
| 19 | * once the answer to that message arrives. |
| 20 | */ |
Akron | a99315e | 2018-07-03 22:56:45 +0200 | [diff] [blame] | 21 | /* |
| 22 | * When loading the script from a remote KorAP instance, |
| 23 | * demand using integrity check: |
| 24 | * https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity |
| 25 | */ |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 26 | |
| 27 | var cs = document.currentScript; |
| 28 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 29 | (function () { |
| 30 | "use strict"; |
| 31 | |
| 32 | var obj = { |
| 33 | |
| 34 | /** |
| 35 | * Create new plugin |
| 36 | */ |
| 37 | create : function () { |
| 38 | return Object.create(this)._init(); |
| 39 | }, |
| 40 | |
| 41 | /* |
| 42 | * Initialize plugin |
| 43 | */ |
| 44 | _init : function () { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 45 | this.widgetID = window.name; |
| 46 | this.server = cs.getAttribute('data-server') || '*'; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 47 | |
| 48 | // Establish the 'message' hook. |
| 49 | this._listener = this._receiveMsg.bind(this); |
| 50 | window.addEventListener("message", this._listener); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 51 | return this; |
| 52 | }, |
| 53 | |
| 54 | // Send a message |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 55 | sendMsg : function (data) { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 56 | data["originID"] = this.widgetID; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 57 | window.parent.postMessage(data, this.server); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 58 | }, |
| 59 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 60 | // Receive a call from the embedded platform. |
| 61 | _receiveMsg : function (e) { |
| 62 | // Get event data |
| 63 | let d = e.data; |
| 64 | |
| 65 | // If no data given - fail |
| 66 | // (probably check that it's an assoc array) |
| 67 | if (!d) |
| 68 | return; |
| 69 | |
| 70 | // TODO: |
| 71 | // check e.origin and d["originID"]!!! |
| 72 | // probably against window.parent! |
| 73 | |
| 74 | if (this.onMessage) |
| 75 | this.onMessage(d) |
| 76 | }, |
| 77 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 78 | /** |
| 79 | * Send a log message to the embedding KorAP |
| 80 | */ |
| 81 | log : function (code, msg) { |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 82 | this.sendMsg({ |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 83 | action : 'log', |
| 84 | code : code, |
| 85 | msg : msg |
| 86 | }); |
| 87 | }, |
| 88 | |
| 89 | /** |
| 90 | * Send a resize command to the |
| 91 | * embedding KorAP |
| 92 | */ |
| 93 | resize : function () { |
Akron | 617d6a2 | 2018-07-09 16:04:19 +0200 | [diff] [blame] | 94 | var de = document.documentElement; |
| 95 | var height = de.scrollHeight; |
| 96 | |
| 97 | // Add assumed scrollbar height |
| 98 | if (de.scrollWidth > de.clientWidth) { |
| 99 | height += 14; |
| 100 | }; |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 101 | this.sendMsg({ |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 102 | 'action' : 'resize', |
Akron | 617d6a2 | 2018-07-09 16:04:19 +0200 | [diff] [blame] | 103 | 'height' : height |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 104 | }); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | // Create plugin on windows load |
| 109 | window.onload = function () { |
| 110 | window.KorAPlugin = window.KorAPlugin || obj.create(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 111 | |
| 112 | // TODO: |
| 113 | // Only do this in case of the client being opened |
| 114 | // as a widget! |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 115 | window.KorAPlugin.resize(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 116 | |
| 117 | if (window.pluginit) |
| 118 | window.pluginit(window.KorAPlugin); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 119 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 120 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 121 | })(); |
| 122 | |