|
3 | 3 | about every page to be searched (macro POD and sample problems). |
4 | 4 | */ |
5 | 5 | (() => { |
6 | | - // ChatGPT generated throttle function similar to Lodash. |
7 | | - function throttle(func, wait) { |
8 | | - let lastCallTime = 0; |
9 | | - let timeout = null; |
10 | | - let lastArgs, lastContext; |
11 | | - |
12 | | - return function throttled(...args) { |
13 | | - const now = Date.now(); |
14 | | - const remaining = wait - (now - lastCallTime); |
15 | | - |
16 | | - lastArgs = args; |
17 | | - lastContext = this; |
18 | | - |
19 | | - if (remaining <= 0 || remaining > wait) { |
20 | | - if (timeout) { |
21 | | - clearTimeout(timeout); |
22 | | - timeout = null; |
23 | | - } |
24 | | - lastCallTime = now; |
25 | | - func.apply(lastContext, lastArgs); |
26 | | - } else if (!timeout) { |
27 | | - timeout = setTimeout(() => { |
28 | | - lastCallTime = Date.now(); |
29 | | - timeout = null; |
30 | | - func.apply(lastContext, lastArgs); |
31 | | - }, remaining); |
32 | | - } |
33 | | - }; |
34 | | - } |
35 | | - |
36 | 6 | const miniSearch = new MiniSearch({ fields: ['terms', 'filename', 'name', 'description', 'methods'] }); |
37 | 7 | let pages; |
38 | 8 | // This is the data from sample-problems/macros POD. |
|
51 | 21 | resultList.innerHTML = ''; |
52 | 22 | }); |
53 | 23 |
|
54 | | - const search = throttle(() => { |
55 | | - const results = miniSearch.search(searchBox.value); |
| 24 | + const search = () => { |
| 25 | + const results = miniSearch.search(searchBox.value, { prefix: true }); |
56 | 26 | const ids = results.map((p) => p.id); |
57 | 27 |
|
| 28 | + const includeMacros = document.getElementById('includeMacros').checked; |
| 29 | + const includeSP = document.getElementById('includeSP').checked; |
| 30 | + |
58 | 31 | resultList.innerHTML = ''; |
| 32 | + |
59 | 33 | ids.forEach((id) => { |
60 | | - const item = document.createElement('div'); |
61 | | - item.classList.add('card'); |
62 | 34 | const p = pages[id - 1]; |
63 | | - const file = p.filename.replace('.pg', ''); |
64 | | - const path = p.type == 'sample problem' ? 'sampleproblems' : p.type == 'macro' ? 'pod' : ''; |
| 35 | + if ((p.type == 'sample problem' && includeSP) || (p.type == 'macro' && includeMacros)) { |
| 36 | + const item = document.createElement('div'); |
| 37 | + item.classList.add('card'); |
| 38 | + |
| 39 | + const file = p.filename.replace('.pg', ''); |
| 40 | + const path = p.type == 'sample problem' ? 'sampleproblems' : p.type == 'macro' ? 'pod' : ''; |
65 | 41 |
|
66 | | - // This is the search results for each page. |
67 | | - item.innerHTML = ` |
68 | | - <div class="card-body"> |
69 | | - <h5 class="card-title"> |
70 | | - <a href=\"/webwork2/${path}/${p.dir}/${file}\">${p.name}</a> |
71 | | - (${p.type}) |
72 | | - </h5> |
73 | | - <p class="card-text">${p.description}</p> |
74 | | - </div> |
75 | | - `; |
| 42 | + // This is the search results for each page. |
76 | 43 |
|
77 | | - resultList.appendChild(item); |
| 44 | + item.innerHTML = ` |
| 45 | + <div class="card-body"> |
| 46 | + <h5 class="card-title"> |
| 47 | + <a href=\"/webwork2/${path}/${p.dir}/${file}\">${p.name}</a> |
| 48 | + (${p.type}) |
| 49 | + </h5> |
| 50 | + <p class="card-text">${p.description}</p> |
| 51 | + </div> |
| 52 | + `; |
| 53 | + |
| 54 | + resultList.appendChild(item); |
| 55 | + } |
78 | 56 | }); |
79 | | - }, 250); |
| 57 | + // If there are no results, say so |
| 58 | + if (resultList.children.length == 0) { |
| 59 | + const item = document.createElement('div'); |
| 60 | + item.classList.add('alert', 'alert-info'); |
| 61 | + item.innerHTML = 'No results found'; |
| 62 | + resultList.append(item); |
| 63 | + } |
| 64 | + }; |
80 | 65 |
|
81 | 66 | searchBox.addEventListener('keypress', search); |
82 | 67 | })(); |
0 commit comments