forked from FormstoneClassic/Selecter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fs.selecter.js
743 lines (640 loc) · 18.3 KB
/
jquery.fs.selecter.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*
* Selecter v3.0.12 - 2014-02-14
* A jQuery plugin for replacing default select elements. Part of the Formstone Library.
* http://formstone.it/selecter/
*
* Copyright 2014 Ben Plum; MIT Licensed
*/
;(function ($, window) {
"use strict";
var guid = 0,
isFirefox = window.navigator.userAgent.toLowerCase().indexOf('firefox') > -1,
isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test( (window.navigator.userAgent||window.navigator.vendor||window.opera) ),
$body = null;
/**
* @options
* @param callback [function] <$.noop> "Select item callback"
* @param cover [boolean] <false> "Cover handle with option set"
* @param customClass [string] <''> "Class applied to instance"
* @param label [string] <''> "Label displayed before selection"
* @param external [boolean] <false> "Open options as links in new window"
* @param links [boolean] <false> "Open options as links in same window"
* @param trim [int] <0> "Trim options to specified length; 0 to disable”
*/
var options = {
callback: $.noop,
cover: false,
customClass: "",
label: "",
external: false,
links: false,
trim: 0
};
var pub = {
/**
* @method
* @name defaults
* @description Sets default plugin options
* @param opts [object] <{}> "Options object"
* @example $.selecter("defaults", opts);
*/
defaults: function(opts) {
options = $.extend(options, opts || {});
return $(this);
},
/**
* @method
* @name destroy
* @description Removes instance of plugin
* @example $(".target").selecter("destroy");
*/
destroy: function() {
return $(this).each(function(i, input) {
var data = $(input).next(".selecter").data("selecter");
if (data) {
if (data.$selecter.hasClass("open")) {
data.$selecter.find(".selecter-selected").trigger("click.selecter");
}
// Scroller support
if ($.fn.scroller !== undefined) {
data.$selecter.find(".selecter-options").scroller("destroy");
}
data.$select[0].tabIndex = data.tabIndex;
data.$select.off(".selecter")
.removeClass("selecter-element")
.show();
data.$selecter.off(".selecter")
.remove();
}
});
},
/**
* @method
* @name disable
* @description Disables target instance or option
* @param option [string] <null> "Target option value"
* @example $(".target").selecter("disable", "1");
*/
disable: function(option) {
return $(this).each(function(i, input) {
var data = $(input).next(".selecter").data("selecter");
if (data) {
if (typeof option !== "undefined") {
var index = data.$items.index( data.$items.filter("[data-value=" + option + "]") );
data.$items.eq(index).addClass("disabled");
data.$options.eq(index).prop("disabled", true);
} else {
if (data.$selecter.hasClass("open")) {
data.$selecter.find(".selecter-selected").trigger("click.selecter");
}
data.$selecter.addClass("disabled");
data.$select.prop("disabled", true);
}
}
});
},
/**
* @method
* @name enable
* @description Enables target instance or option
* @param option [string] <null> "Target option value"
* @example $(".target").selecter("enable", "1");
*/
enable: function(option) {
return $(this).each(function(i, input) {
var data = $(input).next(".selecter").data("selecter");
if (data) {
if (typeof option !== "undefined") {
var index = data.$items.index( data.$items.filter("[data-value=" + option + "]") );
data.$items.eq(index).removeClass("disabled");
data.$options.eq(index).prop("disabled", false);
} else {
data.$selecter.removeClass("disabled");
data.$select.prop("disabled", false);
}
}
});
},
/**
* @method
* @name refresh
* @description Updates instance base on target options
* @example $(".target").selecter("refresh");
*/
refresh: function() {
return $(this).each(function(i, input) {
var data = $(input).next(".selecter").data("selecter");
if (data) {
var index = data.index;
data.$allOptions = data.$select.find("option, optgroup");
data.$options = data.$allOptions.filter("option");
data.index = -1;
index = data.$options.index(data.$options.filter(":selected"));
_buildOptions(data);
if (!data.multiple) {
_update(index, data);
}
}
});
}
};
/**
* @method private
* @name _init
* @description Initializes plugin
* @param opts [object] "Initialization options"
*/
function _init(opts) {
// Local options
opts = $.extend({}, options, opts || {});
// Check for Body
if ($body === null) {
$body = $("body");
}
// 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 $select [jQuery object] "Target jQuery object"
* @param opts [object] <{}> "Options object"
*/
function _build($select, opts) {
if (!$select.hasClass("selecter-element")) {
// EXTEND OPTIONS
opts = $.extend({}, opts, $select.data("selecter-options"));
if (opts.external) {
opts.links = true;
}
// Build options array
var $allOptions = $select.find("option, optgroup"),
$options = $allOptions.filter("option"),
$originalOption = $options.filter(":selected"),
originalIndex = ($originalOption.length > 0) ? $options.index($originalOption) : 1,
wrapperTag = (opts.links) ? "nav" : "div";
if (opts.label !== "") {
originalIndex = -1;
}
// Swap tab index, no more interacting with the actual select!
opts.tabIndex = $select[0].tabIndex;
$select[0].tabIndex = -1;
opts.multiple = $select.prop("multiple");
opts.disabled = $select.is(":disabled");
// Build HTML
var html = '<' + wrapperTag + ' class="selecter ' + opts.customClass;
// Special case classes
if (isMobile) {
html += ' mobile';
} else if (opts.cover) {
html += ' cover';
}
if (opts.multiple) {
html += ' multiple';
} else {
html += ' closed';
}
if (opts.disabled) {
html += ' disabled';
}
html += '" tabindex="' + opts.tabIndex + '">';
if (!opts.multiple) {
html += '<span class="selecter-selected' + ((opts.label !== "") ? ' placeholder' : '') + '">';
html += $('<span></span').text( _trim(((opts.label !== "") ? opts.label : $originalOption.text()), opts.trim) ).html();
html += '</span>';
}
html += '<div class="selecter-options">';
html += '</div>';
html += '</' + wrapperTag + '>';
// Modify DOM
$select.addClass("selecter-element")
.after(html);
// Store plugin data
var $selecter = $select.next(".selecter"),
data = $.extend({
$select: $select,
$allOptions: $allOptions,
$options: $options,
$selecter: $selecter,
$selected: $selecter.find(".selecter-selected"),
$itemsWrapper: $selecter.find(".selecter-options"),
index: -1,
guid: guid++
}, opts);
_buildOptions(data);
if (!data.multiple) {
_update(originalIndex, data);
}
// Scroller support
if ($.fn.scroller !== undefined) {
data.$itemsWrapper.scroller();
}
// Bind click events
data.$selecter.on("touchstart.selecter click.selecter", ".selecter-selected", data, _onClick)
.on("click.selecter", ".selecter-item", data, _onSelect)
.on("close.selecter", data, _onClose)
.data("selecter", data);
// Bind Blur/focus events
//if ((!data.links && !isMobile) || isMobile) {
data.$select.on("change.selecter", data, _onChange);
if (!isMobile) {
data.$selecter.on("focus.selecter", data, _onFocus)
.on("blur.selecter", data, _onBlur);
// handle clicks to associated labels - not on mobile
data.$select.on("focus.selecter", data, function(e) {
e.data.$selecter.trigger("focus");
});
}
//} else {
// Disable browser focus/blur for jump links
//data.$select.hide();
//}
}
}
/**
* @method private
* @name _buildOptions
* @description Builds instance's option set
* @param data [object] "Instance data"
*/
function _buildOptions(data) {
var html = '',
itemTag = (data.links) ? "a" : "span",
j = 0;
for (var i = 0, count = data.$allOptions.length; i < count; i++) {
var $op = data.$allOptions.eq(i);
// Option group
if ($op[0].tagName === "OPTGROUP") {
html += '<span class="selecter-group';
// Disabled groups
if ($op.is(":disabled")) {
html += ' disabled';
}
html += '">' + $op.attr("label") + '</span>';
} else {
var opVal = $op.val();
if (!$op.attr("value")) {
$op.attr("value", opVal);
}
html += '<' + itemTag + ' class="selecter-item';
// Default selected value - now handles multi's thanks to @kuilkoff
if ($op.is(':selected') && data.label === "") {
html += ' selected';
}
// Disabled options
if ($op.is(":disabled")) {
html += ' disabled';
}
html += '" ';
if (data.links) {
html += 'href="' + opVal + '"';
} else {
html += 'data-value="' + opVal + '"';
}
html += '>' + $("<span></span>").text( _trim($op.text(), data.trim) ).html() + '</' + itemTag + '>';
j++;
}
}
data.$itemsWrapper.html(html);
data.$items = data.$selecter.find(".selecter-item");
}
/**
* @method private
* @name _onClick
* @description Handles click to selected item
* @param e [object] "Event data"
*/
function _onClick(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
if (!data.$select.is(":disabled")) {
$(".selecter").not(data.$selecter).trigger("close.selecter", [data]);
// Handle mobile
if (isMobile) {
var el = data.$select[0];
if (window.document.createEvent) { // All
var evt = window.document.createEvent("MouseEvents");
evt.initMouseEvent("mousedown", false, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
} else if (el.fireEvent) { // IE
el.fireEvent("onmousedown");
}
} else {
// Delegate intent
if (data.$selecter.hasClass("closed")) {
_onOpen(e);
} else if (data.$selecter.hasClass("open")) {
_onClose(e);
}
}
}
}
/**
* @method private
* @name _onOpen
* @description Opens option set
* @param e [object] "Event data"
*/
function _onOpen(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
// Make sure it's not alerady open
if (!data.$selecter.hasClass("open")) {
var offset = data.$selecter.offset(),
bodyHeight = $body.outerHeight(),
optionsHeight = data.$itemsWrapper.outerHeight(true),
selectedOffset = (data.index >= 0) ? data.$items.eq(data.index).position() : { left: 0, top: 0 };
// Calculate bottom of document
if (offset.top + optionsHeight > bodyHeight) {
data.$selecter.addClass("bottom");
}
data.$itemsWrapper.show();
// Bind Events
data.$selecter.removeClass("closed")
.addClass("open");
$body.on("click.selecter-" + data.guid, ":not(.selecter-options)", data, _onCloseHelper);
if ($.fn.scroller !== undefined) {
data.$itemsWrapper.scroller("scroll", (data.$itemsWrapper.find(".scroller-content").scrollTop() + selectedOffset.top), 0)
.scroller("reset");
} else {
data.$itemsWrapper.scrollTop( data.$itemsWrapper.scrollTop() + selectedOffset.top );
}
}
}
/**
* @method private
* @name _onCloseHelper
* @description Determines if event target is outside instance before closing
* @param e [object] "Event data"
*/
function _onCloseHelper(e) {
e.preventDefault();
e.stopPropagation();
if ($(e.currentTarget).parents(".selecter").length === 0) {
_onClose(e);
}
}
/**
* @method private
* @name _onClose
* @description Closes option set
* @param e [object] "Event data"
*/
function _onClose(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
// Make sure it's actually open
if (data.$selecter.hasClass("open")) {
data.$itemsWrapper.hide();
data.$selecter.removeClass("open bottom")
.addClass("closed");
$body.off(".selecter-" + data.guid);
}
}
/**
* @method private
* @name _onSelect
* @description Handles option select
* @param e [object] "Event data"
*/
function _onSelect(e) {
e.preventDefault();
e.stopPropagation();
var $target = $(this),
data = e.data;
if (!data.$select.is(":disabled")) {
if (data.$itemsWrapper.is(":visible")) {
// Update
var index = data.$items.index($target);
if (index !== data.index) {
_update(index, data);
_handleChange(data);
}
}
if (!data.multiple) {
// Clean up
_onClose(e);
}
}
}
/**
* @method private
* @name _onChange
* @description Handles external changes
* @param e [object] "Event data"
*/
function _onChange(e, internal) {
var $target = $(this),
data = e.data;
if (!internal && !data.multiple) {
var index = data.$options.index(data.$options.filter("[value='" + _escape($target.val()) + "']"));
_update(index, data);
_handleChange(data);
}
}
/**
* @method private
* @name _onFocus
* @description Handles instance focus
* @param e [object] "Event data"
*/
function _onFocus(e) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
if (!data.$select.is(":disabled") && !data.multiple) {
data.$selecter.addClass("focus")
.on("keydown.selecter" + data.guid, data, _onKeypress);
$(".selecter").not(data.$selecter)
.trigger("close.selecter", [ data ]);
}
}
/**
* @method private
* @name _onBlur
* @description Handles instance focus
* @param e [object] "Event data"
*/
function _onBlur(e, internal, two) {
e.preventDefault();
e.stopPropagation();
var data = e.data;
data.$selecter.removeClass("focus")
.off("keydown.selecter" + data.guid + " keyup.selecter" + data.guid);
$(".selecter").not(data.$selecter)
.trigger("close.selecter", [ data ]);
}
/**
* @method private
* @name _onKeypress
* @description Handles instance keypress, once focused
* @param e [object] "Event data"
*/
function _onKeypress(e) {
var data = e.data;
if (e.keyCode === 13) {
if (data.$selecter.hasClass("open")) {
_onClose(e);
_update(data.index, data);
}
_handleChange(data);
} else if (e.keyCode !== 9 && (!e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey)) {
// Ignore modifiers & tabs
e.preventDefault();
e.stopPropagation();
var total = data.$items.length - 1,
index = (data.index < 0) ? 0 : data.index;
// Firefox left/right support thanks to Kylemade
if ($.inArray(e.keyCode, (isFirefox) ? [38, 40, 37, 39] : [38, 40]) > -1) {
// Increment / decrement using the arrow keys
index = index + ((e.keyCode === 38 || (isFirefox && e.keyCode === 37)) ? -1 : 1);
if (index < 0) {
index = 0;
}
if (index > total) {
index = total;
}
} else {
var input = String.fromCharCode(e.keyCode).toUpperCase(),
letter,
i;
// Search for input from original index
for (i = data.index + 1; i <= total; i++) {
letter = data.$options.eq(i).text().charAt(0).toUpperCase();
if (letter === input) {
index = i;
break;
}
}
// If not, start from the beginning
if (index < 0) {
for (i = 0; i <= total; i++) {
letter = data.$options.eq(i).text().charAt(0).toUpperCase();
if (letter === input) {
index = i;
break;
}
}
}
}
// Update
if (index >= 0) {
_update(index, data);
}
}
}
/**
* @method private
* @name _update
* @description Updates instance based on new target index
* @param index [int] "Selected option index"
* @param data [object] "instance data"
*/
function _update(index, data) {
var $item = data.$items.eq(index),
isSelected = $item.hasClass("selected"),
isDisabled = $item.hasClass("disabled");
// Check for disabled options
if (!isDisabled) {
// Make sure we have a new index to prevent false 'change' triggers
if (index === -1 && data.label !== "") {
data.$selected.html(data.label);
} else if (!isSelected) {
var newLabel = $item.html(),
newValue = $item.data("value");
// Modify DOM
if (data.multiple) {
data.$options.eq(index).prop("selected", true);
} else {
data.$selected.html(newLabel)
.removeClass('placeholder');
data.$items.filter(".selected")
.removeClass("selected");
data.$select[0].selectedIndex = index;
}
$item.addClass("selected");
} else if (data.multiple) {
data.$options.eq(index).prop("selected", null);
$item.removeClass("selected");
}
if (/* !isSelected || */ !data.multiple) {
// Update index
data.index = index;
}
}
}
function _handleChange(data) {
if (data.links) {
_launch(data);
} else {
data.callback.call(data.$selecter, data.$select.val(), data.index);
data.$select.trigger("change", [ true ]);
}
}
/**
* @method private
* @name _launch
* @description Launches link
* @param data [object] "Instance data"
*/
function _launch(data) {
//var url = (isMobile) ? data.$select.val() : data.$options.filter(":selected").attr("href");
var url = data.$select.val();
if (data.external) {
// Open link in a new tab/window
window.open(url);
} else {
// Open link in same tab/window
window.location.href = url;
}
}
/**
* @method private
* @name _trim
* @description Trims text, if specified length is greater then 0
* @param length [int] "Length to trim at"
* @param text [string] "Text to trim"
* @return [string] "Trimmed string"
*/
function _trim(text, length) {
if (length === 0) {
return text;
} else {
if (text.length > length) {
return text.substring(0, length) + "...";
} else {
return text;
}
}
}
/**
* @method private
* @name _escape
* @description Escapes text
* @param text [string] "Text to escape"
*/
function _escape(text) {
return text.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
}
$.fn.selecter = 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;
};
$.selecter = function(method) {
if (method === "defaults") {
pub.defaults.apply(this, Array.prototype.slice.call(arguments, 1));
}
};
})(jQuery, window);