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 | |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 32 | // Similar to randomID in server, but a bit cheaper |
| 33 | function randomID () { |
| 34 | let s = ''; |
| 35 | for (let i = 0; i < 16; i++) { |
| 36 | s += Math.floor((Math.random()*16)%16).toString(16); |
| 37 | }; |
| 38 | return '_' + s; |
| 39 | }; |
| 40 | |
| 41 | let response = {}; |
| 42 | |
| 43 | let obj = { |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Create new plugin |
| 47 | */ |
| 48 | create : function () { |
| 49 | return Object.create(this)._init(); |
| 50 | }, |
| 51 | |
| 52 | /* |
| 53 | * Initialize plugin |
| 54 | */ |
| 55 | _init : function () { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 56 | this.widgetID = window.name; |
| 57 | this.server = cs.getAttribute('data-server') || '*'; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 58 | |
| 59 | // Establish the 'message' hook. |
| 60 | this._listener = this._receiveMsg.bind(this); |
| 61 | window.addEventListener("message", this._listener); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 62 | return this; |
| 63 | }, |
| 64 | |
| 65 | // Send a message |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 66 | sendMsg : function (data) { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 67 | data["originID"] = this.widgetID; |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 68 | window.parent.postMessage(data, this.server); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 69 | }, |
| 70 | |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 71 | // Request data |
| 72 | requestMsg : function (data, cb) { |
| 73 | let id = randomID(); |
| 74 | data["_id"] = id; |
| 75 | response[id] = cb; |
| 76 | this.sendMsg(data); |
| 77 | }, |
| 78 | |
| 79 | // Receive a call from the embedding platform. |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 80 | _receiveMsg : function (e) { |
| 81 | // Get event data |
| 82 | let d = e.data; |
| 83 | |
| 84 | // If no data given - fail |
| 85 | // (probably check that it's an assoc array) |
| 86 | if (!d) |
| 87 | return; |
| 88 | |
| 89 | // TODO: |
| 90 | // check e.origin and d["originID"]!!! |
| 91 | // probably against window.parent! |
| 92 | |
Akron | c300364 | 2020-03-30 10:19:14 +0200 | [diff] [blame] | 93 | // There is an associated callback registered: |
| 94 | // call and remove the function |
| 95 | if (d["_id"]) { |
| 96 | let id = d["_id"]; |
| 97 | if (response[id]) { |
| 98 | response[id](d); |
| 99 | delete response[id]; |
| 100 | }; |
| 101 | return; |
| 102 | }; |
| 103 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 104 | if (this.onMessage) |
| 105 | this.onMessage(d) |
| 106 | }, |
| 107 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 108 | /** |
| 109 | * Send a log message to the embedding KorAP |
| 110 | */ |
| 111 | log : function (code, msg) { |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 112 | this.sendMsg({ |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 113 | action : 'log', |
| 114 | code : code, |
| 115 | msg : msg |
| 116 | }); |
| 117 | }, |
| 118 | |
| 119 | /** |
| 120 | * Send a resize command to the |
| 121 | * embedding KorAP |
| 122 | */ |
| 123 | resize : function () { |
Akron | 617d6a2 | 2018-07-09 16:04:19 +0200 | [diff] [blame] | 124 | var de = document.documentElement; |
| 125 | var height = de.scrollHeight; |
| 126 | |
| 127 | // Add assumed scrollbar height |
| 128 | if (de.scrollWidth > de.clientWidth) { |
| 129 | height += 14; |
| 130 | }; |
Akron | 51ee623 | 2019-12-17 21:00:05 +0100 | [diff] [blame] | 131 | this.sendMsg({ |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 132 | 'action' : 'resize', |
Akron | 617d6a2 | 2018-07-09 16:04:19 +0200 | [diff] [blame] | 133 | 'height' : height |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 134 | }); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | // Create plugin on windows load |
| 139 | window.onload = function () { |
| 140 | window.KorAPlugin = window.KorAPlugin || obj.create(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 141 | |
| 142 | // TODO: |
| 143 | // Only do this in case of the client being opened |
| 144 | // as a widget! |
Akron | e8e2c95 | 2018-07-04 13:43:12 +0200 | [diff] [blame] | 145 | window.KorAPlugin.resize(); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 146 | |
| 147 | if (window.pluginit) |
| 148 | window.pluginit(window.KorAPlugin); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 149 | }; |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 150 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 151 | })(); |
| 152 | |