2017-03-14 10:46:38 +00:00
|
|
|
(function(Modules) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
let normalize = (string) => string.toLowerCase().replace(/ /g,'');
|
|
|
|
|
|
|
|
|
|
let filter = ($searchBox, $targets) => () => {
|
|
|
|
|
|
|
|
|
|
let query = normalize($searchBox.val());
|
|
|
|
|
|
|
|
|
|
$targets.each(function() {
|
|
|
|
|
|
2018-11-22 17:57:05 +00:00
|
|
|
let content = $('.live-search-relevant', this).text() || $(this).text();
|
2017-03-14 10:46:38 +00:00
|
|
|
|
2018-12-05 11:24:16 +00:00
|
|
|
if ($(this).has(':checked').length) {
|
|
|
|
|
$(this).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 17:32:36 +00:00
|
|
|
if (query == '') {
|
|
|
|
|
$(this).css('display', '');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 10:46:38 +00:00
|
|
|
$(this).toggle(
|
|
|
|
|
normalize(content).indexOf(normalize(query)) > -1
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2018-12-21 14:37:01 +00:00
|
|
|
// make sticky JS recalculate its cache of the element's position
|
|
|
|
|
// because live search can change the height document
|
|
|
|
|
if ('stickAtBottomWhenScrolling' in GOVUK) {
|
|
|
|
|
GOVUK.stickAtBottomWhenScrolling.recalculate();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 10:46:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|