-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGlueFrame.js
171 lines (153 loc) · 6.07 KB
/
GlueFrame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var GlueFrame = function(iframe, appName) {
var $this = this;
// GlueFrame version
$this.glueframe = "1.1.3";
// Allow posting messages only to the domain of the app
var _domain = (""+iframe.src).split("/").slice(0,3).join("/");
// Determine method of communication with iframe
var _method = (function() {
if (_domain == (""+window.location).split("/").slice(0,3).join("/") ) {
return "object";
} else if (typeof window.postMessage !== "undefined") {
return "post";
} else {
return "none";
}
})();
// Poll the iframe until the app is bootstrapped
$this.ready = false;
var _readyInterval = window.setInterval(function(){
if (!$this.ready && _method === "object") {
if (iframe.contentWindow[appName] && iframe.contentWindow[appName].bootstrapped) {
$this.ready = true;
window.clearInterval(_readyInterval);
_processQueue();
}
} else if (!$this.ready && _method === "post") {
$this.get("bootstrapped", function(bootstrapped){
if (bootstrapped) {
$this.ready = true;
window.clearInterval(_readyInterval);
_processQueue();
}
}, true);
}
}, 100);
$this.glueFrameId = Math.floor((new Date()).getTime()*Math.random());
var _callbackCount = 0;
var _callbacks = {};
// Store callback functions in the parent window
var _registerCallback = function(callback, requireCallback) {
var callbackIdentifier = $this.glueFrameId + "_" + (++_callbackCount);
if (typeof callback === "function") {
_callbacks[callbackIdentifier] = callback;
} else if (requireCallback) {
throw "GlueFrame: Callback not registered correctly.";
}
return callbackIdentifier;
};
// Queue up method calls until app is ready
var _queue = [];
var _addToQueue = function(method, args) {
_queue.push({method: method, args: args});
};
// Loop through queue when app is ready
var _processQueue = function() {
for (var i = 0; i < _queue.length; i += 1) {
var queueItem = _queue[i];
queueItem.method.apply(null, queueItem.args);
}
_queue = [];
$this.set("queuedEventsProcessed", true);
};
$this.get = function(prop, callback, force) {
if (!$this.ready && !force) {
_addToQueue($this.get, [prop, callback]);
return;
}
var cbId = _registerCallback(callback, true);
if (_method === "object") {
var value = iframe.contentWindow[appName].get.apply(null, [prop]);
if (typeof _callbacks[cbId] !== "undefined") {
_callbacks[cbId].apply(null, [value]);
}
} else if (_method === "post") {
var messageObject = {f: "get", args: [prop], cbId: cbId};
iframe.contentWindow.postMessage( JSON.stringify(messageObject), force ? "*" : _domain );
}
};
$this.set = function(prop, val, callback) {
if (!$this.ready) {
_addToQueue($this.set, [prop, val, callback]);
return;
}
var cbId = _registerCallback(callback, false);
if (_method === "object") {
var value = iframe.contentWindow[appName].set.apply(null, [prop, val]);
if (typeof _callbacks[cbId] !== "undefined") {
_callbacks[cbId].apply(null, [value]);
}
} else if (_method === "post") {
var messageObject = {f: "set", args: [prop, val], cbId: cbId};
iframe.contentWindow.postMessage( JSON.stringify(messageObject), _domain );
}
};
$this.bind = function(event, callback, triggerQueue) {
var triggerQueue = triggerQueue || false;
if (!$this.ready) {
_addToQueue($this.bind, [event, callback, true]);
return;
}
var cbId = _registerCallback(callback, true);
if (_method === "object") {
iframe.contentWindow[appName].bind.apply(null, [event, callback, triggerQueue]);
} else if (_method === "post") {
var messageObject = {f: "bind", args: [event], cbId: cbId, triggerQueue: triggerQueue};
iframe.contentWindow.postMessage( JSON.stringify(messageObject), _domain );
}
};
$this.fire = function(event, obj) {
if (!$this.ready) {
_addToQueue($this.fire, [event, obj]);
return;
}
if (_method === "object") {
return iframe.contentWindow[appName].fire.apply(null, [event, obj]);
} else if (_method === "post") {
var messageObject = {f: "fire", args: [event, obj]};
iframe.contentWindow.postMessage( JSON.stringify(messageObject), _domain );
}
};
// Remove event listeners, callbacks and intervals
$this.destroy = function(){
if (window.addEventListener) {
window.removeEventListener("message", _receiveMessage, false);
} else {
window.detachEvent("onmessage", _receiveMessage);
}
window.clearInterval(_readyInterval);
_callbacks = {};
};
// Parse messages received from iframe
var _receiveMessage = function(e) {
if (e.origin === _domain) {
var data;
try {
data = JSON.parse(e.data);
}catch(e){}
if (typeof data !== "undefined" && typeof data.cbId !== "undefined" && typeof _callbacks[data.cbId] === "function") {
_callbacks[data.cbId].apply(null, [data.a, data.b]);
}
}
};
// Listen for message events if need
if (window.addEventListener) {
window.addEventListener("message", _receiveMessage, false);
} else {
window.attachEvent("onmessage", _receiveMessage);
}
};
if(typeof(window.onGlueFrameAvailable)=='function') window.onGlueFrameAvailable(GlueFrame);
if (typeof module !== 'undefined' && module.exports) {
module.exports = GlueFrame;
}