-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjquery.ui.combify.js
270 lines (236 loc) · 11.4 KB
/
jquery.ui.combify.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
/*
* jQuery UI Combify
* A jQuery UI plugin to change any HTML select into a combobox where the user can enter data that isn't in the select list
*
* Copyright (c) October 2012 Tom Mooney
* licensed under the MIT license, http://www.opensource.org/licenses/mit-license.php
*/
(function ($) {
$.widget("ui.combify", {
options: {
capitalizeInput: false,
maxLength: 0
},
_create: function () {
var self = this,
select = self.element,
options = self.options,
id = select.prop('id'),
hiddenInputSelector = "#" + id,
textInputId = "CombifyInput-" + id,
textInputSelector = "#" + textInputId,
name = select.prop('name'),
selectedValue = select.find(':selected').val(),
selectedText = select.find(':selected').text(),
selectOptions = select.find('option'),
optionArray = new Array();
//Hide the original select
select.hide();
//Insert new HTML for a text input and a button to trigger the dropdown
select.before('<div>' +
'<span class="ui-combobox">' +
'<input type="hidden" class="insertedInput" id="' + id + '" name="' + name + '" value="' + selectedValue + '">' +
'<input type="text" id="' + textInputId + '" class="ui-combobox-input" value="' + selectedText + '">' +
'<a class="ui-combobox-toggle"></a>' +
'</span></div>');
//Remove the the id and name from the original select since they are now on the hidden input so that posted forms will get the correct value
select.removeAttr('id', null)
.removeAttr('name', null)
.on('change', function (event) {
event.stopPropagation();
});
//Get all the options from the select list and put them in an array for use in the autocomplete data source
selectOptions.each(function (i) {
optionArray.push($(this).text());
});
//Add autocomplete to the new input
$(textInputSelector).autocomplete({
source: optionArray,
select: function (event, ui) {
//For some reason selecting a value doesn't automatically trigger the change event on the input, so trigger it here
this.value = ui.item.value;
var option = $(select).find('option').filter(function () { return $(this).html() == ui.item.value; }).first()
var selectValue = option.val();
//set the value of the hidden input to the option value that matches the selected autocomplete value
$(hiddenInputSelector).val(selectValue);
$(this).trigger('change');
}
})
.on('change', function() {
var value = $(this).val();
var option = $(select).find('option').filter(function () { return $(this).html() == value; }).first()
//If no matching option is found in the select list, then set the hidden input to the entered value
if(!option.length){
$(hiddenInputSelector).val(value);
}
else{
$(hiddenInputSelector).val(option.val());
}
})
.on('blur', function () {
$(this).trigger('change');
});
//Convert entered values to upper case if capitalizeInput option is true
if (options.capitalizeInput) {
var input = $(textInputSelector);
input.css("text-transform", "uppercase").data('val', input.val()).on('keyup', function () {
//Make the value upper case if it has changed
var theInput = $(this);
if (theInput.data('val') != this.value) {
theInput.val(this.value.toUpperCase());
}
//Store the current value for comparison on next change
theInput.data('val', this.value);
theInput = null;
});
input = null;
}
//If maximum length is required
if (options.maxLength>0) {
$(textInputSelector).keyup(function () {
if($(this).val().length>options.maxLength) {
var TempVal=$(this).val();
$(this).data('val', TempVal.substring(0,options.maxLength));
$(this).val(TempVal.substring(0,options.maxLength));
}
});
}
//Attach a change event to the select list to put the selected value in the new text input
select.on('change', function () {
var hiddenInput = $(this).prev().find("#" + id).first(); //hidden input
var selectedValue = $(this).val();
var text = $(this).find("option:selected").text();
//find the option that matches the value
var option = $(select).find('option').filter(function () { return $(this).html() == text }).first()
hiddenInput.val(selectedValue);
$(textInputSelector).val(option.text()); //set the visible textbox to the value of the options text
hiddenInput.trigger('change');
});
//Add the button to trigger the dropdown
var button = select.prev().first().find(".ui-combobox-toggle");
button.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
});
//Attach the click event to the button to trigger the dropdown.
button.click(function (event) {
event.stopPropagation();
var minWidth = $(this).prev().first().width();
ExpandSelectList($(this), event, minWidth);
});
//Attach an event to expand the select list if the user presses Alt + DownArrow
$(textInputSelector).keydown(function (event) {
var list = $(this).parent().parent().next();
if (event.which === 40 && event.altKey) {
//If the list is already visible then just hide it
event.preventDefault();
event.stopPropagation();
if (list.is(":visible")) {
list.hide();
}
else {
if (options.capitalizeInput) {
this.value = this.value.toUpperCase();
}
$(textInputSelector).autocomplete("close");
ExpandSelectList($(this), event, $(this).width());
}
}
});
//Attach an event to close the list after a selection is made
var list = select.prev().first().find(".ui-combobox-list");
list.change(function () {
$(this).hide();
});
//private methods
function ExpandSelectList(element, event, minWidth) {
var list = element.parent().parent().next();
//If the list is already open or the autocomplete list is open then close the list.
if (list.is(":visible") || $(textInputSelector).autocomplete("widget").is(":visible")) {
list.hide();
}
else {
//Set the length of the select list to either the number of items in the list or 30, whichever is smaller
var size;
if (optionArray.length <= 30) {
size = optionArray.length;
}
else {
size = 30;
}
var sizeAttr = size === 1 ? 2 : size;
list.css({ "width": "auto",
"position": "absolute",
"z-index": "1"
}) //Puts the list on top of all other elements
.prop("size", sizeAttr) //changes the select list to a listbox so that it will "expand"
.show();
if (minWidth > list.width()) {
list.css("width", minWidth);
}
var listLineHeight = parseInt(list.find('option').first().css('font-size'),10);
list.css("height", listLineHeight * (size + 1));
//Attach a one-time event to the document to close the list if the user clicks anywhere else on the page.
$(document).one("click", function () {
list.hide();
});
function nextItem(event) {
var down = "down",
up = "up"
//If the user presses up arrow move to the previous item in the list
if (event.which === 38 && list.is(":visible")) {
move(up);
return;
}
//If user presess down arrow move to the next item in the list
if (event.which === 40 && list.is(":visible")) {
move(down);
return;
}
//if the user presses enter trigger the change event on the input to set it's value to the selected value
if (event.which === 13 && list.is(":visible")) {
list.trigger("change");
list.hide();
return;
}
if (list.is(":visible")) {
list.hide();
}
function move(direction) {
event.preventDefault();
var selected = list.find(":selected");
if (direction === down) {
var nextItem = selected.next();
}
if (direction === up) {
var nextItem = selected.prev();
}
selected.prop('selected', false);
nextItem.prop('selected', "selected");
}
}
//Attach an event to move through the list with the arrow keys
$(document).off("keydown.combifySelect")
.on("keydown.combifySelect", nextItem);
}
}
},
_destroy: function() {
var select = this.element,
inputObj = select.prev().find('.insertedInput'),
id = "",
name = "";
id = inputObj.prop('id');
name = inputObj.prop('name');
select.attr({
id: id,
name: name,
});
select.off('change');
select.prev().remove();
select.show();
}
});
})(jQuery);