From 19f530a7b483fb290e877a92a06b813f442c79bf Mon Sep 17 00:00:00 2001 From: Rohan Chandra Date: Fri, 17 Nov 2017 10:36:25 +1100 Subject: [PATCH] refactor search JS --- assets/js/search.js | 121 +++++++++++++++++++++++++++----------------- search.html | 2 +- 2 files changed, 75 insertions(+), 48 deletions(-) diff --git a/assets/js/search.js b/assets/js/search.js index 489da8f..144b86e 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -1,66 +1,93 @@ (function () { - function displaySearchResults(results, store) { - var searchResults = document.getElementById('search-results'); + var SEARCH_BOX_ID = "search-box"; + var NO_RESULTS_MESSAGE_ID = "not-found"; + var SEARCH_RESULTS_CONTAINER_ID = "search-results"; + var QUERY_VARIABLE_URL_STRING = "query"; - if (results.length) { // Are there any results? - document.getElementById('not-found').style.visibility = 'hidden'; - var appendString = ''; + function getQueryVariable(queryParam) { + var query = window.location.search.substring(1); + var vars = query.split("&"); + for (var i = 0; i < vars.length; i++) { + var pair = vars[i].split("="); + var param = pair[0]; + var value = pair[1]; - for (var i = 0; i < results.length; i++) { // Iterate over the results - var item = store[results[i].ref]; - appendString += '

' + item.title + '

'; - appendString += "
" + item.date + "
"; - appendString += '

' + item.content.substring(0, 150) + '...

'; + if (param === queryParam) { + return decodeURIComponent(value.replace(/\+/g, "%20")); } - - searchResults.innerHTML = appendString; - } else { - //searchResults.innerHTML = '

{{ site.search_no_results }}

'; - } } - function getQueryVariable(variable) { - var query = window.location.search.substring(1); - var vars = query.split('&'); + function getSearchTerm() { + return getQueryVariable(QUERY_VARIABLE_URL_STRING); + } - for (var i = 0; i < vars.length; i++) { - var pair = vars[i].split('='); + function setSearchBoxValue(searchBoxValue) { + document.getElementById(SEARCH_BOX_ID).setAttribute("value", searchBoxValue); + } - if (pair[0] === variable) { - return decodeURIComponent(pair[1].replace(/\+/g, '%20')); + function showNoResultsMessage() { + document.getElementById(NO_RESULTS_MESSAGE_ID).style.display = "block"; + } + + function setSearchResultsHTML(innerHTML) { + var searchResults = document.getElementById(SEARCH_RESULTS_CONTAINER_ID); + searchResults.innerHTML = innerHTML; + } + + function createPostListingHTML(postItem) { + var headingHTML = "

" + postItem.title + "

"; + var metaHTML = "
" + postItem.date + "
"; + var descriptionHTML = "

" + postItem.content.substring(0, 150) + "...

"; + return headingHTML + metaHTML + descriptionHTML; + } + + function displaySearchResults(results, store) { + if (results.length) { + var postsListingHTML = ""; + for (var i = 0; i < results.length; i++) { + var postItem = store[results[i].ref]; + postsListingHTML += createPostListingHTML(postItem); } + setSearchResultsHTML(postsListingHTML); + } else { + showNoResultsMessage(); } } - var searchTerm = getQueryVariable('query'); + function addPostToSearchIndex(lunrIndex, key, postJSON) { + lunrIndex.add({ + "id": key, + "title": postJSON.title, + "author": postJSON.author, + "category": postJSON.category, + "content": postJSON.content + }); + } - if (searchTerm) { - document.getElementById('search-box').setAttribute("value", searchTerm); + function search(searchTerm) { + setSearchBoxValue(searchTerm); - // Initalize lunr with the fields it will be searching on. I've given title - // a boost of 10 to indicate matches on this field are more important. - var idx = lunr(function () { - this.field('id'); - this.field('title', { - boost: 10 - }); - this.field('author'); - this.field('category'); - this.field('content'); - }); + var lunrIndex = lunr(function () { + this.field("id"); + this.field("title", { + boost: 10 + }); + this.field("author"); + this.field("category"); + this.field("content"); + }); - for (var key in window.store) { // Add the data to lunr - idx.add({ - 'id': key, - 'title': window.store[key].title, - 'author': window.store[key].author, - 'category': window.store[key].category, - 'content': window.store[key].content - }); + for (var key in window.store) { + addPostToSearchIndex(lunrIndex, key, window.store[key]) + } - var results = idx.search(searchTerm); // Get lunr to perform a search - displaySearchResults(results, window.store); // We'll write this in the next section - } + var results = lunrIndex.search(searchTerm); + displaySearchResults(results, window.store); + } + + var searchTerm = getSearchTerm(); + if (searchTerm) { + search(searchTerm); } })(); diff --git a/search.html b/search.html index 5f4ff04..fa87186 100644 --- a/search.html +++ b/search.html @@ -6,7 +6,7 @@