mframejs
Version:
simple framework
346 lines • 14 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var _a;
var exported_1 = require("../../container/exported");
var contextOfObject_1 = require("../contextOfObject");
var setValue_1 = require("../setValue");
var fromView;
var Traverser = (_a = (function () {
function class_1() {
}
return class_1;
}()),
_a.operator = function (val, a, b) {
switch (val) {
case '+': return a + b;
case '-': return b ? a - b : -a;
case '*': return a * b;
case '!': return !a;
case '%': return a % b;
case '>': return a > b;
case '<': return a < b;
case '>=': return a >= b;
case '&&': return a && b;
case '||': return a || b;
case '<=': return a <= b;
case '+=': return a += b;
case '-=': return a -= b;
case '*=': return a *= b;
case '/=': return a /= b;
case '===': return a === b;
case '!==': return a !== b;
case '/': return a / b;
case '^': return a ^ b;
}
},
_a.checkArity = function (ast, ctx, mainctx) {
var value;
if (ast && ast.arity === 'literal') {
value = ast.value;
}
if (ast && ast.arity === 'variable') {
value = Traverser.variable(ast, ctx, mainctx);
}
if (ast && ast.arity === 'binary') {
value = Traverser.binary(ast, ctx, mainctx);
}
if (ast && ast.arity === 'unary') {
value = Traverser.binary(ast, ctx, mainctx);
}
if (ast && ast.arity === 'ternary') {
value = Traverser.ternary(ast, ctx, mainctx);
}
if (ast && ast.arity === 'valueConverter') {
value = Traverser.valueConverter(ast, ctx, mainctx);
}
return value;
},
_a.variable = function (ast, ctx, _mainctx) {
var result;
try {
switch (true) {
case ast.value === 'null' && ast.arity === 'variable':
return null;
case ast.value === 'undefined' && ast.arity === 'variable':
return undefined;
case ast.value === '$this' && ast.arity === 'variable' && ast.root:
return ctx.$context;
case ast.value === '$event' && ast.arity === 'variable' && ast.root:
return ctx.$event;
case ast.arity === 'variable' && typeof ast.value === 'string' && ast.value.toUpperCase() === 'TRUE':
return true;
case ast.arity === 'variable' && typeof ast.value === 'string' && ast.value.toUpperCase() === 'FALSE':
return false;
default:
}
var curObj = void 0;
var curCtx = ctx;
if (ctx.__bindingContext) {
curCtx = ctx.$context;
}
var localVariable = false;
if (ast.value && ast.value[0] === 'string' && ast.value[0][0] !== '$') {
localVariable = true;
}
if (localVariable) {
curObj = curCtx[ast.value];
}
else {
if (ast.root) {
curCtx = contextOfObject_1.getCorrectContext(ast.value, ctx);
if (curCtx.__bindingContext) {
curCtx = curCtx.$context;
}
}
curObj = curCtx[ast.value];
}
result = curObj;
}
catch (e) {
}
return result;
},
_a.binary = function (ast, ctx, mainctx) {
var second;
var value;
var first;
switch (true) {
case ast.assignment === true:
try {
if (ast.first.arity === 'variable') {
var $ctx = ctx.__bindingContext ? ctx.$context : ctx;
if (ast.value === '+=' || ast.value === '-=') {
$ctx[ast.first.value] =
Traverser.operator(ast.value, $ctx[ast.first.value], Traverser.checkArity(ast.second, ctx, mainctx));
}
else {
$ctx[ast.first.value] = Traverser.checkArity(ast.second, ctx, mainctx);
}
}
else {
var done = false;
var astFirst = ast.first;
var x_1 = [];
while (!done) {
if (astFirst.second.arity !== 'binary') {
x_1.push(astFirst.second.value);
}
else {
x_1.push("[" + Traverser.checkArity(astFirst.second, ctx, mainctx) + "]");
}
if (astFirst.first.arity === 'variable') {
x_1.push(astFirst.first.value);
done = true;
}
else {
astFirst = astFirst.first;
}
}
x_1.reverse();
var objectString = x_1.join('.').split('.[').join('[');
if (ast.value === '=') {
setValue_1.setValue(ctx, objectString, Traverser.checkArity(ast.second, ctx, mainctx));
}
else {
first = Traverser.checkArity(ast.first, ctx, mainctx);
setValue_1.setValue(ctx, objectString, Traverser.operator(ast.value, first, Traverser.checkArity(ast.second, ctx, mainctx)));
}
}
}
catch (x) {
console.error('only simple assigments are supported, sorry', ast);
}
break;
case ast.value === '{':
var x_2 = {};
ast.first.forEach(function (y) {
x_2[y.key] = Traverser.checkArity(y, ctx, mainctx);
});
value = x_2;
break;
case ast.checkArray:
first = Traverser.checkArity(ast.first, ctx, mainctx);
if (first && ast.contextReset) {
second = Traverser.checkArity(ast.second, mainctx, mainctx);
second = first[second];
}
else {
second = Traverser.checkArity(ast.second, first, mainctx);
}
value = second ? second : first;
break;
case ast.isObject:
first = Traverser.checkArity(ast.first, ctx, mainctx);
if (first && ast.contextReset) {
second = Traverser.checkArity(ast.second, mainctx, mainctx);
second = first[second];
}
else {
second = Traverser.checkArity(ast.second, first, mainctx);
}
value = second;
break;
case !ast.isFunction:
first = Traverser.checkArity(ast.first, ctx, mainctx);
second = Traverser.checkArity(ast.second, ctx, mainctx);
value = Traverser.operator(ast.value, first, second);
break;
default:
var results_1 = [];
ast.second.forEach(function (res) {
results_1.push(Traverser.evaluate(res, mainctx));
});
var curCtx = ctx.$context;
var valueTrigger = void 0;
if (ast.first.arity === 'binary') {
valueTrigger = Traverser.binary(ast.first, ctx, mainctx);
}
else {
valueTrigger = curCtx[ast.first.value];
}
curCtx = contextOfObject_1.getCorrectContext(ast.first.value, ctx);
if (curCtx && curCtx.$context && typeof curCtx.$context[ast.first.value] === 'function') {
valueTrigger = curCtx.$context[ast.first.value];
}
if (valueTrigger) {
var typeObj = Traverser.binary({ first: ast.first.first, checkArray: true }, ctx, mainctx);
if (Array.isArray(typeObj)) {
value = valueTrigger.apply(typeObj, results_1);
}
else {
value = valueTrigger.apply(curCtx.$context, results_1);
}
}
else {
console.warn("method does not exist:" + ast.first.value);
}
}
return value;
},
_a.ternary = function (ast, ctx, mainctx) {
var first = Traverser.checkArity(ast.first, ctx, mainctx);
var second = Traverser.checkArity(ast.second, ctx, mainctx);
var third = Traverser.checkArity(ast.third, ctx, mainctx);
var value = first ? second : third;
return value;
},
_a.valueConverter = function (ast, ctx, mainctx) {
var result;
var args = [];
ast.args.forEach(function (arg) {
args.push(Traverser.checkArity(arg, ctx, mainctx));
});
var valueConverterExist = exported_1.ContainerValueConverters.findConverter(ast.value);
var _class;
if (valueConverterExist) {
_class = exported_1.ContainerClasses.get(valueConverterExist);
try {
result = fromView ? _class.fromView.apply(ctx, args) : _class.toView.apply(ctx, args);
}
catch (e) {
console.warn('value converter failed:' + ast.value);
}
}
else {
console.warn('value converter does not exist:' + ast.value);
}
return result;
},
_a.evaluate = function (astInput, ctx) {
fromView = false;
var astArray = Array.isArray(astInput) ? astInput : [astInput];
var returnArray = [];
for (var i = 0; i < astArray.length; i++) {
var result = Traverser.checkArity(astArray[i], ctx, ctx);
returnArray.push(result);
}
if (returnArray.length > 1) {
return returnArray.join('');
}
else {
return returnArray[0];
}
},
_a.traverseOnlyValueConverter = function (value, ast, ctx, mainctx) {
var result;
var args = [];
ast.args.forEach(function (arg, i) {
if (i > 0) {
args.push(Traverser.checkArity(arg, ctx, mainctx));
}
});
args = [value].concat(args);
var valueConverterExist = exported_1.ContainerValueConverters.findConverter(ast.value);
var _class;
if (valueConverterExist) {
_class = exported_1.ContainerClasses.get(valueConverterExist);
try {
result = fromView ? _class.fromView.apply(ctx, args) : _class.toView.apply(ctx, args);
}
catch (e) {
console.warn('value converter failed:' + ast.value);
}
}
else {
console.warn('value converter does not exist:' + ast.value);
}
return result;
},
_a.evaluateConverterFromViewOnly = function (astInput, value, ctx, mainctx) {
var astArray = Array.isArray(astInput) ? astInput : [astInput];
var returnArray = [];
fromView = true;
var valueConverted = false;
for (var i = 0; i < astArray.length; i++) {
var result = void 0;
switch (true) {
case astArray[i].arity === 'valueConverter':
valueConverted = true;
result = Traverser.traverseOnlyValueConverter(value, astArray[i], ctx, mainctx);
break;
}
if (result) {
returnArray.push(result);
}
}
if (returnArray.length > 1) {
return returnArray.join('');
}
else {
if (valueConverted) {
return returnArray[0];
}
else {
return value;
}
}
},
_a.getBehaviorFunctions = function (ast) {
var astArray = Array.isArray(ast) ? ast : [ast];
var returnArray = [];
for (var i = 0; i < astArray.length; i++) {
var result = void 0;
switch (true) {
case astArray[i].arity === 'behavior':
result = {
name: astArray[i].value,
args: astArray[i].args.map(function (a) { return a.value; })
};
break;
}
if (result) {
returnArray.push(result);
}
}
return returnArray;
},
_a);
exports.traverseAST = function (ast, ctx) {
return Traverser.evaluate(ast, ctx);
};
exports.valueConvert = function (ast, ctx, value) {
return Traverser.evaluateConverterFromViewOnly(ast, value, ctx, ctx);
};
exports.getBehavior = function (ast) {
return Traverser.getBehaviorFunctions(ast);
};
//# sourceMappingURL=traverseAst.js.map