-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.checkbox.js
329 lines (281 loc) · 9.54 KB
/
ui.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** @file
*
* Provide a sprite-based checkbox.
*
* Requires:
* ui.core.js
* ui.widget.js
*/
/*jslint nomen:false, laxbreak:true, white:false, onevar:false */
/*global jQuery:false */
(function($) {
$.widget("ui.checkbox", {
version: "0.1.1",
/* Remove the strange ui.widget._trigger() class name prefix for events
* so we can bind to events like 'change' instead of 'checkboxchange'.
*
* If you need to know which widget the event was triggered from, either
* bind directly to the widget or look at the event object.
*/
widgetEventPrefix: '',
options: {
// Defaults
css: 'checkbox', // General CSS class
cssOn: 'on', // CSS class when checked
cssOff: 'off', // CSS class when un-checked
titleOn: 'click to turn off',// Title when checked
titleOff: 'click to turn on', // Title when un-checked
useElTitle: true, // Include the title of the source
// element (or it's associated
// label) in the title of this
// checkbox.
hideLabel: false // Hide the associated label? If
// not, clicking on the title will
// be the same as clicking on the
// checkbox.
},
/** @brief Initialize a new instance.
*
* Valid options are:
* css General space-separated CSS class(es) for the
* checkbox [ 'checkbox' ];
* cssOn Space-separated CSS class(es) when checked
* [ 'on' ];
* cssOff Space-separated CSS class(es) when un-checked
* [ 'off' ];
* titleOn Title when checked
* [ 'click to turn off' ];
* titleOff Title when un-checked
* [ 'click to turn on' ];
*
* useElTitle Include the title of the source element (or it's
* associated label) in the title of this checkbox (as
* a prefix to 'titleOn' or 'titleOff')
* [ true ];
*
* hideLabel Hide the associated label? If not, clicking on the
* title will be the same as clicking on the checkbox
* [ false ].
*
* @triggers:
* 'enabled' when element is enabled;
* 'disabled' when element is disabled;
* 'change' / 'checked' when element is checked;
* 'change' / 'unchecked' when element is unchecked.
*/
_create: function() {
var self = this;
var opts = this.options;
opts.enabled = (opts.enabled === undefined
? (self.element.attr('disabled') ? false : true)
: opts.enabled);
opts.checked = (opts.checked === undefined
? (self.element.attr('checked') ? true : false)
: opts.checked);
opts.title = '';
// Remember the original value
self.element.data('value.uicheckbox', opts.checked);
var name = self.element.attr('name');
var id = self.element.attr('id');
// Try to locate the associated label
self.$label = false;
if (id)
{
self.$label = $('label[for='+ id +']');
}
if ( ((! self.$label) || (self.$label.length < 1)) && name)
{
self.$label = $('label[for='+ name +']');
}
if (opts.useElTitle === true)
{
opts.title = self.element.attr('title');
if ( ((! opts.title) || (opts.title.length < 1)) &&
(self.$label.length > 0) )
{
// The element has no 'title', use the text of the label.
opts.title = self.$label.text();
}
}
var title = opts.title
+ (opts.checked
? opts.titleOn
: opts.titleOff);
// Create a new element that will be placed just after the current
self.$el = $('<div />')
.addClass(opts.css)
.addClass('ui-state-default');
var iconCss = [ (opts.enabled ? '' : 'disabled'),
(opts.checked ? opts.cssOn : opts.cssOff) ];
self.$icon = $('<span />')
.addClass( iconCss.join(' ') );
if (title && (title.length > 0))
{
self.$icon.attr('title', title);
}
self.$icon.appendTo( self.$el );
// Insert the new element after the existing and remove the existing.
self.$el.insertAfter(self.element);
// Hide the original element.
self.element.hide();
if (self.$label && (self.$label.length > 0))
{
// We have a label for this field.
if (opts.hideLabel === true)
{
// Hide it.
self.$label.hide();
}
else
{
// Treat a click on the label as a click on the item.
self.$label.click(function(e) {
e.preventDefault();
e.stopPropagation();
self.$el.trigger('click',[e]);
return false;
});
}
}
// Interaction events
self._bindEvents();
},
/************************
* Private methods
*
*/
_bindEvents: function() {
var self = this;
var _mouseenter = function(e) {
if (self.options.enabled === true)
{
self.$el.addClass('ui-state-hover');
}
};
var _mouseleave = function(e) {
self.$el.removeClass('ui-state-hover');
};
var _focus = function(e) {
if (self.options.enabled === true)
{
self.$el.addClass('ui-state-focus');
}
};
var _blur = function(e) {
self.$el.removeClass('ui-state-focus');
};
var _click = function(e) {
self.toggle();
};
self.$el.bind('mouseenter.uicheckbox', _mouseenter)
.bind('mouseleave.uicheckbox', _mouseleave)
.bind('focus.uicheckbox', _focus)
.bind('blur.uicheckbox', _blur)
.bind('click.uicheckbox', _click);
},
/************************
* Public methods
*
*/
isChecked: function() {
return this.options.checked;
},
isEnabled: function() {
return this.options.enabled;
},
val: function() {
return (this.isEnabled() && this.isChecked()
? this.element.val()
: null);
},
enable: function()
{
if (! this.options.enabled)
{
this.options.enabled = true;
this.$el.removeClass('ui-state-disabled');
this._trigger('enabled');
}
},
disable: function()
{
if (this.options.enabled)
{
this.options.enabled = false;
this.$el.addClass('ui-state-disabled');
this._trigger('disabled');
}
},
toggle: function()
{
if (this.options.checked)
{
this.uncheck();
}
else
{
this.check();
}
},
check: function()
{
//if (this.options.enabled && (! this.options.checked))
if (! this.options.checked)
{
this.options.checked = true;
this.$icon
.removeClass(this.options.cssOff)
.addClass(this.options.cssOn)
.attr('title', this.options.title + this.options.titleOn);
//this.element.click();
this._trigger('change', null, 'check');
}
},
uncheck: function()
{
//if (this.options.enabled && this.options.checked)
if (this.options.checked)
{
this.options.checked = false;
this.$icon
.removeClass(this.options.cssOn)
.addClass(this.options.cssOff)
.attr('title', this.options.title + this.options.titleOff);
//this.element.click();
this._trigger('change', null, 'uncheck');
}
},
/** @brief Reset the input to its original (creation or last direct set)
* value.
*/
reset: function()
{
// Remember the original value
if (this.element.data('value.uicheckbox'))
{
this.check();
}
else
{
this.uncheck();
}
},
/** @brief Has the value of this input changed from its original?
*
* @return true | false
*/
hasChanged: function()
{
return (this.options.checked !== this.element.data('value.uicheckbox'));
},
destroy: function() {
if (this.$label)
{
this.$label.show();
}
this.$el.unbind('.uicheckbox');
this.$el.remove();
this.element.show();
}
});
}(jQuery));