Repository navigation
Expand file tree
/
Copy pathbemhtmljs.js
More file actions
260 lines (209 loc) · 6.84 KB
/
Copy pathbemhtmljs.js
File metadata and controls
260 lines (209 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// BEMHTML-like js-based template engine
"use strict";
var b_ = (function() {
var templates = [];
function extend() {
var res = {}, args = Array.prototype.slice.call(arguments);
for (var i = 0, n = args.length; i < n; i++)
for (var prop in args[i])
if (args[i].hasOwnProperty(prop))
res[prop] = args[i][prop];
return res;
}
function template(desc, tmpl) {
desc = (typeof desc === 'object') ? desc : { block: desc, mode: 'content' };
if (desc.block && !desc.elem) desc.elem = undefined;
if (desc.elem && !desc.block) desc.block = undefined;
templates.push({ desc: desc, tmpl: tmpl });
}
function mtch(node, desc, mode) {
node = node || {};
if (desc.mode !== mode) return false;
for (var p in desc) {
if (p !== 'mode' && p !== 'guard' && desc.hasOwnProperty(p)) {
if (desc[p] !== node[p])
return false;
}
}
if (desc.guard) return !!desc.guard(node);
return true;
}
function applyTemplates(ctx, mode, params) {
var _this = this;
if (Array.isArray(ctx)) {
return ctx.map(function(item) { return _this.applyTemplates(item, mode, params) }).join('');
}
for (var i = templates.length, tmplItm; i--;) {
tmplItm = templates[i];
if (mtch(ctx, tmplItm.desc, mode)) {
this.crtTmpl = tmplItm;
if (typeof tmplItm.tmpl === 'function')
return tmplItm.tmpl.call(this, ctx, params);
else
return tmplItm.tmpl;
}
}
}
function applyNext(ctx, mode, params) {
var tmplDesc = this.crtTmpl.desc;
tmplDesc.__flag = +new Date() + '-' + Math.floor(Math.random()*1000000);
var res = this.applyTemplates(ctx, mode, params);
delete tmplDesc.__flag;
return res;
}
return {
template: template,
applyTemplates: applyTemplates,
applyNext: applyNext,
extend: extend
}
})();
b_.template({}, function(ctx, params) {
var res = {},
selfClosing = [
'hr',
'img',
'area',
'base',
'br',
'col',
'command',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr'
];
function isSimple(ctx) {
return typeof ctx === 'string' || typeof ctx === 'number'
}
function isBoolean(ctx) {
return typeof ctx === 'boolean'
}
function buildClass(node) {
var cls = '',
modSep = '_',
modValSep = '_',
elemSep = '__';
if (node.block && !node.elem) {
cls += node.block;
var mods = node.mods;
for (var modName in mods)
if (mods.hasOwnProperty(modName) && mods[modName])
cls += (cls && ' ') + node.block + modSep + modName + modValSep + mods[modName];
}
if (node.elem) {
cls += node.block + elemSep + node.elem;
var elemMods = node.elemMods;
for (var elemModName in elemMods)
if (elemMods.hasOwnProperty(elemModName) && elemMods[elemModName])
cls += (cls && ' ') + node.block + elemSep + node.elem + modSep + elemModName + modValSep + elemMods[elemModName];
}
if (node.cls) {
cls += (cls && ' ') + node.cls;
}
return cls ? ' class="' + cls + '"' : '';
}
function buildJSParams(node) {
var onclick;
if (node.block && !node.elem && node.js) {
onclick = {};
onclick[node.block] = (typeof node.js === 'boolean') ? {} : node.js;
}
return onclick ? ' onclick="return ' + JSON.stringify(onclick) + '"' : '';
}
function buildAttrs(node) {
var attrs = '';
if (node.attrs)
for (var name in node.attrs)
if (node.attrs.hasOwnProperty(name) && node.attrs[name])
attrs += ' ' + name + '="' + node.attrs[name] + '"';
return attrs;
}
if (params && params.parentBlock && !ctx.block && ctx.elem) ctx.block = params.parentBlock;
if (isSimple(ctx)) return ctx;
if (isBoolean(ctx)) return '';
res = this.extend(res, { tag: this.applyTemplates(ctx, 'tag') });
if (res.tag) {
res = this.extend(res,
this.applyTemplates(ctx, 'block'),
this.applyTemplates(ctx, 'elem'),
{ cls: this.applyTemplates(ctx, 'cls') },
{ js: this.applyTemplates(ctx, 'jsattrs') },
{ attrs: this.applyTemplates(ctx, 'attrs') },
{ content: this.applyTemplates(ctx, 'content') }
);
while (res.content && !isSimple(res.content))
res.content = this.applyTemplates(res.content, undefined, { parentBlock: res.block });
var isSelfClosing = selfClosing.indexOf(res.tag) != -1;
return '<' + res.tag + buildClass(res) + buildJSParams(res) + buildAttrs(res) + (isSelfClosing ? '/>' : '>' + res.content + '</' + res.tag + '>');
}
return '';
});
b_.template({ mode: 'tag' }, function(ctx) {
return ctx.tag === '' ? '' : (ctx.tag || 'div')
});
b_.template({ mode: 'block' }, function(ctx) {
var res = {};
if (ctx.block) res.block = ctx.block;
if (ctx.mods) res.mods = ctx.mods;
return res;
});
b_.template({ mode: 'elem' }, function(ctx) {
var res = {};
if (ctx.elem) res.elem = ctx.elem;
if (ctx.elemMods) res.elemMods = ctx.elemMods;
return res;
});
b_.template({ mode: 'cls' }, function(ctx) {
return ctx.cls;
});
b_.template({ mode: 'jsattrs' }, function(ctx) {
return ctx.js;
});
b_.template({ mode: 'attrs' }, function(ctx) {
return ctx.attrs;
});
b_.template({ mode: 'content' }, function(ctx) {
return ctx.content || ''
});
b_.template({ block: 'b-page', mode: undefined }, function(ctx) {
return '<!DOCTYPE html>' + this.applyNext({
tag: 'html',
content: ctx
})
});
b_.template({ block: 'b-page', elem: 'title', mode: 'tag' }, 'title');
b_.template({ block: 'b-link', mode: 'tag' }, 'a');
b_.template({ block: 'b-link', mode: 'attrs' }, function(ctx) {
return { href: ctx.url }
});
var bemjson = {
block: 'b-page',
content: [
{
elem: 'title',
content: 'title'
},
{
tag: 'a',
attrs: { href: 'yandex.ru' },
content: 'yandex'
},
{
tag: 'br'
},
{
block: 'b-link',
url: 'ya.ru',
content: 'ya'
}
]
}
console.log(b_.applyTemplates(bemjson));