sto.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return thisObj[sto.prop];
  123. };
  124. var _binOps = {};
  125. _binOps['+'] = function(left, right) {
  126. if(left instanceof Date) return addDayToDate(left, right);
  127. if(right instanceof Date) return addDayToDate(right, left);
  128. var num = Number(left) + Number(right);
  129. return isNaN(num) ? (String(left) + String(right)) : num;
  130. };
  131. _binOps['-'] = function(left, right) {
  132. if(left instanceof Date) return addDayToDate(left, -right);
  133. return left - right;
  134. };
  135. _binOps['*'] = function(left, right) { return Number(left) * Number(right); };
  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 _getBool(left) == _getBool(right); };
  139. _binOps['!='] = function(left, right) { return _getBool(left) != _getBool(right); };
  140. _binOps['<'] = function(left, right) { return Number(left) < Number(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 _getBool(left) && _getBool(right); };
  145. _binOps['||'] = function(left, right) { return _getBool(left) || _getBool(right); };
  146. // TODO: Move this to generic place to be used.
  147. var addDayToDate = function(date, days) {
  148. var retVal = new Date(date.valueOf());
  149. retVal.setDate(date.getDate() + days);
  150. return retVal;
  151. };
  152. var _unOps = {};
  153. _unOps['+'] = function(arg) { return +arg; };
  154. _unOps['-'] = function(arg) { return -arg; };
  155. _unOps['!'] = function(arg) { return !_getBool(arg); };
  156. _stoHandlers.binOp = function(sto, scope, eventInfo) {
  157. var left = _evaluateSTO(sto.leftSTO, scope, eventInfo);
  158. var right = _evaluateSTO(sto.rightSTO, scope, eventInfo);
  159. return _binOps[sto.op](left, right);
  160. };
  161. _stoHandlers.unOp = function(sto, scope, eventInfo) {
  162. var input = _evaluateSTO(sto.inputSTO, scope, eventInfo);
  163. return _unOps[sto.op](input);
  164. };
  165. var _getBool = function(val) {
  166. var lowerVal = val.toLowerCase ? val.toLowerCase() : val;
  167. return lowerVal == "false" ? false : lowerVal == "true" ? true : val;
  168. };
  169. $ax.getBool = _getBool;
  170. var _evaluateSTO = function(sto, scope, eventInfo) {
  171. if(sto.sto == 'error') return undefined;
  172. return _tryEscapeRichText(castSto(_stoHandlers[sto.sto](sto, scope, eventInfo), sto), eventInfo);
  173. };
  174. $ax.evaluateSTO = _evaluateSTO;
  175. var castSto = function(val, sto) {
  176. var type = sto.computedType || sto.desiredType;
  177. if(type == 'string') val = String(val);
  178. else if(type == 'date' && !(val instanceof Date)) val = new Date(val);
  179. else if(type == 'int' || type == 'float') val = Number(val);
  180. else if(type == 'bool') val = Boolean(val);
  181. return val;
  182. };
  183. var _tryEscapeRichText = function(text, eventInfo) {
  184. return eventInfo.htmlLiteral ? _escapeRichText(text) : text;
  185. };
  186. var _escapeRichText = function(text) {
  187. if(typeof (text) != 'string') return text;
  188. return text.replace('<', '&lt;');
  189. };
  190. });