-
Notifications
You must be signed in to change notification settings - Fork 89
/
checkbox.js
170 lines (129 loc) · 4.48 KB
/
checkbox.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
/**
* @namespace i-bem.js реализация блока checkbox
* @name Checkbox
*/
BEM.DOM.decl('checkbox', /** @lends Checkbox.prototype */ {
onSetMod : {
'js' : function() {
this.setMod('checked', this.elem('control').prop('checked')? 'yes' : '');
this._isControlFocused() && this.setMod('focused', 'yes');
},
'focused' : {
'yes' : function() {
if (this.isDisabled())
return false;
this._isControlFocused() || this.elem('control').focus();
this.setMod(this.elem('box'), 'focused', 'yes');
this.afterCurrentEvent(function() {
this.trigger('focus');
});
},
'' : function() {
this._isControlFocused() && this.elem('control').blur();
this.delMod(this.elem('box'), 'focused');
this.afterCurrentEvent(function() {
this.trigger('blur');
});
}
},
'checked' : function(modName, modVal) {
this.elem('control').prop('checked', modVal === 'yes');
this.afterCurrentEvent(function(){
this.trigger('change');
});
this.setMod(this.elem('box'), 'checked', modVal);
},
'disabled' : function(modName, modVal) {
this.elem('control').prop('disabled', modVal === 'yes');
}
},
/**
* Шорткат для проверки модификатора `_disabled_yes`
* @returns {Boolean}
*/
isDisabled : function() {
return this.hasMod('disabled', 'yes');
},
/**
* Шоткат для проверки модификатора `_checked_yes`
* @returns {Boolean}
*/
isChecked : function() {
return this.hasMod('checked', 'yes');
},
/**
* Хелпер для переключения модификатора `_checked_yes`
*/
toggle : function() {
this.toggleMod('checked', 'yes', '');
},
/**
* Получить/установить значение контрола
* @param {String} [val] значение которое нужно установить
* @returns {String|BEM.DOM}
*/
val : function(val) {
var checkbox = this.elem('control');
if (typeof val === 'undefined')
return checkbox.val();
checkbox.val(val);
return this;
},
_onClick : function(e) {
if (this.isDisabled())
return;
// для клика по всему, кроме самого инпута, отменяем действие по умолчанию и перекючаем чекбокс сами
// иначе, при нескольких кликах подряд, чекбокс залипает в неправильном состоянии
if (e.target !== this.elem('control')[0]) {
e.preventDefault();
this.toggle();
}
this.setMod('focused', 'yes');
},
_onChange : function(e) {
e.target.checked?
this.setMod('checked', 'yes') :
this.delMod('checked');
},
/**
* Обработчик события получения/потери фокуса
* @param e
* @private
*/
_onFocusInFocusOut : function(e) {
this.setMod('focused', e.type === 'focusin'? 'yes' : '');
},
_onMouseOverMouseOut : function(e) {
this.isDisabled() ||
this.setMod('hovered', e.type === 'mouseover'? 'yes' : '');
},
/**
* Проверяет в фокусе ли контрол
* @private
* @returns {Boolean}
*/
_isControlFocused : function() {
try {
return this.containsDomElem($(this.__self.doc[0].activeElement));
} catch(e) {
return false;
}
}
}, /** @lends Checkbox */{
live : function() {
this
.liveBindTo('leftclick', function(e) {
this._onClick(e);
})
.liveBindTo('control', 'change', function(e) {
this._onChange(e);
})
.liveBindTo('control', 'focusin focusout', function(e) {
this._onFocusInFocusOut(e);
})
.liveBindTo('mouseover mouseout', function(e) {
this._onMouseOverMouseOut(e);
});
return false;
}
});