-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjquery.fs.picker.js
322 lines (281 loc) · 7.53 KB
/
jquery.fs.picker.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
/*
* Picker v3.1.2 - 2015-04-04
* A jQuery plugin for replacing default checkboxes and radios. Part of the formstone library.
* http://classic.formstone.it/picker/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
;(function ($, window) {
"use strict";
var isIE8 = (document.all && document.querySelector && !document.addEventListener);
/**
* @options
* @param customClass [string] <''> "Class applied to instance"
* @param toggle [boolean] <false> "Render 'toggle' styles"
* @param labels.on [string] <'ON'> "Label for 'On' position; 'toggle' only"
* @param labels.off [string] <'OFF'> "Label for 'Off' position; 'toggle' only"
*/
var options = {
customClass: "",
toggle: false,
labels: {
on: "ON",
off: "OFF"
}
};
var pub = {
/**
* @method
* @name defaults
* @description Sets default plugin options
* @param opts [object] <{}> "Options object"
* @example $.picker("defaults", opts);
*/
defaults: function(opts) {
options = $.extend(options, opts || {});
return (typeof this === 'object') ? $(this) : true;
},
/**
* @method
* @name destroy
* @description Removes instance of plugin
* @example $(".target").picker("destroy");
*/
destroy: function() {
return $(this).each(function(i, input) {
var data = $(input).data("picker");
if (data) {
data.$picker.off(".picker");
data.$handle.remove();
data.$labels.remove();
data.$input.off(".picker")
.removeClass("picker-element")
.data("picker", null);
data.$label.removeClass("picker-label")
.unwrap();
}
});
},
/**
* @method
* @name disable
* @description Disables target instance
* @example $(".target").picker("disable");
*/
disable: function() {
return $(this).each(function(i, input) {
var data = $(input).data("picker");
if (data) {
data.$input.prop("disabled", true);
data.$picker.addClass("disabled");
}
});
},
/**
* @method
* @name enable
* @description Enables target instance
* @example $(".target").picker("enable");
*/
enable: function() {
return $(this).each(function(i, input) {
var data = $(input).data("picker");
if (data) {
data.$input.prop("disabled", false);
data.$picker.removeClass("disabled");
}
});
},
/**
* @method
* @name update
* @description Updates instance of plugin
* @example $(".target").picker("update");
*/
update: function() {
return $(this).each(function(i, input) {
var data = $(input).data("picker");
if (data && !data.$input.is(":disabled")) {
if (data.$input.is(":checked")) {
_onSelect({ data: data }, true);
} else {
_onDeselect({ data: data }, true);
}
}
});
}
};
/**
* @method private
* @name _init
* @description Initializes plugin
* @param opts [object] "Initialization options"
*/
function _init(opts) {
// Settings
opts = $.extend({}, options, opts);
// Apply to each element
var $items = $(this);
for (var i = 0, count = $items.length; i < count; i++) {
_build($items.eq(i), opts);
}
return $items;
}
/**
* @method private
* @name _build
* @description Builds each instance
* @param $input [jQuery object] "Target jQuery object"
* @param opts [object] <{}> "Options object"
*/
function _build($input, opts) {
if (!$input.data("picker")) {
// EXTEND OPTIONS
opts = $.extend({}, opts, $input.data("picker-options"));
var $parent = $input.closest("label"),
$label = $parent.length ? $parent.eq(0) : $("label[for=" + $input.attr("id") + "]"),
type = $input.attr("type"),
typeClass = "picker-" + (type === "radio" ? "radio" : "checkbox"),
group = $input.attr("name"),
html = '<div class="picker-handle"><div class="picker-flag" /></div>';
if (opts.toggle) {
typeClass += " picker-toggle";
html = '<span class="picker-toggle-label on">' + opts.labels.on + '</span><span class="picker-toggle-label off">' + opts.labels.off + '</span>' + html;
}
// Modify DOM
$input.addClass("picker-element");
if ($label.length) {
$label.wrap('<div class="picker ' + typeClass + ' ' + opts.customClass + '" />')
.before(html)
.addClass("picker-label");
} else {
$input.before('<div class="picker ' + typeClass + ' ' + opts.customClass + '">' + html + '</div>');
}
// Store plugin data
var $picker = ($label.length) ? $label.parents(".picker") : $input.prev(".picker"),
$handle = $picker.find(".picker-handle"),
$labels = $picker.find(".picker-toggle-label");
// Check checked
if ($input.is(":checked")) {
$picker.addClass("checked");
}
// Check disabled
if ($input.is(":disabled")) {
$picker.addClass("disabled");
}
var data = $.extend({}, opts, {
$picker: $picker,
$input: $input,
$handle: $handle,
$label: $label,
$labels: $labels,
group: group,
isRadio: (type === "radio"),
isCheckbox: (type === "checkbox")
});
// Bind click events
data.$input.on("focus.picker", data, _onFocus)
.on("blur.picker", data, _onBlur)
.on("change.picker", data, _onChange)
.on("click.picker", data, _onClick)
.on("deselect.picker", data, _onDeselect)
.data("picker", data);
data.$picker.on("click.picker", data, _onClick);
}
}
/**
* @method private
* @name _onClick
* @description Handles instance clicks
* @param e [object] "Event data"
*/
function _onClick(e) {
e.stopPropagation();
var data = e.data;
if (!$(e.target).is(data.$input)) {
e.preventDefault();
data.$input.trigger("click");
}
}
/**
* @method private
* @name _onChange
* @description Handles external changes
* @param e [object] "Event data"
*/
function _onChange(e) {
var data = e.data;
if (!data.$input.is(":disabled")) {
// Checkbox change events fire after state has changed
var checked = data.$input.is(":checked");
if (data.isCheckbox) {
if (checked) {
_onSelect(e, true);
} else {
_onDeselect(e, true);
}
} else {
// radio
if (checked || (isIE8 && !checked)) {
_onSelect(e);
}
}
}
}
/**
* @method private
* @name _onSelect
* @description Changes input to "checked"
* @param e [object] "Event data"
* @param internal [boolean] "Internal update flag"
*/
function _onSelect(e, internal) {
var data = e.data;
if (typeof data.group !== "undefined" && data.isRadio) {
$('input[name="' + data.group + '"]').not(data.$input).trigger("deselect");
}
data.$picker.addClass("checked");
}
/**
* @method private
* @name _onDeselect
* @description Changes input from "checked"
* @param e [object] "Event data"
* @param internal [boolean] "Internal update flag"
*/
function _onDeselect(e, internal) {
var data = e.data;
data.$picker.removeClass("checked");
}
/**
* @method private
* @name _onFocus
* @description Handles instance focus
* @param e [object] "Event data"
*/
function _onFocus(e) {
e.data.$picker.addClass("focus");
}
/**
* @method private
* @name _onBlur
* @description Handles instance blur
* @param e [object] "Event data"
*/
function _onBlur(e) {
e.data.$picker.removeClass("focus");
}
$.fn.picker = function(method) {
if (pub[method]) {
return pub[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return _init.apply(this, arguments);
}
return this;
};
$.picker = function(method) {
if (method === "defaults") {
pub.defaults.apply(this, Array.prototype.slice.call(arguments, 1));
}
};
})(jQuery);