variables.js 6.5 KB

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