Skip to content
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

Add events and iOS compatibility #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 45 additions & 9 deletions auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
var autoComplete = (function(){
// "use strict";
function autoComplete(options){
var is_scrolling = false;
if (!document.querySelector) return;

// helpers
function hasClass(el, className){ return el.classList ? el.classList.contains(className) : new RegExp('\\b'+ className+'\\b').test(el.className); }

function hideSuggestions(elementSuggestions) {
if (elementSuggestions.sc.style.display !== 'none') {
o.onHideSuggestions();
}
elementSuggestions.sc.style.display = 'none';
}

function addEvent(el, type, handler){
if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler);
}
Expand Down Expand Up @@ -43,6 +51,8 @@ var autoComplete = (function(){
var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>';
},
onShowSuggestions: function() {},
onHideSuggestions: function() {},
onSelect: function(e, term, item){}
};
for (var k in options) { if (options.hasOwnProperty(k)) o[k] = options[k]; }
Expand All @@ -67,6 +77,9 @@ var autoComplete = (function(){
that.sc.style.top = Math.round(rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + o.offsetTop) + 'px';
that.sc.style.width = Math.round(rect.right - rect.left) + 'px'; // outerWidth
if (!resize) {
if (that.sc.style.display !== 'block') {
o.onShowSuggestions();
}
that.sc.style.display = 'block';
if (!that.sc.maxHeight) { that.sc.maxHeight = parseInt((window.getComputedStyle ? getComputedStyle(that.sc, null) : that.sc.currentStyle).maxHeight); }
if (!that.sc.suggestionHeight) that.sc.suggestionHeight = that.sc.querySelector('.autocomplete-suggestion').offsetHeight;
Expand Down Expand Up @@ -100,16 +113,33 @@ var autoComplete = (function(){
var v = this.getAttribute('data-val');
that.value = v;
o.onSelect(e, v, this);
that.sc.style.display = 'none';
hideSuggestions(that);
}
}, that.sc);

live('autocomplete-suggestion', 'touchstart', function(e){
is_scrolling = false;
}, that.sc);

live('autocomplete-suggestion', 'touchmove', function(e){
is_scrolling = true;
}, that.sc);

live('autocomplete-suggestion', 'touchend', function(e){
if (!is_scrolling) {
var v = this.getAttribute('data-val');
that.value = v;
o.onSelect(e, v, this);
hideSuggestions(that);
}
}, that.sc);

that.blurHandler = function(){
try { var over_sb = document.querySelector('.autocomplete-suggestions:hover'); } catch(e){ var over_sb = 0; }
if (!over_sb) {
that.last_val = that.value;
that.sc.style.display = 'none';
setTimeout(function(){ that.sc.style.display = 'none'; }, 350); // hide suggestions on fast input
hideSuggestions(that);
setTimeout(function(){ hideSuggestions(that); }, 350); // hide suggestions on fast input
} else if (that !== document.activeElement) setTimeout(function(){ that.focus(); }, 20);
};
addEvent(that, 'blur', that.blurHandler);
Expand All @@ -123,8 +153,9 @@ var autoComplete = (function(){
that.sc.innerHTML = s;
that.updateSC(0);
}
else
that.sc.style.display = 'none';
else {
hideSuggestions(that);
}
}

that.keydownHandler = function(e){
Expand All @@ -149,11 +180,16 @@ var autoComplete = (function(){
return false;
}
// esc
else if (key == 27) { that.value = that.last_val; that.sc.style.display = 'none'; }
else if (key == 27) { that.value = that.last_val; hideSuggestions(that); }
// enter
else if (key == 13 || key == 9) {
var sel = that.sc.querySelector('.autocomplete-suggestion.selected');
if (sel && that.sc.style.display != 'none') { o.onSelect(e, sel.getAttribute('data-val'), sel); setTimeout(function(){ that.sc.style.display = 'none'; }, 20); }
if (sel && that.sc.style.display != 'none') {
o.onSelect(e, sel.getAttribute('data-val'), sel);
setTimeout(function(){
hideSuggestions(that);
}, 20);
}
}
};
addEvent(that, 'keydown', that.keydownHandler);
Expand All @@ -178,7 +214,7 @@ var autoComplete = (function(){
}
} else {
that.last_val = val;
that.sc.style.display = 'none';
hideSuggestions(that);
}
}
};
Expand Down Expand Up @@ -219,4 +255,4 @@ var autoComplete = (function(){
module.exports = autoComplete;
else
window.autoComplete = autoComplete;
})();
})();