-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (57 loc) · 1.73 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
jQuery(function () {
const storageName = 'searchns_ns';
const $in = jQuery('#qsearchns__in');
const $select = jQuery('#qsearchns__ns');
const $out = jQuery('#qsearch__out');
if (!$in || !$out || !$select) return;
let timeout;
// try restoring a saved namespace, or first option if stored is not present
const toRestore = localStorage.getItem(storageName);
if (typeof toRestore !== 'undefined' && $select.find('option[value="' + toRestore + '"').length > 0) {
$select.val(toRestore);
} else {
$select[0].selectedIndex = 0;
}
/**
* Fetch results with AJAX
*
* @returns {Promise<void>}
*/
async function doSearch() {
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_searchns_qsearch',
q: encodeURI($in.val()),
ns: encodeURI($select.val())
},
function (data) {
if (data.length === 0 ) {
return;
}
$out
.html(data)
.show()
.css('white-space', 'nowrap');
},
'html'
);
}
// event listener on search input
$in.on('keyup', function (evt) {
if ($in.val() === '') {
$out.text = '';
$out.hide();
} else {
if (timeout) {
window.clearTimeout(timeout);
timeout = null;
}
timeout = window.setTimeout(doSearch, 500);
}
});
// event listener on namespace selector
$select.on('change', function (evt) {
localStorage.setItem(storageName, evt.target.value);
});
});