Skip to content

Commit 05edad0

Browse files
committed
improve the search and add a "no results"
1 parent 9e73176 commit 05edad0

5 files changed

Lines changed: 69 additions & 51 deletions

File tree

bin/update-ww.pl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env perl
2+
3+
=head1 NAME
4+
5+
update-ww.pl -- run all needed scripts when webwork is updated.
6+
7+
=cut
8+
9+
use strict;
10+
use warnings;
11+
use feature 'say';
12+
13+
use Mojo::File qw(curfile);
14+
15+
my $ww_root = Mojo::File->new(curfile->dirname, "..")->realpath;
16+
17+
`perl $ww_root/bin/dev_scripts/build-search-json.pl -b all`;
18+

htdocs/DATA/search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

htdocs/js/SampleProblemViewer/search.js

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,6 @@
33
about every page to be searched (macro POD and sample problems).
44
*/
55
(() => {
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-
366
const miniSearch = new MiniSearch({ fields: ['terms', 'filename', 'name', 'description', 'methods'] });
377
let pages;
388
// This is the data from sample-problems/macros POD.
@@ -51,32 +21,47 @@
5121
resultList.innerHTML = '';
5222
});
5323

54-
const search = throttle(() => {
55-
const results = miniSearch.search(searchBox.value);
24+
const search = () => {
25+
const results = miniSearch.search(searchBox.value, { prefix: true });
5626
const ids = results.map((p) => p.id);
5727

28+
const includeMacros = document.getElementById('includeMacros').checked;
29+
const includeSP = document.getElementById('includeSP').checked;
30+
5831
resultList.innerHTML = '';
32+
5933
ids.forEach((id) => {
60-
const item = document.createElement('div');
61-
item.classList.add('card');
6234
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' : '';
6541

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.
7643

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+
}
7856
});
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+
};
8065

8166
searchBox.addEventListener('keypress', search);
8267
})();

templates/ContentGenerator/SampleProblemViewer.html.ep

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<body>
2525
<div class="container-fluid p-3">
2626
<div class="row">
27-
<div class="col-5">
27+
<div class="col-4">
2828
<h1><%= title %></h1>
2929
</div>
30-
<div class="col-6 offset-md-1">
30+
<div class="col-5 offset-md-1">
3131
<div class="input-group mb-3">
3232
<span class="input-group-text" id="basic-addon1">
3333
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
@@ -38,8 +38,23 @@
3838
<button class="btn btn-outline-secondary" type="button" id="clearSearchButton">Clear</button>
3939
</div>
4040
</div>
41+
<div class="col-2">
42+
<div class="form-check">
43+
<input class="form-check-input" type="checkbox" value="" id="includeMacros" checked>
44+
<label class="form-check-label" for="includeMacros">
45+
Include Macros
46+
</label>
47+
</div>
48+
<div class="form-check">
49+
<input class="form-check-input" type="checkbox" value="" id="includeSP" checked>
50+
<label class="form-check-label" for="includeSP">
51+
Include Sample Problems
52+
</label>
53+
</div>
54+
</div>
4155
</div>
4256

57+
4358
<div class="container">
4459
<div class="row">
4560
<div class="col-5 mt-3">

0 commit comments

Comments
 (0)