mirror of
https://github.com/danbulant/api_docs
synced 2026-06-21 23:52:21 +00:00
Add better search results in a sliding search results area
This commit is contained in:
parent
51e537aca2
commit
18cd104883
2 changed files with 17 additions and 20 deletions
|
|
@ -15,9 +15,9 @@
|
||||||
$(bind);
|
$(bind);
|
||||||
|
|
||||||
function populate() {
|
function populate() {
|
||||||
$('h1').each(function() {
|
$('h1, h2').each(function() {
|
||||||
var title = $(this);
|
var title = $(this);
|
||||||
var body = title.nextUntil('h1');
|
var body = title.nextUntil('h1, h2');
|
||||||
index.add({
|
index.add({
|
||||||
id: title.prop('id'),
|
id: title.prop('id'),
|
||||||
title: title.text(),
|
title: title.text(),
|
||||||
|
|
@ -31,13 +31,7 @@
|
||||||
darkBox = $('.dark-box');
|
darkBox = $('.dark-box');
|
||||||
searchResults = $('.search-results');
|
searchResults = $('.search-results');
|
||||||
|
|
||||||
$('#input-search').on('keyup', function(e) {
|
$('#input-search').on('keyup', search);
|
||||||
if ($(this).val() === "") {
|
|
||||||
inactive(e);
|
|
||||||
} else {
|
|
||||||
search(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function search(event) {
|
function search(event) {
|
||||||
|
|
@ -48,25 +42,25 @@
|
||||||
if (event.keyCode === 27) this.value = '';
|
if (event.keyCode === 27) this.value = '';
|
||||||
|
|
||||||
if (this.value) {
|
if (this.value) {
|
||||||
var results = index.search(this.value);
|
var results = index.search(this.value).filter(function(r) {
|
||||||
|
return r.score > 0.0001;
|
||||||
|
});
|
||||||
|
|
||||||
if (results.length) {
|
if (results.length) {
|
||||||
|
searchResults.empty();
|
||||||
$.each(results, function (index, result) {
|
$.each(results, function (index, result) {
|
||||||
var header = $('.tocify-item[data-unique=' + result.ref + ']').closest('.tocify-header');
|
searchResults.append("<li><a href='#" + result.ref + "'>" + $('#'+result.ref).text() + "</a></li>");
|
||||||
if (header.length > 0) header = header[0];
|
|
||||||
if (header) $("#" + header.id + " li a").append("<span>" + result.score + "</span>");
|
|
||||||
});
|
});
|
||||||
highlight.call(this);
|
highlight.call(this);
|
||||||
} else {
|
} else {
|
||||||
searchResults.text('No Results Found for "' + this.value + '"').show();
|
searchResults.html('<li>No Results Found for "' + this.value + '"</li>');
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
unhighlight();
|
||||||
|
searchResults.removeClass('visible');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function inactive() {
|
|
||||||
unhighlight();
|
|
||||||
searchResults.removeClass('visible');
|
|
||||||
}
|
|
||||||
|
|
||||||
function highlight() {
|
function highlight() {
|
||||||
if (this.value) content.highlight(this.value, highlightOpts);
|
if (this.value) content.highlight(this.value, highlightOpts);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,10 @@ under the License.
|
||||||
<div class="search">
|
<div class="search">
|
||||||
<input type="text" class="search" id="input-search" placeholder="Search">
|
<input type="text" class="search" id="input-search" placeholder="Search">
|
||||||
</div>
|
</div>
|
||||||
<div class="search-results"></div>
|
<ul class="search-results">
|
||||||
|
<li><a href="#">Introduction</a></li>
|
||||||
|
<li><a href="#">Cats</a></li>
|
||||||
|
</ul>
|
||||||
<% end %>
|
<% end %>
|
||||||
<div id="toc">
|
<div id="toc">
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue