Files
notifications-admin/app/assets/javascripts/liveSearch.js
Chris Hill-Scott d7e6e6ae90 Make live search restore items to default state
At the moment live search works by either setting something to hidden
or displayed. When the search term is empty, it sets everything to
visible.

This doesn’t work with folders because some of them should be hidden by
default (the ones not at the current level).

We can account for this special case (empty search term) by removing
the `display` CSS attribute when the search term is empty. This means
that each item’s visibility will be controlled by whatever is set in the
CSS file. Using jQuery Setting a property to `''` removes it.
2018-11-23 14:35:58 +00:00

50 lines
837 B
JavaScript

(function(Modules) {
"use strict";
let normalize = (string) => string.toLowerCase().replace(/ /g,'');
let filter = ($searchBox, $targets) => () => {
let query = normalize($searchBox.val());
$targets.each(function() {
let content = $(this).text();
if (query == '') {
$(this).css('display', '');
return;
}
$(this).toggle(
normalize(content).indexOf(normalize(query)) > -1
);
});
};
Modules.LiveSearch = function() {
this.start = function(component) {
let $component = $(component);
let $searchBox = $('input', $component);
let filterFunc = filter(
$searchBox,
$($component.data('targets'))
);
$searchBox.on('keyup input', filterFunc);
filterFunc();
};
};
})(window.GOVUK.Modules);