Skip to content

Instantly share code, notes, and snippets.

@isuvorov
Created October 4, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isuvorov/4d516eeec2a7b57b07a3 to your computer and use it in GitHub Desktop.
Save isuvorov/4d516eeec2a7b57b07a3 to your computer and use it in GitHub Desktop.
match(this._mode === '')(
match()(function () {
this.ctx || (this.ctx = {});
var vBlock = this.ctx.block,
vElem = this.ctx.elem,
block = this._currBlock || this.block;
return apply('default', {
block: vBlock || (vElem ? block : undefined),
_currBlock: (vBlock || vElem) ? undefined : block,
elem: vElem,
mods: vBlock ? this.ctx.mods || (this.ctx.mods = {}) : this.mods,
elemMods: this.ctx.elemMods || {}
});
}),
match(function () {
return this.isArray(this.ctx);
})(function () {
var ctx = this.ctx,
len = ctx.length,
i = 0,
buf = [];
while (i < len)
buf.push(apply({ctx: ctx[i++]}));
return buf;
}),
match(function () {
return !this.ctx;
})(),
match(function () {
return this.isSimple(this.ctx);
})(function () {
var ctx = this.ctx;
if (ctx && ctx !== true || ctx === 0) {
return ctx;
}
})
);
def()(function () {
var BEM_INTERNAL = this.BEM.INTERNAL,
ctx = this.ctx,
isBEM,
tag,
res;
//local({ _str : '' })(function() {
var vBlock = this.block;
tag = apply('tag');
typeof tag !== 'undefined' || (tag = ctx.tag);
typeof tag !== 'undefined' || (tag = 'div');
if (tag) {
var jsParams, js;
if (vBlock && ctx.js !== false) {
js = apply('js');
js = js ? this.extend(ctx.js, js === true ? {} : js) : ctx.js === true ? {} : ctx.js;
js && ((jsParams = {})[BEM_INTERNAL.buildClass(vBlock, ctx.elem)] = js);
}
isBEM = apply('bem');
typeof isBEM !== 'undefined' ||
(isBEM = typeof ctx.bem !== 'undefined' ? ctx.bem : ctx.block || ctx.elem);
var cls = apply('cls');
cls || (cls = ctx.cls);
var addJSInitClass = ctx.block && jsParams && !ctx.elem;
var cls_str = "";
if (isBEM || cls) {
if (isBEM) {
cls_str += BEM_INTERNAL.buildClasses(vBlock, ctx.elem, ctx.elemMods || ctx.mods);
var mix = apply('mix');
ctx.mix && (mix = mix ? [].concat(mix, ctx.mix) : ctx.mix);
if (mix) {
var visited = {},
visitedKey = function (block, elem) {
return (block || '') + '__' + (elem || '');
};
visited[visitedKey(vBlock, this.elem)] = true;
// Transform mix to the single-item array if it's not array
this.isArray(mix) || (mix = [mix]);
for (var i = 0; i < mix.length; i++) {
var mixItem = mix[i];
typeof mixItem === 'string' && (mixItem = {block: mixItem});
var hasItem = (mixItem.block && (vBlock !== ctx.block || mixItem.block !== vBlock)) || mixItem.elem,
mixBlock = mixItem.block || mixItem._block || this.block,
mixElem = mixItem.elem || mixItem._elem || this.elem;
hasItem && (cls_str += ' ');
cls_str += BEM_INTERNAL[hasItem ? 'buildClasses' : 'buildModsClasses'](
mixBlock,
mixItem.elem || mixItem._elem ||
(mixItem.block ? undefined : this.elem),
mixItem.elemMods || mixItem.mods);
if (mixItem.js) {
(jsParams ||
(jsParams = {}))[BEM_INTERNAL.buildClass(mixBlock, mixItem.elem)] = mixItem.js === true ?
{} :
mixItem.js;
addJSInitClass || (addJSInitClass = mixBlock && !mixItem.elem);
}
// Process nested mixes
if (hasItem && !visited[visitedKey(mixBlock, mixElem)]) {
visited[visitedKey(mixBlock, mixElem)] = true;
var nestedMix = apply('mix', {
block: mixBlock,
elem: mixElem
});
if (nestedMix) {
Array.isArray(nestedMix) || (nestedMix = [nestedMix]);
for (var j = 0; j < nestedMix.length; j++) {
var nestedItem = nestedMix[j];
if (!nestedItem.block && !nestedItem.elem || !visited[visitedKey(
nestedItem.block,
nestedItem.elem
)]) {
nestedItem._block = mixBlock;
nestedItem._elem = mixElem;
mix.splice(i + 1, 0, nestedItem);
}
}
}
}
}
}
}
cls && (cls_str += isBEM ? ' ' + cls : cls);
cls_str += addJSInitClass ? ' i-bem' : '';
}
if (isBEM && jsParams) {
this._str += ' data-bem=\'' +
this.jsAttrEscape(JSON.stringify(jsParams)) +
'\'';
}
var attrs = apply('attrs');
// NOTE: maybe we need to make an array for quicker serialization
attrs = this.extend(attrs, ctx.attrs);
if (attrs) {
var name, attr; // TODO: do something with OmetaJS and YUI Compressor
for (name in attrs) {
attr = attrs[name];
if (typeof attr === 'undefined') continue;
this._str += ' ' + name + '="' +
this.attrEscape(this.isSimple(attr) ?
attr :
this.reapply(attr)) +
'"';
}
}
}
var content = apply('content');
if (content || content === 0) {
this._resetApplyNext(this);
isBEM = vBlock || this.elem;
content = apply('', {
_notNewList: false,
position: isBEM ? 1 : this.position,
_listLength: isBEM ? 1 : this._listLength,
ctx: content
});
}
var newCtx = this.ctx._raw ? this.ctx : {};
newCtx.tag = tag;
// нужно свойство attrs
if (!newCtx.attrs && (attrs || cls_str || jsParams)) {
newCtx.attrs = {};
}
if (attrs) {
//newCtx.attrs = this.extend(newCtx.attrs, attrs);
newCtx.attrs = attrs;
}
if (jsParams) {
newCtx.jsParams = jsParams;
newCtx.attrs["data-bem"] = this.jsAttrEscape(JSON.stringify(jsParams));
}
if (cls_str) {
newCtx.attrs.class = cls_str
}
newCtx.content = content;
this._str = newCtx;
return newCtx;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment