expr.js.orig 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // ******* Expr MANAGER ******** //
  2. $axure.internal(function($ax) {
  3. var _expr = $ax.expr = {};
  4. var _binOpHandlers = {
  5. '&&': function(left, right) { return $ax.getBool(left) && $ax.getBool(right); },
  6. '||': function(left, right) { return $ax.getBool(left) || $ax.getBool(right); },
  7. '==': function(left, right) { return isEqual(left, right); },
  8. '!=': function(left, right) { return !isEqual(left, right); },
  9. '>': function(left, right) { return left > Number(right); },
  10. '<': function(left, right) { return left < Number(right); },
  11. '>=': function(left, right) { return left >= Number(right); },
  12. '<=': function(left, right) { return left <= Number(right); }
  13. };
  14. var isEqual = function(left, right) {
  15. if(left instanceof Object && right instanceof Object) {
  16. var prop;
  17. // Go through all of lefts properties and compare them to rights.
  18. for(prop in left) {
  19. if(!left.hasOwnProperty(prop)) continue;
  20. // If left has a property that the right doesn't they are not equal.
  21. if(!right.hasOwnProperty(prop)) return false;
  22. // If any of their properties are not equal, they are not equal.
  23. if(!isEqual(left[prop], right[prop])) return false;
  24. }
  25. for(prop in right) {
  26. // final check to make sure right doesn't have some extra properties that make them not equal.
  27. if(left.hasOwnProperty(prop) != right.hasOwnProperty(prop)) return false;
  28. }
  29. return true;
  30. }
  31. return $ax.getBool(left) == $ax.getBool(right);
  32. };
  33. var _exprHandlers = {};
  34. _exprHandlers.array = function(expr, eventInfo) {
  35. var returnVal = [];
  36. for(var i = 0; i < expr.items.length; i++) {
  37. returnVal[returnVal.length] = _evaluateExpr(expr.items[i], eventInfo);
  38. }
  39. return returnVal;
  40. };
  41. _exprHandlers.binaryOp = function(expr, eventInfo) {
  42. var left = expr.leftExpr && _evaluateExpr(expr.leftExpr, eventInfo);
  43. var right = expr.rightExpr && _evaluateExpr(expr.rightExpr, eventInfo);
  44. if(left == undefined || right == undefined) return false;
  45. return _binOpHandlers[expr.op](left, right);
  46. };
  47. _exprHandlers.block = function(expr, eventInfo) {
  48. var subExprs = expr.subExprs;
  49. for(var i = 0; i < subExprs.length; i++) {
  50. _evaluateExpr(subExprs[i], eventInfo); //ignore the result
  51. }
  52. };
  53. _exprHandlers.booleanLiteral = function(expr) {
  54. return expr.value;
  55. };
  56. _exprHandlers.nullLiteral = function() { return null; };
  57. _exprHandlers.pathLiteral = function(expr, eventInfo) {
  58. if(expr.isThis) return [eventInfo.srcElement];
  59. if(expr.isFocused && window.lastFocusedControl) {
  60. window.lastFocusedControl.focus();
  61. return [window.lastFocusedControl.getAttribute('id')];
  62. }
  63. if(expr.isTarget) return [eventInfo.targetElement];
  64. return $ax.getElementIdsFromPath(expr.value, eventInfo);
  65. };
  66. _exprHandlers.panelDiagramLiteral = function(expr, eventInfo) {
  67. var elementIds = $ax.getElementIdsFromPath(expr.panelPath, eventInfo);
  68. var elementIdsWithSuffix = [];
  69. var suffix = '_state' + expr.panelIndex;
  70. for(var i = 0; i < elementIds.length; i++) {
  71. elementIdsWithSuffix[i] = $ax.repeater.applySuffixToElementId(elementIds[i], suffix);
  72. }
  73. return $jobj(elementIdsWithSuffix).data('label');
  74. };
  75. _exprHandlers.fcall = function(expr, eventInfo) {
  76. var oldTarget = eventInfo.targetElement;
  77. var targets = [];
  78. var fcallArgs = [];
  79. var exprArgs = expr.arguments;
  80. for(var i = 0; i < expr.arguments.length; i++) {
  81. var exprArg = exprArgs[i];
  82. var fcallArg = '';
  83. if(targets.length) {
  84. for(var j = 0; j < targets.length; j++) {
  85. eventInfo.targetElement = targets[j];
  86. fcallArgs[j][i] = _evaluateExpr(exprArg, eventInfo);
  87. }
  88. } else {
  89. fcallArg = _evaluateExpr(exprArg, eventInfo);
  90. fcallArgs[i] = fcallArg;
  91. }
  92. // We do support null exprArgs...
  93. // TODO: This makes 2 assumptions that may change in the future. 1. The pathLiteral is the always the first arg. 2. there is always only 1 pathLiteral
  94. if(exprArg && exprArg.exprType == 'pathLiteral') {
  95. targets = fcallArg;
  96. // fcallArgs is now an array of an array of args
  97. for(j = 0; j < targets.length; j++) fcallArgs[j] = [[fcallArg[j]]];
  98. }
  99. }
  100. // we want to preserve the target element from outside this function.
  101. eventInfo.targetElement = oldTarget;
  102. var retval = '';
  103. if(targets.length) {
  104. // Go backwards so retval is the first item.
  105. for(i = targets.length - 1; i >= 0; i--) {
  106. var args = fcallArgs[i];
  107. // Add event info to the end
  108. args[args.length] = eventInfo;
  109. retval = _exprFunctions[expr.functionName].apply(this, args);
  110. }
  111. } else fcallArgs[fcallArgs.length] = eventInfo;
  112. return targets.length ? retval : _exprFunctions[expr.functionName].apply(this, fcallArgs);
  113. };
  114. _exprHandlers.globalVariableLiteral = function(expr) {
  115. return expr.variableName;
  116. };
  117. _exprHandlers.keyPressLiteral = function(expr) {
  118. var keyInfo = {};
  119. keyInfo.keyCode = expr.keyCode;
  120. keyInfo.ctrl = expr.ctrl;
  121. keyInfo.alt = expr.alt;
  122. keyInfo.shift = expr.shift;
  123. return keyInfo;
  124. };
  125. _exprHandlers.adaptiveViewLiteral = function(expr) {
  126. return expr.id;
  127. };
  128. var _substituteSTOs = function(expr, eventInfo) {
  129. //first evaluate the local variables
  130. var scope = {};
  131. for(var varName in expr.localVariables) {
  132. scope[varName] = $ax.expr.evaluateExpr(expr.localVariables[varName], eventInfo);
  133. }
  134. // TODO: [ben] Date and data object (obj with info for url or image) both need to return non-strings.
  135. var i = 0;
  136. var retval;
  137. var retvalString = expr.value.replace(/\[\[(?!\[)(.*?)\]\](?=\]*)/g, function(match) {
  138. var sto = expr.stos[i++];
  139. if(sto.sto == 'error') return match;
  140. try {
  141. var result = $ax.evaluateSTO(sto, scope, eventInfo);
  142. } catch(e) {
  143. return match;
  144. }
  145. if((result instanceof Object) && i == 1 && expr.value.substring(0, 2) == '[[' &&
  146. expr.value.substring(expr.value.length - 2) == ']]') {
  147. // If the result was an object, this was the first result, and the whole thing was this expresion.
  148. retval = result;
  149. }
  150. return ((result instanceof Object) && (result.label || result.text)) || result;
  151. });
  152. // If more than one group returned, the object is not valid
  153. if(i != 1) retval = false;
  154. return retval || retvalString;
  155. };
  156. _exprHandlers.htmlLiteral = function(expr, eventInfo) {
  157. return _substituteSTOs(expr, eventInfo);
  158. };
  159. _exprHandlers.stringLiteral = function(expr, eventInfo) {
  160. return _substituteSTOs(expr, eventInfo);
  161. };
  162. var _exprFunctions = {};
  163. _exprFunctions.SetCheckState = function(elementIds, value) {
  164. var toggle = value == 'toggle';
  165. var boolValue = Boolean(value) && value != 'false';
  166. for(var i = 0; i < elementIds.length; i++) {
  167. var query = $ax('#' + elementIds[i]);
  168. query.selected(toggle ? !query.selected() : boolValue);
  169. }
  170. };
  171. _exprFunctions.SetSelectedOption = function(elementIds, value) {
  172. for(var i = 0; i < elementIds.length; i++) {
  173. var elementId = elementIds[i];
  174. var obj = $jobj($ax.INPUT(elementId));
  175. if(obj.val() == value) return;
  176. obj.val(value);
  177. if($ax.event.HasSelectionChanged($ax.getObjectFromElementId(elementId))) $ax.event.raiseSyntheticEvent(elementId, 'onSelectionChange');
  178. }
  179. };
  180. _exprFunctions.SetGlobalVariableValue = function(varName, value) {
  181. $ax.globalVariableProvider.setVariableValue(varName, value);
  182. };
  183. _exprFunctions.SetWidgetFormText = function(elementIds, value) {
  184. for(var i = 0; i < elementIds.length; i++) {
  185. var elementId = elementIds[i];
  186. var inputId = $ax.repeater.applySuffixToElementId(elementId, '_input');
  187. var obj = $jobj(inputId);
  188. if(obj.val() == value || (value == '' && $ax.placeholderManager.isActive(elementId))) return;
  189. obj.val(value);
  190. $ax.placeholderManager.updatePlaceholder(elementId, !value);
  191. if($ax.event.HasTextChanged($ax.getObjectFromElementId(elementId))) $ax.event.TryFireTextChanged(elementId);
  192. }
  193. };
  194. _exprFunctions.SetFocusedWidgetText = function(elementId, value) {
  195. if(window.lastFocusedControl) {
  196. window.lastFocusedControl.focus();
  197. window.lastFocusedControl.value = value;
  198. }
  199. };
  200. _exprFunctions.GetRtfElementHeight = function(rtfElement) {
  201. if(rtfElement.innerHTML == '') rtfElement.innerHTML = '&nbsp;';
  202. return rtfElement.offsetHeight;
  203. };
  204. _exprFunctions.SetWidgetRichText = function(ids, value, plain) {
  205. // Converts dates, widgetinfo, and the like to strings.
  206. value = _exprFunctions.ToString(value);
  207. //Replace any newlines with line breaks
  208. value = value.replace(/\r\n/g, '<br>').replace(/\n/g, '<br>');
  209. for(var i = 0; i < ids.length; i++) {
  210. var id = ids[i];
  211. // If calling this on button shape, get the id of the rich text panel inside instead
  212. var type = $obj(id).type;
  213. if(type != 'richTextPanel' && type != 'hyperlink') {
  214. id = $jobj(id).find('.text')[0].id;
  215. }
  216. var element = window.document.getElementById(id);
  217. $ax.visibility.SetVisible(element, true);
  218. var spans = $jobj(id).find('span');
  219. if(plain) {
  220. // Wrap in span and p, style them accordingly.
  221. var span = $('<span></span>');
  222. if(spans.length > 0) {
  223. span.attr('style', $(spans[0]).attr('style'));
  224. span.attr('id', $(spans[0]).attr('id'));
  225. }
  226. span.html(value);
  227. var p = $('<p></p>');
  228. var ps = $jobj(id).find('p');
  229. if(ps.length > 0) {
  230. p.attr('style', $(ps[0]).attr('style'));
  231. p.attr('id', $(ps[0]).attr('id'));
  232. }
  233. p.append(span);
  234. value = $('<div></div>').append(p).html();
  235. }
  236. $ax.style.transformTextWithVerticalAlignment(id, function() {
  237. element.innerHTML = value;
  238. });
  239. if(!plain) $ax.style.CacheOriginalText(id, true);
  240. }
  241. };
  242. _exprFunctions.GetCheckState = function(ids) {
  243. return $ax('#' + ids[0]).selected();
  244. };
  245. _exprFunctions.GetSelectedOption = function(ids) {
  246. return $jobj($ax.INPUT(ids[0]))[0].value;
  247. };
  248. _exprFunctions.GetNum = function(str) {
  249. //Setting a GlobalVariable to some blank text then setting a widget to the value of that variable would result in 0 not ""
  250. //I have fixed this another way so commenting this should be fine now
  251. //if (!str) return "";
  252. return isNaN(str) ? str : Number(str);
  253. };
  254. _exprFunctions.GetGlobalVariableValue = function(id) {
  255. return $ax.globalVariableProvider.getVariableValue(id);
  256. };
  257. _exprFunctions.GetGlobalVariableLength = function(id) {
  258. return _exprFunctions.GetGlobalVariableValue(id).length;
  259. };
  260. _exprFunctions.GetWidgetText = function(ids) {
  261. if($ax.placeholderManager.isActive(ids[0])) return '';
  262. var input = $ax.INPUT(ids[0]);
  263. return $ax('#' + ($jobj(input).length ? input : ids[0])).text();
  264. };
  265. _exprFunctions.GetFocusedWidgetText = function() {
  266. if(window.lastFocusedControl) {
  267. return window.lastFocusedControl.value;
  268. } else {
  269. return "";
  270. }
  271. };
  272. _exprFunctions.GetWidgetValueLength = function(ids) {
  273. var id = ids[0];
  274. if(!id) return undefined;
  275. if($ax.placeholderManager.isActive(id)) return 0;
  276. var obj = $jobj($ax.INPUT(id));
  277. if(!obj.length) obj = $jobj(id);
  278. return obj[0].value.length;
  279. };
  280. _exprFunctions.GetPanelState = function(ids) {
  281. var id = ids[0];
  282. if(!id) return undefined;
  283. var stateId = $ax.visibility.GetPanelState(id);
  284. return stateId && $jobj(stateId).data('label');
  285. };
  286. _exprFunctions.GetWidgetVisibility = function(ids) {
  287. var id = ids[0];
  288. if(!id) return undefined;
  289. return $ax.visibility.IsIdVisible(id);
  290. };
  291. // ***************** Validation Functions ***************** //
  292. _exprFunctions.IsValueAlpha = function(val) {
  293. var isAlphaRegex = new RegExp("^[a-z\\s]+$", "gi");
  294. return isAlphaRegex.test(val);
  295. };
  296. _exprFunctions.IsValueNumeric = function(val) {
  297. var isNumericRegex = new RegExp("^[0-9,\\.\\s]+$", "gi");
  298. return isNumericRegex.test(val);
  299. };
  300. _exprFunctions.IsValueAlphaNumeric = function(val) {
  301. var isAlphaNumericRegex = new RegExp("^[0-9a-z\\s]+$", "gi");
  302. return isAlphaNumericRegex.test(val);
  303. };
  304. _exprFunctions.IsValueOneOf = function(val, values) {
  305. for(var i = 0; i < values.length; i++) {
  306. var option = values[i];
  307. if(val == option) return true;
  308. }
  309. //by default, return false
  310. return false;
  311. };
  312. _exprFunctions.IsValueNotAlpha = function(val) {
  313. return !_exprFunctions.IsValueAlpha(val);
  314. };
  315. _exprFunctions.IsValueNotNumeric = function(val) {
  316. return !_exprFunctions.IsValueNumeric(val);
  317. };
  318. _exprFunctions.IsValueNotAlphaNumeric = function(val) {
  319. return !_exprFunctions.IsValueAlphaNumeric(val);
  320. };
  321. _exprFunctions.IsValueNotOneOf = function(val, values) {
  322. return !_exprFunctions.IsValueOneOf(val, values);
  323. };
  324. _exprFunctions.GetKeyPressed = function(eventInfo) {
  325. return eventInfo.keyInfo;
  326. };
  327. _exprFunctions.GetCursorRectangles = function() {
  328. var rects = new Object();
  329. rects.lastRect = new $ax.drag.Rectangle($ax.lastMouseLocation.x, $ax.lastMouseLocation.y, 1, 1);
  330. rects.currentRect = new $ax.drag.Rectangle($ax.mouseLocation.x, $ax.mouseLocation.y, 1, 1);
  331. return rects;
  332. };
  333. _exprFunctions.GetWidgetRectangles = function(elementId, eventInfo) {
  334. var rects = new Object();
  335. var jObj = $jobj(elementId);
  336. if(jObj.length == 0) {
  337. rects.lastRect = rects.currentRect = new $ax.drag.Rectangle(-1, -1, -1, -1);
  338. return rects;
  339. }
  340. rects.lastRect = new $ax.drag.Rectangle(
  341. $ax.legacy.getAbsoluteLeft(jObj),
  342. $ax.legacy.getAbsoluteTop(jObj),
  343. jObj.width(),
  344. jObj.height());
  345. rects.currentRect = rects.lastRect;
  346. return rects;
  347. };
  348. _exprFunctions.GetWidget = function(elementId) {
  349. return $ax.getWidgetInfo(elementId[0]);
  350. };
  351. _exprFunctions.GetAdaptiveView = function() {
  352. return $ax.adaptive.currentViewId || '';
  353. };
  354. _exprFunctions.IsEntering = function(movingRects, targetRects) {
  355. return !movingRects.lastRect.IntersectsWith(targetRects.currentRect) && movingRects.currentRect.IntersectsWith(targetRects.currentRect);
  356. };
  357. _exprFunctions.IsLeaving = function(movingRects, targetRects) {
  358. return movingRects.lastRect.IntersectsWith(targetRects.currentRect) && !movingRects.currentRect.IntersectsWith(targetRects.currentRect);
  359. };
  360. var _IsOver = _exprFunctions.IsOver = function(movingRects, targetRects) {
  361. return movingRects.currentRect.IntersectsWith(targetRects.currentRect);
  362. };
  363. _exprFunctions.IsNotOver = function(movingRects, targetRects) {
  364. return !_IsOver(movingRects, targetRects);
  365. };
  366. _exprFunctions.ValueContains = function(inputString, value) {
  367. return inputString.indexOf(value) > -1;
  368. };
  369. _exprFunctions.ValueNotContains = function(inputString, value) {
  370. return !_exprFunctions.ValueContains(inputString, value);
  371. };
  372. _exprFunctions.ToString = function(value) {
  373. if(value.isWidget) {
  374. return value.Text;
  375. }
  376. return String(value);
  377. };
  378. var _evaluateExpr = $ax.expr.evaluateExpr = function(expr, eventInfo, toString) {
  379. if(expr === undefined || expr === null) return undefined;
  380. var result = _exprHandlers[expr.exprType](expr, eventInfo);
  381. return toString ? _exprFunctions.ToString(result) : result;
  382. };
  383. });