mirror of
https://github.com/danbulant/api_docs
synced 2026-06-24 17:12:01 +00:00
Add configurable keypress delay before initiating search (#764)
This commit is contained in:
parent
a6794138ef
commit
2a63948592
1 changed files with 29 additions and 6 deletions
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
var content, searchResults;
|
var content, searchResults;
|
||||||
var highlightOpts = { element: 'span', className: 'search-highlight' };
|
var highlightOpts = { element: 'span', className: 'search-highlight' };
|
||||||
|
var searchDelay = 0;
|
||||||
|
var timeoutHandle = 0;
|
||||||
|
|
||||||
var index = new lunr.Index();
|
var index = new lunr.Index();
|
||||||
|
|
||||||
|
|
@ -27,24 +29,44 @@
|
||||||
body: body.text()
|
body: body.text()
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
determineSearchDelay();
|
||||||
|
}
|
||||||
|
function determineSearchDelay() {
|
||||||
|
if(index.tokenStore.length>5000) {
|
||||||
|
searchDelay = 300;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function bind() {
|
function bind() {
|
||||||
content = $('.content');
|
content = $('.content');
|
||||||
searchResults = $('.search-results');
|
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) {
|
function search(event) {
|
||||||
|
|
||||||
|
var searchInput = $('#input-search')[0];
|
||||||
|
|
||||||
unhighlight();
|
unhighlight();
|
||||||
searchResults.addClass('visible');
|
searchResults.addClass('visible');
|
||||||
|
|
||||||
// ESC clears the field
|
// ESC clears the field
|
||||||
if (event.keyCode === 27) this.value = '';
|
if (event.keyCode === 27) searchInput.value = '';
|
||||||
|
|
||||||
if (this.value) {
|
if (searchInput.value) {
|
||||||
var results = index.search(this.value).filter(function(r) {
|
var results = index.search(searchInput.value).filter(function(r) {
|
||||||
return r.score > 0.0001;
|
return r.score > 0.0001;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -54,10 +76,10 @@
|
||||||
var elem = document.getElementById(result.ref);
|
var elem = document.getElementById(result.ref);
|
||||||
searchResults.append("<li><a href='#" + result.ref + "'>" + $(elem).text() + "</a></li>");
|
searchResults.append("<li><a href='#" + result.ref + "'>" + $(elem).text() + "</a></li>");
|
||||||
});
|
});
|
||||||
highlight.call(this);
|
highlight.call(searchInput);
|
||||||
} else {
|
} else {
|
||||||
searchResults.html('<li></li>');
|
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 {
|
} else {
|
||||||
unhighlight();
|
unhighlight();
|
||||||
|
|
@ -73,3 +95,4 @@
|
||||||
content.unhighlight(highlightOpts);
|
content.unhighlight(highlightOpts);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue