-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbem.backbone.js
145 lines (109 loc) · 4.32 KB
/
bem.backbone.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
(function($){
if (!Backbone || !Backbone.View) {
return;
}
/**
* Utilities
*/
var utilities = {
errorMessage: (console && console.error) ? console.error : function(errorText){},
elementModDataAttribute: 'bemMods',
elementBaseClassDataAttribute: 'bemBaseClass',
getMods: function(jqueryMethodArgs){
var mods = {};
if (jqueryMethodArgs.length == 1) {
mods = jqueryMethodArgs[0];
} else {
mods[jqueryMethodArgs[0]] = jqueryMethodArgs[1];
}
return mods;
},
getElementMods: function($element, baseClass){
var elementMods = $element.data(utilities.elementModDataAttribute);
if (!elementMods) {
var elementClasses = $element.attr('class').split(/\s+/gm);
var modifierRegexp = new RegExp('^' + baseClass + '_([A-Za-z0-9\-]+)_([A-Za-z0-9\-]+)$');
elementMods = {};
for (var i = 0; i < elementClasses.length; i++) {
var matches = modifierRegexp.exec(elementClasses[i]);
if (matches){
elementMods[matches[1]] = matches[2];
}
}
$element.data(utilities.elementModDataAttribute, elementMods);
}
return elementMods;
},
setElementMod: function($element, modName, modValue){
var elementMods = utilities.getElementMods($element, '');
elementMods[modName] = modValue;
$element.data(utilities.elementModDataAttribute, elementMods);
}
};
/**
* jQuery methods for setting and removing modifiers
*/
$.fn.setMod = function(){
if (arguments.length == 0 || arguments.length > 2) {
utilities.errorMessage('.setMod() takes one or two attributes');
return this;
}
var mods = utilities.getMods(arguments);
return this.each(function(){
var $element = $(this);
var baseClass = $element.data(utilities.elementBaseClassDataAttribute);
if (!baseClass) {
utilities.errorMessage('.setMod() error');
return;
}
var elementMods = utilities.getElementMods($element, baseClass);
for (var modName in mods) {
if (modName in elementMods) {
$element.removeClass(baseClass + '_' + modName + '_' + elementMods[modName]);
}
$element.addClass(baseClass + '_' + modName + '_' + mods[modName]);
utilities.setElementMod($element, modName, mods[modName]);
}
});
};
$.fn.removeMod = function(modName){
return this.each(function(){
var $element = $(this);
var baseClass = $element.data(utilities.elementBaseClassDataAttribute);
if (!baseClass) {
utilities.errorMessage('.removeMod() error');
return;
}
var elementMods = utilities.getElementMods($element, baseClass);
if (modName in elementMods) {
$element.removeClass(baseClass + '_' + modName + '_' + elementMods[modName]);
}
});
};
Backbone.BemView = Backbone.View.extend({
blockName: '',
setMod: function(){
var $block = this.$el;
$block.data(utilities.elementBaseClassDataAttribute, this.blockName);
$block.setMod(utilities.getMods(arguments));
return $block;
},
removeMod: function(modName){
var $block = this.$el;
$block.data(utilities.elementBaseClassDataAttribute, this.blockName);
$block.removeMod(modName);
return $block;
},
element: function(elementName){
var that = this;
var $block = this.$el;
var elementSelector = '.' + this.blockName + '__' + elementName;
var $elements = $block.find(elementSelector);
// Set blockName and elementName
$elements.each(function(){
$(this).data(utilities.elementBaseClassDataAttribute, that.blockName + '__' + elementName)
});
return $elements;
}
});
})(jQuery);