Skip to content

Commit d805466

Browse files
committed
Allos filtering by chip *and* search text. Eg click 'Clock' and search 'Anton'
1 parent 2938c27 commit d805466

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

js/index.js

+36-30
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ function refreshLibrary(options) {
523523
// Work out what we should be filtering, based on the URL
524524
let searchType = ""; // possible values: hash, chip, full, id
525525
let searchValue = ""; // the actual value to search for
526+
let searchChip = ""; // if a chip was selected, this is the one to use
526527

527528
if (window.location.hash) {
528529
searchValue = decodeURIComponent(window.location.hash.slice(1)).toLowerCase();
@@ -539,17 +540,19 @@ function refreshLibrary(options) {
539540
searchType = "full";
540541
}
541542
if (searchParams.has("c")) {
542-
searchValue = searchParams.get("c").toLowerCase();
543-
searchType = "chip";
543+
searchChip = searchParams.get("c").toLowerCase();
544544
}
545545
}
546-
if (searchType === "hash" && chips.indexOf(searchValue)>=0)
547-
searchType = "chip";
546+
if (searchType === "hash" && chips.indexOf(searchValue)>=0) {
547+
searchType = "";
548+
searchValue = "";
549+
searchChip = searchValue;
550+
}
548551
// Update the 'chips' to match the current window location
549552
let filtersContainer = document.querySelector("#librarycontainer .filter-nav");
550553
filtersContainer.querySelector('.active').classList.remove('active');
551-
if(searchType == "chip") {
552-
let hashFilter = filtersContainer.querySelector('.chip[filterid="'+searchValue+'"]');
554+
if(searchChip) {
555+
let hashFilter = filtersContainer.querySelector('.chip[filterid="'+searchChip+'"]');
553556
if (hashFilter) hashFilter.classList.add('active');
554557
} else filtersContainer.querySelector('.chip[filterid]').classList.add('active');
555558
// update the search box value
@@ -562,29 +565,28 @@ function refreshLibrary(options) {
562565
// Now filter according to what was set
563566
let visibleApps = appJSON.slice(); // clone so we don't mess with the original
564567
let sortedByRelevance = false;
568+
// filter visibleApps by chip
569+
let searchResult; // array of { app:app, relevance:number }
570+
if (searchChip) {
571+
if (searchChip == "favourites") {
572+
visibleApps = visibleApps.filter(app => app.id?SETTINGS.favourites.filter(e => e == app.id).length:0);
573+
} else {
574+
// Some chips represent a metadata "type" element:
575+
// - the "Clocks" chip must show only apps with "type": "clock"
576+
// - the "Widgets" chip must show only apps with "type": "widget"
577+
// and so on.
578+
// If the type is NOT in the array below then the search will be tag-based instead
579+
// of type-based.
580+
const supportedMetadataTypes = ["clock", "widget", "launch", "textinput", "ram"];
581+
if (supportedMetadataTypes.includes(searchChip.toLowerCase()))
582+
visibleApps = visibleApps.filter(app => (app.type||"app").toLowerCase() == searchChip.toLowerCase() );
583+
else
584+
visibleApps = visibleApps.filter(app => app.tags && app.tags.split(',').includes(searchChip) );
585+
}
586+
}
587+
// Now do our search, put the values in searchResult
565588
if (searchValue) {
566-
// Now do our search, put the values in searchResult
567-
let searchResult; // array of { app:app, relevance:number }
568-
if (searchType === "chip") {
569-
if (searchValue == "favourites") {
570-
searchResult = visibleApps.map(app => ({
571-
app : app,
572-
relevance : app.id?SETTINGS.favourites.filter(e => e == app.id).length:0
573-
}));
574-
} else {
575-
// Some chips represent a metadata "type" element:
576-
// - the "Clocks" chip must show only apps with "type": "clock"
577-
// - the "Widgets" chip must show only apps with "type": "widget"
578-
// and so on.
579-
// If the type is NOT in the array below then the search will be tag-based instead
580-
// of type-based.
581-
const supportedMetadataTypes = ["clock", "widget", "launch", "textinput", "ram"];
582-
if (supportedMetadataTypes.includes(searchValue.toLowerCase()))
583-
searchResult = visibleApps.map(app => ({ app:app, relevance: (app.type||"app").toLowerCase() == searchValue.toLowerCase() ? 1 : 0 }));
584-
else
585-
searchResult = visibleApps.map(app => ({ app:app, relevance: (app.tags && app.tags.split(',').includes(searchValue)) ? 1 : 0 }));
586-
}
587-
} else if (searchType === "hash") {
589+
if (searchType === "hash") {
588590
sortedByRelevance = true;
589591
searchResult = visibleApps.map(app => ({
590592
app : app,
@@ -1140,9 +1142,13 @@ const searchInputChangedDebounced = debounce(function() {
11401142
refreshLibrary({dontChangeSearchBox:true});
11411143
}, 300);
11421144
librarySearchInput.addEventListener('input', evt => {
1143-
var searchValue = evt.target.value.toLowerCase();
1145+
let searchValue = evt.target.value.toLowerCase();
11441146
// Update window URL
1145-
window.history.replaceState(null, null, "?q=" + searchValue);
1147+
let c = "";
1148+
let searchParams = new URLSearchParams(window.location.search);
1149+
if (searchParams.has("c"))
1150+
c = `c=${encodeURIComponent(searchParams.get("c").toLowerCase())}&`;
1151+
window.history.replaceState(null, null, `?${c}q=${encodeURIComponent(searchValue)}`);
11461152
searchInputChangedDebounced();
11471153
});
11481154

0 commit comments

Comments
 (0)