axplayer.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. if (!window.$axure) window.$axure = function () { };
  2. if (typeof console == 'undefined') console = {
  3. log: function () { }
  4. };
  5. if(window._axUtils) $axure.utils = _axUtils;
  6. $axure.loadDocument = function(document) {
  7. $axure.document = document;
  8. };
  9. function setUpController() {
  10. //$axure.utils = _axUtils;
  11. var _page = {};
  12. $axure.page = _page;
  13. $axure.utils.makeBindable(_page, ['load']);
  14. var _player = function() {
  15. };
  16. $axure.player = _player;
  17. //-----------------------------------------
  18. //Global Var array, getLinkUrl function and setGlobalVar listener are
  19. //for use in setting global vars in page url string when clicking a
  20. //page in the sitemap
  21. //-----------------------------------------
  22. var _globalVars = {};
  23. //-----------------------------------------
  24. //Used by getLinkUrl below to check if local server is running
  25. //in order to send back the global variables as a query string
  26. //in the page url
  27. //-----------------------------------------
  28. var _shouldSendVarsToServer = function () {
  29. //If exception occurs (due to page in content frame being from a different domain, etc)
  30. //then run the check without the url (which will end up checking against sitemap url)
  31. try {
  32. var mainFrame = document.getElementById("mainFrame");
  33. return $axure.shouldSendVarsToServer(mainFrame.contentWindow.location.href);
  34. } catch (e) {
  35. return $axure.shouldSendVarsToServer();
  36. }
  37. };
  38. var _getLinkUrl = function (baseUrl) {
  39. var toAdd = '';
  40. for(var globalVarName in _globalVars) {
  41. var val = _globalVars[globalVarName];
  42. if(val != null) {
  43. if(toAdd.length > 0) toAdd += '&';
  44. toAdd += globalVarName + '=' + encodeURIComponent(val);
  45. }
  46. }
  47. return toAdd.length > 0 ? baseUrl + (_shouldSendVarsToServer() ? '?' : '#') + toAdd + "&CSUM=1" : baseUrl;
  48. };
  49. $axure.getLinkUrlWithVars = _getLinkUrl;
  50. $axure.messageCenter.addMessageListener(function(message, data) {
  51. if (message == 'setGlobalVar'){
  52. _globalVars[data.globalVarName] = data.globalVarValue;
  53. }
  54. });
  55. $axure.messageCenter.addStateListener('page.data', function (key, value) {
  56. for (var subKey in value) {
  57. _page[subKey] = value[subKey];
  58. }
  59. $axure.page.triggerEvent('load');
  60. });
  61. // ---------------------------------------------
  62. // Navigates the main frame (setting the currently visible page). If the link is relative,
  63. // this method should test if it is actually a axure rp page being loaded and properly set
  64. // up all the controller for the page if it is
  65. // ---------------------------------------------
  66. _page.navigate = function (url, includeVariables) {
  67. var mainFrame = document.getElementById("mainFrame");
  68. //var mainFrame = window.parent.mainFrame;
  69. // if this is a relative url...
  70. var urlToLoad;
  71. if (url.indexOf(':') < 0 || url[0] == '/') {
  72. var winHref = window.location.href;
  73. var page = winHref.substring(0, winHref.lastIndexOf('/') + 1) + url;
  74. urlToLoad = page;
  75. } else {
  76. urlToLoad = url;
  77. }
  78. if (!includeVariables) {
  79. mainFrame.contentWindow.location.href = urlToLoad;
  80. return;
  81. }
  82. var urlWithVars = $axure.getLinkUrlWithVars(urlToLoad);
  83. var currentData = $axure.messageCenter.getState('page.data');
  84. var currentUrl = currentData && currentData.location;
  85. if(currentUrl && currentUrl.indexOf('#') != -1) currentUrl = currentUrl.substring(0, currentUrl.indexOf('#'))
  86. // this is so we can make sure the current frame reloads if the variables have changed
  87. // by default, if the location is the same but the hash code is different, the browser will not
  88. // trigger a reload
  89. mainFrame.contentWindow.location.href =
  90. currentUrl && urlToLoad.toLowerCase() != currentUrl.toLowerCase()
  91. ? urlWithVars
  92. : 'resources/reload.html#' + encodeURI(urlWithVars);
  93. };
  94. var pluginIds = [];
  95. var plugins = {};
  96. var currentVisibleHostId = null;
  97. // ---------------------------------------------
  98. // Adds a tool box frame from a url to the interface. This is useful for loading plugins
  99. // settings is an object that supports the following properties:
  100. // - id : the id of the element for the plugin
  101. // - context : the context to create the plugin host for
  102. // - title : the user-visible caption for the plugin
  103. // ---------------------------------------------
  104. _player.createPluginHost = function (settings) {
  105. // right now we only understand an interface context
  106. if (!(!settings.context || settings.context === 'interface')) {
  107. throw ('unknown context type');
  108. }
  109. if (!settings.id) throw ('each plugin host needs an id');
  110. var host = $('<div id=' + settings.id + '></div>')
  111. .appendTo('#interfaceControlFrameHostContainer');
  112. host.hide();
  113. var headerLink = $('<a pluginId="' + settings.id + '" >' + settings.title.toUpperCase() + '</a>');
  114. headerLink
  115. .click($axure.utils.curry(interfaceControlHeaderButton_click, settings.id)).wrap('<li id="' + settings.id + 'Btn">');
  116. if((settings.id == 'feedbackHost' || settings.id == 'feedbackContainer') && pluginIds[pluginIds.length - 1] == 'debugHost') headerLink.parent().insertBefore('#debugHostBtn');
  117. else headerLink.parent().appendTo('#interfaceControlFrameHeader');
  118. pluginIds[pluginIds.length] = settings.id;
  119. plugins[settings.id] = settings;
  120. $(document).trigger('pluginCreated', [settings.gid]);
  121. };
  122. // private methods
  123. var interfaceControlHeaderButton_click = function (id) {
  124. var clickedPlugin = $('#interfaceControlFrameHeader a[pluginId=' + id + ']');
  125. if(clickedPlugin.hasClass('selected')) {
  126. clickedPlugin.removeClass('selected');
  127. $('#' + id).hide();
  128. _player.collapseToBar();
  129. $(document).trigger('pluginShown',['']);
  130. } else {
  131. $('#interfaceControlFrameHeader a').removeClass('selected');
  132. clickedPlugin.addClass('selected');
  133. $('#' + currentVisibleHostId).hide();
  134. $('#' + id).show();
  135. currentVisibleHostId = id;
  136. _player.expandFromBar();
  137. $(document).trigger('pluginShown', [plugins[id].gid]);
  138. }
  139. $(document).trigger('ContainerHeightChange');
  140. };
  141. $axure.player.showPlugin = function(gid) {
  142. for(var id in plugins) {
  143. if(plugins[id].gid == gid) {
  144. $('a[pluginId="' + id + '"]').click();
  145. break;
  146. }
  147. }
  148. };
  149. }
  150. function setUpDocumentStateManager() {
  151. var mgr = $axure.prototype.documentStateManager = {};
  152. $axure.utils.makeBindable(mgr, ['globalVariableChanged']);
  153. mgr.globalVariableValues = {};
  154. mgr.setGlobalVariable = function(varname, value, source) {
  155. var arg = {};
  156. arg.variableName = varname;
  157. arg.newValue = value;
  158. arg.oldValue = this.getGlobalVariable(varname);
  159. arg.source = source;
  160. mgr.globalVariableValues[varname] = value;
  161. this.triggerEvent('globalVariableChanged', arg);
  162. };
  163. mgr.getGlobalVariable = function(varname) {
  164. return mgr.globalVariableValues[varname];
  165. };
  166. }
  167. function setUpPageStateManager() {
  168. var mgr = $axure.prototype.pageStateManager = {};
  169. mgr.panelToStateIds = {};
  170. }