-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.autocompleter.js
597 lines (548 loc) · 20 KB
/
jquery.autocompleter.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/**
* AutoCompleter jQuery plugin - autocompletion plugin for jQuery
* Copyright (C) 2009 Emmanuel Surleau
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Email: [email protected]
*/
// See included README for details on how to use it
jQuery.fn.autoCompleter = function(url, options) {
if (options == null && url == null)
throwError("No options and no URL specified");
if (options == null) {
if (typeof url == "string" || url instanceof String)
options = { url: url };
else
options = url;
}
else {
options.url = url;
}
var $input = jQuery(this);
addAutoCompleterDivs($input);
$input.attr("autocomplete", "off"); // don't want the browser to try and auto complete
// lock to avoid concurrency issues
var lock = false;
// cache is not stored in a single autocompleter - in theory, we might have
// several autocompleters plugged to the same URL - in this case, it's
// smarter to share the result of the queries
var cache = {};
var cacheSize = 0;
var reqId = 0; // Request id - make sure we execute the autocomplete action
//for the *last* input (avoids concurrency issues).
var settings = jQuery.extend({
rows: 10, // number of rows to display
resultKey: "results", // name of the key in the JSON result
autoWidth: true, // autocompute width based on the input tag (otherwise rely on CSS)
autoPosition: true, // autocompute position based on the input tag (otherwise rely on CSS)
rowHash: false, // JSON results are { display: "something to display", value : "something else to insert in the input box" }
method: "GET", // method to do the AJAX request
delay: 100, // delay in ms between invocation of the autocomplete
minChars: 1, // minimum number of characters to have in order to trigger the autocomplete
debugLevel: 0, // verbosity of the debug code (works only in FF but doesn't break in IE - from 0 (no output) to 3 (maximum verbosity)
maxQueriesToCache: 5, // hide five queries
autoCompleterKey: "autocompleter.self", // key used in the input's data hash to store its associated autocompleter object
originalValueKey: "autocompleter.originalValue", // key used in the jquery hash to remember the input's original value
rowValueKey: "autocompleter.rowValue", // key used to store the real value of a row
inputFilterCallback: null, // callback to filter the input value before making the query
queryCallback: null, // query to do the callback
selectRowCallback: null, // callback triggered when a row is selected
confirmSuggestionCallback: null, // callback after a suggestion is confirmed
resultsFilterCallback: null, // callback to filter the AJAX result hash
createResultRowCallback: null, // callback to create a row to add to the autocomplete popup
cachePrefix: '', // uses a prefix when doing lookups in the cache, for when
//autocompleters are plugged to different URLs
catchEnterEvent: false // if false, the keypress event is caught when the user presses 'enter' to select a suggestion, for example to prevent the submission of a form - which you don't want when forms have multiple fields
}, options);
$input.data(settings.autoCompleterKey, null);
$input.data(settings.originalValueKey, null);
// "Global" functions
function throwError(text) {
throw "Error in the jQuery AutoCompleter plugin: " + text;
}
function debug1(text) {
if (settings.debugLevel >= 1 && window.console)
window.console.debug("jQuery AutoCompleter debug: ", text);
}
function debug2(text) {
if (settings.debugLevel >= 2 && window.console)
window.console.debug("jQuery AutoCompleter debug: ", text);
}
function debug3(text) {
if (settings.debugLevel >= 3 && window.console)
window.console.debug("jQuery AutoCompleter debug: ", text);
}
function addAutoCompleterDivs($input) {
$input.after(
"<div class=\"autocompleter\" style=\"display: none;\"/>");
};
// Make sure that this particular autocompler is not accessed concurrently
function synchronize(callback) {
if (!lock) {
lock = true;
try {
callback();
}
catch(e) {
throwError(e);
}
finally {
lock = false;
}
}
// try again in 10 ms, pray the lock will be free
else setTimeout(callback, 10);
};
// Generates an autocompleter if needed
function getAutoCompleter($input) {
var key = settings.autoCompleterKey;
if (!$input.data(key))
$input.data(key, new AutoCompleter(settings, $input));
return $input.data(key);
}
// Handles an autocomplete session
function AutoCompleter(settings, $input) {
this.init(settings, $input);
};
// Constructor
AutoCompleter.prototype.init = function(settings, $input) {
this.settings = settings;
this.$input = $input;
this.$popup = this.getPopup();
}
// Returns the popup
AutoCompleter.prototype.getPopup = function() {
var $popup = this.$input.next().eq(0);
if (!$popup || !$popup.hasClass("autocompleter"))
throwError("The autocompleter div has disappeared!");
return $popup;
}
// Handles non-movement key processing
AutoCompleter.prototype.processKey = function(e) {
debug3("Received keypress event with code " + e.keyCode);
var isPopupVisible = this.isPopupVisible();
if (isPopupVisible)
this.$selectedRow = this.getSelectedRow();
switch (e.keyCode) {
case 40: // down
case 38: // up
case 39: // right
case 37: // left
break; // catch it in processMovementKeys
case 13: // enter
if (isPopupVisible) {
if (!this.settings.catchEnterEvent) {
this.strangleEvent(e);
}
this.confirmSuggestion();
}
break;
case 27: // escape
this.killPopup();
break;
case 8: // backspace
this.killPopup();
this.delayedAutoComplete();
break;
default:
this.isChanged = false;//Check
this.delayedAutoComplete();
break;
};
};
// Separate function, just to handle movement keys (due to problems in IE and
// webkit-based browsers)
AutoCompleter.prototype.processMovementKey = function(e) {
debug3("Received keydown event with code " + e.keyCode);
var isPopupVisible = this.isPopupVisible();
if (isPopupVisible)
this.$selectedRow = this.getSelectedRow();
switch (e.keyCode) {
case 40: // down
if (isPopupVisible) {
this.strangleEvent(e);
if (!this.nextSuggestion())
this.rollback();
}
else
this.delayedAutoComplete();
break;
case 38: // up
if (isPopupVisible) {
this.strangleEvent(e);
if (!this.previousSuggestion())
this.rollback();
}
break;
case 39: // right
if (isPopupVisible) {
this.strangleEvent(e);
this.confirmSuggestion();
}
break;
case 37: // left arrow
this.killPopup();
break;
};
}
// Strangles the event (prevents it from propagating)
AutoCompleter.prototype.strangleEvent = function(e) {
if (e.preventDefault) e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
}
// Retrieves the currently selected row, if any
AutoCompleter.prototype.getSelectedRow = function() {
var $selectedRows = this.$popup.find(".selected");
return $selectedRows.length > 0 ? $selectedRows.eq(0) : null;
}
// Retrieves the value of the currently selected row, if any
AutoCompleter.prototype.getSelectedValue = function() {
var $row = this.getSelectedRow();
if (!$row)
return null;
var value;
if (this.settings.rowHash)
value = $row.data(this.settings.rowValueKey);
else
value = $row.text();
return value;
}
// Select the next suggestion in the popup
AutoCompleter.prototype.nextSuggestion = function() {
debug2("Selecting next suggestion");
var newSelectedRows = this.$selectedRow != null ?
this.$selectedRow.next() : // select the row followig the current one
this.$popup.children(); // select the first row
var newSelectedRow = newSelectedRows.length > 0 ?
newSelectedRows.eq(0) :
null;
this.selectRow(newSelectedRow);
return (newSelectedRow != null);
}
// Select the previous suggestion in the popup
AutoCompleter.prototype.previousSuggestion = function() {
debug2("Selecting previous suggestion");
var newSelectedRows = this.$selectedRow != null ?
this.$selectedRow.prev() : // select the row preceding the current one
this.$popup.children(); // select the last row
var newSelectedRow = newSelectedRows.length > 0 ?
newSelectedRows.eq(newSelectedRows.length - 1) :
null;
this.selectRow(newSelectedRow);
return (newSelectedRow != null);
}
// Select a new row, if any
AutoCompleter.prototype.selectRow = function($row) {
this.$popup.find(".selected").removeClass("selected");
this.$selectedRow = $row;
if ($row != null) {
debug3("Selected row: " + $row);
$row.addClass("selected");
var rowOffset = 0;
$row.prevAll().each(function() { rowOffset += $(this).height() });
// check if the newly selected row is visible
if ((rowOffset >= (this.$popup.innerHeight() + this.$popup.scrollTop())) ||
(rowOffset < this.$popup.scrollTop()) )
this.$popup.scrollTop(rowOffset);
var key = this.settings.originalValueKey;
var originalValue = $input.data(key);
if (!originalValue)
$input.data(key, this.$input.val());
var value = this.getSelectedValue();
if (this.settings.selectRowCallback)
this.settings.selectRowCallback.call(this, value, $row.html());
else
$input.val(value);
this.isChanged = true;
}
else {
debug3("Selected null row");
}
}
// Returns the original value of the input box, before the popup was displayed
AutoCompleter.prototype.originalValue = function() {
return this.$input.data(this.settings.originalValueKey);
}
// Revert to original value
AutoCompleter.prototype.rollback = function() {
if (this.isInputTestChanged()) {
var originalValue = this.originalValue();
if (this.settings.rollbackCallback)
this.settings.rollbackCallback.call(this, originalValue);
else
this.$input.val(originalValue);
}
}
AutoCompleter.prototype.isInputTestChanged= function() {
return this.isChanged;
}
// Closes the popup and rolls back the changes
AutoCompleter.prototype.killPopup = function() {
if (this.isPopupVisible()) {
this.rollback(); // restore original content
this.$input.removeData(this.settings.originalValueKey);
this.hidePopup();
}
}
// User has selected an item
AutoCompleter.prototype.confirmSuggestion = function() {
if (this.$selectedRow != null) {
this.isChanged = false; //This is now the new default Input value.
this.$input.removeData(this.settings.originalValueKey);
this.hidePopup();
if (this.settings.confirmSuggestionCallback) {
debug1("Triggering confirmSuggestionCallback");
this.settings.confirmSuggestionCallback.call(this, this.getSelectedValue(), this.$selectedRow.html());
}
}
else {
this.killPopup();
}
}
// Returns true or false depending on whether the autocomplete popup is
// visible
AutoCompleter.prototype.isPopupVisible = function() {
return this.$popup.is(":visible");
}
// Starts the autocomplete with a delay if one has been set
AutoCompleter.prototype.delayedAutoComplete = function() {
var delay = this.settings.delay;
if (delay) {
var me = this;
reqId++;
var pReqId = reqId;
setTimeout(function () { me.autoComplete(pReqId); }, delay);
}
}
// Utility function to return the caret position in the input field
// Borrowed from http://www.webdeveloper.com/forum/showthread.php?t=74982
AutoCompleter.prototype.caretPosition = function() {
// Initialize
var iCaretPos = 0;
var oField = this.$input.get(0);
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus ();
// To get cursor position, get empty selection range
var oSel = document.selection.createRange ();
// Move selection start to 0 position
oSel.moveStart ('character', -oField.value.length);
// The caret position is selection length
iCaretPos = oSel.text.length;
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0')
iCaretPos = oField.selectionStart;
// Return results
return iCaretPos;
}
// Main action
AutoCompleter.prototype.autoComplete = function(pReqId) {
// Make sure we only execute the autocomplete for the *last* input action
if (reqId != pReqId){
return;
}
var criterion = this.$input.val();
//Save the criterion for restoring it later
this.$input.data(this.settings.originalValueKey, criterion);
// apply filter function, if any
if (this.settings.inputFilterCallback)
criterion = this.settings.inputFilterCallback.call(this, criterion);
if (criterion == null)
return;
// Not enough characters in the input, move along
if (this.settings.minChars && criterion.length < this.settings.minChars) {
debug2("Not enough characters to start the autocompletion");
return;
}
var me = this;
if (cache[me.settings.cachePrefix + criterion]) {
var resultsHash = me.cache(me.settings.cachePrefix + criterion);
me.handleQueryResults(resultsHash, pReqId);
}
// no cache
else {
var callback = function (results) {
me.cache(me.settings.cachePrefix + criterion, results);
me.handleQueryResults(results, pReqId);
};
if (this.settings.queryCallback) {
debug1("Triggering queryCallback");
this.settings.queryCallback.call(this, callback);
}
else {
var url = this.settings.url;
if (!url)
throwError("No url specified and no queryCallback");
var method = this.settings.method;
var params = {
q: criterion,
contentType: 'application/json; charset=utf-8'
};
debug1("Triggering AJAX call with method " + method + " to URL " + url + " with parameter q=" + criterion);
if (method == 'POST')
jQuery.post(url, params, callback, "json");
else jQuery.getJSON(url, params, callback);
}
}
}
AutoCompleter.prototype.cache = function(criterion, resultsHash) {
// we want to retrieve the results
if (resultsHash == null) {
var resultsData = cache[criterion];
if (resultsData) {
// update last access date
resultsData.lastAccess = new Date().getTime();
return resultsData.results;
}
else {
return null;
}
}
// we want to store them
else {
if (cacheSize >= this.maxQueriesToCache) {
// oups, we're exceeding our allowance, discard the oldest query
var oldestTime = new Date().getTime();
var oldestCrit = null;
var accessTime;
for (var key in cache) {
accessTime = cache[key].latestAccess;
if (accessTime < oldestTime) {
oldestCrit = key;
oldestTime = accessTime;
}
}
delete cache[oldestCrit];
cacheSize--;
}
// actually store in cache
cache[criterion] = {
results: resultsHash,
lastAccess: new Date().getTime()
};
cacheSize++;
}
}
// Handles the JSON results
AutoCompleter.prototype.handleQueryResults = function(resultsHash, pReqId) {
if (reqId != pReqId)
return;
debug2("Received following data: ")
debug2(resultsHash);
if (!resultsHash instanceof Object)
throwError("The data received is not a hash");
var results = null;
if (this.settings.resultsFilterCallback) {
results = this.settings.resultsFilterCallback.call(this, resultsHash);
}
else {
var key = this.settings.resultKey;
results = resultsHash[key];
if (results == null)
throwError("The result hash didn't contain the results key " + key);
}
if (results.length > 0) {
var me = this;
var callback = this.settings.createResultRowCallback ?
function(i, val) {
return me.settings.createResultRowCallback.call(me, i, val);
} :
function(i, val) {
return me.createResultRow(val);
}
this.$popup.empty();
// Create rows and append them to the popup
var maxRowCount = Math.min(this.settings.rows, (results.length - 1) );
me.$popup.css("height", "");
// Add this to the height of the popup to accomodate IE
var borderWidth = jQuery.browser.msie ?
(this.$popup.outerHeight() - this.$popup.innerHeight()) :
0;
jQuery.each(results, function(i, val) {
var display = me.settings.rowHash ? val.display : val;
var value = me.settings.rowHash ? val.value : val;
var row = callback(i, display);
debug3("Created from val " + value + " :row =" + row);
if (row) {
var $row = $(row);
if (me.settings.rowHash)
$row.data(me.settings.rowValueKey, value);
me.$popup.append($row);
if (maxRowCount != null && maxRowCount == i) {
// Looks stupid, innit? But this way we can force the height,
// and make sure we only display maxRowCount
me.$popup.height(me.$popup.height() + borderWidth);
}
$row.mousedown(function(e) {
me.strangleEvent(e);
me.selectRow($(this));
me.confirmSuggestion();
});
}
});
this.showPopup();
}
};
// Creates a single row to display a line of result in the popup
AutoCompleter.prototype.createResultRow = function(val) {
var row = document.createElement("div");
row.setAttribute("class", "row");
$(row).html(val);
return row;
};
AutoCompleter.prototype.showPopup = function() {
// same width as parent input, otherwise rely on CSS
var $popup = this.$popup;
var $input = this.$input;
if (settings.autoWidth == true)
$popup.width($input.width());
// same position wrt to the parent element as the input, otherwise rely on
// CSS
if (settings.autoPosition == true) {
var position = $input.position();
$popup.css("left", position.left);
$popup.css("top", position.top + $input.outerHeight());
};
debug3("Displaying the popup");
$popup.show();
};
AutoCompleter.prototype.hidePopup = function() {
this.$popup.hide();
};
// JQuery callback
return this.each(function() {
var $me = jQuery(this);
$me.keypress(function(e) {
synchronize(function() {
getAutoCompleter($me).processKey(e);
});
}).keyup(function(e) {
synchronize(function() {
getAutoCompleter($me).processMovementKey(e);
});
});
// Make the popup when the user clicks away
$me.blur(function() {
// use setTimeout, because blur will be sent also when the user clicks on
// a row in the popup - we need to associated action to do its thing first
setTimeout(function() {
synchronize(function() {
getAutoCompleter($me).killPopup();
});
}, 100);
});
});
};