mframejs
Version:
simple framework
124 lines • 5.47 kB
JavaScript
import { tokenize } from './ast/tokenize';
import { generateAST } from './ast/generateAst';
import { ClassPropertyObserverCreator } from './property/classPropertyObserverCreator';
import { Cache } from '../utils/exported';
import { CONSTANTS } from '../interface/exported';
import { getCorrectContext } from './contextOfObject';
export function createBindingExpression(expression, _class, setterClass) {
let attributesValues = [];
let ast;
if (expression) {
if (!setterClass.attributesValues) {
let tokens;
let tokenValues = [];
let tokenObject = [];
if (Cache.expressionMap.has(expression)) {
const cacheX = Cache.expressionMap.get(expression);
ast = cacheX.ast;
tokens = cacheX.tokens;
tokenValues = cacheX.tokenValues;
tokenObject = cacheX.tokenObject;
}
else {
tokens = tokenize(expression);
ast = generateAST(tokens);
let curVal = null;
let curObject = [];
for (let next = 0; next < tokens.length; next++) {
const tok = tokens[next];
if (tok.type === 'variable') {
if (!curVal) {
curVal = tok.value;
curObject.push(tok.value);
}
else {
curVal = curVal + tok.value;
curObject.push(tok.value);
}
}
else {
if (tok.type === 'operator' && tok.value === '.') {
if (curVal) {
curVal = curVal + '.';
}
}
else {
if (tok.type === 'operator'
&& tok.value === '['
&& tokens[next + 2]
&& tokens[next + 2].type === 'operator'
&& tokens[next + 2].value === ']') {
if (curVal) {
curVal = curVal + '[' + tokens[next + 1].value + ']';
}
next = next + 2;
}
else {
if (curVal) {
tokenValues.push(curVal);
curVal = null;
tokenObject.push(curObject);
curObject = [];
}
}
}
}
}
if (curVal) {
tokenValues.push(curVal);
tokenObject.push(curObject);
curObject = [];
curVal = null;
}
Cache.expressionMap.set(expression, {
ast: ast,
tokens: tokens,
tokenValues: tokenValues,
tokenObject: tokenObject
});
}
tokenValues.forEach((tok) => {
const newctx = getCorrectContext(tok, _class);
if (typeof tok === 'string'
&& tok[0] !== '$'
&& newctx
&& newctx.__bindingContext
&& newctx.$context.__proto__
&& newctx.$context.__proto__[CONSTANTS.META_COMPUTEDFROM]
&& newctx.$context.__proto__[CONSTANTS.META_COMPUTEDFROM][tok]) {
const computedFrom = newctx.$context.__proto__[CONSTANTS.META_COMPUTEDFROM];
computedFrom[tok].attributes.forEach((val) => {
if (attributesValues.indexOf(val) === -1) {
attributesValues.push(val);
}
});
}
else {
if (attributesValues.indexOf(tok) === -1) {
attributesValues.push(tok);
}
}
});
setterClass.setAst(ast);
}
else {
attributesValues = setterClass.attributesValues;
}
for (let i = 0; i < attributesValues.length; i++) {
const newctx = getCorrectContext(attributesValues[i], _class);
ClassPropertyObserverCreator.create(newctx || _class, attributesValues[i], setterClass);
}
setterClass.init();
setterClass.attributesValues = attributesValues;
}
}
export function removeBindingExpression(text, _class, handlerClass) {
if (text && handlerClass.attributesValues) {
const attributesValues = handlerClass.attributesValues;
for (let i = 0; i < attributesValues.length; i++) {
const newctx = getCorrectContext(attributesValues[i], _class);
ClassPropertyObserverCreator.remove(newctx || _class, attributesValues[i], handlerClass);
}
}
}
//# sourceMappingURL=createBindingExpression.js.map