axplayer.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 && val.length > 0) {
  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 currentVisibleHostId = null;
  96. // ---------------------------------------------
  97. // Adds a tool box frame from a url to the interface. This is useful for loading plugins
  98. // settings is an object that supports the following properties:
  99. // - id : the id of the element for the plugin
  100. // - context : the context to create the plugin host for
  101. // - title : the user-visible caption for the plugin
  102. // ---------------------------------------------
  103. _player.createPluginHost = function (settings) {
  104. // right now we only understand an interface context
  105. if (!(!settings.context || settings.context === 'interface')) {
  106. throw ('unknown context type');
  107. }
  108. if (!settings.id) throw ('each plugin host needs an id');
  109. var host = $('<div id=' + settings.id + '></div>')
  110. .appendTo('#interfaceControlFrameContainer');
  111. var isCurrentDefault = (pluginIds.length == 0);
  112. if (!isCurrentDefault) {
  113. host.hide();
  114. } else {
  115. currentVisibleHostId = settings.id;
  116. }
  117. //$('#interfaceControlFrameHeader').append('<li>' + settings.title + '</li>');
  118. var headerLink = $('<a pluginId="' + settings.id + '" >' + settings.title + '</a>');
  119. headerLink
  120. .click($axure.utils.curry(interfaceControlHeaderButton_click, settings.id)).wrap('<li>')
  121. .parent().appendTo('#interfaceControlFrameHeader');
  122. if (isCurrentDefault) {
  123. headerLink.addClass('selected');
  124. }
  125. pluginIds[pluginIds.length] = settings.id;
  126. };
  127. // private methods
  128. var interfaceControlHeaderButton_click = function (id) {
  129. $('#interfaceControlFrameHeader a').removeClass('selected');
  130. $('#interfaceControlFrameHeader a[pluginId=' + id + ']').addClass('selected');
  131. $('#' + currentVisibleHostId).hide();
  132. $('#' + id).show();
  133. currentVisibleHostId = id;
  134. };
  135. }
  136. function setUpDocumentStateManager() {
  137. var mgr = $axure.prototype.documentStateManager = {};
  138. $axure.utils.makeBindable(mgr, ['globalVariableChanged']);
  139. mgr.globalVariableValues = {};
  140. mgr.setGlobalVariable = function(varname, value, source) {
  141. var arg = {};
  142. arg.variableName = varname;
  143. arg.newValue = value;
  144. arg.oldValue = this.getGlobalVariable(varname);
  145. arg.source = source;
  146. mgr.globalVariableValues[varname] = value;
  147. this.triggerEvent('globalVariableChanged', arg);
  148. };
  149. mgr.getGlobalVariable = function(varname) {
  150. return mgr.globalVariableValues[varname];
  151. };
  152. }
  153. function setUpPageStateManager() {
  154. var mgr = $axure.prototype.pageStateManager = {};
  155. mgr.panelToStateIds = {};
  156. }