From 175d89c151188f93de6c5e91eb9339034eb0de2d Mon Sep 17 00:00:00 2001 From: Sagargg Date: Mon, 7 Aug 2023 15:40:19 +0545 Subject: [PATCH] [improve][xs]: facet search improvement --- ckanext/rvr/assets/js/facetSearch.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ckanext/rvr/assets/js/facetSearch.js b/ckanext/rvr/assets/js/facetSearch.js index 170fd17..0c15344 100644 --- a/ckanext/rvr/assets/js/facetSearch.js +++ b/ckanext/rvr/assets/js/facetSearch.js @@ -14,7 +14,7 @@ this.ckan.module('facet-search', function (jQuery, _) { var listItems = this.el.find('nav ul li'); this.options[facet] = listItems; - input.bind('change paste keyup', this._onChange); + input.bind('keyup', this._onChange); }, _onChange: function (event) { @@ -25,24 +25,31 @@ this.ckan.module('facet-search', function (jQuery, _) { var queryText = event.target.value.toLowerCase(); var listItems = options[options.facet]; - var targetItems = listItems.filter(function () { - return $(this).find("span.item-label").text().trim().toLowerCase().indexOf(queryText.toLowerCase()) > -1; - }); - targetItems.each(function () { + listItems.removeClass("search"); + + var searchedItems = listItems.filter(function () { + var itemLabel = $(this).find("span.item-label").text().trim().toLowerCase(); + return ( + itemLabel.startsWith(queryText.toLowerCase()) || + (queryText.length >= 3 && itemLabel.indexOf(queryText) !== -1) + ); + }) + + searchedItems.each(function () { $(this).toggleClass("search", queryText !== ""); }); var listEl = this.el.find('nav ul'); - if (targetItems.length === 0 && queryText !== "") { + if (searchedItems.length === 0 && queryText !== "") { listEl.replaceWith(" "); - } else if (targetItems.length === 0 && queryText === "") { - var newItems = options[options.facet].clone(); + } else if (searchedItems.length === 0 && queryText === "") { + var resetItems = options[options.facet].clone(); listEl.find("span.no-results").remove(); - listEl.empty().append(newItems); + listEl.empty().append(resetItems); } else { listEl.find("span.no-results").remove(); - listEl.prepend(targetItems).addClass('list-unstyled nav nav-simple nav-facet'); + listEl.prepend(searchedItems).addClass('list-unstyled nav nav-simple nav-facet'); } } }