-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ui.checkable.js
296 lines (244 loc) · 11.1 KB
/
jquery.ui.checkable.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* jQuery UI Checkable v1.3.0
* Copyright (c) 2011, Gregory Waxman. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
* 4. All private modifications should be offered to the author at https://github.com/Akkuma/jquery-ui-checkable
* 5. All public forks must notify the author of their existence. Using github to directly fork the project will be considered sufficient notification and will fulfill this obligation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function ($) {
var NAMESPACE = 'checkable';
var DELEGATE_CLASS = NAMESPACE + '-del';
function check(useDefaultBehavior) {
var element = this.element[0];
if ((useDefaultBehavior && !element.disabled) || !useDefaultBehavior) {
var classNames = this.options.classNames
, $parent = this.parent
, $label = this._label;
if (element.type === 'radio') {
$('input[name="' + element.name + '"]').parent().removeClass(classNames.checked);
}
element.checked = true;
$parent.addClass(classNames.checked);
$label && $label.addClass(classNames.checked);
}
}
function uncheck(useDefaultBehavior) {
var element = this.element[0];
if ((useDefaultBehavior && !element.disabled) || !useDefaultBehavior) {
var classNames = this.options.classNames
, $parent = this.parent
, $label = this._label;
element.checked = false;
$parent.removeClass(classNames.checked);
$label && $label.removeClass(classNames.checked);
}
}
function disable($element, options) {
var element = this.element[0]
, $parent = this.parent
, $label = this._label
, classNames = this.options.classNames;
element.disabled = true;
$parent.addClass(classNames.disabled);
$label && $label.addClass(classNames.disabled);
}
function enable() {
var element = this.element[0]
, $parent = this.parent
, $label = this._label
, classNames = this.options.classNames;
element.disabled = false;
$parent.removeClass(classNames.disabled);
$label && $label.removeClass(classNames.disabled);
}
function click(useDefaultBehavior) {
var element = this.element[0];
var func;
if (element.type === 'radio') {
func = check;
}
else {
if (element.checked) {
func = uncheck;
}
else {
func = check;
}
}
func.call(this, useDefaultBehavior);
}
$.widget("ui.checkable", {
// default options
options: {
classNames: {
checkbox: NAMESPACE + '-checkbox',
radio: NAMESPACE + '-radio',
checked: NAMESPACE + '-checked',
hide: NAMESPACE + '-hide',
hover: NAMESPACE + '-hover',
label: NAMESPACE + '-label',
labelCheckbox: NAMESPACE + '-label-checkbox',
labelRadio: NAMESPACE + '-label-radio',
disabled: NAMESPACE + '-disabled'
}
},
_create: function () {
var $element = this.element;
var element = $element[0];
var type = element.type;
if ((type === 'radio' || type === 'checkbox')) {
var classNames;
var options = this.options;
var isDisabled = !!element.disabled;
if (type === 'checkbox') {
classNames = {
input: options.classNames.checkbox,
labelType: options.classNames.labelCheckbox
};
}
else {
classNames = {
input: options.classNames.radio,
labelType: options.classNames.labelRadio
};
}
classNames.disabled = options.classNames.disabled;
$element.addClass([options.classNames.hide, DELEGATE_CLASS].join(' '))
.wrap('<div class="' + [classNames.input, DELEGATE_CLASS, (isDisabled ? classNames.disabled : ''), (element.checked ? options.classNames.checked : '')].join(' ') + '">');
this.parent = $element.parent();
var inputId = element.id;
if (inputId) {
var $label = $('label[for=' + inputId + ']');
$label.addClass([options.classNames.label, DELEGATE_CLASS, classNames.labelType, (isDisabled ? classNames.disabled : '')].join(' '));
this._label = $label;
}
}
else {
//jQuery UI's Widgets does not offer developers the flexibility to pass in an arbitrary set of elements.
//It does a bunch of internal setup for your widget in _createWidget
$.Widget.prototype.destroy.apply(this, arguments)
}
},
_setOption: function (key, value) {
if (key === "classNames") {
value = $.extend({}, this.options.classNames, value);
this._setClassNames(value);
}
},
_setClassNames: function (value) {
var $element = this.element
, element = $element[0]
, $parent = this.parent
, originalTypeClassNames
, type = element.type
, classNames = this.options.classNames
, isDisabled = !!element.disabled
, newClassNames = value
, newTypeClassNames = {};
if (type === 'checkbox') {
originalTypeClassNames = {
input: classNames.checkbox,
labelType: classNames.labelCheckbox
};
newTypeClassNames = {
input: newClassNames.checkbox,
labelType: newClassNames.labelCheckbox
};
}
else {
originalTypeClassNames = {
input: classNames.radio,
labelType: classNames.labelRadio
};
newTypeClassNames = {
input: newClassNames.radio,
labelType: newClassNames.labelRadio
};
}
if (classNames.hide !== newClassNames.hide) {
$element.removeClass(classNames.hide).addClass(newClassNames.hide);
}
$parent.removeClass([originalTypeClassNames.input, (isDisabled ? classNames.disabled : ''), (element.checked ? classNames.checked : '')].join(' '))
.addClass([newTypeClassNames.input, (isDisabled ? newClassNames.disabled : ''), (element.checked ? newClassNames.checked : '')].join(' '));
if (this._label) {
this._label.removeClass([classNames.label, originalTypeClassNames.labelType, (isDisabled ? classNames.disabled : '')].join(' '))
.addClass([newClassNames.label, newTypeClassNames.labelType, (isDisabled ? newTypeClassNames.disabled : '')].join(' '));
}
this.options.classNames = newClassNames;
},
isChecked: function () {
return this.element[0].checked;
},
check: function () {
check.call(this);
},
uncheck: function () {
uncheck.call(this);
},
click: function (useDefaultBehavior) {
click.call(this, useDefaultBehavior);
},
enable: function () {
enable.call(this);
},
disable: function () {
disable.call(this);
},
refresh: function () {
var type = this.element[0].type;
if (type === 'radio' || type === 'checkbox') {
if (this.element[0].checked) {
check.call(this);
}
else {
uncheck.call(this);
}
if(this.element[0].disabled) {
disable.call(this);
}
else {
enable.call(this);
}
}
},
destroy: function () {
$.Widget.prototype.destroy.apply(this, arguments); // default destroy
// now do other stuff particular to this widget
var classNames = this.options.classNames
, $element = this.element
, $label = this._label;
$element.unwrap();
$element.removeClass([classNames.hide, DELEGATE_CLASS].join(' '));
if($label) {
$label.removeClass([DELEGATE_CLASS, classNames.label, classNames.labelCheckbox, classNames.labelRadio, classNames.disabled].join(' '));
}
}
});
$(function () {
$(document.body).delegate('input.' + DELEGATE_CLASS, 'click', function ($e) {
var $this = $(this);
var $data = $.data(this, NAMESPACE);
var func;
if (this.checked) {
func = check;
}
else {
func = uncheck;
}
func.call($data, true);
}).delegate('div.' + DELEGATE_CLASS, 'hover', function () {
var $this = $(this);
$this.toggleClass($.data($this.children()[0], NAMESPACE).options.classNames.hover);
}).delegate('label.' + DELEGATE_CLASS, 'hover', function () {
var $input = $('#' + this.htmlFor);
var $divWrapper = $input.parent();
$divWrapper.toggleClass($.data($input[0], NAMESPACE).options.classNames.hover);
});
});
}(jQuery));