-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
combobox.js
410 lines (388 loc) · 12.1 KB
/
combobox.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
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
if (typeof UTILITIES_VERSION == "undefined" || UTILITIES_VERSION < 0.1) {
console.log("A suitable version of the Utilities class is not available");
}
COMBOBOX_VERSION = 0.1;
/*
Sample CSS for the combobox
.comboBoxList {
padding: 0px;
border: 1px solid #999;
background-color: #f7f7ff;
overflow: visible;
}
.comboBoxItem {
margin: 0px;
padding: 2px 5px;
background-color: inherit;
cursor: default;
}
.comboBoxSelectedItem {
background-color: #ddf;
}
*/
/**
* I create a new combobox, using the specified text input field and
* population callback. The item list is styled with three CSS
* classes: comboBoxList, comboBoxItem, and comboBoxSelectedItem, which
* are for the containing DIV, the individual item DIVs, and for the
* currently selected item DIV. Note that the selected item has both
* the item and selectedItem classes applied. Sample CSS is available
* in a comment at the top of the implementation file.
*
* The 'config' argument allows passing of additional parameters that
* further govern the behaviour of the combo box. Supported parameters
* are listed here:
* allowMultipleValues - whether the form field should allow multiple
* values to be provided. Each individual value will get it's own
* separate dropdown with, so a field value such as "dog,ca" would
* operate as if the value were just "ca" (i.e. just "ca" would be
* passed to the callback, and a selection choice would only
* replace the "ca"). Defaults to false.
* valueDelimiter - if allowMultipleValues is set to true, this is the
* character used to delimit the values. Defaults to a comma.
*
* @param idOrField The ID of the text field the combobox is based around, or the field itself.
* @param callback The function to call when the typed value changes.
* The function will be passed the current value of the field, and
* must return an array of values to display in the dropdown.
* @param config additional config parameters, as explained above.
* @param config An object containing configuration parameters for the
* instance.
*/
function ComboBox(idOrField, callback, config) {
var self = this;
// instance variables
this.config = config || new Object();
this.callback = callback;
this.availableItems = new Array();
this.selectedItemIndex = -1;
this.field =
typeof idOrField == "string"
? document.getElementById(idOrField)
: idOrField;
if (typeof this.field == "undefined") {
alert(
"You have specified an invalid id for the field you want to turn into a combo box",
);
}
this.dropdown = document.createElement("div");
this.isDropdownShowing = false;
this.oldonsubmit = null;
// configure the dropdown div
this.dropdown.className = "comboBoxList";
document.body.appendChild(this.dropdown);
this.dropdown.style.position = "absolute";
this.moveDropdown();
this.hideDropdown();
// initialize the field
this.field.comboBox = this;
this.field.oldValue = this.field.value;
this.field.onkeyup = ComboBox.onKeyUp;
this.field.moveCaretToEnd = function () {
if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(false);
range.select();
} else if (this.setSelectionRange) {
this.focus();
var length = this.value.length;
this.setSelectionRange(length, length);
}
};
this.field.onfocus = function () {
this.comboBox.oldonsubmit = this.form.onsubmit;
this.form.onsubmit = function () {
if (self.isDropdownShowing) {
return false;
}
if (self.oldonsubmit) {
self.oldonsubmit.call(this);
}
return true;
};
// repopulate and display the dropdown
this.comboBox.valueChanged();
};
this.field.onblur = function () {
var cb = this.comboBox;
this.hideTimeout = setTimeout(function () {
cb.hideDropdown();
}, 100);
this.form.onsubmit = cb.oldonsubmit;
};
// privileged methods
this.getConfigParam = function (name, defVal) {
return self.config[name] || defVal;
};
}
/**
* I am the onKeyDown listener that gets installed on the input field
* that is the core of the ComboBox. I handle action operations.
*
* @param e The event object on Mozilla browsers, null on IE
*/
ComboBox.onKeyDown = function (e) {
if (!e) {
e = window.event;
}
var capture = function () {
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
};
switch (e.keyCode) {
case 13: // enter
case 9: // tab
this.comboBox.chooseSelection();
capture();
return false;
case 27: // escape
this.comboBox.hideDropdown();
capture();
break;
case 38: // up arrow
this.comboBox.selectPrevious();
capture();
break;
case 40: // down arrow
this.comboBox.selectNext();
capture();
break;
}
};
/**
* I am the onKeyUp listener that gets installed on the input field
* that is the core of the ComboBox. I handle value-change operations.
*
* @param e The event object on Mozilla browsers, null on IE
*/
ComboBox.onKeyUp = function (e) {
if (!e) {
e = window.event;
}
var capture = function () {
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
};
switch (e.keyCode) {
case 38: // up arrow
case 40: // down arrow
this.moveCaretToEnd();
capture();
break;
default:
if (this.value != this.oldValue) {
this.comboBox.valueChanged();
this.oldValue = this.value;
}
capture();
}
};
/**
* I am called by the onKeyUp listener when the entered value changes,
* and am responsible for invoking the application callback function
* and repopulating the dropdown, if appropriate.
*/
ComboBox.prototype.valueChanged = function () {
var value = this.field.value;
if (this.getConfigParam("allowMultipleValues", false)) {
value = value.split(this.getConfigParam("valueDelimiter", ","));
value = value[value.length - 1].replace(/^ +/, "").replace(/ +$/, "");
}
var a = this.callback(value, this);
if (typeof a == "undefined") {
// to catch null returns
return;
}
this.setItems(a);
};
/**
* I can be called at any time with a new set of items to display in
* the dropdown.
*
* @param items The array of items that should be used for the dropdown
* values.
*/
ComboBox.prototype.setItems = function (items) {
if (typeof items != "object") {
alert("setItems wasn't passed a valid array: " + typeof a);
return;
}
this.availableItems = items;
this.populateDropdown();
};
/**
* I am called to repopulate the dropdown. There should never be a
* need to invoke me externally.
*/
ComboBox.prototype.populateDropdown = function () {
if (this.availableItems.length > 0) {
Utilities.removeChildren(this.dropdown);
for (var i = 0; i < this.availableItems.length; i++) {
var item = document.createElement("div");
item.className = "comboBoxItem";
item.innerText = this.availableItems[i];
item.id = "item_" + this.availableItems[i];
item.comboBox = this;
item.comboBoxIndex = i;
item.onmouseover = function () {
this.comboBox.select(this.comboBoxIndex);
};
item.onmousedown = function () {
this.comboBox.choose(this.comboBoxIndex);
};
this.dropdown.appendChild(item);
}
this.selectedItemIndex = 0;
this.updateSelection();
this.showDropdown();
} else {
this.selectedItemIndex = -1;
this.hideDropdown();
}
};
/**
* I am called by a mouse listener on the dropdown items to choose a
* specific item straight away.
*
* @param index The index of the item to choose
*/
ComboBox.prototype.choose = function (index) {
if (this.select(index)) {
this.chooseSelection();
}
};
/**
* I am called by the onKeyUp listener to indicate that the user wants
* to use the current selection as the new value of the field.
*/
ComboBox.prototype.chooseSelection = function () {
var i = this.selectedItemIndex;
var a = this.availableItems;
if (i >= 0 && i < a.length) {
var valueToAdd = a[i].replace(/<[^>]+>/g, "");
if (this.getConfigParam("allowMultipleValues", false)) {
var currentValue = "";
var delim = this.getConfigParam("valueDelimiter", ",");
var values = this.field.value.split(delim);
for (var j = 0; j < values.length - 1; j++) {
currentValue = Utilities.listAppend(currentValue, values[j], delim);
}
this.field.value = Utilities.listAppend(currentValue, valueToAdd, delim);
} else {
this.field.value = valueToAdd;
}
this.field.oldValue = this.field.value;
this.field.focus();
this.field.moveCaretToEnd();
this.hideDropdown();
}
};
/**
* I am called by a mouse listener on the dropdown items to select a
* specific item straight away.
*
* @param index The index of the item to select
* @return whether the selection happened (the index was valid)
*/
ComboBox.prototype.select = function (index) {
if (index < 0 || index >= this.availableItems.length) {
return false;
}
this.selectedItemIndex = index;
this.updateSelection();
return true;
};
/**
* I am called by the onKeyUp listener to indicate that the user wants
* to select the next option in the dropdown.
*/
ComboBox.prototype.selectNext = function () {
if (this.selectedItemIndex >= this.availableItems.length - 1) {
return false;
}
this.selectedItemIndex++;
this.updateSelection();
return true;
};
/**
* I am called by the onKeyUp listener to indicate that the user wants
* to select the previous option in the dropdown.
*/
ComboBox.prototype.selectPrevious = function () {
if (this.selectedItemIndex <= 0) {
return false;
}
this.selectedItemIndex--;
this.updateSelection();
};
/**
* I show the dropdown DIV.
*/
ComboBox.prototype.showDropdown = function () {
this.moveDropdown();
clearTimeout(this.field.hideTimeout);
this.dropdown.style.display = "block";
this.field.onkeydown = ComboBox.onKeyDown;
this.isDropdownShowing = true;
};
/**
* I hide the dropdown DIV.
*/
ComboBox.prototype.hideDropdown = function () {
var self = this;
setTimeout(function () {
self.isDropdownShowing = false;
}, 100);
this.field.onkeydown = null;
this.dropdown.style.display = "none";
};
ComboBox.prototype.moveDropdown = function () {
var offsets = Utilities.getOffsets(this.field);
this.dropdown.style.top =
offsets.y + (this.field.offsetHeight ? this.field.offsetHeight : 22) + "px";
this.dropdown.style.left = offsets.x + "px";
this.dropdown.style.width =
(this.field.offsetWidth ? this.field.offsetWidth : 100) + "px";
};
/**
* I update the dropdown so that the display reflects the internally
* selected item,
*/
ComboBox.prototype.updateSelection = function () {
for (var i = 0; i < this.dropdown.childNodes.length; i++) {
if (i == this.selectedItemIndex) {
this.dropdown.childNodes[i].className += " comboBoxSelectedItem";
} else {
this.dropdown.childNodes[i].className = this.dropdown.childNodes[
i
].className.replace(/ *comboBoxSelectedItem */g, "");
}
}
};