Skip to content

Fix cache with spaces. Now you can find with multiple words. #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Copyright (c) 2014 Simon Steinberger / Pixabay
GitHub: https://github.com/Pixabay/JavaScript-autoComplete
License: http://www.opensource.org/licenses/mit-license.php
Fixed by: https://github.com/ProtocolNebula
*/

var autoComplete = (function(){
Expand Down Expand Up @@ -116,7 +117,8 @@ var autoComplete = (function(){

var suggest = function(data){
var val = that.value;
that.cache[val] = data;
var cleanVal = that.getClean(val);
that.cache[cleanVal] = data;
if (data.length && val.length >= o.minChars) {
var s = '';
for (var i=0;i<data.length;i++) s += o.renderItem(data[i], val);
Expand Down Expand Up @@ -163,17 +165,20 @@ var autoComplete = (function(){
if (!key || (key < 35 || key > 40) && key != 13 && key != 27) {
var val = that.value;
if (val.length >= o.minChars) {
if (o.cache) {
// Fix by ProtocolNebula for bug with spaces
var cleanVal = that.getClean(val);
if (cleanVal in that.cache) { suggest(that.cache[cleanVal]); return; }
// no requests if previous suggestions were empty
for (var i=1; i<cleanVal.length-o.minChars; i++) {
var part = cleanVal.slice(0, cleanVal.length-i);
if (part in that.cache && !that.cache[part].length) { suggest([]); return; }
}
}
if (val != that.last_val) {
that.last_val = val;
clearTimeout(that.timer);
if (o.cache) {
if (val in that.cache) { suggest(that.cache[val]); return; }
// no requests if previous suggestions were empty
for (var i=1; i<val.length-o.minChars; i++) {
var part = val.slice(0, val.length-i);
if (part in that.cache && !that.cache[part].length) { suggest([]); return; }
}
}

that.timer = setTimeout(function(){ o.source(val, suggest) }, o.delay);
}
} else {
Expand All @@ -184,11 +189,17 @@ var autoComplete = (function(){
};
addEvent(that, 'keyup', that.keyupHandler);

that.focusHandler = function(e){
that.last_val = '\n';
that.keyupHandler(e)
that.focusHandler = function(){
that.keyupHandler({keyCode:true, which: true});
};
addEvent(that, 'focus', that.focusHandler);

if (!o.minChars) addEvent(that, 'focus', that.focusHandler);

// clean a value to use as key
that.getClean = function(val) {
return val.replace(/[^a-zA-Z0-9]/g, '-');
}
}

// public destroy method
Expand Down
3 changes: 0 additions & 3 deletions auto-complete.min.js

This file was deleted.