Sha256: de5ad1ec2eecd205c4dac8a54a829268e03db7a1b58510d017ec5bd0954b3943

Contents?: true

Size: 1.79 KB

Versions: 14

Compression:

Stored size: 1.79 KB

Contents

//= require ../lib/_lunr
//= require ../lib/_jquery
//= require ../lib/_jquery.highlight
(function () {
  'use strict';

  var content, searchResults;
  var highlightOpts = { element: 'span', className: 'search-highlight' };

  var index = new lunr.Index();

  index.ref('id');
  index.field('title', { boost: 10 });
  index.field('body');
  index.pipeline.add(lunr.trimmer, lunr.stopWordFilter);

  $(populate);
  $(bind);

  function populate() {
    $('h1, h2').each(function() {
      var title = $(this);
      var body = title.nextUntil('h1, h2');
      index.add({
        id: title.prop('id'),
        title: title.text(),
        body: body.text()
      });
    });
  }

  function bind() {
    content = $('.content');
    searchResults = $('.search-results');

    $('#input-search').on('keyup', search);
  }

  function search(event) {
    unhighlight();
    searchResults.addClass('visible');

    // ESC clears the field
    if (event.keyCode === 27) this.value = '';

    if (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 elem = document.getElementById(result.ref);
          searchResults.append("<li><a href='#" + result.ref + "'>" + $(elem).text() + "</a></li>");
        });
        highlight.call(this);
      } else {
        searchResults.html('<li></li>');
        $('.search-results li').text('No Results Found for "' + this.value + '"');
      }
    } else {
      unhighlight();
      searchResults.removeClass('visible');
    }
  }

  function highlight() {
    if (this.value) content.highlight(this.value, highlightOpts);
  }

  function unhighlight() {
    content.unhighlight(highlightOpts);
  }
})();

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
blocks-4.0.1 docs/javascripts/app/search.js
blocks-4.0.0 docs/javascripts/app/search.js
blocks-4.0.0.rc2 docs/javascripts/app/search.js
blocks-4.0.0.rc1 docs/javascripts/app/search.js
blocks-3.0.4 docs/javascripts/app/search.js
blocks-3.0.3 docs/javascripts/app/search.js
blocks-3.0.2 docs/javascripts/app/search.js
blocks-3.0.1 docs/javascripts/app/search.js
blocks-3.0.0 docs/javascripts/app/search.js
blocks-3.0.0.rc9 docs/javascripts/app/search.js
blocks-3.0.0.rc8 docs/javascripts/app/search.js
blocks-3.0.0.rc7 docs/javascripts/app/search.js
blocks-3.0.0.rc6 docs/javascripts/app/search.js
blocks-3.0.0.rc5 docs/javascripts/app/search.js