|
45 | 45 | 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 |
46 | 46 | let jobFilters = $state([]); |
47 | 47 | let nameFilter = $state(""); |
48 | | - let sorting = $state({ field: "totalJobs", direction: "down" }); |
| 48 | + let sorting = $state({ field: "totalJobs", direction: "desc" }); |
49 | 49 |
|
50 | 50 | /* Derived Vars */ |
51 | 51 | let stats = $derived( |
|
67 | 67 | ); |
68 | 68 |
|
69 | 69 | /* 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 | + } |
72 | 92 | } |
73 | 93 |
|
74 | 94 | function sort(stats, sorting, nameFilter) { |
75 | | - const idCmp = sorting.direction == "up" |
| 95 | + const idCmp = sorting.direction == "asc" |
76 | 96 | ? (a, b) => b.id.localeCompare(a.id) |
77 | 97 | : (a, b) => a.id.localeCompare(b.id) |
78 | 98 |
|
79 | 99 | // Force empty or undefined strings to the end of the list |
80 | | - const nameCmp = sorting.direction == "up" |
| 100 | + const nameCmp = sorting.direction == "asc" |
81 | 101 | ? (a, b) => !a?.name ? 1 : (!b?.name ? -1 : (b.name.localeCompare(a.name))) |
82 | 102 | : (a, b) => !a?.name ? 1 : (!b?.name ? -1 : (a.name.localeCompare(b.name))) |
83 | 103 |
|
84 | | - const intCmp = sorting.direction == "up" |
| 104 | + const intCmp = sorting.direction == "asc" |
85 | 105 | ? (a, b) => a[sorting.field] - b[sorting.field] |
86 | 106 | : (a, b) => b[sorting.field] - a[sorting.field]; |
87 | 107 |
|
|
141 | 161 | > |
142 | 162 | {#if sorting?.field == "id"} |
143 | 163 | <!-- 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'}`} /> |
145 | 165 | {:else} |
146 | 166 | <Icon name="three-dots-vertical" /> |
147 | 167 | {/if} |
|
156 | 176 | onclick={() => changeSorting("name")} |
157 | 177 | > |
158 | 178 | {#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'}`} /> |
160 | 180 | {:else} |
161 | 181 | <Icon name="three-dots-vertical" /> |
162 | 182 | {/if} |
|
172 | 192 | > |
173 | 193 | {#if sorting?.field == "totalJobs"} |
174 | 194 | <!-- 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'}`} /> |
176 | 196 | {:else} |
177 | 197 | <Icon name="three-dots-vertical" /> |
178 | 198 | {/if} |
|
186 | 206 | onclick={() => changeSorting("totalWalltime")} |
187 | 207 | > |
188 | 208 | {#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'}`} /> |
190 | 210 | {:else} |
191 | 211 | <Icon name="three-dots-vertical" /> |
192 | 212 | {/if} |
|
200 | 220 | onclick={() => changeSorting("totalCoreHours")} |
201 | 221 | > |
202 | 222 | {#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'}`} /> |
204 | 224 | {:else} |
205 | 225 | <Icon name="three-dots-vertical" /> |
206 | 226 | {/if} |
|
214 | 234 | onclick={() => changeSorting("totalAccHours")} |
215 | 235 | > |
216 | 236 | {#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'}`} /> |
218 | 238 | {:else} |
219 | 239 | <Icon name="three-dots-vertical" /> |
220 | 240 | {/if} |
|
0 commit comments