sto.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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((sto.desiredType == 'int' || sto.desiredType == 'float')) {
  85. var num = new Number(retVal);
  86. retVal = isNaN(num.valueOf()) ? retVal : num;
  87. }
  88. return retVal;
  89. };
  90. //TODO: Perhaps repeaterId can be detirmined at generation, and stored in the sto info.
  91. _stoHandlers.item = function(sto, scope, eventInfo, prop) {
  92. prop = prop || (eventInfo.data ? 'data' : eventInfo.link ? 'url' : eventInfo.image ? 'img' : 'text');
  93. var id = sto.isTarget || !$ax.repeater.hasData(eventInfo.srcElement, sto.name) ? eventInfo.targetElement : eventInfo.srcElement;
  94. return getData(id, sto.name, prop);
  95. };
  96. var getData = function(id, name, prop) {
  97. var repeaterId = $ax.getParentRepeaterFromScriptId($ax.repeater.getScriptIdFromElementId(id));
  98. var itemId = $ax.repeater.getItemIdFromElementId(id);
  99. return $ax.repeater.getData(repeaterId, itemId, name, prop);
  100. };
  101. _stoHandlers.paren = function(sto, scope, eventInfo) {
  102. return _evaluateSTO(sto.innerSTO, scope, eventInfo);
  103. };
  104. _stoHandlers.fCall = function(sto, scope, eventInfo) {
  105. //TODO: [mas] handle required type
  106. var thisObj = _evaluateSTO(sto.thisSTO, scope, eventInfo);
  107. if(sto.thisSTO.desiredType == 'string' && sto.thisSTO.computedType != 'string') thisObj = thisObj.toString();
  108. var args = [];
  109. for(var i = 0; i < sto.arguments.length; i++) {
  110. args[i] = _evaluateSTO(sto.arguments[i], scope, eventInfo);
  111. }
  112. var fn = (funcs.hasOwnProperty(sto.func) && funcs[sto.func]) || thisObj[sto.func];
  113. return fn.apply(thisObj, args);
  114. };
  115. _stoHandlers.propCall = function(sto, scope, eventInfo) {
  116. //TODO: [mas] handle required type
  117. if((sto.prop == 'url' || sto.prop == 'img') && sto.thisSTO.sto == 'item') return _stoHandlers.item(sto.thisSTO, scope, eventInfo, sto.prop);
  118. var thisObj = _evaluateSTO(sto.thisSTO, scope, eventInfo);
  119. return thisObj[sto.prop];
  120. };
  121. var _binOps = {};
  122. _binOps['+'] = function(left, right) {
  123. if(left instanceof Date) return addDayToDate(left, right);
  124. if(right instanceof Date) return addDayToDate(right, left);
  125. var num = Number(left) + Number(right);
  126. return isNaN(num) ? (String(left) + String(right)) : num;
  127. };
  128. _binOps['-'] = function(left, right) {
  129. if(left instanceof Date) return addDayToDate(left, -right);
  130. return left - right;
  131. };
  132. _binOps['*'] = function(left, right) { return Number(left) * Number(right); };
  133. _binOps['/'] = function(left, right) { return Number(left) / Number(right); };
  134. _binOps['%'] = function(left, right) { return Number(left) % Number(right); };
  135. _binOps['=='] = function(left, right) { return _getBool(left) == _getBool(right); };
  136. _binOps['!='] = function(left, right) { return _getBool(left) != _getBool(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 Number(left) > Number(right); };
  140. _binOps['>='] = function(left, right) { return Number(left) >= Number(right); };
  141. _binOps['&&'] = function(left, right) { return _getBool(left) && _getBool(right); };
  142. _binOps['||'] = function(left, right) { return _getBool(left) || _getBool(right); };
  143. // TODO: Move this to generic place to be used.
  144. var addDayToDate = function(date, days) {
  145. var retVal = new Date(date.valueOf());
  146. retVal.setDate(date.getDate() + days);
  147. return retVal;
  148. };
  149. var _unOps = {};
  150. _unOps['+'] = function(arg) { return +arg; };
  151. _unOps['-'] = function(arg) { return -arg; };
  152. _unOps['!'] = function(arg) { return !_getBool(arg); };
  153. _stoHandlers.binOp = function(sto, scope, eventInfo) {
  154. var left = _evaluateSTO(sto.leftSTO, scope, eventInfo);
  155. var right = _evaluateSTO(sto.rightSTO, scope, eventInfo);
  156. return _binOps[sto.op](left, right);
  157. };
  158. _stoHandlers.unOp = function(sto, scope, eventInfo) {
  159. var input = _evaluateSTO(sto.inputSTO, scope, eventInfo);
  160. return _unOps[sto.op](input);
  161. };
  162. var _getBool = function(val) {
  163. var lowerVal = val.toLowerCase ? val.toLowerCase() : val;
  164. return lowerVal == "false" ? false : lowerVal == "true" ? true : val;
  165. };
  166. $ax.getBool = _getBool;
  167. var _evaluateSTO = function(sto, scope, eventInfo) {
  168. if(sto.sto == 'error') return undefined;
  169. return castSto(_stoHandlers[sto.sto](sto, scope, eventInfo), sto);
  170. };
  171. $ax.evaluateSTO = _evaluateSTO;
  172. var castSto = function(val, sto) {
  173. var type = sto.computedType || sto.desiredType;
  174. if(type == 'string') val = String(val);
  175. else if(type == 'date' && !(val instanceof Date)) val = new Date(val);
  176. else if(type == 'int' || type == 'float') val = Number(val);
  177. else if(type == 'bool') val = Boolean(val);
  178. return val;
  179. };
  180. });