From 72e11c0eda1f89ff822ff5fd3ab22e1fcc34317a Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Thu, 8 Aug 2019 00:16:30 +0530 Subject: [PATCH] attemp at fixing search not working --- assets/js/widgets/search.js | 130 ------------------------------------ 1 file changed, 130 deletions(-) delete mode 100755 assets/js/widgets/search.js diff --git a/assets/js/widgets/search.js b/assets/js/widgets/search.js deleted file mode 100755 index 9ee6a35..0000000 --- a/assets/js/widgets/search.js +++ /dev/null @@ -1,130 +0,0 @@ -"use strict"; -{{ $isRel := and $.Site.Params.ref_type (eq $.Site.Params.ref_type "relative") }} -{{ if $isRel }} -// JavaScript trickery to make relative paths work -var lunrJSON = ""; -var pathToRoot = ""; -var scripts = document.getElementsByTagName('script'); -for (var i = 0; i < scripts.length; i++ ) { - var scr = scripts[i]; - if (scr.hasAttribute("src") && ((scr.getAttribute("src")[0] === '/' && scr.getAttribute("src")[1] !== '/') || scr.getAttribute("src")[0] === '.')) { - pathToRoot = scr.getAttribute("src").replace(/\/js\/.+$/, ""); - lunrJSON = pathToRoot + "/index.json"; - break; - } -} -{{ else }} -var lunrJSON = "{{ partial "make_link" (dict "URL" "/index.json" "Root" $) }}"; -{{ end }} -var lunrIndex = undefined; -var rawIndex = []; -var sbox = document.getElementById("searchbox"); -var sresults = document.getElementById("search-results"); -var searchAnalytics = undefined; - -function getJSON(url) { - return new Promise(function(resolve, reject) { - var req = new XMLHttpRequest(); - req.responseType = 'json'; - req.open('GET', url, true); - req.addEventListener("load", function() { - var status = req.status; - if (status === 200) - resolve(req.response); - else - reject(status); - }); - console.log("Loading search index"); - req.send(); - }); -} - -function loadIndex(url) { - return new Promise(function(resolve, reject) { - getJSON(url).then( - function(json) { - rawIndex = json; - lunrIndex = lunr(function () { - this.ref('id'); - // These are all fields searched for results - this.field('url'); - this.field('name'); - this.field('description'); - this.field('body'); - - for (var i = 0; i < rawIndex.length; i++) { - // Adding id allows to access other properties later - rawIndex[i].id = i; - this.add(rawIndex[i]); - } - }); - resolve(lunrIndex); - }, - function(status) { - console.error("Unable to load search index. Status: " + status); - reject(status); - } - ); - }); -} - -function populateSearchbox() { - // Trailing asterisk wildcards a potentially incomplete word - var content = sbox.value; - if (content.length > 0 && content[content.length - 1] !== " ") - content += "*"; - // Empty previous results - sresults.innerHTML = ""; - - if (lunrIndex !== undefined) { - var results = lunrIndex.search(content); - - // Add each result to a
Name
- // and append to the results - results.forEach(function(data) { - var result = document.createElement("div"); - result.setAttribute("class", "search-result cell button"); - result.innerHTML = rawIndex[data.ref].name; - var link = document.createElement("a"); - {{ if $isRel }} - link.setAttribute("href", pathToRoot + rawIndex[data.ref].url); - {{ else }} - link.setAttribute("href", rawIndex[data.ref].url); - {{ end }} - // Add event handler to results to log search if using piwik - if (typeof _paq !== "undefined") { - link.addEventListener("click", function() { - searchAnalytics = { - keyword: sbox.value, - numResults: results.length - }; - }); - } - link.appendChild(result); - sresults.appendChild(link); - }); - - // Hide if no content (only asterisk, thanks to above line) - sresults.setAttribute("class", results.length === 0 ? "hide" : ""); - } -} - -sbox.addEventListener("keyup", populateSearchbox); -window.addEventListener("beforeunload", function(){ - if (typeof _paq !== "undefined" && searchAnalytics !== undefined) { - _paq.push(['trackSiteSearch', - // Search keyword searched for - searchAnalytics.keyword, - // Search category selected in your search engine. If you do not need this, set to false - false, - // Number of results on the Search results page. Zero indicates a 'No Result Search Keyword'. Set to false if you don't know - searchAnalytics.numResults - ]); - } -}); - -var lunrIndex = undefined; -loadIndex(lunrJSON).then( - function(index) { lunrIndex = index; }, - function(status){} -);