mframejs
Version:
simple framework
342 lines • 13.7 kB
JavaScript
var _a;
import { ContainerValueConverters, ContainerClasses } from '../../container/exported';
import { getCorrectContext } from '../contextOfObject';
import { setValue } from '../setValue';
let fromView;
const Traverser = (_a = class {
},
_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) {
let 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) {
let 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:
}
let curObj;
let curCtx = ctx;
if (ctx.__bindingContext) {
curCtx = ctx.$context;
}
let 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 = 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) {
let second;
let value;
let first;
switch (true) {
case ast.assignment === true:
try {
if (ast.first.arity === 'variable') {
const $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 {
let done = false;
let astFirst = ast.first;
const x = [];
while (!done) {
if (astFirst.second.arity !== 'binary') {
x.push(astFirst.second.value);
}
else {
x.push(`[${Traverser.checkArity(astFirst.second, ctx, mainctx)}]`);
}
if (astFirst.first.arity === 'variable') {
x.push(astFirst.first.value);
done = true;
}
else {
astFirst = astFirst.first;
}
}
x.reverse();
const objectString = x.join('.').split('.[').join('[');
if (ast.value === '=') {
setValue(ctx, objectString, Traverser.checkArity(ast.second, ctx, mainctx));
}
else {
first = Traverser.checkArity(ast.first, ctx, mainctx);
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 === '{':
const x = {};
ast.first.forEach((y) => {
x[y.key] = Traverser.checkArity(y, ctx, mainctx);
});
value = x;
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:
const results = [];
ast.second.forEach(function (res) {
results.push(Traverser.evaluate(res, mainctx));
});
let curCtx = ctx.$context;
let valueTrigger;
if (ast.first.arity === 'binary') {
valueTrigger = Traverser.binary(ast.first, ctx, mainctx);
}
else {
valueTrigger = curCtx[ast.first.value];
}
curCtx = getCorrectContext(ast.first.value, ctx);
if (curCtx && curCtx.$context && typeof curCtx.$context[ast.first.value] === 'function') {
valueTrigger = curCtx.$context[ast.first.value];
}
if (valueTrigger) {
const typeObj = Traverser.binary({ first: ast.first.first, checkArray: true }, ctx, mainctx);
if (Array.isArray(typeObj)) {
value = valueTrigger.apply(typeObj, results);
}
else {
value = valueTrigger.apply(curCtx.$context, results);
}
}
else {
console.warn(`method does not exist:${ast.first.value}`);
}
}
return value;
},
_a.ternary = function (ast, ctx, mainctx) {
const first = Traverser.checkArity(ast.first, ctx, mainctx);
const second = Traverser.checkArity(ast.second, ctx, mainctx);
const third = Traverser.checkArity(ast.third, ctx, mainctx);
const value = first ? second : third;
return value;
},
_a.valueConverter = function (ast, ctx, mainctx) {
let result;
const args = [];
ast.args.forEach((arg) => {
args.push(Traverser.checkArity(arg, ctx, mainctx));
});
const valueConverterExist = ContainerValueConverters.findConverter(ast.value);
let _class;
if (valueConverterExist) {
_class = 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;
const astArray = Array.isArray(astInput) ? astInput : [astInput];
const returnArray = [];
for (let i = 0; i < astArray.length; i++) {
const 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) {
let result;
let args = [];
ast.args.forEach((arg, i) => {
if (i > 0) {
args.push(Traverser.checkArity(arg, ctx, mainctx));
}
});
args = [value, ...args];
const valueConverterExist = ContainerValueConverters.findConverter(ast.value);
let _class;
if (valueConverterExist) {
_class = 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) {
const astArray = Array.isArray(astInput) ? astInput : [astInput];
const returnArray = [];
fromView = true;
let valueConverted = false;
for (let i = 0; i < astArray.length; i++) {
let result;
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) {
const astArray = Array.isArray(ast) ? ast : [ast];
const returnArray = [];
for (let i = 0; i < astArray.length; i++) {
let result;
switch (true) {
case astArray[i].arity === 'behavior':
result = {
name: astArray[i].value,
args: astArray[i].args.map((a) => a.value)
};
break;
}
if (result) {
returnArray.push(result);
}
}
return returnArray;
},
_a);
export const traverseAST = function (ast, ctx) {
return Traverser.evaluate(ast, ctx);
};
export const valueConvert = function (ast, ctx, value) {
return Traverser.evaluateConverterFromViewOnly(ast, value, ctx, ctx);
};
export const getBehavior = function (ast) {
return Traverser.getBehaviorFunctions(ast);
};
//# sourceMappingURL=traverseAst.js.map