Skip to content

Commit 1791e66

Browse files
add default orders on list sorting change
1 parent a713410 commit 1791e66

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

web/frontend/src/List.root.svelte

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
let filterComponent = $state(); // see why here: https://stackoverflow.com/questions/58287729/how-can-i-export-a-function-from-a-svelte-component-that-changes-a-value-in-the
4646
let jobFilters = $state([]);
4747
let nameFilter = $state("");
48-
let sorting = $state({ field: "totalJobs", direction: "down" });
48+
let sorting = $state({ field: "totalJobs", direction: "desc" });
4949
5050
/* Derived Vars */
5151
let stats = $derived(
@@ -67,21 +67,41 @@
6767
);
6868
6969
/* Functions */
70-
function changeSorting(field) {
71-
sorting = { field, direction: sorting?.direction == "down" ? "up" : "down" };
70+
function changeSorting(newField) {
71+
if (sorting.field == newField) {
72+
// Same Field, Change Direction
73+
sorting = { field: newField, direction: sorting.direction == "desc" ? "asc" : "desc" };
74+
} else {
75+
// Change Field, Apply Field Dependent Default
76+
switch (newField) {
77+
case "id":
78+
case "name":
79+
case "totalJobs":
80+
case "totalWalltime":
81+
sorting = { field: newField, direction: "desc" };
82+
break
83+
case "totalCoreHours":
84+
case "totalAccHours":
85+
sorting = { field: newField, direction: "asc" };
86+
break
87+
default:
88+
// Fallback: Change only Field
89+
sorting = { field: newField, direction: sorting.direction };
90+
}
91+
}
7292
}
7393
7494
function sort(stats, sorting, nameFilter) {
75-
const idCmp = sorting.direction == "up"
95+
const idCmp = sorting.direction == "asc"
7696
? (a, b) => b.id.localeCompare(a.id)
7797
: (a, b) => a.id.localeCompare(b.id)
7898
7999
// Force empty or undefined strings to the end of the list
80-
const nameCmp = sorting.direction == "up"
100+
const nameCmp = sorting.direction == "asc"
81101
? (a, b) => !a?.name ? 1 : (!b?.name ? -1 : (b.name.localeCompare(a.name)))
82102
: (a, b) => !a?.name ? 1 : (!b?.name ? -1 : (a.name.localeCompare(b.name)))
83103
84-
const intCmp = sorting.direction == "up"
104+
const intCmp = sorting.direction == "asc"
85105
? (a, b) => a[sorting.field] - b[sorting.field]
86106
: (a, b) => b[sorting.field] - a[sorting.field];
87107
@@ -141,7 +161,7 @@
141161
>
142162
{#if sorting?.field == "id"}
143163
<!-- Note on Icon-Name: Arrow-indicator always down, only alpha-indicator switches -->
144-
<Icon name={`sort-alpha-${sorting?.direction == 'down' ? 'down' : 'down-alt'}`} />
164+
<Icon name={`sort-alpha-${sorting?.direction == 'desc' ? 'down' : 'down-alt'}`} />
145165
{:else}
146166
<Icon name="three-dots-vertical" />
147167
{/if}
@@ -156,7 +176,7 @@
156176
onclick={() => changeSorting("name")}
157177
>
158178
{#if sorting?.field == "name"}
159-
<Icon name={`sort-alpha-${sorting?.direction == 'down' ? 'down' : 'down-alt'}`} />
179+
<Icon name={`sort-alpha-${sorting?.direction == 'desc' ? 'down' : 'down-alt'}`} />
160180
{:else}
161181
<Icon name="three-dots-vertical" />
162182
{/if}
@@ -172,7 +192,7 @@
172192
>
173193
{#if sorting?.field == "totalJobs"}
174194
<!-- Note on Icon-Name: Arrow-indicator always down, only numeric-indicator switches -->
175-
<Icon name={`sort-numeric-${sorting?.direction == 'down' ? 'down-alt' : 'down'}`} />
195+
<Icon name={`sort-numeric-${sorting?.direction == 'desc' ? 'down-alt' : 'down'}`} />
176196
{:else}
177197
<Icon name="three-dots-vertical" />
178198
{/if}
@@ -186,7 +206,7 @@
186206
onclick={() => changeSorting("totalWalltime")}
187207
>
188208
{#if sorting?.field == "totalWalltime"}
189-
<Icon name={`sort-numeric-${sorting?.direction == 'down' ? 'down-alt' : 'down'}`} />
209+
<Icon name={`sort-numeric-${sorting?.direction == 'desc' ? 'down-alt' : 'down'}`} />
190210
{:else}
191211
<Icon name="three-dots-vertical" />
192212
{/if}
@@ -200,7 +220,7 @@
200220
onclick={() => changeSorting("totalCoreHours")}
201221
>
202222
{#if sorting?.field == "totalCoreHours"}
203-
<Icon name={`sort-numeric-${sorting?.direction == 'down' ? 'down-alt' : 'down'}`} />
223+
<Icon name={`sort-numeric-${sorting?.direction == 'desc' ? 'down-alt' : 'down'}`} />
204224
{:else}
205225
<Icon name="three-dots-vertical" />
206226
{/if}
@@ -214,7 +234,7 @@
214234
onclick={() => changeSorting("totalAccHours")}
215235
>
216236
{#if sorting?.field == "totalAccHours"}
217-
<Icon name={`sort-numeric-${sorting?.direction == 'down' ? 'down-alt' : 'down'}`} />
237+
<Icon name={`sort-numeric-${sorting?.direction == 'desc' ? 'down-alt' : 'down'}`} />
218238
{:else}
219239
<Icon name="three-dots-vertical" />
220240
{/if}

0 commit comments

Comments
 (0)