|
15 | 15 | // ===----------------------------------------------------------------------===// |
16 | 16 | ;(function (globalScope) { |
17 | 17 | /* eslint-disable no-var */ |
18 | | - var config = document.getElementById('search-ui-script').dataset |
19 | | - var snippetLength = parseInt(config.snippetLength || 100, 10) |
20 | | - var siteRootPath = config.siteRootPath || '' |
| 18 | + const config = document.getElementById('search-ui-script').dataset; |
| 19 | + const snippetLength = parseInt(config.snippetLength || 100, 10); |
| 20 | + const siteRootPath = config.siteRootPath || ''; |
21 | 21 | appendStylesheet(config.stylesheet) |
22 | | - var searchInput = document.getElementById('search-input') |
23 | | - var searchResult = document.createElement('div') |
| 22 | + const searchInput = document.getElementById('search-input'); |
| 23 | + const searchResult = document.createElement('div'); |
24 | 24 | searchResult.classList.add('search-result-dropdown-menu') |
25 | 25 | searchInput.parentNode.appendChild(searchResult) |
26 | 26 |
|
|
30 | 30 | } |
31 | 31 |
|
32 | 32 | function highlightText (doc, position) { |
33 | | - var hits = [] |
34 | | - var start = position[0] |
35 | | - var length = position[1] |
| 33 | + const hits = []; |
| 34 | + const start = position[0]; |
| 35 | + const length = position[1]; |
36 | 36 |
|
37 | | - var text = doc.text |
38 | | - var highlightSpan = document.createElement('span') |
| 37 | + const text = doc.text; |
| 38 | + const highlightSpan = document.createElement('span'); |
39 | 39 | highlightSpan.classList.add('search-result-highlight') |
40 | 40 | highlightSpan.innerText = text.substr(start, length) |
41 | 41 |
|
42 | | - var end = start + length |
43 | | - var textEnd = text.length - 1 |
44 | | - var contextAfter = end + snippetLength > textEnd ? textEnd : end + snippetLength |
45 | | - var contextBefore = start - snippetLength < 0 ? 0 : start - snippetLength |
| 42 | + const end = start + length; |
| 43 | + const textEnd = text.length - 1; |
| 44 | + const contextAfter = end + snippetLength > textEnd ? textEnd : end + snippetLength; |
| 45 | + const contextBefore = start - snippetLength < 0 ? 0 : start - snippetLength; |
46 | 46 | if (start === 0 && end === textEnd) { |
47 | 47 | hits.push(highlightSpan) |
48 | 48 | } else if (start === 0) { |
|
60 | 60 | } |
61 | 61 |
|
62 | 62 | function highlightTitle (hash, doc, position) { |
63 | | - var hits = [] |
64 | | - var start = position[0] |
65 | | - var length = position[1] |
| 63 | + const hits = []; |
| 64 | + const start = position[0]; |
| 65 | + const length = position[1]; |
66 | 66 |
|
67 | | - var highlightSpan = document.createElement('span') |
| 67 | + const highlightSpan = document.createElement('span'); |
68 | 68 | highlightSpan.classList.add('search-result-highlight') |
69 | | - var title |
| 69 | + let title; |
70 | 70 | if (hash) { |
71 | 71 | title = doc.titles.filter(function (item) { |
72 | 72 | return item.id === hash |
|
76 | 76 | } |
77 | 77 | highlightSpan.innerText = title.substr(start, length) |
78 | 78 |
|
79 | | - var end = start + length |
80 | | - var titleEnd = title.length - 1 |
| 79 | + const end = start + length; |
| 80 | + const titleEnd = title.length - 1; |
81 | 81 | if (start === 0 && end === titleEnd) { |
82 | 82 | hits.push(highlightSpan) |
83 | 83 | } else if (start === 0) { |
|
95 | 95 | } |
96 | 96 |
|
97 | 97 | function highlightHit (metadata, hash, doc) { |
98 | | - var hits = [] |
99 | | - for (var token in metadata) { |
100 | | - var fields = metadata[token] |
101 | | - for (var field in fields) { |
102 | | - var positions = fields[field] |
| 98 | + let hits = []; |
| 99 | + for (const token in metadata) { |
| 100 | + const fields = metadata[token]; |
| 101 | + for (const field in fields) { |
| 102 | + const positions = fields[field]; |
103 | 103 | if (positions.position) { |
104 | | - var position = positions.position[0] // only higlight the first match |
| 104 | + const position = positions.position[0]; // only higlight the first match |
105 | 105 | if (field === 'title') { |
106 | 106 | hits = highlightTitle(hash, doc, position) |
107 | 107 | } else if (field === 'text') { |
|
115 | 115 |
|
116 | 116 | function createSearchResult (result, store, searchResultDataset) { |
117 | 117 | result.forEach(function (item) { |
118 | | - var url = item.ref |
119 | | - var hash |
| 118 | + let url = item.ref; |
| 119 | + let hash; |
120 | 120 | if (url.includes('#')) { |
121 | 121 | hash = url.substring(url.indexOf('#') + 1) |
122 | 122 | url = url.replace('#' + hash, '') |
123 | 123 | } |
124 | | - var doc = store[url] |
125 | | - var metadata = item.matchData.metadata |
126 | | - var hits = highlightHit(metadata, hash, doc) |
| 124 | + const doc = store[url]; |
| 125 | + const metadata = item.matchData.metadata; |
| 126 | + const hits = highlightHit(metadata, hash, doc); |
127 | 127 | searchResultDataset.appendChild(createSearchResultItem(doc, item, hits)) |
128 | 128 | }) |
129 | 129 | } |
130 | 130 |
|
131 | 131 | function createSearchResultItem (doc, item, hits) { |
132 | | - var documentTitle = document.createElement('div') |
| 132 | + const documentTitle = document.createElement('div'); |
133 | 133 | documentTitle.classList.add('search-result-document-title') |
134 | 134 | documentTitle.innerText = doc.title |
135 | | - var documentHit = document.createElement('div') |
| 135 | + const documentHit = document.createElement('div'); |
136 | 136 | documentHit.classList.add('search-result-document-hit') |
137 | | - var documentHitLink = document.createElement('a') |
| 137 | + const documentHitLink = document.createElement('a'); |
138 | 138 | documentHitLink.href = siteRootPath + item.ref |
139 | 139 | documentHit.appendChild(documentHitLink) |
140 | 140 | hits.forEach(function (hit) { |
141 | 141 | documentHitLink.appendChild(hit) |
142 | 142 | }) |
143 | | - var searchResultItem = document.createElement('div') |
| 143 | + const searchResultItem = document.createElement('div'); |
144 | 144 | searchResultItem.classList.add('search-result-item') |
145 | 145 | searchResultItem.appendChild(documentTitle) |
146 | 146 | searchResultItem.appendChild(documentHit) |
|
151 | 151 | } |
152 | 152 |
|
153 | 153 | function createNoResult (text) { |
154 | | - var searchResultItem = document.createElement('div') |
| 154 | + const searchResultItem = document.createElement('div'); |
155 | 155 | searchResultItem.classList.add('search-result-item') |
156 | | - var documentHit = document.createElement('div') |
| 156 | + const documentHit = document.createElement('div'); |
157 | 157 | documentHit.classList.add('search-result-document-hit') |
158 | | - var message = document.createElement('strong') |
| 158 | + const message = document.createElement('strong'); |
159 | 159 | message.innerText = 'No results found for query "' + text + '"' |
160 | 160 | documentHit.appendChild(message) |
161 | 161 | searchResultItem.appendChild(documentHit) |
|
169 | 169 |
|
170 | 170 | function search (index, text) { |
171 | 171 | // execute an exact match search |
172 | | - var result = index.search(text) |
| 172 | + let result = index.search(text); |
173 | 173 | if (result.length > 0) { |
174 | 174 | return result |
175 | 175 | } |
|
188 | 188 | if (text.trim() === '') { |
189 | 189 | return |
190 | 190 | } |
191 | | - var result = search(index, text) |
192 | | - var searchResultDataset = document.createElement('div') |
| 191 | + const result = search(index, text); |
| 192 | + const searchResultDataset = document.createElement('div'); |
193 | 193 | searchResultDataset.classList.add('search-result-dataset') |
194 | 194 | searchResult.appendChild(searchResultDataset) |
195 | 195 | if (result.length > 0) { |
|
204 | 204 | } |
205 | 205 |
|
206 | 206 | function debounce (func, wait, immediate) { |
207 | | - var timeout |
| 207 | + let timeout; |
208 | 208 | return function () { |
209 | | - var context = this |
210 | | - var args = arguments |
211 | | - var later = function () { |
| 209 | + const context = this; |
| 210 | + const args = arguments; |
| 211 | + const later = function () { |
212 | 212 | timeout = null |
213 | 213 | if (!immediate) func.apply(context, args) |
214 | | - } |
215 | | - var callNow = immediate && !timeout |
| 214 | + }; |
| 215 | + const callNow = immediate && !timeout; |
216 | 216 | clearTimeout(timeout) |
217 | 217 | timeout = setTimeout(later, wait) |
218 | 218 | if (callNow) func.apply(context, args) |
219 | 219 | } |
220 | 220 | } |
221 | 221 |
|
222 | 222 | function initSearch (lunr, data) { |
223 | | - var index = Object.assign({ index: lunr.Index.load(data.index), store: data.store }) |
224 | | - var debug = 'URLSearchParams' in globalScope && new URLSearchParams(globalScope.location.search).has('lunr-debug') |
| 223 | + const index = Object.assign({index: lunr.Index.load(data.index), store: data.store}); |
| 224 | + const debug = 'URLSearchParams' in globalScope && new URLSearchParams(globalScope.location.search).has('lunr-debug'); |
225 | 225 | searchInput.addEventListener( |
226 | 226 | 'keydown', |
227 | 227 | debounce(function (e) { |
| 228 | + const query = searchInput.value; |
228 | 229 | if (e.key === 'Escape' || e.key === 'Esc') return clearSearchResults(true) |
229 | 230 | try { |
230 | | - var query = searchInput.value |
231 | 231 | if (!query) return clearSearchResults() |
232 | 232 | searchIndex(index.index, index.store, searchInput.value) |
233 | 233 | } catch (err) { |
|
0 commit comments