-
Notifications
You must be signed in to change notification settings - Fork 45
/
jquery.relevant-dropdown.js
194 lines (165 loc) · 5.79 KB
/
jquery.relevant-dropdown.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
(function($) {
// Make jQuery's :contains case insensitive (like HTML5 datalist)
// Changed the name to prevent overriding original functionality
$.expr[":"].RD_contains = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
$.fn.relevantDropdown = function(options) {
options = $.extend({
fadeOutSpeed: 'normal', // speed to fade out the dataList Popup
change: null
}, options);
return this.each(function() {
var $input = $(this),
list_id = $input.attr('list'),
$datalist = $("#" + list_id),
datalistItems = $datalist.find("option"),
searchPosition,
scrollValue = 0,
// Used to prevent reflow
temp_items = document.createDocumentFragment(),
temp_item = null;
// make sure we haven't already converted the datalist
if (!$("ul#" + list_id).is($datalist)) {
// Insert home for new fake datalist
$("<ul />", {
"class": "datalist",
"id" : list_id
}).appendTo($input.parent());
// Remove old datalist
$datalist.remove();
// Update pointer
$datalist = $("#" + list_id);
// Fill new fake datalist
datalistItems.each(function() {
temp_item = $("<li />", {
// .val is required here, not .text or .html
// HTML *needs* to be <option value="xxx"> not <option>xxx</option> (IE)
"text": $(this).val()
})[0];
temp_items.appendChild(temp_item);
});
$datalist.append(temp_items);
}
// Update pointer
datalistItems = $datalist.find("li");
// Typey type type
$input
.on("focus", function() {
// Reset scroll
$datalist.scrollTop(0);
scrollValue = 0;
})
.on("blur", function() {
// If this fires immediately, it prevents click-to-select from working
setTimeout(function() {
$datalist.fadeOut(options.fadeOutSpeed);
datalistItems.removeClass("active");
}, 500);
})
.on("keyup focus", function(e) {
searchPosition = $input.position();
// Build datalist
$datalist
.show()
.css({
top: searchPosition.top + $(this).outerHeight(),
left: searchPosition.left,
width: $input.outerWidth()
});
datalistItems.hide();
$datalist.find("li:RD_contains('" + $input.val() + "')").show();
});
// Don't want to use :hover in CSS so doing this instead
// really helps with arrow key navigation
datalistItems
.on("mouseenter", function() {
$(this).addClass("active").siblings().removeClass("active");
})
.on("mouseleave", function() {
$(this).removeClass("active");
});
// Window resize
$(window).resize(function() {
searchPosition = $input.position();
$datalist
.css({
top: searchPosition.top + $(this).outerHeight(),
left: searchPosition.left,
width: $input.outerWidth()
});
});
// Watch arrow keys for up and down
$input.on("keydown", function(e) {
var active = $datalist.find("li.active"),
datalistHeight = $datalist.outerHeight(),
datalistItemsHeight = datalistItems.outerHeight();
// up arrow
if ( e.keyCode == 38 ) {
if (active.length) {
prevAll = active.prevAll("li:visible");
if (prevAll.length > 0) {
active.removeClass("active");
prevAll.eq(0).addClass("active");
}
if ( prevAll.length && prevAll.position().top < 0 && scrollValue > 0 ){
$datalist.scrollTop(scrollValue-=datalistItemsHeight);
}
}
}
// down arrow
if ( e.keyCode == 40 ) {
if (active.length) {
var nextAll = active.nextAll("li:visible");
if (nextAll.length > 0) {
active.removeClass("active");
nextAll.eq(0).addClass("active");
}
if ( nextAll.length && (nextAll.position().top + datalistItemsHeight) >= datalistHeight ){
$datalist.stop().animate({
scrollTop: scrollValue += datalistItemsHeight
}, 200);
}
} else {
datalistItems.removeClass("active");
$datalist.find("li:visible:first").addClass("active");
}
}
// return or tab key
if ( e.keyCode == 13 || e.keyCode == 9 ){
if (active.length) {
$input.val(active.text());
item_selected(active.text());
}
$datalist.fadeOut(options.fadeOutSpeed);
datalistItems.removeClass("active");
}
// keys
if ( e.keyCode != 13 && e.keyCode != 38 && e.keyCode != 40 ){
// Reset active class
datalistItems.removeClass("active");
$datalist.find("li:visible:first").addClass("active");
// Reset scroll
$datalist.scrollTop(0);
scrollValue = 0;
}
});
// When choosing from dropdown
datalistItems.on("click", function() {
var active = $("li.active");
if (active.length) {
$input.val($(this).text());
}
$datalist.fadeOut(options.fadeOutSpeed);
datalistItems.removeClass("active");
item_selected($(this).text());
});
function item_selected(new_text) {
if( typeof options.change === 'function' )
options.change.call(this, new_text);
}
});
};
})(jQuery);