blob: 2f34c4e2eda039686284600f85b46bfe5c6dc7c9 [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
Akronc3003642020-03-30 10:19:14 +020032 // 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 = {
Akron479994e2018-07-02 13:21:44 +020044
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 () {
Akrona6c32b92018-07-02 18:39:42 +020056 this.widgetID = window.name;
57 this.server = cs.getAttribute('data-server') || '*';
Akron22598cd2019-12-09 14:59:03 +010058
59 // Establish the 'message' hook.
60 this._listener = this._receiveMsg.bind(this);
61 window.addEventListener("message", this._listener);
Akron479994e2018-07-02 13:21:44 +020062 return this;
63 },
64
65 // Send a message
Akron51ee6232019-12-17 21:00:05 +010066 sendMsg : function (data) {
Akrona6c32b92018-07-02 18:39:42 +020067 data["originID"] = this.widgetID;
Akrona6c32b92018-07-02 18:39:42 +020068 window.parent.postMessage(data, this.server);
Akron479994e2018-07-02 13:21:44 +020069 },
70
Akronc3003642020-03-30 10:19:14 +020071 // 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.
Akron22598cd2019-12-09 14:59:03 +010080 _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
Akronc3003642020-03-30 10:19:14 +020093 // 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
Akron22598cd2019-12-09 14:59:03 +0100104 if (this.onMessage)
105 this.onMessage(d)
106 },
107
Akron479994e2018-07-02 13:21:44 +0200108 /**
109 * Send a log message to the embedding KorAP
110 */
111 log : function (code, msg) {
Akron51ee6232019-12-17 21:00:05 +0100112 this.sendMsg({
Akron479994e2018-07-02 13:21:44 +0200113 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 () {
Akron617d6a22018-07-09 16:04:19 +0200124 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 };
Akron51ee6232019-12-17 21:00:05 +0100131 this.sendMsg({
Akron479994e2018-07-02 13:21:44 +0200132 'action' : 'resize',
Akron617d6a22018-07-09 16:04:19 +0200133 'height' : height
Akron479994e2018-07-02 13:21:44 +0200134 });
135 }
136 };
137
138 // Create plugin on windows load
139 window.onload = function () {
140 window.KorAPlugin = window.KorAPlugin || obj.create();
Akron22598cd2019-12-09 14:59:03 +0100141
142 // TODO:
143 // Only do this in case of the client being opened
144 // as a widget!
Akrone8e2c952018-07-04 13:43:12 +0200145 window.KorAPlugin.resize();
Akron22598cd2019-12-09 14:59:03 +0100146
147 if (window.pluginit)
148 window.pluginit(window.KorAPlugin);
Akron479994e2018-07-02 13:21:44 +0200149 };
Akron22598cd2019-12-09 14:59:03 +0100150
Akron479994e2018-07-02 13:21:44 +0200151})();
152