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 | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 47 | this.resize(); |
| 48 | return this; |
| 49 | }, |
| 50 | |
| 51 | // Send a message |
| 52 | _sendMsg : function (data) { |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 53 | data["originID"] = this.widgetID; |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 54 | |
| 55 | // TODO: This should send a correct origin |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 56 | window.parent.postMessage(data, this.server); |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 57 | }, |
| 58 | |
| 59 | /** |
| 60 | * Send a log message to the embedding KorAP |
| 61 | */ |
| 62 | log : function (code, msg) { |
| 63 | this._sendMsg({ |
| 64 | action : 'log', |
| 65 | code : code, |
| 66 | msg : msg |
| 67 | }); |
| 68 | }, |
| 69 | |
| 70 | /** |
| 71 | * Send a resize command to the |
| 72 | * embedding KorAP |
| 73 | */ |
| 74 | resize : function () { |
| 75 | var body = document.body; |
| 76 | |
Akron | 479994e | 2018-07-02 13:21:44 +0200 | [diff] [blame] | 77 | // recognize margin of first element |
| 78 | // (don't know why in FF) |
| 79 | var cs = getComputedStyle(body.children[0]); |
| 80 | |
| 81 | var offsetHeight = parseInt(body.offsetHeight) + |
| 82 | parseInt(cs.getPropertyValue("margin-top")) + |
| 83 | parseInt(cs.getPropertyValue("margin-bottom")); |
| 84 | |
| 85 | this._sendMsg({ |
| 86 | 'action' : 'resize', |
| 87 | 'height' : offsetHeight |
| 88 | }); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | // Create plugin on windows load |
| 93 | window.onload = function () { |
| 94 | window.KorAPlugin = window.KorAPlugin || obj.create(); |
| 95 | }; |
| 96 | })(); |
| 97 | |
| 98 | |