You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.4 KiB

3 years ago
  1. 
  2. $axure.internal(function($ax) {
  3. var funcs = {};
  4. var weekday = new Array(7);
  5. weekday[0] = "Sunday";
  6. weekday[1] = "Monday";
  7. weekday[2] = "Tuesday";
  8. weekday[3] = "Wednesday";
  9. weekday[4] = "Thursday";
  10. weekday[5] = "Friday";
  11. weekday[6] = "Saturday";
  12. funcs.getDayOfWeek = function() {
  13. return _getDayOfWeek(this.getDay());
  14. };
  15. var _getDayOfWeek = $ax.getDayOfWeek = function(day) {
  16. return weekday[day];
  17. };
  18. var month = new Array(12);
  19. month[0] = "January";
  20. month[1] = "February";
  21. month[2] = "March";
  22. month[3] = "April";
  23. month[4] = "May";
  24. month[5] = "June";
  25. month[6] = "July";
  26. month[7] = "August";
  27. month[8] = "September";
  28. month[9] = "October";
  29. month[10] = "November";
  30. month[11] = "December";
  31. funcs.getMonthName = function() {
  32. return _getMonthName(this.getMonth());
  33. };
  34. var _getMonthName = $ax.getMonthName = function(monthNum) {
  35. return month[monthNum];
  36. };
  37. funcs.getMonth = function() {
  38. return this.getMonth() + 1;
  39. };
  40. funcs.addYears = function(years) {
  41. var retVal = new Date(this.valueOf());
  42. retVal.setFullYear(this.getFullYear() + Number(years));
  43. return retVal;
  44. };
  45. funcs.addMonths = function(months) {
  46. var retVal = new Date(this.valueOf());
  47. retVal.setMonth(this.getMonth() + Number(months));
  48. return retVal;
  49. };
  50. funcs.addDays = function(days) {
  51. var retVal = new Date(this.valueOf());
  52. retVal.setDate(this.getDate() + Number(days));
  53. return retVal;
  54. };
  55. funcs.addHours = function(hours) {
  56. var retVal = new Date(this.valueOf());
  57. retVal.setHours(this.getHours() + Number(hours));
  58. return retVal;
  59. };
  60. funcs.addMinutes = function(minutes) {
  61. var retVal = new Date(this.valueOf());
  62. retVal.setMinutes(this.getMinutes() + Number(minutes));
  63. return retVal;
  64. };
  65. funcs.addSeconds = function(seconds) {
  66. var retVal = new Date(this.valueOf());
  67. retVal.setSeconds(this.getSeconds() + Number(seconds));
  68. return retVal;
  69. };
  70. funcs.addMilliseconds = function(milliseconds) {
  71. var retVal = new Date(this.valueOf());
  72. retVal.setMilliseconds(this.getMilliseconds() + Number(milliseconds));
  73. return retVal;
  74. };
  75. var _stoHandlers = {};
  76. _stoHandlers.literal = function(sto, scope, eventInfo) {
  77. return sto.value;
  78. };
  79. //need angle bracket syntax because var is a reserved word
  80. _stoHandlers['var'] = function(sto, scope, eventInfo) {
  81. // Can't us 'A || B' here, because the first value can be false, true, or empty string and still be valid.
  82. var retVal = scope.hasOwnProperty(sto.name) ? scope[sto.name] : $ax.globalVariableProvider.getVariableValue(sto.name, eventInfo);
  83. // Handle desired type here?
  84. if(retVal && retVal.exprType) {
  85. retVal = $ax.expr.evaluateExpr(retVal, eventInfo);
  86. }
  87. if((sto.desiredType == 'int' || sto.desiredType == 'float')) {
  88. var num = new Number(retVal);
  89. retVal = isNaN(num.valueOf()) ? retVal : num;
  90. }
  91. return retVal;
  92. };
  93. //TODO: Perhaps repeaterId can be detirmined at generation, and stored in the sto info.
  94. _stoHandlers.item = function(sto, scope, eventInfo, prop) {
  95. prop = prop || (eventInfo.data ? 'data' : eventInfo.link ? 'url' : eventInfo.image ? 'img' : 'text');
  96. var id = sto.isTarget || !$ax.repeater.hasData(eventInfo.srcElement, sto.name) ? eventInfo.targetElement : eventInfo.srcElement;
  97. return getData(eventInfo, id, sto.name, prop);
  98. };
  99. var getData = function(eventInfo, id, name, prop) {
  100. var repeaterId = $ax.getParentRepeaterFromScriptId($ax.repeater.getScriptIdFromElementId(id));
  101. var itemId = $ax.repeater.getItemIdFromElementId(id);
  102. return $ax.repeater.getData(eventInfo, repeaterId, itemId, name, prop);
  103. };
  104. _stoHandlers.paren = function(sto, scope, eventInfo) {
  105. return _evaluateSTO(sto.innerSTO, scope, eventInfo);
  106. };
  107. _stoHandlers.fCall = function(sto, scope, eventInfo) {
  108. //TODO: [mas] handle required type
  109. var thisObj = _evaluateSTO(sto.thisSTO, scope, eventInfo);
  110. if(sto.thisSTO.desiredType == 'string' && sto.thisSTO.computedType != 'string') thisObj = thisObj.toString();
  111. var args = [];
  112. for(var i = 0; i < sto.arguments.length; i++) {
  113. args[i] = _evaluateSTO(sto.arguments[i], scope, eventInfo);
  114. }
  115. var fn = (funcs.hasOwnProperty(sto.func) && funcs[sto.func]) || thisObj[sto.func];
  116. return fn.apply(thisObj, args);
  117. };
  118. _stoHandlers.propCall = function(sto, scope, eventInfo) {
  119. //TODO: [mas] handle required type
  120. if((sto.prop == 'url' || sto.prop == 'img') && sto.thisSTO.sto == 'item') return _stoHandlers.item(sto.thisSTO, scope, eventInfo, sto.prop);
  121. var thisObj = _evaluateSTO(sto.thisSTO, scope, eventInfo);
  122. var prop = thisObj[sto.prop] instanceof Function ? thisObj[sto.prop]() : thisObj[sto.prop];
  123. return prop;
  124. };
  125. var _binOps = {};
  126. _binOps['+'] = function(left, right) {
  127. if(left instanceof Date) return addDayToDate(left, right);
  128. if(right instanceof Date) return addDayToDate(right, left);
  129. var num = Number(left) + Number(right);
  130. return isNaN(num) ? (String(left) + String(right)) : num;
  131. };
  132. _binOps['-'] = function(left, right) {
  133. if(left instanceof Date) return addDayToDate(left, -right);
  134. return left - right;
  135. };
  136. _binOps['*'] = function(left, right) { return Number(left) * Number(right); };
  137. _binOps['/'] = function(left, right) { return Number(left) / Number(right); };
  138. _binOps['%'] = function(left, right) { return Number(left) % Number(right); };
  139. _binOps['=='] = function(left, right) { return _getBool(left) == _getBool(right); };
  140. _binOps['!='] = function(left, right) { return _getBool(left) != _getBool(right); };
  141. _binOps['<'] = function(left, right) { return Number(left) < Number(right); };
  142. _binOps['<='] = function(left, right) { return Number(left) <= Number(right); };
  143. _binOps['>'] = function(left, right) { return Number(left) > Number(right); };
  144. _binOps['>='] = function(left, right) { return Number(left) >= Number(right); };
  145. _binOps['&&'] = function(left, right) { return _getBool(left) && _getBool(right); };
  146. _binOps['||'] = function(left, right) { return _getBool(left) || _getBool(right); };
  147. // TODO: Move this to generic place to be used.
  148. var addDayToDate = function(date, days) {
  149. var retVal = new Date(date.valueOf());
  150. retVal.setDate(date.getDate() + days);
  151. return retVal;
  152. };
  153. var _unOps = {};
  154. _unOps['+'] = function(arg) { return +arg; };
  155. _unOps['-'] = function(arg) { return -arg; };
  156. _unOps['!'] = function(arg) { return !_getBool(arg); };
  157. _stoHandlers.binOp = function(sto, scope, eventInfo) {
  158. var left = _evaluateSTO(sto.leftSTO, scope, eventInfo);
  159. var right = _evaluateSTO(sto.rightSTO, scope, eventInfo);
  160. return _binOps[sto.op](left, right);
  161. };
  162. _stoHandlers.unOp = function(sto, scope, eventInfo) {
  163. var input = _evaluateSTO(sto.inputSTO, scope, eventInfo);
  164. return _unOps[sto.op](input);
  165. };
  166. var _getBool = function(val) {
  167. var lowerVal = val.toLowerCase ? val.toLowerCase() : val;
  168. return lowerVal == "false" ? false : lowerVal == "true" ? true : val;
  169. };
  170. $ax.getBool = _getBool;
  171. var _evaluateSTO = function(sto, scope, eventInfo) {
  172. if(sto.sto == 'error') return undefined;
  173. return _tryEscapeRichText(castSto(_stoHandlers[sto.sto](sto, scope, eventInfo), sto), eventInfo);
  174. };
  175. $ax.evaluateSTO = _evaluateSTO;
  176. var castSto = function(val, sto) {
  177. var type = sto.computedType || sto.desiredType;
  178. if(type == 'string') val = String(val);
  179. else if(type == 'date' && !(val instanceof Date)) val = new Date(val);
  180. else if(type == 'int' || type == 'float') val = Number(val);
  181. else if(type == 'bool') val = Boolean(val);
  182. return val;
  183. };
  184. var _tryEscapeRichText = function(text, eventInfo) {
  185. return eventInfo.htmlLiteral ? _escapeRichText(text) : text;
  186. };
  187. var _escapeRichText = function(text) {
  188. if(typeof (text) != 'string') return text;
  189. return text.replace('<', '&lt;');
  190. };
  191. });