blob: 8c73625bdfcbefedb0ebfd9546f8a57f01eb74b4 [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') || '*';
Akron22598cd2019-12-09 14:59:03 +010047
48 // Establish the 'message' hook.
49 this._listener = this._receiveMsg.bind(this);
50 window.addEventListener("message", this._listener);
Akron479994e2018-07-02 13:21:44 +020051 return this;
52 },
53
54 // Send a message
55 _sendMsg : function (data) {
Akrona6c32b92018-07-02 18:39:42 +020056 data["originID"] = this.widgetID;
Akrona6c32b92018-07-02 18:39:42 +020057 window.parent.postMessage(data, this.server);
Akron479994e2018-07-02 13:21:44 +020058 },
59
Akron22598cd2019-12-09 14:59:03 +010060 // Receive a call from the embedded platform.
61 _receiveMsg : function (e) {
62 // Get event data
63 let d = e.data;
64
65 // If no data given - fail
66 // (probably check that it's an assoc array)
67 if (!d)
68 return;
69
70 // TODO:
71 // check e.origin and d["originID"]!!!
72 // probably against window.parent!
73
74 if (this.onMessage)
75 this.onMessage(d)
76 },
77
Akron479994e2018-07-02 13:21:44 +020078 /**
79 * Send a log message to the embedding KorAP
80 */
81 log : function (code, msg) {
82 this._sendMsg({
83 action : 'log',
84 code : code,
85 msg : msg
86 });
87 },
88
89 /**
90 * Send a resize command to the
91 * embedding KorAP
92 */
93 resize : function () {
Akron617d6a22018-07-09 16:04:19 +020094 var de = document.documentElement;
95 var height = de.scrollHeight;
96
97 // Add assumed scrollbar height
98 if (de.scrollWidth > de.clientWidth) {
99 height += 14;
100 };
Akron479994e2018-07-02 13:21:44 +0200101 this._sendMsg({
102 'action' : 'resize',
Akron617d6a22018-07-09 16:04:19 +0200103 'height' : height
Akron479994e2018-07-02 13:21:44 +0200104 });
105 }
106 };
107
108 // Create plugin on windows load
109 window.onload = function () {
110 window.KorAPlugin = window.KorAPlugin || obj.create();
Akron22598cd2019-12-09 14:59:03 +0100111
112 // TODO:
113 // Only do this in case of the client being opened
114 // as a widget!
Akrone8e2c952018-07-04 13:43:12 +0200115 window.KorAPlugin.resize();
Akron22598cd2019-12-09 14:59:03 +0100116
117 if (window.pluginit)
118 window.pluginit(window.KorAPlugin);
Akron479994e2018-07-02 13:21:44 +0200119 };
Akron22598cd2019-12-09 14:59:03 +0100120
Akron479994e2018-07-02 13:21:44 +0200121})();
122