messagecenter.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. if (typeof console == 'undefined') console = {
  2. log: function () { }
  3. };
  4. // sniff chrome
  5. var CHROME_5_LOCAL = false;
  6. var CHROME = false;
  7. var SAFARI = false;
  8. var FIREFOX = false;
  9. var WEBKIT = false;
  10. var OS_MAC = false;
  11. var IOS = false;
  12. var MOBILE_DEVICE = false;
  13. var IE = false;
  14. var IE_10_AND_BELOW = false; //ie 10 and lower
  15. var IE_11_AND_ABOVE = false; //ie 11 and above
  16. var BROWSER_VERSION = 5000;
  17. (function () {
  18. if(!window.$axure) window.$axure = function() {};
  19. var useragent = window.navigator.userAgent;
  20. var edgeRegex = /Edge\/([0-9]+)/g;
  21. var edgeMatch = edgeRegex.exec(useragent);
  22. $axure.browser = { isEdge: Boolean(edgeMatch) };
  23. if(!$axure.browser.isEdge) {
  24. var chromeRegex = /Chrome\/([0-9]+).([0-9]+)/g;
  25. var chromeMatch = chromeRegex.exec(useragent);
  26. CHROME = Boolean(chromeMatch);
  27. CHROME_5_LOCAL = chromeMatch &&
  28. Number(chromeMatch[1]) >= 5 &&
  29. location.href.indexOf('file://') >= 0;
  30. }
  31. var safariRegex = /Safari\/([0-9]+)/g;
  32. var safariMatch = safariRegex.exec(useragent);
  33. SAFARI = Boolean(safariMatch) && !CHROME; //because chrome also inserts safari string into user agent
  34. var webkitRegex = /WebKit\//g ;
  35. WEBKIT = Boolean(webkitRegex.exec(useragent));
  36. FIREFOX = useragent.toLowerCase().indexOf('firefox') > -1;
  37. var macRegex = /Mac/g ;
  38. OS_MAC = Boolean(macRegex.exec(window.navigator.platform));
  39. IOS = useragent.match(/iPhone/i) || useragent.match(/iPad/i) || useragent.match(/iPod/i);
  40. MOBILE_DEVICE = navigator.userAgent.match(/Android/i)
  41. || navigator.userAgent.match(/webOS/i)
  42. || navigator.userAgent.match(/iPhone/i)
  43. || navigator.userAgent.match(/iPad/i)
  44. || navigator.userAgent.match(/iPod/i)
  45. || navigator.userAgent.match(/BlackBerry/i)
  46. || navigator.userAgent.match(/Tablet PC/i)
  47. || navigator.userAgent.match(/Windows Phone/i);
  48. if($.browser) {
  49. if($.browser.msie) IE_10_AND_BELOW = true;
  50. else IE_11_AND_ABOVE = useragent.toLowerCase().indexOf('trident') > -1;
  51. BROWSER_VERSION = $.browser.version;
  52. }
  53. IE = IE_10_AND_BELOW || IE_11_AND_ABOVE;
  54. //Used by sitemap and variables.js getLinkUrl functions so that they know
  55. //whether to embed global variables in URL as query string or hash string
  56. //_shouldSendVars persists the value for sitemap instead of re-checking every time
  57. var _shouldSendVars;
  58. var _shouldSendVarsToServer = function(url) {
  59. if(typeof _shouldSendVars != 'undefined') {
  60. return _shouldSendVars;
  61. }
  62. if(SAFARI || (IE_10_AND_BELOW && BROWSER_VERSION < 10)) {
  63. var urlToCheck = typeof url != 'undefined' ? url : window.location.href;
  64. var serverRegex = /http:\/\/127\.0\.0\.1:[0-9]{5}/g;
  65. var serverMatch = serverRegex.exec(urlToCheck);
  66. var previewRegex = /[0-9]{2}\.[0-9]{2}\.[0-9]{2}/g;
  67. var previewMatch = previewRegex.exec(urlToCheck);
  68. if(Boolean(serverMatch) && Boolean(previewMatch)) {
  69. _shouldSendVars = true;
  70. return _shouldSendVars;
  71. }
  72. }
  73. _shouldSendVars = false;
  74. return _shouldSendVars;
  75. };
  76. $axure.shouldSendVarsToServer = _shouldSendVarsToServer;
  77. })();
  78. (function() {
  79. var _topMessageCenter;
  80. var _messageCenter = {};
  81. var _listeners = [];
  82. var _stateListeners = [];
  83. var _state = {};
  84. var _eventObject = null;
  85. var _queuedMessages = [];
  86. var _initialized = false;
  87. // this is for the non Chrome 5 local scenarios. The "top" message center will dispatch to all the bottom ones
  88. var _childrenMessageCenters = [];
  89. // create $axure if it hasn't been created
  90. if (!window.$axure) window.$axure = function() {};
  91. $axure.messageCenter = _messageCenter;
  92. // isolate scope, and initialize _topMessageCenter.
  93. (function() {
  94. if (!CHROME_5_LOCAL) {
  95. var topAxureWindow = window;
  96. try {
  97. while(topAxureWindow.parent && topAxureWindow.parent !== topAxureWindow
  98. && topAxureWindow.parent.$axure) topAxureWindow = topAxureWindow.parent;
  99. } catch(e) {}
  100. _topMessageCenter = topAxureWindow.$axure.messageCenter;
  101. }
  102. })();
  103. $(window.document).ready(function() {
  104. if (CHROME_5_LOCAL) {
  105. $('body').append("<div id='axureEventReceiverDiv' style='display:none'></div>" +
  106. "<div id='axureEventSenderDiv' style='display:none'></div>");
  107. _eventObject = window.document.createEvent('Event');
  108. _eventObject.initEvent('axureMessageSenderEvent', true, true);
  109. $('#axureEventReceiverDiv').bind('axureMessageReceiverEvent', function () {
  110. var request = JSON.parse($(this).text());
  111. _handleRequest(request);
  112. });
  113. } else {
  114. if (_topMessageCenter != _messageCenter) {
  115. _topMessageCenter.addChildMessageCenter(_messageCenter);
  116. console.log('adding from ' + window.location.toString());
  117. }
  118. }
  119. });
  120. var _handleRequest = function (request) {
  121. // route the request to all the listeners
  122. for(var i = 0; i < _listeners.length; i++) _listeners[i](request.message, request.data);
  123. // now handle the queued messages if we're initializing
  124. if (request.message == 'initialize') {
  125. _initialized = true;
  126. // send all the queued messages and return
  127. for (var i = 0; i < _queuedMessages.length; i++) {
  128. var qRequest = _queuedMessages[i];
  129. _messageCenter.postMessage(qRequest.message, qRequest.data);
  130. }
  131. _queuedMessages = [];
  132. }
  133. // and then handle the set state messages, if necessary
  134. if (request.message == 'setState') {
  135. _state[request.data.key] = request.data.value;
  136. for (var i = 0; i < _stateListeners.length; i++) {
  137. var keyListener = _stateListeners[i];
  138. // if thep passed a null or empty value, always post the message
  139. if (!keyListener.key || keyListener.key == request.data.key) {
  140. keyListener.listener(request.data.key, request.data.value);
  141. }
  142. }
  143. }
  144. };
  145. // -----------------------------------------------------------------------------------------
  146. // This method allows for dispatching messages in the non-chromelocal scenario.
  147. // Each child calls this on _topMessageCenter
  148. // -----------------------------------------------------------------------------------------
  149. _messageCenter.addChildMessageCenter = function(messageCenter) {
  150. _childrenMessageCenters[_childrenMessageCenters.length] = messageCenter;
  151. };
  152. // -----------------------------------------------------------------------------------------
  153. // This method allows for dispatching messages in the non-chromelocal scenario.
  154. // Each child calls this on _topMessageCenter
  155. // -----------------------------------------------------------------------------------------
  156. _messageCenter.dispatchMessage = function(message, data) {
  157. _handleRequest({
  158. message: message,
  159. data: data
  160. });
  161. };
  162. // -----------------------------------------------------------------------------------------
  163. // -----------------------------------------------------------------------------------------
  164. _messageCenter.dispatchMessageRecursively = function(message, data) {
  165. console.log("dispatched to " + window.location.toString());
  166. // dispatch to the top center first
  167. _messageCenter.dispatchMessage(message, data);
  168. $('iframe').each(function(index, frame) {
  169. //try,catch to handle permissions error in FF when loading pages from another domain
  170. try {
  171. if (frame.contentWindow.$axure && frame.contentWindow.$axure.messageCenter) {
  172. frame.contentWindow.$axure.messageCenter.dispatchMessageRecursively(message, data);
  173. }
  174. }catch(e) {}
  175. });
  176. };
  177. _messageCenter.postMessage = function(message, data) {
  178. if(!CHROME_5_LOCAL) {
  179. _topMessageCenter.dispatchMessageRecursively(message, data);
  180. } else {
  181. var request = {
  182. message: message,
  183. data: data
  184. };
  185. if(_initialized) {
  186. var senderDiv = window.document.getElementById('axureEventSenderDiv');
  187. var messageText = JSON.stringify(request);
  188. // console.log('sending event: ' + messageText);
  189. senderDiv.innerText = messageText;
  190. senderDiv.dispatchEvent(_eventObject);
  191. // console.log('event sent');
  192. } else {
  193. _queuedMessages[_queuedMessages.length] = request;
  194. }
  195. }
  196. };
  197. _messageCenter.setState = function(key, value) {
  198. var data = {
  199. key: key,
  200. value: value
  201. };
  202. _messageCenter.postMessage('setState', data);
  203. };
  204. _messageCenter.getState = function(key) {
  205. return _state[key];
  206. };
  207. _messageCenter.addMessageListener = function(listener) {
  208. _listeners[_listeners.length] = listener;
  209. };
  210. _messageCenter.addStateListener = function(key, listener) {
  211. _stateListeners[_stateListeners.length] = {
  212. key: key,
  213. listener: listener
  214. };
  215. };
  216. })();