-
Notifications
You must be signed in to change notification settings - Fork 0
/
bem.js
282 lines (273 loc) · 6.64 KB
/
bem.js
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict';
const gulp = require('gulp'),
path = require('path'),
del = require('del'),
file = require('gulp-file'),
insert = require('gulp-insert');
const src = path.resolve(__dirname, 'src');
const dist = path.resolve(__dirname, 'dist');
function err_log(error) {
console.log([
'',
"----------ERROR MESSAGE START----------",
("[" + error.name + " in " + error.plugin + "]"),
error.message,
"----------ERROR MESSAGE END----------",
''
].join('\n'));
}
class _Item{
constructor(opts){
this.opts = opts || false;
this.block = opts.block || false;
this.element = opts.element || false;
this.modificator = opts.modificator || false;
this.items = opts.items || false;
this.sep = '';
this.prev = false;
}
add(){
if( this.exists() ){
throw new Error(`item already exists`);
}else{
this.addInstance();
this.addFile();
//this.addImport();
}
}
remove(){
if( this.exists() ){
this.removeInstance();
this.removeFile();
//this.removeImport();
}else{
throw new Error(`item not found`);
}
}
importRule(){
return `@import '${this.shortname()}/${this.fullname()}.css';\r\n`;
}
addFile(){
file(`${this.fullname()}.css`, `\r\n.${this.fullname()}\{\r\n\r\n\}\r\n\r\n` )
.pipe(gulp.dest( path.resolve(src, 'css', `${this.folder()}/${this.shortname()}` ) ));
}
addImport(){
gulp.src( path.resolve(src, 'css', this.folder(), `${this.parentname()}.css`) )
.pipe(insert.prepend( this.importRule() ))
.pipe(gulp.dest( path.resolve(src, 'css', this.folder()) ));
}
removeFile(){
del( path.resolve(src, 'css', this.folder(), this.shortname()) );
}
removeImport(){
let self = this;
gulp.src( path.resolve(src, 'css', this.folder(), `${this.parentname()}.css`) )
.pipe(insert.transform(function(contents, file){
contents = contents.replace( self.importRule(), '' );
return contents;
}))
.pipe(gulp.dest( path.resolve(src, 'css', this.folder()) ));
}
updateItems(){
file('items.json', JSON.stringify(this.items) )
.pipe(gulp.dest( path.resolve(src, 'css', 'blocks') ));
}
}
class _Block extends _Item{
constructor(opts){
super(opts);
}
fullname(){
return `${this.block}`;
}
shortname(){
return `${this.sep}${this.block}`;
}
folder(){
return `blocks`;
}
parentname(){
return `blocks`;
}
exists(){
let rez = false;
this.items.map((block)=>{
if( block.name === this.block ){
rez = true;
return;
}
});
return rez;
}
addInstance(){
this.items.push({name: this.block, elements: [], modificators: []});
this.updateItems();
}
removeInstance(){
this.items = this.items.filter((block)=>{
return block.name === this.block ? false : true;
});
this.updateItems();
}
}
class _Element extends _Item{
constructor(opts){
super(opts);
this.sep = '__';
}
fullname(){
return `${this.block}${this.sep}${this.element}`;
}
shortname(){
return `${this.sep}${this.element}`;
}
folder(){
return `blocks/${this.block}`;
}
parentname(){
return `${this.block}`;
}
exists(){
let rez = false;
this.items.map((block)=>{
if( block.name === this.block ){
this.prev = block;
block.elements.map((element)=>{
if( element.name === this.element ){
rez = true;
return;
}
});
}
});
return rez;
}
add(){
let parent = new _Block(this.opts);
if( !parent.exists() ){
throw new Error(`parent item not found`);
}
super.add();
}
addInstance(){
this.prev.elements.push({name: this.element, modificators: []});
this.updateItems();
}
removeInstance(){
this.prev.elements = this.prev.elements.filter((element)=>{
return element.name === this.element ? false : true;
});
this.updateItems();
}
}
class _Modificator extends _Item{
constructor(opts){
super(opts);
this.sep = '--';
this.parent = opts.block&&opts.element ? new _Element(opts) : new _Block(opts);
}
fullname(){
if( this.parent instanceof _Block ){
return `${this.block}${this.sep}${this.modificator}`;
}else{
return `${this.block}${this.parent.sep}${this.element}${this.sep}${this.modificator}`;
}
}
shortname(){
return `${this.sep}${this.modificator}`;
}
folder(){
if( this.parent instanceof _Block ){
return `blocks/${this.block}`;
}else{
return `blocks/${this.block}/${this.parent.sep}${this.element}`;
}
}
parentname(){
if( this.parent instanceof _Block ){
return `${this.block}`;
}else{
return `${this.block}${this.parent.sep}${this.element}`;
}
}
exists(){
let rez = false;
if( this.parent instanceof _Block ){
this.items.map((block)=>{
if( block.name === this.block ){
this.prev = block;
block.modificators.map((modificator)=>{
if( modificator === this.modificator ){
rez = true;
return;
}
});
}
});
}else{
this.items.map((block)=>{
if( block.name === this.block ){
block.elements.map((element)=>{
if( element.name === this.element ){
this.prev = element;
element.modificators.map((modificator)=>{
if( modificator === this.modificator ){
rez = true;
return;
}
});
}
});
}
});
}
return rez;
}
add(){
let parent;
parent = this.parent instanceof _Block ? new _Block(this.opts) : new _Element(this.opts);
if( !parent.exists() ){
throw new Error(`parent item not found`);
}
super.add();
}
addInstance(){
this.prev.modificators.push(this.modificator);
this.updateItems();
}
removeInstance(){
this.prev.modificators = this.prev.modificators.filter((modificator)=>{
return modificator === this.modificator ? false : true;
});
this.updateItems();
}
}
module.exports = function BEM(args, items){
let selector = args.add || args.rm;
let opts = {
block: selector.match(/^[^(--|__)]*/)[0],
element: selector.match(/(?:__)([^(--|__)]*)/),
modificator: selector.match(/(?:--)(.*)$/),
items: items
};
console.log(opts.element);
opts.element = opts.element !== null ? opts.element[1] : null;
opts.modificator = opts.modificator !== null ? opts.modificator[1] : null;
try{
if(opts.block&&opts.element&&opts.modificator){ //block__elem--mod
let modificator = new _Modificator(opts);
args.add ? modificator.add() : modificator.remove();
}else if(opts.block&&opts.element){ //block__elem
let element = new _Element(opts);
args.add ? element.add() : element.remove();
}else if(opts.block&&opts.modificator){ //block--mod
let modificator = new _Modificator(opts);
args.add ? modificator.add() : modificator.remove();
}else if(opts.block){ //block
let block = new _Block(opts);
args.add ? block.add() : block.remove();
}
}catch(e){
err_log(e);
}
}