|
3 | 3 | about every page to be searched (macro POD and sample problems). |
4 | 4 | */ |
5 | 5 | (() => { |
6 | | - |
7 | 6 | // ChatGPT generated throttle function similar to Lodash. |
8 | 7 | function throttle(func, wait) { |
9 | 8 | let lastCallTime = 0; |
|
18 | 17 | lastContext = this; |
19 | 18 |
|
20 | 19 | if (remaining <= 0 || remaining > wait) { |
21 | | - if (timeout) { |
22 | | - clearTimeout(timeout); |
23 | | - timeout = null; |
24 | | - } |
25 | | - lastCallTime = now; |
26 | | - func.apply(lastContext, lastArgs); |
27 | | - } else if (!timeout) { |
28 | | - timeout = setTimeout(() => { |
29 | | - lastCallTime = Date.now(); |
30 | | - timeout = null; |
| 20 | + if (timeout) { |
| 21 | + clearTimeout(timeout); |
| 22 | + timeout = null; |
| 23 | + } |
| 24 | + lastCallTime = now; |
31 | 25 | func.apply(lastContext, lastArgs); |
32 | | - }, remaining); |
| 26 | + } else if (!timeout) { |
| 27 | + timeout = setTimeout(() => { |
| 28 | + lastCallTime = Date.now(); |
| 29 | + timeout = null; |
| 30 | + func.apply(lastContext, lastArgs); |
| 31 | + }, remaining); |
33 | 32 | } |
34 | 33 | }; |
35 | 34 | } |
36 | 35 |
|
37 | | - const miniSearch = new MiniSearch({fields: ['terms', 'filename', 'name', 'description']}); |
| 36 | + const miniSearch = new MiniSearch({ fields: ['terms', 'filename', 'name', 'description'] }); |
38 | 37 | let pages; |
39 | 38 | // This is the data from sample-problems/macros POD. |
40 | 39 | fetch('../../DATA/search.json') |
|
45 | 44 | }) |
46 | 45 | .catch((e) => console.error(e)); |
47 | 46 |
|
48 | | - const resultList = document.getElementById("searchResults"); |
49 | | - const searchBox = document.getElementById("searchDocs"); |
50 | | - document.getElementById("clearSearchButton").addEventListener('click', () => { |
| 47 | + const resultList = document.getElementById('searchResults'); |
| 48 | + const searchBox = document.getElementById('searchDocs'); |
| 49 | + document.getElementById('clearSearchButton').addEventListener('click', () => { |
51 | 50 | searchBox.value = ''; |
52 | 51 | resultList.innerHTML = ''; |
53 | 52 | }); |
54 | 53 |
|
55 | | - |
56 | 54 | const search = throttle(() => { |
57 | 55 | const results = miniSearch.search(searchBox.value); |
58 | 56 | const ids = results.map((p) => p.id); |
59 | 57 |
|
60 | 58 | resultList.innerHTML = ''; |
61 | | - ids.forEach(id => { |
62 | | - const item = document.createElement("div"); |
63 | | - item.classList.add("card"); |
64 | | - const p = pages[id-1]; |
65 | | - const file = p.filename.replace('.pg',''); |
| 59 | + ids.forEach((id) => { |
| 60 | + const item = document.createElement('div'); |
| 61 | + item.classList.add('card'); |
| 62 | + const p = pages[id - 1]; |
| 63 | + const file = p.filename.replace('.pg', ''); |
66 | 64 | const path = p.type == 'sample problem' ? 'sampleproblems' : p.type == 'macro' ? 'pod' : ''; |
67 | 65 |
|
68 | 66 | // This is the search results for each page. |
|
75 | 73 | </div> |
76 | 74 | `; |
77 | 75 |
|
78 | | - resultList.appendChild(item) |
| 76 | + resultList.appendChild(item); |
79 | 77 | }); |
80 | | - |
81 | 78 | }, 250); |
82 | 79 |
|
83 | 80 | searchBox.addEventListener('keypress', search); |
84 | | - |
85 | | - |
86 | 81 | })(); |
0 commit comments