|
1 | 1 | (function (global) { |
2 | 2 |
|
3 | 3 | var $global = $(global); |
4 | | - var content, darkBox, searchInfo; |
| 4 | + var content, darkBox, searchResults; |
5 | 5 | var highlightOpts = { element: 'span', className: 'search-highlight' }; |
6 | 6 |
|
7 | | - var index = new lunr.Index; |
| 7 | + var index = new lunr.Index(); |
8 | 8 |
|
9 | 9 | index.ref('id'); |
10 | 10 | index.field('title', { boost: 10 }); |
|
14 | 14 | $(populate); |
15 | 15 | $(bind); |
16 | 16 |
|
17 | | - function populate () { |
18 | | - $('h1').each(function () { |
| 17 | + function populate() { |
| 18 | + $('h1, h2').each(function() { |
19 | 19 | var title = $(this); |
20 | | - var body = title.nextUntil('h1'); |
21 | | - var wrapper = $('<section id="section-' + title.prop('id') + '"></section>'); |
22 | | - |
23 | | - title.after(wrapper.append(body)); |
24 | | - wrapper.prepend(title); |
25 | | - |
| 20 | + var body = title.nextUntil('h1, h2'); |
26 | 21 | index.add({ |
27 | 22 | id: title.prop('id'), |
28 | 23 | title: title.text(), |
|
31 | 26 | }); |
32 | 27 | } |
33 | 28 |
|
34 | | - function bind () { |
| 29 | + function bind() { |
35 | 30 | content = $('.content'); |
36 | 31 | darkBox = $('.dark-box'); |
37 | | - searchInfo = $('.search-info'); |
38 | | - |
39 | | - $('#input-search') |
40 | | - .on('keyup', search) |
41 | | - .on('focus', active) |
42 | | - .on('blur', inactive); |
43 | | - } |
44 | | - |
45 | | - function refToHeader (itemRef) { |
46 | | - return $('.tocify-item[data-unique=' + itemRef + ']').closest('.tocify-header'); |
47 | | - } |
| 32 | + searchResults = $('.search-results'); |
48 | 33 |
|
49 | | - function sortDescending (obj2, obj1) { |
50 | | - var s1 = parseInt(obj1.id.replace(/[^\d]/g, ''), 10); |
51 | | - var s2 = parseInt(obj2.id.replace(/[^\d]/g, ''), 10); |
52 | | - return s1 === s2 ? 0 : s1 < s2 ? -1 : 1; |
| 34 | + $('#input-search').on('keyup', search); |
53 | 35 | } |
54 | 36 |
|
55 | | - function resetHeaderLocations () { |
56 | | - var headers = $(".tocify-header").sort(sortDescending); |
57 | | - $.each(headers, function (index, item) { |
58 | | - $(item).insertBefore($("#toc ul:first-child")); |
59 | | - }); |
60 | | - } |
61 | | - |
62 | | - function search (event) { |
63 | | - var sections = $('section, #toc .tocify-header'); |
64 | | - |
65 | | - searchInfo.hide(); |
| 37 | + function search(event) { |
66 | 38 | unhighlight(); |
| 39 | + searchResults.addClass('visible'); |
67 | 40 |
|
68 | 41 | // ESC clears the field |
69 | 42 | if (event.keyCode === 27) this.value = ''; |
70 | 43 |
|
71 | 44 | if (this.value) { |
72 | | - sections.hide(); |
73 | | - // results are sorted by score in descending order |
74 | | - var results = index.search(this.value); |
| 45 | + var results = index.search(this.value).filter(function(r) { |
| 46 | + return r.score > 0.0001; |
| 47 | + }); |
75 | 48 |
|
76 | 49 | if (results.length) { |
77 | | - resetHeaderLocations(); |
78 | | - var lastRef; |
79 | | - $.each(results, function (index, item) { |
80 | | - if (item.score <= 0.0001) return; // remove low-score results |
81 | | - var itemRef = item.ref; |
82 | | - $('#section-' + itemRef).show(); |
83 | | - // headers must be repositioned in the DOM |
84 | | - var closestHeader = refToHeader(itemRef); |
85 | | - if (lastRef) { |
86 | | - refToHeader(lastRef).insertBefore(closestHeader); |
87 | | - } |
88 | | - closestHeader.show(); |
89 | | - lastRef = itemRef; |
| 50 | + searchResults.empty(); |
| 51 | + $.each(results, function (index, result) { |
| 52 | + searchResults.append("<li><a href='#" + result.ref + "'>" + $('#'+result.ref).text() + "</a></li>"); |
90 | 53 | }); |
91 | | - |
92 | | - // position first element. it wasn't positioned above if len > 1 |
93 | | - if (results.length > 1) { |
94 | | - var firstRef = results[0].ref; |
95 | | - var secondRef = results[1].ref |
96 | | - refToHeader(firstRef).insertBefore(refToHeader(secondRef)); |
97 | | - } |
98 | | - |
99 | 54 | highlight.call(this); |
100 | 55 | } else { |
101 | | - sections.show(); |
102 | | - searchInfo.text('No Results Found for "' + this.value + '"').show(); |
| 56 | + searchResults.html('<li>No Results Found for "' + this.value + '"</li>'); |
103 | 57 | } |
104 | 58 | } else { |
105 | | - sections.show(); |
| 59 | + unhighlight(); |
| 60 | + searchResults.removeClass('visible'); |
106 | 61 | } |
107 | | - |
108 | | - // HACK trigger tocify height recalculation |
109 | | - $global.triggerHandler('scroll.tocify'); |
110 | | - $global.triggerHandler('resize'); |
111 | | - } |
112 | | - |
113 | | - function active () { |
114 | | - search.call(this, {}); |
115 | | - } |
116 | | - |
117 | | - function inactive () { |
118 | | - unhighlight(); |
119 | | - searchInfo.hide(); |
120 | 62 | } |
121 | 63 |
|
122 | | - function highlight () { |
| 64 | + function highlight() { |
123 | 65 | if (this.value) content.highlight(this.value, highlightOpts); |
124 | 66 | } |
125 | 67 |
|
126 | | - function unhighlight () { |
| 68 | + function unhighlight() { |
127 | 69 | content.unhighlight(highlightOpts); |
128 | 70 | } |
129 | 71 |
|
|
0 commit comments