// ******* Expr MANAGER ******** //
$axure.internal(function($ax) {
var _expr = $ax.expr = {};
var _binOpHandlers = {
'&&': function(left, right) { return $ax.getBool(left) && $ax.getBool(right); },
'||': function(left, right) { return $ax.getBool(left) || $ax.getBool(right); },
'==': function(left, right) { return isEqual(left, right); },
'!=': function(left, right) { return !isEqual(left, right); },
'>': function(left, right) { return left > Number(right); },
'<': function(left, right) { return left < Number(right); },
'>=': function(left, right) { return left >= Number(right); },
'<=': function(left, right) { return left <= Number(right); }
};
var isEqual = function(left, right) {
if(left instanceof Object && right instanceof Object) {
var prop;
// Go through all of lefts properties and compare them to rights.
for(prop in left) {
if(!left.hasOwnProperty(prop)) continue;
// If left has a property that the right doesn't they are not equal.
if(!right.hasOwnProperty(prop)) return false;
// If any of their properties are not equal, they are not equal.
if(!isEqual(left[prop], right[prop])) return false;
}
for(prop in right) {
// final check to make sure right doesn't have some extra properties that make them not equal.
if(left.hasOwnProperty(prop) != right.hasOwnProperty(prop)) return false;
}
return true;
}
return $ax.getBool(left) == $ax.getBool(right);
};
var _exprHandlers = {};
_exprHandlers.array = function(expr, eventInfo) {
var returnVal = [];
for(var i = 0; i < expr.items.length; i++) {
returnVal[returnVal.length] = _evaluateExpr(expr.items[i], eventInfo);
}
return returnVal;
};
_exprHandlers.binaryOp = function(expr, eventInfo) {
var left = expr.leftExpr && _evaluateExpr(expr.leftExpr, eventInfo);
var right = expr.rightExpr && _evaluateExpr(expr.rightExpr, eventInfo);
if(left == undefined || right == undefined) return false;
return _binOpHandlers[expr.op](left, right);
};
_exprHandlers.block = function(expr, eventInfo) {
var subExprs = expr.subExprs;
for(var i = 0; i < subExprs.length; i++) {
_evaluateExpr(subExprs[i], eventInfo); //ignore the result
}
};
_exprHandlers.booleanLiteral = function(expr) {
return expr.value;
};
_exprHandlers.nullLiteral = function() { return null; };
_exprHandlers.pathLiteral = function(expr, eventInfo) {
if(expr.isThis) return [eventInfo.srcElement];
if(expr.isFocused && window.lastFocusedControl) {
window.lastFocusedControl.focus();
return [window.lastFocusedControl.getAttribute('id')];
}
if(expr.isTarget) return [eventInfo.targetElement];
return $ax.getElementIdsFromPath(expr.value, eventInfo);
};
_exprHandlers.panelDiagramLiteral = function(expr, eventInfo) {
var elementIds = $ax.getElementIdsFromPath(expr.panelPath, eventInfo);
var elementIdsWithSuffix = [];
var suffix = '_state' + expr.panelIndex;
for(var i = 0; i < elementIds.length; i++) {
elementIdsWithSuffix[i] = $ax.repeater.applySuffixToElementId(elementIds[i], suffix);
}
return $jobj(elementIdsWithSuffix).data('label');
};
_exprHandlers.fcall = function(expr, eventInfo) {
var oldTarget = eventInfo.targetElement;
var targets = [];
var fcallArgs = [];
var exprArgs = expr.arguments;
for(var i = 0; i < expr.arguments.length; i++) {
var exprArg = exprArgs[i];
var fcallArg = '';
if(targets.length) {
for(var j = 0; j < targets.length; j++) {
eventInfo.targetElement = targets[j];
fcallArgs[j][i] = _evaluateExpr(exprArg, eventInfo);
}
} else {
fcallArg = _evaluateExpr(exprArg, eventInfo);
fcallArgs[i] = fcallArg;
}
// We do support null exprArgs...
// 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
if(exprArg && exprArg.exprType == 'pathLiteral') {
targets = fcallArg;
// fcallArgs is now an array of an array of args
for(j = 0; j < targets.length; j++) fcallArgs[j] = [[fcallArg[j]]];
}
}
// we want to preserve the target element from outside this function.
eventInfo.targetElement = oldTarget;
var retval = '';
if(targets.length) {
// Go backwards so retval is the first item.
for(i = targets.length - 1; i >= 0; i--) {
var args = fcallArgs[i];
// Add event info to the end
args[args.length] = eventInfo;
retval = _exprFunctions[expr.functionName].apply(this, args);
}
} else fcallArgs[fcallArgs.length] = eventInfo;
return targets.length ? retval : _exprFunctions[expr.functionName].apply(this, fcallArgs);
};
_exprHandlers.globalVariableLiteral = function(expr) {
return expr.variableName;
};
_exprHandlers.keyPressLiteral = function(expr) {
var keyInfo = {};
keyInfo.keyCode = expr.keyCode;
keyInfo.ctrl = expr.ctrl;
keyInfo.alt = expr.alt;
keyInfo.shift = expr.shift;
return keyInfo;
};
_exprHandlers.adaptiveViewLiteral = function(expr) {
return expr.id;
};
var _substituteSTOs = function(expr, eventInfo) {
//first evaluate the local variables
var scope = {};
for(var varName in expr.localVariables) {
scope[varName] = $ax.expr.evaluateExpr(expr.localVariables[varName], eventInfo);
}
// TODO: [ben] Date and data object (obj with info for url or image) both need to return non-strings.
var i = 0;
var retval;
var retvalString = expr.value.replace(/\[\[(?!\[)(.*?)\]\](?=\]*)/g, function(match) {
var sto = expr.stos[i++];
if(sto.sto == 'error') return match;
try {
var result = $ax.evaluateSTO(sto, scope, eventInfo);
} catch(e) {
return match;
}
if((result instanceof Object) && i == 1 && expr.value.substring(0, 2) == '[[' &&
expr.value.substring(expr.value.length - 2) == ']]') {
// If the result was an object, this was the first result, and the whole thing was this expresion.
retval = result;
}
return ((result instanceof Object) && (result.label || result.text)) || result;
});
// If more than one group returned, the object is not valid
if(i != 1) retval = false;
return retval || retvalString;
};
_exprHandlers.htmlLiteral = function(expr, eventInfo) {
return _substituteSTOs(expr, eventInfo);
};
_exprHandlers.stringLiteral = function(expr, eventInfo) {
return _substituteSTOs(expr, eventInfo);
};
var _exprFunctions = {};
_exprFunctions.SetCheckState = function(elementIds, value) {
var toggle = value == 'toggle';
var boolValue = Boolean(value) && value != 'false';
for(var i = 0; i < elementIds.length; i++) {
var query = $ax('#' + elementIds[i]);
query.selected(toggle ? !query.selected() : boolValue);
}
};
_exprFunctions.SetSelectedOption = function(elementIds, value) {
for(var i = 0; i < elementIds.length; i++) {
var elementId = elementIds[i];
var obj = $jobj($ax.INPUT(elementId));
if(obj.val() == value) return;
obj.val(value);
if($ax.event.HasSelectionChanged($ax.getObjectFromElementId(elementId))) $ax.event.raiseSyntheticEvent(elementId, 'onSelectionChange');
}
};
_exprFunctions.SetGlobalVariableValue = function(varName, value) {
$ax.globalVariableProvider.setVariableValue(varName, value);
};
_exprFunctions.SetWidgetFormText = function(elementIds, value) {
for(var i = 0; i < elementIds.length; i++) {
var elementId = elementIds[i];
var inputId = $ax.repeater.applySuffixToElementId(elementId, '_input');
var obj = $jobj(inputId);
if(obj.val() == value || (value == '' && $ax.placeholderManager.isActive(elementId))) return;
obj.val(value);
$ax.placeholderManager.updatePlaceholder(elementId, !value);
if($ax.event.HasTextChanged($ax.getObjectFromElementId(elementId))) $ax.event.TryFireTextChanged(elementId);
}
};
_exprFunctions.SetFocusedWidgetText = function(elementId, value) {
if(window.lastFocusedControl) {
window.lastFocusedControl.focus();
window.lastFocusedControl.value = value;
}
};
_exprFunctions.GetRtfElementHeight = function(rtfElement) {
if(rtfElement.innerHTML == '') rtfElement.innerHTML = ' ';
return rtfElement.offsetHeight;
};
_exprFunctions.SetWidgetRichText = function(ids, value, plain) {
// Converts dates, widgetinfo, and the like to strings.
value = _exprFunctions.ToString(value);
//Replace any newlines with line breaks
value = value.replace(/\r\n/g, '
').replace(/\n/g, '
');
for(var i = 0; i < ids.length; i++) {
var id = ids[i];
// If calling this on button shape, get the id of the rich text panel inside instead
var type = $obj(id).type;
if(type != 'richTextPanel' && type != 'hyperlink') {
id = $jobj(id).find('.text')[0].id;
}
var element = window.document.getElementById(id);
$ax.visibility.SetVisible(element, true);
var spans = $jobj(id).find('span');
if(plain) {
// Wrap in span and p, style them accordingly.
var span = $('');
if(spans.length > 0) {
span.attr('style', $(spans[0]).attr('style'));
span.attr('id', $(spans[0]).attr('id'));
}
span.html(value);
var p = $('