mirror of
https://github.com/devbridge/jQuery-Autocomplete.git
synced 2024-11-09 23:01:00 +00:00
Update spacing and code style
This commit is contained in:
parent
b4de69f24f
commit
dec9dc298e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/*.user
|
@ -5,7 +5,7 @@
|
|||||||
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
|
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
|
||||||
* For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
|
* For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
|
||||||
*
|
*
|
||||||
* Last Review: 07/24/2012
|
* Last Review: 11/08/2012
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*jslint browser: true, white: true, plusplus: true */
|
/*jslint browser: true, white: true, plusplus: true */
|
||||||
@ -53,29 +53,28 @@
|
|||||||
this.el.data('autocomplete', this);
|
this.el.data('autocomplete', this);
|
||||||
}
|
}
|
||||||
|
|
||||||
$.fn.autocomplete = function (options, optionName) {
|
$.fn.autocomplete = function (options, args) {
|
||||||
|
var instance;
|
||||||
var autocompleteControl;
|
|
||||||
|
|
||||||
if (typeof options === 'string') {
|
if (typeof options === 'string') {
|
||||||
autocompleteControl = this.data('autocomplete');
|
instance = this.data('autocomplete');
|
||||||
if (typeof autocompleteControl[options] === 'function') {
|
if (typeof instance[options] === 'function') {
|
||||||
autocompleteControl[options](optionName);
|
instance[options](args);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
autocompleteControl = new Autocomplete(this.get(0) || $('<input />'), options);
|
instance = new Autocomplete(this.get(0) || $('<input />'), options);
|
||||||
}
|
}
|
||||||
return autocompleteControl;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
};
|
||||||
|
|
||||||
Autocomplete.prototype = {
|
Autocomplete.prototype = {
|
||||||
|
|
||||||
killerFn: null,
|
killerFn: null,
|
||||||
|
|
||||||
initialize: function () {
|
initialize: function () {
|
||||||
|
|
||||||
var me, uid, autocompleteElId;
|
var me, uid, autocompleteElId;
|
||||||
|
|
||||||
me = this;
|
me = this;
|
||||||
uid = Math.floor(Math.random() * 0x100000).toString(16);
|
uid = Math.floor(Math.random() * 0x100000).toString(16);
|
||||||
autocompleteElId = 'Autocomplete_' + uid;
|
autocompleteElId = 'Autocomplete_' + uid;
|
||||||
@ -111,12 +110,16 @@
|
|||||||
|
|
||||||
setOptions: function (options) {
|
setOptions: function (options) {
|
||||||
var o = this.options;
|
var o = this.options;
|
||||||
|
|
||||||
this.extendOptions(options);
|
this.extendOptions(options);
|
||||||
|
|
||||||
if (o.lookup || o.isLocal) {
|
if (o.lookup || o.isLocal) {
|
||||||
this.isLocal = true;
|
this.isLocal = true;
|
||||||
if ($.isArray(o.lookup)) { o.lookup = { suggestions: o.lookup, data: [] }; }
|
if ($.isArray(o.lookup)) { o.lookup = { suggestions: o.lookup, data: [] }; }
|
||||||
}
|
}
|
||||||
|
|
||||||
$('#' + this.mainContainerId).css({ zIndex: o.zIndex });
|
$('#' + this.mainContainerId).css({ zIndex: o.zIndex });
|
||||||
|
|
||||||
this.container.css({ maxHeight: o.maxHeight + 'px', width: o.width });
|
this.container.css({ maxHeight: o.maxHeight + 'px', width: o.width });
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -150,8 +153,11 @@
|
|||||||
|
|
||||||
killSuggestions: function () {
|
killSuggestions: function () {
|
||||||
var me = this;
|
var me = this;
|
||||||
this.stopKillSuggestions();
|
me.stopKillSuggestions();
|
||||||
this.intervalId = window.setInterval(function () { me.hide(); me.stopKillSuggestions(); }, 300);
|
me.intervalId = window.setInterval(function () {
|
||||||
|
me.hide();
|
||||||
|
me.stopKillSuggestions();
|
||||||
|
}, 300);
|
||||||
},
|
},
|
||||||
|
|
||||||
stopKillSuggestions: function () {
|
stopKillSuggestions: function () {
|
||||||
@ -163,9 +169,10 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
onKeyPress: function (e) {
|
onKeyPress: function (e) {
|
||||||
if (this.disabled || !this.enabled) { return; }
|
if (this.disabled || !this.enabled) {
|
||||||
// return will exit the function
|
return;
|
||||||
// and event will not be prevented
|
}
|
||||||
|
|
||||||
switch (e.keyCode) {
|
switch (e.keyCode) {
|
||||||
case 27: //KEY_ESC:
|
case 27: //KEY_ESC:
|
||||||
this.el.val(this.currentValue);
|
this.el.val(this.currentValue);
|
||||||
@ -178,7 +185,9 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.select(this.selectedIndex);
|
this.select(this.selectedIndex);
|
||||||
if (e.keyCode === 9) { return; }
|
if (e.keyCode === 9) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 38: //KEY_UP:
|
case 38: //KEY_UP:
|
||||||
this.moveUp();
|
this.moveUp();
|
||||||
@ -189,18 +198,25 @@
|
|||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cancel event if function did not return:
|
||||||
e.stopImmediatePropagation();
|
e.stopImmediatePropagation();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
},
|
},
|
||||||
|
|
||||||
onKeyUp: function (e) {
|
onKeyUp: function (e) {
|
||||||
if (this.disabled) { return; }
|
if (this.disabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (e.keyCode) {
|
switch (e.keyCode) {
|
||||||
case 38: //KEY_UP:
|
case 38: //KEY_UP:
|
||||||
case 40: //KEY_DOWN:
|
case 40: //KEY_DOWN:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
clearInterval(this.onChangeInterval);
|
clearInterval(this.onChangeInterval);
|
||||||
|
|
||||||
if (this.currentValue !== this.el.val()) {
|
if (this.currentValue !== this.el.val()) {
|
||||||
if (this.options.deferRequestBy > 0) {
|
if (this.options.deferRequestBy > 0) {
|
||||||
// Defer lookup in case when value changes very quickly:
|
// Defer lookup in case when value changes very quickly:
|
||||||
@ -217,10 +233,12 @@
|
|||||||
this.currentValue = this.el.val();
|
this.currentValue = this.el.val();
|
||||||
var q = this.getQuery(this.currentValue);
|
var q = this.getQuery(this.currentValue);
|
||||||
this.selectedIndex = -1;
|
this.selectedIndex = -1;
|
||||||
|
|
||||||
if (this.ignoreValueChange) {
|
if (this.ignoreValueChange) {
|
||||||
this.ignoreValueChange = false;
|
this.ignoreValueChange = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (q === '' || q.length < this.options.minChars) {
|
if (q === '' || q.length < this.options.minChars) {
|
||||||
this.hide();
|
this.hide();
|
||||||
} else {
|
} else {
|
||||||
@ -231,17 +249,21 @@
|
|||||||
getQuery: function (val) {
|
getQuery: function (val) {
|
||||||
var d, arr;
|
var d, arr;
|
||||||
d = this.options.delimiter;
|
d = this.options.delimiter;
|
||||||
if (!d) { return $.trim(val); }
|
if (!d) {
|
||||||
|
return $.trim(val);
|
||||||
|
}
|
||||||
arr = val.split(d);
|
arr = val.split(d);
|
||||||
return $.trim(arr[arr.length - 1]);
|
return $.trim(arr[arr.length - 1]);
|
||||||
},
|
},
|
||||||
|
|
||||||
getSuggestionsLocal: function (q) {
|
getSuggestionsLocal: function (q) {
|
||||||
var ret, arr, len, val, i;
|
var ret, arr, len, val, i;
|
||||||
|
|
||||||
arr = this.options.lookup;
|
arr = this.options.lookup;
|
||||||
len = arr.suggestions.length;
|
len = arr.suggestions.length;
|
||||||
ret = { suggestions: [], data: [] };
|
ret = { suggestions: [], data: [] };
|
||||||
q = q.toLowerCase();
|
q = q.toLowerCase();
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
val = arr.suggestions[i];
|
val = arr.suggestions[i];
|
||||||
if (val.toLowerCase().indexOf(q) === 0) {
|
if (val.toLowerCase().indexOf(q) === 0) {
|
||||||
@ -249,13 +271,15 @@
|
|||||||
ret.data.push(arr.data[i]);
|
ret.data.push(arr.data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
|
|
||||||
getSuggestions: function (q) {
|
getSuggestions: function (q) {
|
||||||
|
|
||||||
var cr, me;
|
var cr, me;
|
||||||
cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q]; //dadeta this.options.isLocal ||
|
|
||||||
|
cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q];
|
||||||
|
|
||||||
if (cr && $.isArray(cr.suggestions)) {
|
if (cr && $.isArray(cr.suggestions)) {
|
||||||
this.suggestions = cr.suggestions;
|
this.suggestions = cr.suggestions;
|
||||||
this.data = cr.data;
|
this.data = cr.data;
|
||||||
@ -263,15 +287,21 @@
|
|||||||
} else if (!this.isBadQuery(q)) {
|
} else if (!this.isBadQuery(q)) {
|
||||||
me = this;
|
me = this;
|
||||||
me.options.params.query = q;
|
me.options.params.query = q;
|
||||||
$.get(this.serviceUrl, me.options.params, function (txt) { me.processResponse(txt); }, 'text');
|
$.get(this.serviceUrl, me.options.params, function (txt) {
|
||||||
|
me.processResponse(txt);
|
||||||
|
}, 'text');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
isBadQuery: function (q) {
|
isBadQuery: function (q) {
|
||||||
var i = this.badQueries.length;
|
var i = this.badQueries.length;
|
||||||
|
|
||||||
while (i--) {
|
while (i--) {
|
||||||
if (q.indexOf(this.badQueries[i]) === 0) { return true; }
|
if (q.indexOf(this.badQueries[i]) === 0) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -282,20 +312,32 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
suggest: function () {
|
suggest: function () {
|
||||||
|
|
||||||
if (this.suggestions.length === 0) {
|
if (this.suggestions.length === 0) {
|
||||||
this.hide();
|
this.hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var me, len, div, f, v, i, s, mOver, mClick;
|
var me, len, div, f, v, i, s, mOver, mClick;
|
||||||
|
|
||||||
me = this;
|
me = this;
|
||||||
len = this.suggestions.length;
|
len = this.suggestions.length;
|
||||||
f = this.options.fnFormatResult;
|
f = this.options.fnFormatResult;
|
||||||
v = this.getQuery(this.currentValue);
|
v = this.getQuery(this.currentValue);
|
||||||
mOver = function (xi) { return function () { me.activate(xi); }; };
|
|
||||||
mClick = function (xi) { return function () { me.select(xi); }; };
|
mOver = function (xi) {
|
||||||
|
return function () {
|
||||||
|
me.activate(xi);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
mClick = function (xi) {
|
||||||
|
return function () {
|
||||||
|
me.select(xi);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
this.container.hide().empty();
|
this.container.hide().empty();
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
s = this.suggestions[i];
|
s = this.suggestions[i];
|
||||||
div = $((me.selectedIndex === i ? '<div class="selected"' : '<div') + ' title="' + s + '">' + f(s, this.data[i], v) + '</div>');
|
div = $((me.selectedIndex === i ? '<div class="selected"' : '<div') + ' title="' + s + '">' + f(s, this.data[i], v) + '</div>');
|
||||||
@ -303,6 +345,7 @@
|
|||||||
div.click(mClick(i));
|
div.click(mClick(i));
|
||||||
this.container.append(div);
|
this.container.append(div);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.enabled = true;
|
this.enabled = true;
|
||||||
this.container.show();
|
this.container.show();
|
||||||
},
|
},
|
||||||
@ -310,14 +353,24 @@
|
|||||||
processResponse: function (text) {
|
processResponse: function (text) {
|
||||||
/*jslint evil: true */
|
/*jslint evil: true */
|
||||||
var response;
|
var response;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = eval('(' + text + ')');
|
response = eval('(' + text + ')');
|
||||||
} catch (err) { return; }
|
} catch (err) {
|
||||||
if (!$.isArray(response.data)) { response.data = []; }
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$.isArray(response.data)) {
|
||||||
|
response.data = [];
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.options.noCache) {
|
if (!this.options.noCache) {
|
||||||
this.cachedResponse[response.query] = response;
|
this.cachedResponse[response.query] = response;
|
||||||
if (response.suggestions.length === 0) { this.badQueries.push(response.query); }
|
if (response.suggestions.length === 0) {
|
||||||
|
this.badQueries.push(response.query);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (response.query === this.getQuery(this.currentValue)) {
|
if (response.query === this.getQuery(this.currentValue)) {
|
||||||
this.suggestions = response.suggestions;
|
this.suggestions = response.suggestions;
|
||||||
this.data = response.data;
|
this.data = response.data;
|
||||||
@ -327,6 +380,7 @@
|
|||||||
|
|
||||||
activate: function (index) {
|
activate: function (index) {
|
||||||
var divs, activeItem;
|
var divs, activeItem;
|
||||||
|
|
||||||
divs = this.container.children();
|
divs = this.container.children();
|
||||||
// Clear previous selection:
|
// Clear previous selection:
|
||||||
if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) {
|
if (this.selectedIndex !== -1 && divs.length > this.selectedIndex) {
|
||||||
@ -346,17 +400,23 @@
|
|||||||
|
|
||||||
deactivate: function (div, index) {
|
deactivate: function (div, index) {
|
||||||
div.className = '';
|
div.className = '';
|
||||||
if (this.selectedIndex === index) { this.selectedIndex = -1; }
|
if (this.selectedIndex === index) {
|
||||||
|
this.selectedIndex = -1;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
select: function (i) {
|
select: function (i) {
|
||||||
var selectedValue, f;
|
var selectedValue, f;
|
||||||
|
|
||||||
selectedValue = this.suggestions[i];
|
selectedValue = this.suggestions[i];
|
||||||
|
|
||||||
if (selectedValue) {
|
if (selectedValue) {
|
||||||
this.el.val(selectedValue);
|
this.el.val(selectedValue);
|
||||||
if (this.options.autoSubmit) {
|
if (this.options.autoSubmit) {
|
||||||
f = this.el.parents('form');
|
f = this.el.parents('form');
|
||||||
if (f.length > 0) { f.get(0).submit(); }
|
if (f.length > 0) {
|
||||||
|
f.get(0).submit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.ignoreValueChange = true;
|
this.ignoreValueChange = true;
|
||||||
this.hide();
|
this.hide();
|
||||||
@ -365,73 +425,97 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
change: function (i) {
|
change: function (i) {
|
||||||
var selectedValue, fn, me, s, d;
|
var selectedValue, onChange, me, s, d;
|
||||||
|
|
||||||
me = this;
|
me = this;
|
||||||
selectedValue = this.suggestions[i];
|
selectedValue = this.suggestions[i];
|
||||||
|
|
||||||
if (selectedValue) {
|
if (selectedValue) {
|
||||||
s = me.suggestions[i];
|
s = me.suggestions[i];
|
||||||
d = me.data[i];
|
d = me.data[i];
|
||||||
me.el.val(me.getValue(s));
|
me.el.val(me.getValue(s));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
s = '';
|
s = '';
|
||||||
d = -1;
|
d = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn = me.options.onChange;
|
onChange = me.options.onChange;
|
||||||
if ($.isFunction(fn)) { fn(s, d, me.el); }
|
if ($.isFunction(onChange)) {
|
||||||
|
onChange(s, d, me.el);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
moveUp: function () {
|
moveUp: function () {
|
||||||
if (this.selectedIndex === -1) { return; }
|
if (this.selectedIndex === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.selectedIndex === 0) {
|
if (this.selectedIndex === 0) {
|
||||||
this.container.children().get(0).className = '';
|
this.container.children().get(0).className = '';
|
||||||
this.selectedIndex = -1;
|
this.selectedIndex = -1;
|
||||||
this.el.val(this.currentValue);
|
this.el.val(this.currentValue);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.adjustScroll(this.selectedIndex - 1);
|
this.adjustScroll(this.selectedIndex - 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
moveDown: function () {
|
moveDown: function () {
|
||||||
if (this.selectedIndex === (this.suggestions.length - 1)) { return; }
|
if (this.selectedIndex === (this.suggestions.length - 1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.adjustScroll(this.selectedIndex + 1);
|
this.adjustScroll(this.selectedIndex + 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
adjustScroll: function (i) {
|
adjustScroll: function (i) {
|
||||||
var activeItem, offsetTop, upperBound, lowerBound;
|
var activeItem, offsetTop, upperBound, lowerBound;
|
||||||
|
|
||||||
activeItem = this.activate(i);
|
activeItem = this.activate(i);
|
||||||
offsetTop = activeItem.offsetTop;
|
offsetTop = activeItem.offsetTop;
|
||||||
upperBound = this.container.scrollTop();
|
upperBound = this.container.scrollTop();
|
||||||
lowerBound = upperBound + this.options.maxHeight - 25;
|
lowerBound = upperBound + this.options.maxHeight - 25;
|
||||||
|
|
||||||
if (offsetTop < upperBound) {
|
if (offsetTop < upperBound) {
|
||||||
this.container.scrollTop(offsetTop);
|
this.container.scrollTop(offsetTop);
|
||||||
} else if (offsetTop > lowerBound) {
|
} else if (offsetTop > lowerBound) {
|
||||||
this.container.scrollTop(offsetTop - this.options.maxHeight + 25);
|
this.container.scrollTop(offsetTop - this.options.maxHeight + 25);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.el.val(this.getValue(this.suggestions[i]));
|
this.el.val(this.getValue(this.suggestions[i]));
|
||||||
},
|
},
|
||||||
|
|
||||||
onSelect: function (i) {
|
onSelect: function (i) {
|
||||||
var me, fn, s, d;
|
var me = this,
|
||||||
me = this;
|
callback = me.options.onSelect,
|
||||||
fn = me.options.onSelect;
|
sugestion = me.suggestions[i],
|
||||||
s = me.suggestions[i];
|
data = me.data[i];
|
||||||
d = me.data[i];
|
|
||||||
me.el.val(me.getValue(s));
|
me.el.val(me.getValue(sugestion));
|
||||||
if ($.isFunction(fn)) { fn(s, d, me.el); }
|
|
||||||
|
if ($.isFunction(callback)) {
|
||||||
|
callback(sugestion, data, me.el);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getValue: function (value) {
|
getValue: function (value) {
|
||||||
var del, currVal, arr, me;
|
var me = this,
|
||||||
me = this;
|
separator = me.options.delimiter,
|
||||||
del = me.options.delimiter;
|
currentValue,
|
||||||
if (!del) { return value; }
|
array;
|
||||||
currVal = me.currentValue;
|
|
||||||
arr = currVal.split(del);
|
if (!separator) {
|
||||||
if (arr.length === 1) { return value; }
|
return value;
|
||||||
return currVal.substr(0, currVal.length - arr[arr.length - 1].length) + value;
|
}
|
||||||
|
|
||||||
|
currentValue = me.currentValue;
|
||||||
|
array = currentValue.split(separator);
|
||||||
|
|
||||||
|
if (array.length === 1) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentValue.substr(0, currentValue.length - array[array.length - 1].length) + value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}(jQuery));
|
}(jQuery));
|
||||||
|
Loading…
Reference in New Issue
Block a user