diff options
Diffstat (limited to 'static/js/tinysearch.js')
| -rw-r--r-- | static/js/tinysearch.js | 434 |
1 files changed, 222 insertions, 212 deletions
diff --git a/static/js/tinysearch.js b/static/js/tinysearch.js index 9ee0cc9..183eb05 100644 --- a/static/js/tinysearch.js +++ b/static/js/tinysearch.js @@ -1,244 +1,254 @@ -// in page results when press enter or click search icon from search box -function closeSearchNow() { - const main = document.querySelector("main"); - main.innerHTML = window.main -} -function goSearchNow() { - const main = document.querySelector("main"); - if (!window.main) { - window.main = main.innerHTML - }; - var results = document.getElementById("suggestions");// suggestions div generated by search box +window.onload = function () { + if (document.body.contains(document.goSearch)) { + document.goSearch.onsubmit = function () { return goSearchNow() }; - var ResultsClone = results.cloneNode(true);// make a clone of the results, so that we can alter it - ResultsClone.id = "results";// alter the id of our clone, so that we can apply different css style + (function () {//anonymous function to keep from defining global variables. - var headerDiv = document.createElement("div");// create a div element + var searchinput = document.getElementById("searchinput"); + function suggestionFocus(e) { + if (e.keyCode === 191//forward slash + && document.activeElement.tagName !== "INPUT" + && document.activeElement.tagName !== "TEXTAREA") { + e.preventDefault(); + searchinput.focus(); + suggestions.classList.remove('d-none'); + } - var headerContent = '<form name="closeSearch"><h2><button type="submit" title="Close Search"><i class="svgs x"></i></button> <i class="svgs search"></i> '.concat(document.getElementById("searchinput").value, "</h2></form>");// header to use at top of results page + if (e.keyCode === 27 ) {//escape + searchinput.blur(); + suggestions.classList.add('d-none'); + closeAllLists(); + } - headerDiv.innerHTML = headerContent;// document element div (headerDiv), set the inner contents to our header html (headerContent) + const focusableSuggestions= suggestions.querySelectorAll('a'); + if (suggestions.classList.contains('d-none') + || focusableSuggestions.length === 0) { + return; + } + const focusable= [...focusableSuggestions]; + const index = focusable.indexOf(document.activeElement); - ResultsClone.insertBefore(headerDiv, ResultsClone.firstChild);//insert our header div at the top of the page + let nextIndex = 0; - main.innerHTML = ResultsClone.outerHTML;//display ResultsClone.outerHTML as the page - results.innerHTML = "";// clear the suggestions div popup - document.getElementById("searchinput").value = "";// clear the search input box - document.body.contains(document.closeSearch) && (document.closeSearch.onsubmit = function() { closeSearchNow() }) - return false -} -var loaded = false; -window.onload = function() { - document.body.contains(document.goSearch) && (document.goSearch.onsubmit = function() { return goSearchNow() }) - document.getElementById('searchinput').onfocus = function() { - if (!loaded) { - lazyLoad(); - loaded = true; - document.body.contains(document.goSearch) && (document.goSearch.onsubmit = function() { return goSearchNow() }) - } - document.getElementById('searchinput').onfocus = ''; - } -}; + if (e.keyCode === 38) {//up arrow + e.preventDefault(); + nextIndex= index > 0 ? index-1 : 0; + focusableSuggestions[nextIndex].focus(); + } + else if (e.keyCode === 40) {//down arrow + e.preventDefault(); + nextIndex= index+1 < focusable.length ? index+1 : index; + focusableSuggestions[nextIndex].focus(); + } -async function lazyLoad() { - var baseUrl = document.querySelector("meta[name='base']").getAttribute("content"); - if (baseUrl.slice(-1) == "/") { - baseUrl = baseUrl.slice(0, -1); - } - await init(baseUrl + "/tinysearch_engine_bg.wasm"); -} + } + document.addEventListener("keydown", suggestionFocus); -/* Close search suggestion popup list */ -function closeAllLists(elmnt) { - var suggestions = document.getElementById("suggestions"); - while (suggestions.firstChild) { - suggestions.removeChild(suggestions.firstChild); - } -} + var loaded = false; + document.getElementById('searchinput').onfocus = function() { + if (!loaded) { + lazyLoad(); + loaded = true; + } + document.getElementById('searchinput').onfocus = ''; + } + async function lazyLoad() { + var baseUrl = document.querySelector("meta[name='base']").getAttribute("content"); + if (baseUrl.slice(-1) == "/") { + baseUrl = baseUrl.slice(0, -1); + } + await init(baseUrl + "/tinysearch_engine_bg.wasm"); + } -function markTerm(input, term) { - return input.replace(new RegExp('(^|)(' + term + ')(|$)','ig'), '$1<mark><b>$2</b></mark>$3'); -} + // in page results when press enter or click search icon from search box + function closeSearchNow() { + const main = document.querySelector("main"); + main.innerHTML = window.main + } + function goSearchNow() { + const main = document.querySelector("main"); + if (!window.main) { + window.main = main.innerHTML + }; + var results = document.getElementById("suggestions");// suggestions div generated by search box -function autocomplete(inp) { + var ResultsClone = results.cloneNode(true);// make a clone of the results, so that we can alter it + ResultsClone.id = "results";// alter the id of our clone, so that we can apply different css style - inp.addEventListener("input", function (e) { - var suggestions, entry, i, val = this.value; + var headerDiv = document.createElement("div");// create a div element - /*close any already open lists of autocompleted values*/ - closeAllLists(); - if (!val) { - return false; - } + var headerContent = '<form name="closeSearch"><h2><button type="submit" title="Close Search"><i class="svgs x"></i></button> <i class="svgs search"></i> '.concat(document.getElementById("searchinput").value, "</h2></form>");// header to use at top of results page - suggestions = document.getElementById("suggestions");// suggestions div, generated by search box + headerDiv.innerHTML = headerContent;// document element div (headerDiv), set the inner contents to our header html (headerContent) - let arr = search(val, 99);//limit results + ResultsClone.insertBefore(headerDiv, ResultsClone.firstChild);//insert our header div at the top of the page - for (i = 0; i < arr.length; i++) { - let elem = arr[i]; + main.innerHTML = ResultsClone.outerHTML;//display ResultsClone.outerHTML as the page + results.innerHTML = "";// clear the suggestions div popup + document.getElementById("searchinput").value = "";// clear the search input box + document.body.contains(document.closeSearch) && (document.closeSearch.onsubmit = function() { closeSearchNow() }) + return false + } - entry = document.createElement("DIV"); - entry.innerHTML = '<a href><span></span><span></span></a>'; - var a, t, d; - a = entry.querySelector('a'), - t = entry.querySelector('span:first-child'), - d = entry.querySelector('span:nth-child(2)'); - a.href = `${elem[1]}?q=${encodeURIComponent(val)}`;//a link - t.innerHTML = markTerm(elem[0], val);//title - d.innerHTML = markTerm(elem[2], val);//description + /* Close search suggestion popup list */ + function closeAllLists(elmnt) { + var suggestions = document.getElementById("suggestions"); + while (suggestions.firstChild) { + suggestions.removeChild(suggestions.firstChild); + } + } - suggestions.appendChild(entry); - } - }) -} -var searchinput = document.getElementById("searchinput"); -function suggestionFocus(e) { - if (e.keyCode === 191//forward slash - && document.activeElement.tagName !== "INPUT" - && document.activeElement.tagName !== "TEXTAREA") { - e.preventDefault(); - searchinput.focus(); - suggestions.classList.remove('d-none'); - } + function markTerm(input, term) { + return input.replace(new RegExp('(^|)(' + term + ')(|$)','ig'), '$1<mark>$2</mark>$3'); + } - if (e.keyCode === 27 ) {//escape - searchinput.blur(); - suggestions.classList.add('d-none'); - closeAllLists(); - } + function autocomplete(inp) { - const focusableSuggestions= suggestions.querySelectorAll('a'); - if (suggestions.classList.contains('d-none') - || focusableSuggestions.length === 0) { - return; - } - const focusable= [...focusableSuggestions]; - const index = focusable.indexOf(document.activeElement); + inp.addEventListener("input", function (e) { + var suggestions, entry, i, val = this.value; - let nextIndex = 0; + /*close any already open lists of autocompleted values*/ + closeAllLists(); + if (!val) { + return false; + } - if (e.keyCode === 38) {//up arrow - e.preventDefault(); - nextIndex= index > 0 ? index-1 : 0; - focusableSuggestions[nextIndex].focus(); - } - else if (e.keyCode === 40) {//down arrow - e.preventDefault(); - nextIndex= index+1 < focusable.length ? index+1 : index; - focusableSuggestions[nextIndex].focus(); - } + suggestions = document.getElementById("suggestions");// suggestions div, generated by search box -} -document.addEventListener("keydown", suggestionFocus); + let arr = search(val, 99);//limit results -let wasm, cachedTextDecoder = (autocomplete(document.getElementById("searchinput")), new TextDecoder("utf-8", { - ignoreBOM: !0, - fatal: !0 - })), - cachegetUint8Memory0 = (cachedTextDecoder.decode(), null); + for (i = 0; i < arr.length; i++) { + let elem = arr[i]; -function getUint8Memory0() { - return cachegetUint8Memory0 = null !== cachegetUint8Memory0 && cachegetUint8Memory0.buffer === wasm.memory.buffer ? cachegetUint8Memory0 : new Uint8Array(wasm.memory.buffer) -} + entry = document.createElement("DIV"); + entry.innerHTML = '<a href><span></span><span></span></a>'; + var a, t, d; + a = entry.querySelector('a'), + t = entry.querySelector('span:first-child'), + d = entry.querySelector('span:nth-child(2)'); + a.href = `${elem[1]}?q=${encodeURIComponent(val)}`;//a link + //t.innerHTML = markTerm(elem[0], val);//title + t.innerHTML = elem[0];//title + d.innerHTML = markTerm(elem[2], val);//description -function getStringFromWasm0(e, t) { - return cachedTextDecoder.decode(getUint8Memory0().subarray(e, e + t)) -} -const heap = new Array(32).fill(void 0); -heap.push(void 0, null, !0, !1); -let heap_next = heap.length; + suggestions.appendChild(entry); + } + }) + } -function addHeapObject(e) { - heap_next === heap.length && heap.push(heap.length + 1); - var t = heap_next; - return heap_next = heap[t], heap[t] = e, t -} -let WASM_VECTOR_LEN = 0, - cachedTextEncoder = new TextEncoder("utf-8"); -const encodeString = "function" == typeof cachedTextEncoder.encodeInto ? function(e, t) { - return cachedTextEncoder.encodeInto(e, t) -} : function(e, t) { - var n = cachedTextEncoder.encode(e); - return t.set(n), { - read: e.length, - written: n.length - } -}; -function passStringToWasm0(e, t, n) { - if (void 0 === n) { - const n = cachedTextEncoder.encode(e), - a = t(n.length); - return getUint8Memory0().subarray(a, a + n.length).set(n), WASM_VECTOR_LEN = n.length, a - } - let a = e.length, - o = t(a); - const i = getUint8Memory0(); - let r = 0; - for (; r < a; r++) { - const t = e.charCodeAt(r); - if (127 < t) break; - i[o + r] = t - } - if (r !== a) { - 0 !== r && (e = e.slice(r)), o = n(o, a, a = r + 3 * e.length); - const t = getUint8Memory0().subarray(o + r, o + a); - r += encodeString(e, t).written - } - return WASM_VECTOR_LEN = r, o -} + let wasm, cachedTextDecoder = (autocomplete(document.getElementById("searchinput")), new TextDecoder("utf-8", { + ignoreBOM: !0, + fatal: !0 + })), + cachegetUint8Memory0 = (cachedTextDecoder.decode(), null); -function getObject(e) { - return heap[e] -} + function getUint8Memory0() { + return cachegetUint8Memory0 = null !== cachegetUint8Memory0 && cachegetUint8Memory0.buffer === wasm.memory.buffer ? cachegetUint8Memory0 : new Uint8Array(wasm.memory.buffer) + } -function dropObject(e) { - e < 36 || (heap[e] = heap_next, heap_next = e) -} + function getStringFromWasm0(e, t) { + return cachedTextDecoder.decode(getUint8Memory0().subarray(e, e + t)) + } + const heap = new Array(32).fill(void 0); + heap.push(void 0, null, !0, !1); + let heap_next = heap.length; -function takeObject(e) { - var t = getObject(e); - return dropObject(e), t -} + function addHeapObject(e) { + heap_next === heap.length && heap.push(heap.length + 1); + var t = heap_next; + return heap_next = heap[t], heap[t] = e, t + } + let WASM_VECTOR_LEN = 0, + cachedTextEncoder = new TextEncoder("utf-8"); + const encodeString = "function" == typeof cachedTextEncoder.encodeInto ? function(e, t) { + return cachedTextEncoder.encodeInto(e, t) + } : function(e, t) { + var n = cachedTextEncoder.encode(e); + return t.set(n), { + read: e.length, + written: n.length + } + }; -function search(e, t) { - var e = passStringToWasm0(e, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc), - n = WASM_VECTOR_LEN; - return takeObject(wasm.search(e, n, t)) -} -async function load(e, t) { - if ("function" == typeof Response && e instanceof Response) { - if ("function" == typeof WebAssembly.instantiateStreaming) try { - return await WebAssembly.instantiateStreaming(e, t) - } catch (t) { - if ("application/wasm" == e.headers.get("Content-Type")) throw t; - console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", t) - } - var n = await e.arrayBuffer(); - return WebAssembly.instantiate(n, t) - } - n = await WebAssembly.instantiate(e, t); - return n instanceof WebAssembly.Instance ? { - instance: n, - module: e - } : n -} -async function init(e) { - var baseUrl = document.querySelector("meta[name='base']").getAttribute("content"); - if (baseUrl.slice(-1) == "/") { - baseUrl = baseUrl.slice(0, -1); + function passStringToWasm0(e, t, n) { + if (void 0 === n) { + const n = cachedTextEncoder.encode(e), + a = t(n.length); + return getUint8Memory0().subarray(a, a + n.length).set(n), WASM_VECTOR_LEN = n.length, a + } + let a = e.length, + o = t(a); + const i = getUint8Memory0(); + let r = 0; + for (; r < a; r++) { + const t = e.charCodeAt(r); + if (127 < t) break; + i[o + r] = t + } + if (r !== a) { + 0 !== r && (e = e.slice(r)), o = n(o, a, a = r + 3 * e.length); + const t = getUint8Memory0().subarray(o + r, o + a); + r += encodeString(e, t).written + } + return WASM_VECTOR_LEN = r, o + } + + function getObject(e) { + return heap[e] + } + + function dropObject(e) { + e < 36 || (heap[e] = heap_next, heap_next = e) + } + + function takeObject(e) { + var t = getObject(e); + return dropObject(e), t + } + + function search(e, t) { + var e = passStringToWasm0(e, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc), + n = WASM_VECTOR_LEN; + return takeObject(wasm.search(e, n, t)) + } + async function load(e, t) { + if ("function" == typeof Response && e instanceof Response) { + if ("function" == typeof WebAssembly.instantiateStreaming) try { + return await WebAssembly.instantiateStreaming(e, t) + } catch (t) { + if ("application/wasm" == e.headers.get("Content-Type")) throw t; + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", t) + } + var n = await e.arrayBuffer(); + return WebAssembly.instantiate(n, t) + } + n = await WebAssembly.instantiate(e, t); + return n instanceof WebAssembly.Instance ? { + instance: n, + module: e + } : n + } + async function init(e) { + var baseUrl = document.querySelector("meta[name='base']").getAttribute("content"); + if (baseUrl.slice(-1) == "/") { + baseUrl = baseUrl.slice(0, -1); + } + void 0 === e && (e = new URL(baseUrl + "/tinysearch_engine_bg.wasm")); + const t = { + wbg: {} + }; + t.wbg.__wbindgen_json_parse = function(e, t) { + return addHeapObject(JSON.parse(getStringFromWasm0(e, t))) + }; + var { + instance: e, + module: n + } = await load(await (e = "string" == typeof e || "function" == typeof Request && e instanceof Request || "function" == typeof URL && e instanceof URL ? fetch(e) : e), t); + return wasm = e.exports, init.__wbindgen_wasm_module = n, wasm + } + + document.goSearch.onsubmit = function () { return goSearchNow() }; + }()); } - void 0 === e && (e = new URL(baseUrl + "/tinysearch_engine_bg.wasm")); - const t = { - wbg: {} - }; - t.wbg.__wbindgen_json_parse = function(e, t) { - return addHeapObject(JSON.parse(getStringFromWasm0(e, t))) - }; - var { - instance: e, - module: n - } = await load(await (e = "string" == typeof e || "function" == typeof Request && e instanceof Request || "function" == typeof URL && e instanceof URL ? fetch(e) : e), t); - return wasm = e.exports, init.__wbindgen_wasm_module = n, wasm -} +}; |
