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