variables.js.orig 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // ******* GLOBAL VARIABLE PROVIDER ******** //
  2. $axure.internal(function($ax) {
  3. var _globalVariableValues = {};
  4. var _globalVariableProvider = {};
  5. $ax.globalVariableProvider = _globalVariableProvider;
  6. var setVariableValue = function(variable, value, suppressBroadcast) {
  7. if(!(value instanceof Object)) {
  8. value = value.toString();
  9. //truncate values to prevent overflows.
  10. if(value.length > 300) {
  11. value = value.substring(0, 300);
  12. }
  13. }
  14. variable = variable.toLowerCase();
  15. _globalVariableValues[variable] = value;
  16. if(suppressBroadcast !== true) {
  17. var varData = {
  18. globalVarName: variable,
  19. globalVarValue: value.toString()
  20. };
  21. $axure.messageCenter.postMessage('setGlobalVar', varData);
  22. }
  23. //Post global var values only if pageData is loaded (suppresses exception which occurs when page loads)
  24. if($ax.pageData) {
  25. _postGlobalVarVals();
  26. }
  27. };
  28. _globalVariableProvider.setVariableValue = setVariableValue;
  29. var getVariableValue = function(variable, eventInfo, ignoreDefaultsForLinkUrl) {
  30. variable = variable.toLowerCase();
  31. if(_globalVariableValues[variable] !== undefined) {
  32. //If this is for the GetLinkUrl function and
  33. //the current value of the global variable is the same as the default defined in the document, don't return it
  34. if(ignoreDefaultsForLinkUrl == true && $ax.document.globalVariables[variable] == _globalVariableValues[variable]) {
  35. return null;
  36. }
  37. return _globalVariableValues[variable];
  38. }
  39. if($ax.document.globalVariables[variable] !== undefined) return ignoreDefaultsForLinkUrl == true ? null : $ax.document.globalVariables[variable];
  40. switch(variable) {
  41. case "pagename": return $ax.pageData.page.name;
  42. case "now": return new Date();
  43. case "gendate": return $ax.pageData.generationDate;
  44. case "dragx": return $ax.drag.GetDragX();
  45. case "dragy": return $ax.drag.GetDragY();
  46. case "totaldragx": return $ax.drag.GetTotalDragX();
  47. case "totaldragy": return $ax.drag.GetTotalDragY();
  48. case "dragtime": return $ax.drag.GetDragTime();
  49. case "math": return Math;
  50. case "window": return eventInfo && eventInfo.window;
  51. case "this": return eventInfo && eventInfo.thiswidget;
  52. case "item": return (eventInfo && eventInfo.item && eventInfo.item.valid && eventInfo.item) || getVariableValue('targetitem', eventInfo, ignoreDefaultsForLinkUrl);
  53. case "targetitem": return eventInfo && eventInfo.targetElement && $ax.getItemInfo(eventInfo.targetElement);
  54. case "repeater": return eventInfo && eventInfo.repeater;
  55. case "target": return eventInfo && eventInfo.targetElement && $ax.getWidgetInfo(eventInfo.targetElement);
  56. case "cursor": return eventInfo && eventInfo.cursor;
  57. default:
  58. var gen = variable.substr(0, 3) == "gen";
  59. var date = gen ? $ax.pageData.generationDate : new Date();
  60. var prop = gen ? variable.substr(3) : variable;
  61. switch(prop) {
  62. case "day": return date.getDate();
  63. case "month": return date.getMonth() + 1;
  64. case "monthname": return $ax.getMonthName(date.getMonth());
  65. case "dayofweek": return $ax.getDayOfWeek(date.getDay());
  66. case "year": return date.getFullYear();
  67. case "time": return date.toLocaleTimeString();
  68. case "hours": return date.getHours();
  69. case "minutes": return date.getMinutes();
  70. case "seconds": return date.getSeconds();
  71. default: return '';
  72. }
  73. }
  74. };
  75. _globalVariableProvider.getVariableValue = getVariableValue;
  76. _globalVariableProvider.viewIdOverride = undefined;
  77. var load = function() {
  78. var csum = false;
  79. var query = (window.location.href.split("#")[1] || ''); //hash.substring(1); Firefox decodes this so & in variables breaks
  80. if(query.length > 0) {
  81. var vars = query.split("&");
  82. for(var i = 0; i < vars.length; i++) {
  83. var pair = vars[i].split("=");
  84. var varName = pair[0];
  85. var varValue = pair[1];
  86. if(varName) {
  87. if(varName == 'CSUM') {
  88. csum = true;
  89. } else if(varName = 'AXVIEWIDOVERRIDE') {
  90. _globalVariableProvider.viewIdOverride = decodeURIComponent(varValue);
  91. } else setVariableValue(varName, decodeURIComponent(varValue), true);
  92. }
  93. }
  94. if(!csum && query.length > 250) {
  95. window.alert('Prototype Warning: The variable values were too long to pass to this page.\nIf you are using IE, using Firefox will support more data.');
  96. }
  97. }
  98. };
  99. var getLinkUrl = function(baseUrl) {
  100. var toAdd = '';
  101. var definedVariables = _getDefinedVariables();
  102. for(var i = 0; i < definedVariables.length; i++) {
  103. var key = definedVariables[i];
  104. var val = getVariableValue(key, undefined, true);
  105. if(val != null && val.length > 0) {
  106. if(toAdd.length > 0) toAdd += '&';
  107. toAdd += key + '=' + encodeURIComponent(val);
  108. }
  109. }
  110. return toAdd.length > 0 ? baseUrl + '#' + toAdd + "&CSUM=1" : baseUrl;
  111. };
  112. _globalVariableProvider.getLinkUrl = getLinkUrl;
  113. var _getDefinedVariables = function() {
  114. return $ax.pageData.variables;
  115. };
  116. _globalVariableProvider.getDefinedVariables = _getDefinedVariables;
  117. var _postGlobalVarVals = function() {
  118. var retVal = {};
  119. var definedVariables = _getDefinedVariables();
  120. for(var i = 0; i < definedVariables.length; i++) {
  121. var key = definedVariables[i];
  122. var val = getVariableValue(key);
  123. if(val != null) {
  124. retVal[key] = val;
  125. }
  126. }
  127. $ax.messageCenter.postMessage('globalVariableValues', retVal);
  128. };
  129. $ax.messageCenter.addMessageListener(function(message, data) {
  130. if(message == 'getGlobalVariables') {
  131. _postGlobalVarVals();
  132. } else if(message == 'resetGlobalVariables') {
  133. _globalVariableValues = {};
  134. _postGlobalVarVals();
  135. }
  136. });
  137. load();
  138. });