forked from scottdarby/Stylish-Select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.stylish-select.js
503 lines (432 loc) · 18.1 KB
/
jquery.stylish-select.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
/**
* Stylish Select 0.4.9 - jQuery plugin to replace a select drop down box with a stylable unordered list
* http://github.com/scottdarby/Stylish-Select
*
* Requires: jQuery 1.3 or newer
*
* Contributions from Justin Beasley: http://www.harvest.org/
* Anatoly Ressin: http://www.artazor.lv/ Wilfred Hughes: https://github.com/Wilfred
* Grigory Zarubin: https://github.com/Craigy-
*
* Dual licensed under the MIT and GPL licenses.
*/
(function($){
//Cross-browser implementation of indexOf from MDN: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf){
Array.prototype.indexOf = function(searchElement /*, fromIndex */){
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (len === 0)
return -1;
var n = 0;
if (arguments.length > 0){
n = Number(arguments[1]);
if (n !== n) // shortcut for verifying if it's NaN
n = 0;
else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
if (n >= len)
return -1;
var k = n >= 0
? n
: Math.max(len - Math.abs(n), 0);
for (; k < len; k++){
if (k in t && t[k] === searchElement)
return k;
}
return -1;
};
}
$(window).bind("disposeSS", function(){
$('html').removeClass('stylish-select');
$(window).unbind('resize.sSelect scroll.sSelect');
$(document).unbind('click.sSelect');
});
//utility methods
$.fn.extend({
getSetSSValue: function(value){
if (value){
//set value and trigger change event
$(this).val(value).change();
return this;
} else {
return $(this).find(':selected').val();
}
},
//added by Justin Beasley
resetSS: function(){
var oldOpts = $(this).data('ssOpts');
$this = $(this);
$this.next().remove();
//unbind all events and redraw
$this.unbind('.sSelect').sSelect(oldOpts);
}
});
$.fn.sSelect = function(options){
//add class to html tag
if(!$("html").hasClass('stylish-select')) $('html').addClass('stylish-select');
return this.each(function(){
var defaults = {
defaultText: 'Please select',
animationSpeed: 0, //set speed of dropdown
ddMaxHeight: '', //set css max-height value of dropdown
containerClass: '' //additional classes for container div
};
//initial variables
var opts = $.extend(defaults, options),
$input = $(this),
$containerDivText = $('<div class="selectedTxt"></div>'),
$containerDiv = $('<div class="newListSelected ' + opts.containerClass + ($input.is(':disabled') ? ' newListDisabled' : '') + '"></div>'),
$containerDivWrapper = $('<div class="SSContainerDivWrapper" style="visibility:hidden;"></div>'),
$newUl = $('<ul class="newList"></ul>'),
currentIndex = -1,
prevIndex = -1,
keys = [],
prevKey = false,
prevented = false,
$newLi;
//added by Justin Beasley
$(this).data('ssOpts',options);
if( $(this).next('.newListSelected').length ) {
return;
}
//build new list
$containerDiv.insertAfter($input);
$containerDiv.attr("tabindex", $input.attr("tabindex") || "0");
$containerDivText.prependTo($containerDiv);
$newUl.appendTo($containerDiv);
$newUl.wrap($containerDivWrapper);
$containerDivWrapper = $newUl.parent();
$input.hide();
if($input.is(':disabled')){
return;
}
//added by Justin Beasley (used for lists initialized while hidden)
$containerDivText.data('ssReRender',!$containerDivText.is(':visible'));
//add one item to list
function addItem(item, container) {
var option = $(item).text(),
key = $(item).val(),
isDisabled = $(item).is(':disabled');
if (!isDisabled && !$(item).parents().is(':disabled')) {
//add first letter of each word to array
keys.push(option.charAt(0).toLowerCase());
}
container.append($('<li><a'+(isDisabled ? ' class="newListItemDisabled"' : '')+' href="JavaScript:void(0);">'+option+'</a></li>').data({
'key' : key,
'selected' : $(item).is(':selected')
}));
}
$input.children().each(function(){
if ($(this).is('option')){
addItem(this, $newUl);
} else {
var optionTitle = $(this).attr('label'),
$optGroup = $('<li class="newListOptionTitle ' + ($(this).is(':disabled') ? 'newListOptionDisabled' : '') + '">'+optionTitle+'</li>'),
$optGroupList = $('<ul></ul>');
$optGroup.appendTo($newUl);
$optGroupList.appendTo($optGroup);
$(this).children().each(function(){
addItem(this, $optGroupList);
});
}
});
//cache list items object
$newLi = $newUl.find('li a:not(.newListItemDisabled)').not(function(){
return $(this).parents().hasClass('newListOptionDisabled');
});
//get selected item from new list (because it doesn't contain disabled options)
$newLi.each(function(i){
if ($(this).parent().data('selected')){
opts.defaultText = $(this).html();
currentIndex = prevIndex = i;
}
});
//get heights of new elements for use later
var newUlHeight = $newUl.height(),
containerHeight = $containerDiv.height(),
newLiLength = $newLi.length;
//check if a value is selected
if (currentIndex != -1){
navigateList(currentIndex);
} else {
//set placeholder text
$containerDivText.text(opts.defaultText);
}
//decide if to place the new list above or below the drop-down
function newUlPos(){
var containerPosY = $containerDiv.offset().top,
docHeight = $(window).height(),
scrollTop = $(window).scrollTop();
//if height of list is greater then max height, set list height to max height value
if (newUlHeight > parseInt(opts.ddMaxHeight)){
newUlHeight = parseInt(opts.ddMaxHeight);
}
containerPosY = containerPosY-scrollTop;
if (containerPosY+newUlHeight >= docHeight){
$newUl.css({
height: newUlHeight
});
$containerDivWrapper.css({
top: '-'+newUlHeight+'px',
height: newUlHeight
});
$input.onTop = true;
} else {
$newUl.css({
height: newUlHeight
});
$containerDivWrapper.css({
top: containerHeight+'px',
height: newUlHeight
});
$input.onTop = false;
}
}
//run function on page load
newUlPos();
//run function on browser window resize
$(window).bind('resize.sSelect scroll.sSelect', newUlPos);
//positioning
function positionFix(){
$containerDiv.css('position','relative');
}
function positionHideFix(){
$containerDiv.css(
{
position: 'static'
});
}
$containerDivText.bind('click.sSelect',function(event){
event.stopPropagation();
//added by Justin Beasley
if($(this).data('ssReRender')){
newUlHeight = $newUl.height('').height();
$containerDivWrapper.height('');
containerHeight = $containerDiv.height();
$(this).data('ssReRender',false);
newUlPos();
}
//hide all menus apart from this one
$('.SSContainerDivWrapper')
.not($(this).next())
.hide()
.parent()
.css('position', 'static')
.removeClass('newListSelFocus');
//show/hide this menu
$containerDivWrapper.toggle();
if($containerDivWrapper.is(":visible")){
$containerDiv.addClass("dropdown-active");
}else{
$containerDiv.removeClass("dropdown-active");
}
positionFix();
//scroll list to selected item
if(currentIndex == -1) currentIndex = 0;
try {
$newLi.eq(currentIndex).focus();
} catch(ex) {}
});
function closeDropDown(fireChange, resetText){
if(fireChange == true){
prevIndex = currentIndex;
$input.change();
}
if(resetText == true){
currentIndex = prevIndex;
navigateList(currentIndex);
}
$containerDiv.removeClass("dropdown-active");
$containerDivWrapper.hide();
positionHideFix();
}
$newLi.bind('click.sSelect',function(e){
var $clickedLi = $(e.target);
//update counter
currentIndex = $newLi.index($clickedLi);
//remove all hilites, then add hilite to selected item
prevented = true;
navigateList(currentIndex, true);
closeDropDown();
});
$newLi.bind('mouseenter.sSelect',
function(e){
var $hoveredLi = $(e.target);
$hoveredLi.addClass('newListHover');
}).bind('mouseleave.sSelect',
function(e){
var $hoveredLi = $(e.target);
$hoveredLi.removeClass('newListHover');
});
function navigateList(currentIndex, fireChange){
if(currentIndex == -1){
$containerDivText.text(opts.defaultText);
$newLi.removeClass('hiLite');
} else {
$newLi.removeClass('hiLite')
.eq(currentIndex)
.addClass('hiLite');
var text = $newLi.eq(currentIndex).text(),
val = $newLi.eq(currentIndex).parent().data('key');
try {
$input.val(val);
} catch(ex) {
// handle ie6 exception
$input[0].selectedIndex = currentIndex;
}
$containerDivText.text(text);
//only fire change event if specified
if(fireChange == true){
prevIndex = currentIndex;
$input.change();
}
if ($containerDivWrapper.is(':visible')){
try {
$newLi.eq(currentIndex).focus();
} catch(ex) {}
}
}
}
$input.bind('change.sSelect',function(event){
var $targetInput = $(event.target);
//stop change function from firing
if (prevented == true){
prevented = false;
return false;
}
var $currentOpt = $targetInput.find(':selected');
currentIndex = $targetInput.find('option').index($currentOpt);
navigateList(currentIndex);
});
//handle up and down keys
function keyPress(element){
//when keys are pressed
$(element).unbind('keydown.sSelect').bind('keydown.sSelect',function(e){
var keycode = e.which;
//prevent change function from firing
prevented = true;
switch(keycode){
case 40: //down
case 39: //right
incrementList();
return false;
break;
case 38: //up
case 37: //left
decrementList();
return false;
break;
case 33: //page up
case 36: //home
gotoFirst();
return false;
break;
case 34: //page down
case 35: //end
gotoLast();
return false;
break;
case 13: //enter
case 27: //esc
closeDropDown(true);
return false;
break;
case 9: //tab
closeDropDown(true);
nextFormElement();
return false;
break;
}
//check for keyboard shortcuts
keyPressed = String.fromCharCode(keycode).toLowerCase();
var currentKeyIndex = keys.indexOf(keyPressed);
if (typeof currentKeyIndex != 'undefined'){ //if key code found in array
++currentIndex;
currentIndex = keys.indexOf(keyPressed, currentIndex); //search array from current index
if (currentIndex == -1 || currentIndex == null || prevKey != keyPressed){
// if no entry was found or new key pressed search from start of array
currentIndex = keys.indexOf(keyPressed);
}
navigateList(currentIndex);
//store last key pressed
prevKey = keyPressed;
return false;
}
});
}
function incrementList(){
if (currentIndex < (newLiLength-1)){
++currentIndex;
navigateList(currentIndex);
}
}
function decrementList(){
if (currentIndex > 0){
--currentIndex;
navigateList(currentIndex);
}
}
function gotoFirst(){
currentIndex = 0;
navigateList(currentIndex);
}
function gotoLast(){
currentIndex = newLiLength-1;
navigateList(currentIndex);
}
$containerDiv.bind('click.sSelect',function(e){
e.stopPropagation();
keyPress(this);
});
$containerDiv.bind('focus.sSelect',function(){
$(this).addClass('newListSelFocus');
keyPress(this);
});
$containerDiv.bind('blur.sSelect',function(){
$(this).removeClass('newListSelFocus');
});
//hide list on blur
$(document).bind('click.sSelect',function(){
$containerDiv.removeClass('newListSelFocus');
if ($containerDivWrapper.is(':visible')){
closeDropDown(false, true);
} else {
closeDropDown(false);
}
});
//select next form element in document
function nextFormElement() {
var fields = $('body').find('button,input,textarea,select'),
index = fields.index($input);
if (index > -1 && (index + 1) < fields.length) {
fields.eq(index + 1).focus();
}
return false;
}
// handle focus on original select element
$input.focus(function(){
$input.next().focus();
});
//add classes on hover
$containerDivText.bind('mouseenter.sSelect',
function(e){
var $hoveredTxt = $(e.target);
$hoveredTxt.parent().addClass('newListSelHover');
}).bind('mouseleave.sSelect',
function(e){
var $hoveredTxt = $(e.target);
$hoveredTxt.parent().removeClass('newListSelHover');
});
//reset left property and hide
$containerDivWrapper.css({
left: '0',
display: 'none',
visibility: 'visible'
});
});
};
})(jQuery);