messagecenter.js
10.3 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
if (typeof console == 'undefined') console = {
log: function () { }
};
// sniff chrome
var CHROME_5_LOCAL = false;
var CHROME = false;
var SAFARI = false;
var FIREFOX = false;
var WEBKIT = false;
var OS_MAC = false;
var IOS = false;
var ANDROID = false;
var MOBILE_DEVICE = false;
var IE = false;
var IE_10_AND_BELOW = false; //ie 10 and lower
var IE_11_AND_ABOVE = false; //ie 11 and above
var BROWSER_VERSION = 5000;
(function () {
if(!window.$axure) window.$axure = function() {};
var useragent = window.navigator.userAgent;
var edgeRegex = /Edge\/([0-9]+)/g;
var edgeMatch = edgeRegex.exec(useragent);
$axure.browser = { isEdge: Boolean(edgeMatch) };
if(!$axure.browser.isEdge) {
var chromeRegex = /Chrome\/([0-9]+).([0-9]+)/g;
var chromeMatch = chromeRegex.exec(useragent);
CHROME = Boolean(chromeMatch);
CHROME_5_LOCAL = chromeMatch &&
Number(chromeMatch[1]) >= 5 &&
location.href.indexOf('file://') >= 0;
}
var safariRegex = /Safari\/([0-9]+)/g;
var safariMatch = safariRegex.exec(useragent);
SAFARI = Boolean(safariMatch) && !CHROME; //because chrome also inserts safari string into user agent
var webkitRegex = /WebKit\//g ;
WEBKIT = Boolean(webkitRegex.exec(useragent));
FIREFOX = useragent.toLowerCase().indexOf('firefox') > -1;
var macRegex = /Mac/g ;
OS_MAC = Boolean(macRegex.exec(window.navigator.platform));
IOS = useragent.match(/iPhone/i) || useragent.match(/iPad/i) || useragent.match(/iPod/i);
ANDROID = useragent.match(/Android/i);
MOBILE_DEVICE = ANDROID || IOS
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Tablet PC/i)
|| navigator.userAgent.match(/Windows Phone/i);
if($.browser) {
if($.browser.msie) IE_10_AND_BELOW = true;
else IE_11_AND_ABOVE = useragent.toLowerCase().indexOf('trident') > -1;
BROWSER_VERSION = $.browser.version;
}
IE = IE_10_AND_BELOW || IE_11_AND_ABOVE;
//Used by sitemap and variables.js getLinkUrl functions so that they know
//whether to embed global variables in URL as query string or hash string
//_shouldSendVars persists the value for sitemap instead of re-checking every time
var _shouldSendVars;
var _shouldSendVarsToServer = function(url) {
if(typeof _shouldSendVars != 'undefined') {
return _shouldSendVars;
}
if(SAFARI || (IE_10_AND_BELOW && BROWSER_VERSION < 10)) {
var urlToCheck = typeof url != 'undefined' ? url : window.location.href;
var serverRegex = /http:\/\/127\.0\.0\.1:[0-9]{5}/g;
var serverMatch = serverRegex.exec(urlToCheck);
var previewRegex = /[0-9]{2}\.[0-9]{2}\.[0-9]{2}/g;
var previewMatch = previewRegex.exec(urlToCheck);
if(Boolean(serverMatch) && Boolean(previewMatch)) {
_shouldSendVars = true;
return _shouldSendVars;
}
}
_shouldSendVars = false;
return _shouldSendVars;
};
$axure.shouldSendVarsToServer = _shouldSendVarsToServer;
})();
(function() {
var _topMessageCenter;
var _messageCenter = {};
var _listeners = [];
var _stateListeners = [];
var _state = {};
var _eventObject = null;
var _queuedMessages = [];
var _initialized = false;
// this is for the non Chrome 5 local scenarios. The "top" message center will dispatch to all the bottom ones
var _childrenMessageCenters = [];
// create $axure if it hasn't been created
if (!window.$axure) window.$axure = function() {};
$axure.messageCenter = _messageCenter;
// isolate scope, and initialize _topMessageCenter.
(function() {
if (!CHROME_5_LOCAL) {
var topAxureWindow = window;
try {
while(topAxureWindow.parent && topAxureWindow.parent !== topAxureWindow
&& topAxureWindow.parent.$axure) topAxureWindow = topAxureWindow.parent;
} catch(e) {}
_topMessageCenter = topAxureWindow.$axure.messageCenter;
}
})();
$(window.document).ready(function() {
if (CHROME_5_LOCAL) {
$('body').append("<div id='axureEventReceiverDiv' style='display:none'></div>" +
"<div id='axureEventSenderDiv' style='display:none'></div>");
_eventObject = window.document.createEvent('Event');
_eventObject.initEvent('axureMessageSenderEvent', true, true);
$('#axureEventReceiverDiv').bind('axureMessageReceiverEvent', function () {
var request = JSON.parse($(this).text());
_handleRequest(request);
});
} else {
if (_topMessageCenter != _messageCenter) {
_topMessageCenter.addChildMessageCenter(_messageCenter);
console.log('adding from ' + window.location.toString());
}
}
});
var _handleRequest = function (request) {
// route the request to all the listeners
for(var i = 0; i < _listeners.length; i++) _listeners[i](request.message, request.data);
// now handle the queued messages if we're initializing
if (request.message == 'initialize') {
_initialized = true;
// send all the queued messages and return
for (var i = 0; i < _queuedMessages.length; i++) {
var qRequest = _queuedMessages[i];
_messageCenter.postMessage(qRequest.message, qRequest.data);
}
_queuedMessages = [];
}
// and then handle the set state messages, if necessary
if (request.message == 'setState') {
_state[request.data.key] = request.data.value;
for (var i = 0; i < _stateListeners.length; i++) {
var keyListener = _stateListeners[i];
// if thep passed a null or empty value, always post the message
if (!keyListener.key || keyListener.key == request.data.key) {
keyListener.listener(request.data.key, request.data.value);
}
}
}
};
// -----------------------------------------------------------------------------------------
// This method allows for dispatching messages in the non-chromelocal scenario.
// Each child calls this on _topMessageCenter
// -----------------------------------------------------------------------------------------
_messageCenter.addChildMessageCenter = function(messageCenter) {
_childrenMessageCenters[_childrenMessageCenters.length] = messageCenter;
};
// -----------------------------------------------------------------------------------------
// This method allows for dispatching messages in the non-chromelocal scenario.
// Each child calls this on _topMessageCenter
// -----------------------------------------------------------------------------------------
_messageCenter.dispatchMessage = function(message, data) {
_handleRequest({
message: message,
data: data
});
};
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
_messageCenter.dispatchMessageRecursively = function(message, data) {
console.log("dispatched to " + window.location.toString());
// dispatch to the top center first
_messageCenter.dispatchMessage(message, data);
$('iframe').each(function(index, frame) {
//try,catch to handle permissions error in FF when loading pages from another domain
try {
if (frame.contentWindow.$axure && frame.contentWindow.$axure.messageCenter) {
frame.contentWindow.$axure.messageCenter.dispatchMessageRecursively(message, data);
}
}catch(e) {}
});
};
var _combineEventMessages = false;
var _compositeEventMessageData = [];
_messageCenter.startCombineEventMessages = function() {
_combineEventMessages = true;
}
_messageCenter.endCombineEventMessages = function () {
_messageCenter.sendCompositeEventMessage();
_combineEventMessages = false;
}
_messageCenter.sendCompositeEventMessage = function () {
_messageCenter.postMessage('axCompositeEventMessage', _compositeEventMessageData);
_compositeEventMessageData = [];
}
_messageCenter.postMessage = function (message, data) {
if(_combineEventMessages) {
if(message == 'axEvent' || message == 'axCase' || message == 'axAction' || message == 'axEventComplete') {
_compositeEventMessageData.push({ 'message': message, 'data': data });
if(_compositeEventMessageData.length >= 10) _messageCenter.sendCompositeEventMessage();
return;
}
}
if(!CHROME_5_LOCAL) {
_topMessageCenter.dispatchMessageRecursively(message, data);
} else {
var request = {
message: message,
data: data
};
if(_initialized) {
var senderDiv = window.document.getElementById('axureEventSenderDiv');
var messageText = JSON.stringify(request);
// console.log('sending event: ' + messageText);
senderDiv.innerText = messageText;
senderDiv.dispatchEvent(_eventObject);
// console.log('event sent');
} else {
_queuedMessages[_queuedMessages.length] = request;
}
}
};
_messageCenter.setState = function(key, value) {
var data = {
key: key,
value: value
};
_messageCenter.postMessage('setState', data);
};
_messageCenter.getState = function(key) {
return _state[key];
};
_messageCenter.addMessageListener = function(listener) {
_listeners[_listeners.length] = listener;
};
_messageCenter.addStateListener = function(key, listener) {
_stateListeners[_stateListeners.length] = {
key: key,
listener: listener
};
};
})();