blob: 4e6ec3c0aec37fcd8140ae4cd753ba798bec24f3 [file] [log] [blame]
Akron479994e2018-07-02 13:21:44 +02001/**
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
Akrona6c32b92018-07-02 18:39:42 +020011/*
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*/
Akrona99315e2018-07-03 22:56:45 +020021/*
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 */
Akrona6c32b92018-07-02 18:39:42 +020026
27var cs = document.currentScript;
28
Akron479994e2018-07-02 13:21:44 +020029(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 () {
Akrona6c32b92018-07-02 18:39:42 +020045 this.widgetID = window.name;
46 this.server = cs.getAttribute('data-server') || '*';
Akron479994e2018-07-02 13:21:44 +020047 return this;
48 },
49
50 // Send a message
51 _sendMsg : function (data) {
Akrona6c32b92018-07-02 18:39:42 +020052 data["originID"] = this.widgetID;
Akrona6c32b92018-07-02 18:39:42 +020053 window.parent.postMessage(data, this.server);
Akron479994e2018-07-02 13:21:44 +020054 },
55
56 /**
57 * Send a log message to the embedding KorAP
58 */
59 log : function (code, msg) {
60 this._sendMsg({
61 action : 'log',
62 code : code,
63 msg : msg
64 });
65 },
66
67 /**
68 * Send a resize command to the
69 * embedding KorAP
70 */
71 resize : function () {
Akron617d6a22018-07-09 16:04:19 +020072 var de = document.documentElement;
73 var height = de.scrollHeight;
74
75 // Add assumed scrollbar height
76 if (de.scrollWidth > de.clientWidth) {
77 height += 14;
78 };
Akron479994e2018-07-02 13:21:44 +020079 this._sendMsg({
80 'action' : 'resize',
Akron617d6a22018-07-09 16:04:19 +020081 'height' : height
Akron479994e2018-07-02 13:21:44 +020082 });
83 }
84 };
85
86 // Create plugin on windows load
87 window.onload = function () {
88 window.KorAPlugin = window.KorAPlugin || obj.create();
Akrone8e2c952018-07-04 13:43:12 +020089 window.KorAPlugin.resize();
Akron479994e2018-07-02 13:21:44 +020090 };
91})();
92