-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlevels.js
215 lines (198 loc) · 6.14 KB
/
levels.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
var inherit = require('inherit');
module.exports = inherit(/** @lends Levels.prototype */{
/**
* @constructs Levels
* @classdesc Works with list of levels.
* @see {@link Level}
*
* @param {Level[]} items — levels instances.
*/
__constructor: function (items) {
this.items = items;
},
/**
* Returns info about blocks.
*
* @param {String} blockName
* @returns {Object[]}
*/
getBlocks: function (blockName) {
var block,
blocks = [];
for (var i = 0, l = this.items.length; i < l; i++) {
block = this.items[i].blocks[blockName];
if (block) {
blocks.push(block);
}
}
return blocks;
},
/**
* Returns info about elements.
*
* @param {String} blockName
* @param {String} elemName
* @returns {Object[]}
*/
getElems: function (blockName, elemName) {
var block,
elements = [];
for (var i = 0, l = this.items.length; i < l; i++) {
block = this.items[i].blocks[blockName];
if (block && block.elements[elemName]) {
elements.push(block.elements[elemName]);
}
}
return elements;
},
/**
* Returns files and directories for block or modifier of block.
*
* @param {String} blockName
* @param {String} modName
* @param {String} modVal
* @returns {{files: Object[], dirs: Object[]}}
*/
getBlockEntities: function (blockName, modName, modVal) {
var block,
files = [],
dirs = [],
blocks = this.getBlocks(blockName);
modVal || (modVal = '*');
for (var i = 0, l = blocks.length; i < l; i++) {
block = blocks[i];
if (modName) {
var mods = block.mods[modName],
res = mods && (mods[modVal] || mods['*']);
if (res) {
files = files.concat(res.files);
dirs = dirs.concat(res.dirs);
}
} else {
files = files.concat(block.files);
dirs = dirs.concat(block.dirs);
}
}
return { files: files, dirs: dirs };
},
/**
* Returns files and directories for element or modifier of element.
*
* @param {String} blockName
* @param {String} elemName
* @param {String} modName
* @param {String} modVal
* @returns {{files: Object[], dirs: Object[]}}
*/
getElemEntities: function (blockName, elemName, modName, modVal) {
var elem,
files = [],
dirs = [],
elems = this.getElems(blockName, elemName);
modVal || (modVal = '*');
for (var i = 0, l = elems.length; i < l; i++) {
elem = elems[i];
if (modName) {
var mods = elem.mods[modName],
res = mods && (mods[modVal] || mods['*']);
if (res) {
files = files.concat(res.files);
dirs = dirs.concat(res.dirs);
}
} else {
files = files.concat(elem.files);
dirs = dirs.concat(elem.dirs);
}
}
return { files: files, dirs: dirs };
},
/**
* Returns files for block or modifier of block.
*
* @param {String} blockName
* @param {String} modName
* @param {String} modVal
* @returns {Object[]}
*/
getBlockFiles: function (blockName, modName, modVal) {
return this.getBlockEntities(blockName, modName, modVal).files;
},
/**
* Returns files for element or modifier of element.
*
* @param {String} blockName
* @param {String} elemName
* @param {String} modName
* @param {String} modVal
* @returns {Object[]}
*/
getElemFiles: function (blockName, elemName, modName, modVal) {
return this.getElemEntities(blockName, elemName, modName, modVal).files;
},
/**
* Returns files by declaration.
*
* @param {String} blockName
* @param {String} elemName
* @param {String} modName
* @param {String} modVal
* @returns {Object[]}
*/
getFilesByDecl: function (blockName, elemName, modName, modVal) {
if (elemName) {
return this.getElemFiles(blockName, elemName, modName, modVal);
} else {
return this.getBlockFiles(blockName, modName, modVal);
}
},
/**
* Returns files by suffix.
*
* @param {String} suffix
* @returns {Object[]}
*/
getFilesBySuffix: function (suffix) {
var files = [];
this.items.forEach(function (level) {
var blocks = level.blocks;
Object.keys(blocks).forEach(function (blockName) {
files = files.concat(getFilesInElementBySuffix(blocks[blockName], suffix));
});
});
return files;
},
/**
* Returns value of block modifier.
*
* @param {String} blockName
* @param {String} modName
* @returns {String[]}
*/
getModValues: function (blockName, modName) {
var modVals = [];
this.items.forEach(function (level) {
var blockInfo = level.blocks[blockName];
if (blockInfo && blockInfo.mods && blockInfo.mods[modName]) {
modVals = modVals.concat(Object.keys(blockInfo.mods[modName]));
}
});
return modVals;
}
});
function getFilesInElementBySuffix(element, suffix) {
var files = element.files.filter(function (f) { return f.suffix === suffix; }),
mods = element.mods,
elements = element.elements;
Object.keys(mods).forEach(function (modName) {
var mod = mods[modName];
Object.keys(mod).forEach(function (modVal) {
files = files.concat(mod[modVal].files.filter(function (f) { return f.suffix === suffix; }));
});
});
if (elements) {
Object.keys(elements).forEach(function (elemName) {
files = files.concat(getFilesInElementBySuffix(elements[elemName], suffix));
});
}
return files;
}