blob: 6918da44a73e7f4909f1954a378c7fbdfe149d49 [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*/
21
22var cs = document.currentScript;
23
Akron479994e2018-07-02 13:21:44 +020024(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 () {
Akrona6c32b92018-07-02 18:39:42 +020040 this.widgetID = window.name;
41 this.server = cs.getAttribute('data-server') || '*';
Akron479994e2018-07-02 13:21:44 +020042 this.resize();
43 return this;
44 },
45
46 // Send a message
47 _sendMsg : function (data) {
Akrona6c32b92018-07-02 18:39:42 +020048 data["originID"] = this.widgetID;
Akron479994e2018-07-02 13:21:44 +020049
50 // TODO: This should send a correct origin
Akrona6c32b92018-07-02 18:39:42 +020051 window.parent.postMessage(data, this.server);
Akron479994e2018-07-02 13:21:44 +020052 },
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
Akron479994e2018-07-02 13:21:44 +020072 // 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