/
jquery.sparkbox-select.js
195 lines (155 loc) · 6.54 KB
/
jquery.sparkbox-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
(function($) {
var selectboxCounter = 0;
$.fn.sbCustomSelect = function(options) {
var iOS = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)),
android = (navigator.userAgent.match(/Android/i)),
UP = 38, DOWN = 40, SPACE = 32, RETURN = 13, TAB = 9,
matchString = '',
settings = $.extend({
appendTo: false
}, options);
// Sync custom display with original select box and set selected class and the correct <li>
var updateSelect = function() {
var $this = $(this),
$dropdown = $('.sb-dropdown[data-id=' + $this.parent().data('id') + ']'),
$sbSelect = $this.siblings('.sb-select');
if (this.selectedIndex != -1) {
$sbSelect.val(this[this.selectedIndex].innerHTML);
$dropdown.children().removeClass('selected')
.filter(':contains(' + this[this.selectedIndex].innerHTML + ')').addClass('selected');
}
};
// Update original select box, hide <ul>, and fire change event to keep everything in sync
var dropdownSelection = function(e) {
var $target = $(e.target),
id = $target.closest('ul').attr('data-id'),
$option = $('.sb-custom[data-id=' + id + ']').find('option').filter('[value="' + $target.parent().data('value') + '"]');
e.preventDefault();
$option[0].selected = true;
$target.closest('ul').fadeOut('fast');
$option.parent().trigger('change');
};
// Create the <ul> that will be used to change the selection on a non iOS/Android browser
var createDropdown = function($select, i) {
var $options = $select.children(),
$dropdown = $('<ul data-id="' + i + '" class="sb-dropdown"/>');
$options.each(function() {
$this = $(this);
$dropdown.append('<li data-value="' + $this.val() + '"><a href=".">' + $this.text() + '</a></li>');
});
$dropdown.bind('click', dropdownSelection);
return $dropdown;
};
// Clear keystroke matching string and show dropdown
var viewList = function(e) {
var $this = $(this),
id = $this.data('id');
clearKeyStrokes();
$('.sb-dropdown').filter('[data-id!=' + id + ']').fadeOut('fast');
$('.sb-dropdown').filter('[data-id=' + id + ']').fadeIn('fast');
e.preventDefault();
};
// Hide the custom dropdown
var hideDropdown = function(e) {
if (!$(e.target).closest('.sb-custom').length) {
$('.sb-dropdown').fadeOut('fast');
}
};
// Manage keypress to replicate browser functionality
var selectKeypress = function(e) {
var $this = $(this),
$current = $('.sb-dropdown[data-id=' + $this.data('id') + ']').find('.selected');
// if ($('.sb-dropdown[data-id=' + $this.data('id') + ']').find('.selected') || $('.sb-dropdown[data-id=' + $this.data('id') + ']');
// $this.siblings('ul').find('.selected');
if ((e.keyCode == UP || e.keyCode == DOWN || e.keyCode == SPACE) && $current.is(':hidden')) {
$current.focus();
return;
}
if (e.keyCode == UP && $current.prev().length) {
e.preventDefault();
$current.removeClass('selected');
$current.prev().addClass('selected');
} else if (e.keyCode == DOWN && $current.next().length) {
e.preventDefault();
$current.removeClass('selected');
$current.next().addClass('selected');
}
if (e.keyCode == RETURN || e.keyCode == SPACE) {
$current.trigger('click');
return;
}
if (e.keyCode >= 48 && e.keyCode <= 90) {
matchString += String.fromCharCode(e.keyCode);
checkforMatch(e);
}
if (e.keyCode == TAB && $current.is(':visible')) {
e.preventDefault();
$current.trigger('click');
hideDropdown(e);
}
};
// Check keys pressed to see if there is a text match with one of the options
var checkforMatch = function(e) {
var re = '/' + matchString + '.*/';
$(e.target).siblings('ul').find('a').each(function() {
if (this.innerHTML.toUpperCase().indexOf(matchString) === 0) {
$(this).trigger('click');
return;
}
});
};
// Clear the string used for matching keystrokes to select options
var clearKeyStrokes = function() {
matchString = '';
};
/* jQuery Plugin Loop
*
* Take the select box out of the tab order.
*
* Add the field that will show the currently selected item and attach the change event to update the .sb-select input.
*
* If this is iOS or Android then we want to use the browsers standard UI controls. Set the opacity of the select to 0
* and lay it over our custom display of the current value.
* Otherwise, we're going to create a custom <ul> for the dropdown
*
* After all of the setup is complete, trigger the change event on the original select box to update the .sb-select input
*/
this.each(function() {
var $self = $(this);
$self.attr('tabindex', -1)
.wrap('<div data-id="' + selectboxCounter + '" class="sb-custom"/>')
.after('<input data-id="' + selectboxCounter + '" type="text" class="sb-select" readonly="readonly" />')
.bind('change', updateSelect);
if (iOS || android) {
$self.show().css({
'display': 'block',
'height': $self.next().innerHeight(),
'opacity': 0,
'position': 'absolute',
'width': '100%',
'z-index': 1000
});
} else {
$self.next().bind('click', viewList);
if (!settings.appendTo) {
$self.after(createDropdown($self, selectboxCounter));
} else {
var offset = $self.parent().offset();
$(settings.appendTo).append(createDropdown($self, selectboxCounter).css({
'top': offset.top,
'left': offset.left,
'width': $self.parent().width() * 0.8
}));
}
}
$self.trigger('change');
selectboxCounter++;
});
// Hide dropdown when click is outside of the input or dropdown
$(document).bind('click', hideDropdown);
$('.sb-custom').find('.sb-select').live('keydown', selectKeypress);
$('.sb-custom').bind('blur', clearKeyStrokes);
$(document).delegate('.sb-dropdown', 'focus', viewList);
return this;
};
})(jQuery);