From 18cd104883e870aa10e159c2300b56de3bcbea68 Mon Sep 17 00:00:00 2001 From: Robert Lord Date: Wed, 2 Jul 2014 23:43:25 -0700 Subject: [PATCH] Add better search results in a sliding search results area --- source/javascripts/app/search.js | 32 +++++++++++++------------------- source/layouts/layout.erb | 5 ++++- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/source/javascripts/app/search.js b/source/javascripts/app/search.js index 55c222f..cb81989 100644 --- a/source/javascripts/app/search.js +++ b/source/javascripts/app/search.js @@ -15,9 +15,9 @@ $(bind); function populate() { - $('h1').each(function() { + $('h1, h2').each(function() { var title = $(this); - var body = title.nextUntil('h1'); + var body = title.nextUntil('h1, h2'); index.add({ id: title.prop('id'), title: title.text(), @@ -31,13 +31,7 @@ darkBox = $('.dark-box'); searchResults = $('.search-results'); - $('#input-search').on('keyup', function(e) { - if ($(this).val() === "") { - inactive(e); - } else { - search(e); - } - }); + $('#input-search').on('keyup', search); } function search(event) { @@ -48,25 +42,25 @@ if (event.keyCode === 27) 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) { + searchResults.empty(); $.each(results, function (index, result) { - var header = $('.tocify-item[data-unique=' + result.ref + ']').closest('.tocify-header'); - if (header.length > 0) header = header[0]; - if (header) $("#" + header.id + " li a").append("" + result.score + ""); + searchResults.append("
  • " + $('#'+result.ref).text() + "
  • "); }); highlight.call(this); } else { - searchResults.text('No Results Found for "' + this.value + '"').show(); + searchResults.html('
  • No Results Found for "' + this.value + '"
  • '); } + } else { + unhighlight(); + searchResults.removeClass('visible'); } } - function inactive() { - unhighlight(); - searchResults.removeClass('visible'); - } - function highlight() { if (this.value) content.highlight(this.value, highlightOpts); } diff --git a/source/layouts/layout.erb b/source/layouts/layout.erb index 9609754..c28d88d 100644 --- a/source/layouts/layout.erb +++ b/source/layouts/layout.erb @@ -64,7 +64,10 @@ under the License. -
    + <% end %>