Add configurable keypress delay before initiating search (#764)

This commit is contained in:
eliat123
2017-06-19 09:15:59 +03:00
committed by Robert Lord
parent a6794138ef
commit 2a63948592

View File

@@ -6,6 +6,8 @@
var content, searchResults;
var highlightOpts = { element: 'span', className: 'search-highlight' };
var searchDelay = 0;
var timeoutHandle = 0;
var index = new lunr.Index();
@@ -27,24 +29,44 @@
body: body.text()
});
});
determineSearchDelay();
}
function determineSearchDelay() {
if(index.tokenStore.length>5000) {
searchDelay = 300;
}
}
function bind() {
content = $('.content');
searchResults = $('.search-results');
$('#input-search').on('keyup', search);
$('#input-search').on('keyup',function(e) {
var wait = function() {
return function(executingFunction, waitTime){
clearTimeout(timeoutHandle);
timeoutHandle = setTimeout(executingFunction, waitTime);
};
}();
wait(function(){
search(e);
}, searchDelay );
});
}
function search(event) {
var searchInput = $('#input-search')[0];
unhighlight();
searchResults.addClass('visible');
// ESC clears the field
if (event.keyCode === 27) this.value = '';
if (event.keyCode === 27) searchInput.value = '';
if (this.value) {
var results = index.search(this.value).filter(function(r) {
if (searchInput.value) {
var results = index.search(searchInput.value).filter(function(r) {
return r.score > 0.0001;
});
@@ -54,10 +76,10 @@
var elem = document.getElementById(result.ref);
searchResults.append("<li><a href='#" + result.ref + "'>" + $(elem).text() + "</a></li>");
});
highlight.call(this);
highlight.call(searchInput);
} else {
searchResults.html('<li></li>');
$('.search-results li').text('No Results Found for "' + this.value + '"');
$('.search-results li').text('No Results Found for "' + searchInput.value + '"');
}
} else {
unhighlight();
@@ -73,3 +95,4 @@
content.unhighlight(highlightOpts);
}
})();