Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved filters slightly #50

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions frontend/src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ label {
position: absolute;
background-color: var(--color-background);
border: 1px solid black;
min-height: 15rem;
min-height: 10rem;
height: fit-content;
max-height: 30rem;
width: 20rem;
Expand All @@ -211,14 +211,16 @@ label {
display: none;
}

.filter-top {
.filter-bottom {
display: flex;
align-items: center;
justify-content: center;
height: 2.5rem;
width: 100%;
background-color: lightgray;
gap: 0.5rem;
margin-top: 0.5rem;
font-size: 14px;

.sort-item {
display: flex;
Expand All @@ -243,9 +245,9 @@ label {

.filter-list {
padding: 0rem 0rem;
overflow-y: scroll;
max-height: calc(30rem - 2.5rem - 2.75rem);
min-height: calc(15rem - 2.5rem - 2.75rem);
overflow-y: auto;
max-height: calc(28.5rem - 2.5rem - 2.75rem);
min-height: calc(10.75rem - 2.5rem - 2.75rem);

label {
display: flex;
Expand All @@ -255,13 +257,20 @@ label {

.filter-search {
display: flex;
border-top: 1px solid gray;
border: 0px;
height: 2.75rem;
width: 100%;
width: auto;
position: relative;
padding: 5px;
align-items: center;
gap: 0.5rem;
margin: 0.5rem 0.5rem 0 0.5rem;

button {
font-size: 25px;
border: 0px;
background-color: inherit;
}
}
}

Expand Down
66 changes: 50 additions & 16 deletions frontend/src/lib/Filter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@
let sort_by: SortType = SortType.Name;
let sort_direction: boolean = true;

function clickOutside(node: HTMLElement, {enabled: initialEnabled, cb}: { enabled: boolean, cb: Function }) {
const handleOutsideClick = ({target}: MouseEvent) => {
if (target instanceof Node && !node.contains(target)) {
cb();
}
};

function update({enabled}: { enabled: boolean }) {
if (enabled) {
window.addEventListener('click', handleOutsideClick);
} else {
window.removeEventListener('click', handleOutsideClick);
}
}

update({enabled: initialEnabled});
return {
update,
destroy() {
window.removeEventListener('click', handleOutsideClick);
}
};
}

function sort(items: { [key: string]: number[] }, sort_by: SortType, sort_direction: boolean) {
let checked = Object.entries(items).filter((i) => selected.includes(i[1]));
let unchecked = Object.entries(items).filter((i) => !selected.includes(i[1]));
Expand Down Expand Up @@ -55,24 +79,21 @@
$: open = $open_filter == name;
</script>

<div class={open ? 'filter open' : 'filter'}>
<div class={open ? 'filter open' : 'filter'} use:clickOutside={{ enabled: open, cb: () => open = false }}>
<button class="filter-box" on:click={() => $open_filter === name ? $open_filter = "" : $open_filter = name}>
<span class="filter-name">{name}</span>
{#if count > 0}<span class="filter-count">{count}</span>{/if}
<span class="filter-wedge">❮</span>
</button>
<div class={open ? 'filter-popup' : 'filter-popup hidden'}>
<div class="filter-top">
<button class={ sort_by === SortType.Name ? sort_direction ? "sort-item sorted asc" : 'sort-item sorted desc' : "sort-item"}
on:click={() => update_sort(SortType.Name)}>Name
</button>
<button class={ sort_by === SortType.Count ? sort_direction ? "sort-item sorted asc" : 'sort-item sorted desc' : "sort-item"}
on:click={() => update_sort(SortType.Count)}>Count
</button>

<button on:click={select_all}>All</button>
<button on:click={select_none}>None</button>
</div>

{#if Object.entries(values).length > 10}
<div class="filter-search">
🔍 <input type="text" bind:value={filter} placeholder="Search { name }"/>
<!--<button on:click={() => filter = ""}>𐄂</button>-->
</div>
{/if}


<div class="filter-list">
{#each sorted_values as [key, value]}
Expand All @@ -84,9 +105,22 @@
{/if}
{/each}
</div>
<div class="filter-search">
🔍 <input type="text" bind:value={filter}/>
<button on:click={() => filter = ""}>Clear</button>
</div>

{#if Object.entries(values).length > 5}
<div class="filter-bottom">
Sort by
<button class={ sort_by === SortType.Name ? sort_direction ? "sort-item sorted asc" : 'sort-item sorted desc' : "sort-item"}
on:click={() => update_sort(SortType.Name)}>Name
</button>
<button class={ sort_by === SortType.Count ? sort_direction ? "sort-item sorted asc" : 'sort-item sorted desc' : "sort-item"}
on:click={() => update_sort(SortType.Count)}>Count
</button>

Select
<button on:click={select_all}>🗹</button>
<button on:click={select_none}>☐</button>
</div>
{/if}

</div>
</div>