messagecenter.js.orig 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 WEBKIT = false;
  8. var OS_MAC = false;
  9. (function () {
  10. var chromeRegex = /Chrome\/([0-9]+).([0-9]+)/g ;
  11. var chromeMatch = chromeRegex.exec(window.navigator.userAgent);
  12. CHROME = Boolean(chromeMatch);
  13. CHROME_5_LOCAL = chromeMatch &&
  14. Number(chromeMatch[1]) >= 5 &&
  15. location.href.indexOf('file://') >= 0;
  16. var webkitRegex = /WebKit\//g ;
  17. WEBKIT = Boolean(webkitRegex.exec(window.navigator.userAgent));
  18. var macRegex = /Mac/g ;
  19. OS_MAC = Boolean(macRegex.exec(window.navigator.platform));
  20. })();
  21. (function() {
  22. var _topMessageCenter;
  23. var _messageCenter = {};
  24. var _listeners = [];
  25. var _stateListeners = [];
  26. var _state = {};
  27. var _eventObject = null;
  28. var _queuedMessages = [];
  29. var _initialized = false;
  30. // this is for the non Chrome 5 local scenarios. The "top" message center will dispatch to all the bottom ones
  31. var _childrenMessageCenters = [];
  32. // create $axure if it hasn't been created
  33. if (!window.$axure) window.$axure = function() {};
  34. $axure.messageCenter = _messageCenter;
  35. // isolate scope, and initialize _topMessageCenter.
  36. (function() {
  37. if (!CHROME_5_LOCAL) {
  38. var topAxureWindow = window;
  39. try {
  40. while(topAxureWindow.parent && topAxureWindow.parent !== topAxureWindow
  41. && topAxureWindow.parent.$axure) topAxureWindow = topAxureWindow.parent;
  42. } catch(e) {}
  43. _topMessageCenter = topAxureWindow.$axure.messageCenter;
  44. }
  45. })();
  46. $(window.document).ready(function() {
  47. if (CHROME_5_LOCAL) {
  48. $('body').append("<div id='axureEventReceiverDiv' style='display:none'></div>" +
  49. "<div id='axureEventSenderDiv' style='display:none'></div>");
  50. _eventObject = window.document.createEvent('Event');
  51. _eventObject.initEvent('axureMessageSenderEvent', true, true);
  52. $('#axureEventReceiverDiv').bind('axureMessageReceiverEvent', function () {
  53. var request = JSON.parse($(this).text());
  54. _handleRequest(request);
  55. });
  56. } else {
  57. if (_topMessageCenter != _messageCenter) {
  58. _topMessageCenter.addChildMessageCenter(_messageCenter);
  59. console.log('adding from ' + window.location.toString());
  60. }
  61. }
  62. });
  63. var _handleRequest = function (request) {
  64. // route the request to all the listeners
  65. for(var i = 0; i < _listeners.length; i++) _listeners[i](request.message, request.data);
  66. // now handle the queued messages if we're initializing
  67. if (request.message == 'initialize') {
  68. _initialized = true;
  69. // send all the queued messages and return
  70. for (var i = 0; i < _queuedMessages.length; i++) {
  71. var qRequest = _queuedMessages[i];
  72. _messageCenter.postMessage(qRequest.message, qRequest.data);
  73. }
  74. _queuedMessages = [];
  75. }
  76. // and then handle the set state messages, if necessary
  77. if (request.message == 'setState') {
  78. _state[request.data.key] = request.data.value;
  79. for (var i = 0; i < _stateListeners.length; i++) {
  80. var keyListener = _stateListeners[i];
  81. // if thep passed a null or empty value, always post the message
  82. if (!keyListener.key || keyListener.key == request.data.key) {
  83. keyListener.listener(request.data.key, request.data.value);
  84. }
  85. }
  86. }
  87. };
  88. // -----------------------------------------------------------------------------------------
  89. // This method allows for dispatching messages in the non-chromelocal scenario.
  90. // Each child calls this on _topMessageCenter
  91. // -----------------------------------------------------------------------------------------
  92. _messageCenter.addChildMessageCenter = function(messageCenter) {
  93. _childrenMessageCenters[_childrenMessageCenters.length] = messageCenter;
  94. };
  95. // -----------------------------------------------------------------------------------------
  96. // This method allows for dispatching messages in the non-chromelocal scenario.
  97. // Each child calls this on _topMessageCenter
  98. // -----------------------------------------------------------------------------------------
  99. _messageCenter.dispatchMessage = function(message, data) {
  100. _handleRequest({
  101. message: message,
  102. data: data
  103. });
  104. };
  105. // -----------------------------------------------------------------------------------------
  106. // -----------------------------------------------------------------------------------------
  107. _messageCenter.dispatchMessageRecursively = function(message, data) {
  108. console.log("dispatched to " + window.location.toString());
  109. // dispatch to the top center first
  110. _messageCenter.dispatchMessage(message, data);
  111. $('iframe').each(function(index, frame) {
  112. //try,catch to handle permissions error in FF when loading pages from another domain
  113. try {
  114. if (frame.contentWindow.$axure && frame.contentWindow.$axure.messageCenter) {
  115. frame.contentWindow.$axure.messageCenter.dispatchMessageRecursively(message, data);
  116. }
  117. }catch(e) {}
  118. });
  119. };
  120. _messageCenter.postMessage = function(message, data) {
  121. if(!CHROME_5_LOCAL) {
  122. _topMessageCenter.dispatchMessageRecursively(message, data);
  123. } else {
  124. var request = {
  125. message: message,
  126. data: data
  127. };
  128. if(_initialized) {
  129. var senderDiv = window.document.getElementById('axureEventSenderDiv');
  130. var messageText = JSON.stringify(request);
  131. // console.log('sending event: ' + messageText);
  132. senderDiv.innerText = messageText;
  133. senderDiv.dispatchEvent(_eventObject);
  134. // console.log('event sent');
  135. } else {
  136. _queuedMessages[_queuedMessages.length] = request;
  137. }
  138. }
  139. };
  140. _messageCenter.setState = function(key, value) {
  141. var data = {
  142. key: key,
  143. value: value
  144. };
  145. _messageCenter.postMessage('setState', data);
  146. };
  147. _messageCenter.getState = function(key) {
  148. return _state[key];
  149. };
  150. _messageCenter.addMessageListener = function(listener) {
  151. _listeners[_listeners.length] = listener;
  152. };
  153. _messageCenter.addStateListener = function(key, listener) {
  154. _stateListeners[_stateListeners.length] = {
  155. key: key,
  156. listener: listener
  157. };
  158. };
  159. })();