sbis3-node-ws
Version:
Модуль позволяет использовать ядро интерфейсного фреймворка sbis3-ws(`core.js`) и модуль работы с данными (`Source.js`) в приложении на nodejs.
133 lines (125 loc) • 5.55 kB
JavaScript
define('js!Core/tmpl/js/astModules/for',
[
'js!Core/tmpl/js/helpers/State',
'js!Core/tmpl/js/helpers/utils',
'js!Core/tmpl/js/helpers/processExpressions',
'js!Core/tmpl/js/jison/beforejs',
'js!Core/tmpl/js/helpers/scopes',
'js!Core/tmpl/js/helpers/errorHandling'
],
function (State, utils, processExpressions, beforejs, scopes, errorHandling) {
'use strict';
var forM = {
parse: function forParse(tag) {
function resolveStatement() {
var state = State.make(),
concreteSourceStrings = {
splittingKey: ' in ',
key: ' as '
},
forStampArguments,
source = '',
findForAllArguments = function forFindAllArguments(value, main) {
var crStringArray = value.split(concreteSourceStrings.key);
if (crStringArray.length > 1) {
return {
key: crStringArray[0],
value: crStringArray[1],
main: main
};
}
return {
key: undefined,
value: crStringArray[0],
main: main
};
},
fromAttr = tag.attribs.hasOwnProperty('for');
try {
if (fromAttr) {
source = utils.clone(tag.attribs.for);
} else {
source = tag.attribs.data;
}
forStampArguments = source.split(concreteSourceStrings.splittingKey);
tag.forSource = findForAllArguments(forStampArguments[0], beforejs.parse(forStampArguments[1]));
tag.attribs = this._traverseTagAttributes(tag.attribs);
} catch (err) {
errorHandling('Wrong arguments in for statement ' + tag.raw, this.filename);
}
this.traversingAST(tag.children, tag.key).when(
function dataTraversing(tagDataAst) {
tag.children = tagDataAst;
state.keep(tag);
}.bind(this)
);
return state.promise;
}
return function forResolve() {
return resolveStatement.call(this);
};
},
module: function forModule(tag, data) {
var statelessTag,
fromAttr = tag.attribs.hasOwnProperty('for');
statelessTag = { attribs: tag.attribs, children: tag.children, name: tag.name, raw: tag.raw, type: tag.type };
tag.key = tag.prefix ? tag.prefix + '-' + tag.key : tag.key;
function presetScope(object, data, key, firstArgument) {
if (firstArgument.key) {
data[firstArgument.key] = key;
}
data[firstArgument.value] = object;
return data;
}
function resolveStatement() {
var scopeArray = processExpressions({ type: 'var', name: tag.forSource.main, value: undefined }, data, this.calculators, this.filename),
iterator,
source,
result;
if (fromAttr) {
source = utils.clone(tag.attribs.for = undefined);
tag.attribs.for = undefined;
}
if (!this.iterators || !this.iterators.length) {
errorHandling('No iterators found!', this.filename);
}
for (var i = 0; i < this.iterators.length && !iterator; i++) {
if (this.iterators[i].is(scopeArray)) {
iterator = this.iterators[i].iterator;
}
}
if (!scopeArray) {
console.warn("For tag, source: " + tag.raw + ". " + tag.forSource.main.string + " object is: " + scopeArray);
}
if (!iterator) {
errorHandling('No iterator for specific type of variable ' + tag.forSource.main.string + ' found!', this.filename);
}
function iterate(scopeFigureArray, data, iterator) {
var children = [], it = 0;
iterator(scopeFigureArray, function entityIterator(entity, key) {
var statelessTagChildren = utils.mapForLoop(fromAttr ? [statelessTag] : statelessTag.children, function statelessTagIterator(stTag) {
stTag.key = it++;
return stTag;
});
children.push(this._process(statelessTagChildren, presetScope(entity, data, key, tag.forSource), tag.key));
}.bind(this));
if (children.length > 0) {
return children;
}
return undefined;
}
result = iterate.call(this, scopeArray, scopes.createScope(data), iterator);
if (fromAttr) {
tag.attribs.for = source;
}
return result;
}
return function forModuleReturnable() {
if (tag.children !== undefined) {
return resolveStatement.call(this);
}
};
}
};
return forM;
});