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