Newer
Older
KaiFengAxure / 开封原型海绵综合一张图 / resources / scripts / axure / variables.js
@zhangdeliang zhangdeliang on 4 Jun 5 KB 1
  1. // ******* GLOBAL VARIABLE PROVIDER ******** //
  2. $axure.internal(function($ax) {
  3. var _globalVariableValues = {};
  4.  
  5. var _globalVariableProvider = {};
  6. $ax.globalVariableProvider = _globalVariableProvider;
  7.  
  8. var setVariableValue = function(variable, value, suppressBroadcast) {
  9. if(!(value instanceof Object)) value = value.toString();
  10.  
  11. variable = variable.toLowerCase();
  12. _globalVariableValues[variable] = value;
  13.  
  14. if(suppressBroadcast !== true) {
  15. var varData = {
  16. globalVarName: variable,
  17. globalVarValue: value.toString()
  18. };
  19.  
  20. $axure.messageCenter.postMessage('setGlobalVar', varData);
  21. }
  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.  
  30. var getVariableValue = function(variable, eventInfo, ignoreDefaultsForLinkUrl) {
  31. variable = variable.toLowerCase();
  32. if(_globalVariableValues[variable] !== undefined) {
  33. //If this is for the GetLinkUrl function and
  34. //the current value of the global variable is the same as the default defined in the document, don't return it
  35. if(ignoreDefaultsForLinkUrl == true && $ax.document.globalVariables[variable] == _globalVariableValues[variable]) {
  36. return null;
  37. }
  38.  
  39. return _globalVariableValues[variable];
  40. }
  41. if($ax.document.globalVariables[variable] !== undefined) return ignoreDefaultsForLinkUrl == true ? null : $ax.document.globalVariables[variable];
  42. switch(variable) {
  43. case "pagename": return $ax.pageData.page.name;
  44.  
  45. case "now": return eventInfo.now;
  46. case "gendate": return $ax.pageData.generationDate;
  47.  
  48. case "dragx": return $ax.drag.GetDragX();
  49. case "dragy": return $ax.drag.GetDragY();
  50. case "totaldragx": return $ax.drag.GetTotalDragX();
  51. case "totaldragy": return $ax.drag.GetTotalDragY();
  52. case "dragtime": return $ax.drag.GetDragTime();
  53.  
  54. case "math": return Math;
  55. case "date": return Date;
  56.  
  57. case "window": return eventInfo && eventInfo.window;
  58. case "this": return eventInfo && eventInfo.thiswidget && $ax.getWidgetInfo(eventInfo.thiswidget.elementId);
  59. case "item": return (eventInfo && eventInfo.item && eventInfo.item.valid && eventInfo.item) || getVariableValue('targetitem', eventInfo, ignoreDefaultsForLinkUrl);
  60. case "targetitem": return eventInfo && eventInfo.targetElement && $ax.getItemInfo(eventInfo.targetElement);
  61. case "repeater": return eventInfo && eventInfo.repeater;
  62. case "target": return eventInfo && eventInfo.targetElement && $ax.getWidgetInfo(eventInfo.targetElement);
  63. case "cursor": return eventInfo && eventInfo.cursor;
  64. default:
  65. var gen = variable.substr(0, 3) == "gen";
  66. var date = gen ? $ax.pageData.generationDate : new Date();
  67. var prop = gen ? variable.substr(3) : variable;
  68. switch(prop) {
  69. case "day": return date.getDate();
  70. case "month": return date.getMonth() + 1;
  71. case "monthname": return $ax.getMonthName(date.getMonth());
  72. case "dayofweek": return $ax.getDayOfWeek(date.getDay());
  73. case "year": return date.getFullYear();
  74. case "time": return date.toLocaleTimeString();
  75. case "hours": return date.getHours();
  76. case "minutes": return date.getMinutes();
  77. case "seconds": return date.getSeconds();
  78. default: return '';
  79. }
  80. }
  81. };
  82. _globalVariableProvider.getVariableValue = getVariableValue;
  83.  
  84. var load = function() {
  85. let query = (window.location.href.split("#")[1] || ''); //hash.substring(1); Firefox decodes this so & in variables breaks
  86. if(query.length > 0) {
  87. $ax.utils.parseGlobalVars(query, setVariableValue);
  88. }
  89. };
  90.  
  91. var getLinkUrl = function(baseUrl, useGlobalVarName) {
  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. else if(useGlobalVarName) toAdd = GLOBAL_VAR_NAME;
  100. toAdd += key + '=' + encodeURIComponent(val);
  101. }
  102. }
  103. return toAdd.length > 0 ? baseUrl + (useGlobalVarName ? '' : $axure.shouldSendVarsToServer() ? '?' : '#') + toAdd + "&" + GLOBAL_VAR_CHECKSUM + "=1" : baseUrl;
  104. };
  105. _globalVariableProvider.getLinkUrl = getLinkUrl;
  106.  
  107. var _getDefinedVariables = function() {
  108. return $ax.pageData.variables;
  109. };
  110. _globalVariableProvider.getDefinedVariables = _getDefinedVariables;
  111.  
  112. var _postGlobalVarVals = function() {
  113. var retVal = {};
  114. var definedVariables = _getDefinedVariables();
  115. for(var i = 0; i < definedVariables.length; i++) {
  116. var key = definedVariables[i];
  117. var val = getVariableValue(key);
  118. if(val != null) {
  119. retVal[key] = val;
  120. }
  121. }
  122.  
  123. $ax.messageCenter.postMessage('globalVariableValues', retVal);
  124. };
  125.  
  126. $ax.messageCenter.addMessageListener(function(message, data) {
  127. if(message == 'getGlobalVariables') {
  128. _postGlobalVarVals();
  129. } else if(message == 'resetGlobalVariables') {
  130. _globalVariableValues = {};
  131. _postGlobalVarVals();
  132. }
  133. });
  134.  
  135. load();
  136. });