-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (83 loc) · 2.38 KB
/
index.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
(function(root, factory) {
if (typeof exports === 'object') {
// CommonJS
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else {
// Browser globals
root.bemcls = factory();
}
})(this, function() {
var objToString = Object.prototype.toString;
var arraySlice = Array.prototype.slice;
var EXACT_KEY = '__exact';
function isObject(it) {
return objToString.apply(it) === '[object Object]';
}
function isArray(it) {
return objToString.apply(it) === '[object Array]';
}
function makeExact(it) {
if (!it[EXACT_KEY]) {
it = new String(it);
it[EXACT_KEY] = true;
}
return it;
}
function normClsName(blockName, elementName, exactMode) {
if (!elementName) {
return [];
} else if (elementName[EXACT_KEY] || exactMode) {
return [makeExact(elementName)];
}
return [blockName + '__' + elementName];
}
function getClsFromObj(blockName, obj, exactMode) {
var clsNames = [];
for (var key in obj) {
if (obj.hasOwnProperty(key) && obj[key]) {
clsNames = clsNames.concat(normClsName(blockName, key, exactMode));
}
}
return clsNames;
}
function getClsInner(blockName, arg, exactMode) {
var clsNames = [];
if (isObject(arg)) {
clsNames = clsNames.concat(getClsFromObj(blockName, arg, exactMode));
} else if (isArray(arg)) {
arg.forEach(function(item){
clsNames = clsNames.concat(getClsInner(blockName, item, exactMode));
});
} else if (arg) {
clsNames = clsNames.concat(normClsName(blockName, arg, exactMode));
}
return clsNames;
}
function getCls(blockName, args, exactMode) {
var dict = {};
var clsNames = getClsInner(blockName, args, exactMode).filter(function(name){
var exist = dict[name];
dict[name] = true;
return !exist;
});
var ret = clsNames.join(' ');
return ret && makeExact(ret);
}
return function(blockName) {
function ret() {
if (arguments.length === 0) {
return ret.block.toString();
} else {
return getCls(blockName, arraySlice.apply(arguments)).toString();
}
}
ret.block = makeExact(blockName);
ret.exact = function() {
return getCls(blockName, arraySlice.apply(arguments), true);
};
return ret;
};
});