Skip to content

Commit 5ce0883

Browse files
committed
Add keys sorting
1 parent 082ec7d commit 5ce0883

File tree

16 files changed

+138
-32
lines changed

16 files changed

+138
-32
lines changed

assets/css/styles.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
.rotate-180 {
2+
--tw-translate-x: 0;
3+
--tw-translate-y: 0;
4+
--tw-rotate: 0;
5+
--tw-skew-x: 0;
6+
--tw-skew-y: 0;
7+
--tw-scale-x: 1;
8+
--tw-scale-y: 1;
9+
}
10+
111
.shadow, .shadow-lg {
212
--tw-ring-offset-shadow: 0 0 #0000;
313
--tw-ring-shadow: 0 0 #0000;
@@ -632,6 +642,15 @@ video {
632642
table-layout: auto;
633643
}
634644

645+
.rotate-180 {
646+
--tw-rotate: 180deg;
647+
transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y));
648+
}
649+
650+
.cursor-pointer {
651+
cursor: pointer;
652+
}
653+
635654
.select-none {
636655
-webkit-user-select: none;
637656
-moz-user-select: none;
@@ -985,6 +1004,10 @@ video {
9851004
color: #fff;
9861005
}
9871006

1007+
.opacity-30 {
1008+
opacity: 0.3;
1009+
}
1010+
9881011
.shadow {
9891012
--tw-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
9901013
--tw-shadow-colored: 0 1px 3px 0 var(--tw-shadow-color), 0 1px 2px -1px var(--tw-shadow-color);

assets/icons/down.svg

Lines changed: 3 additions & 0 deletions
Loading

assets/js/scripts.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ const ajax = (endpoint, callback, data = null) => {
1515
request.send(data);
1616
}
1717

18-
const replace_query_param = (param, value) => {
18+
const query_params = (...args) => {
1919
const url = new URL(location.href);
20-
const params = new URLSearchParams(url.search);
21-
22-
params.set(param, value);
20+
const search_params = new URLSearchParams(url.search);
21+
22+
if (args.length === 2 && typeof args[0] === 'string') {
23+
search_params.set(args[0], String(args[1]));
24+
} else if (args.length === 1 && typeof args[0] === 'object') {
25+
Object.entries(args[0]).forEach(([key, value]) => {
26+
if (value === null) {
27+
search_params.delete(key);
28+
} else {
29+
search_params.set(key, String(value));
30+
}
31+
});
32+
}
2333

24-
url.search = params.toString();
34+
url.search = search_params.toString();
2535
location.href = url.toString();
2636
}
2737

@@ -30,7 +40,7 @@ const select_and_redirect = (id, param) => {
3040

3141
if (select) {
3242
select.addEventListener('change', e => {
33-
replace_query_param(param, e.target.value);
43+
query_params(param, e.target.value);
3444
});
3545
}
3646
}
@@ -180,7 +190,7 @@ const search_form = document.getElementById('search_form');
180190
if (search_form) {
181191
const submit_search = document.getElementById('submit_search');
182192
submit_search.addEventListener('click', () => {
183-
replace_query_param('s', document.getElementById('search_key').value)
193+
query_params('s', document.getElementById('search_key').value)
184194
});
185195

186196
const search_key = document.getElementById('search_key');
@@ -198,6 +208,28 @@ if (search_form) {
198208
});
199209
}
200210

211+
/**
212+
* Table sorting
213+
*/
214+
document.querySelectorAll('[data-sortcol]').forEach(element => {
215+
element.addEventListener('click', () => {
216+
const sort_col = element.getAttribute('data-sortcol');
217+
const search_params = new URLSearchParams(window.location.search);
218+
const current_sort_dir = search_params.get('sortcol') === sort_col ? search_params.get('sortdir') || 'none' : 'none';
219+
220+
const sort_dir_cycle = ['none', 'asc', 'desc'];
221+
const current_index = sort_dir_cycle.indexOf(current_sort_dir);
222+
const new_sort_dir = sort_dir_cycle[(current_index + 1) % sort_dir_cycle.length];
223+
element.setAttribute('data-sortdir', new_sort_dir);
224+
225+
if (new_sort_dir === 'none') {
226+
query_params({sortdir: null, sortcol: null});
227+
} else {
228+
query_params({p: null, sortdir: new_sort_dir, sortcol: sort_col});
229+
}
230+
});
231+
});
232+
201233
/**
202234
* Light / Dark mode
203235
*/

src/Dashboards/APCu/APCuTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ private function getAllKeys(): array {
207207
}
208208
}
209209

210+
$keys = Helpers::sortKeys($this->template, $keys);
211+
210212
return $keys;
211213
}
212214

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ private function getAllKeys(): array {
227227
}
228228
}
229229

230+
$keys = Helpers::sortKeys($this->template, $keys);
231+
230232
return $keys;
231233
}
232234

src/Dashboards/OPCache/OPCacheTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ private function getCachedScripts(): array {
148148
}
149149
}
150150

151+
$cached_scripts = Helpers::sortKeys($this->template, $cached_scripts);
152+
151153
return $cached_scripts;
152154
}
153155

src/Dashboards/Realpath/RealpathTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace RobiNN\Pca\Dashboards\Realpath;
1010

1111
use RobiNN\Pca\Format;
12+
use RobiNN\Pca\Helpers;
1213
use RobiNN\Pca\Http;
1314

1415
trait RealpathTrait {
@@ -60,6 +61,8 @@ private function getAllKeys(): array {
6061
}
6162
}
6263

64+
$keys = Helpers::sortKeys($this->template, $keys);
65+
6366
return $keys;
6467
}
6568
}

src/Dashboards/Redis/RedisTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ private function getAllKeys(): array {
358358
];
359359
}
360360

361+
$keys = Helpers::sortKeys($this->template, $keys);
362+
361363
return $keys;
362364
}
363365

src/Helpers.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,31 @@ public static function serverSelector(Template $template, array $servers, int $s
209209
'class' => 'mb-4',
210210
]);
211211
}
212+
213+
/**
214+
* @param array<int, array<string, mixed>> $keys
215+
*
216+
* @return array<int, array<string, mixed>>
217+
*/
218+
public static function sortKeys(Template $template, array $keys): array {
219+
$dir = Http::get('sortdir', 'none');
220+
$column = Http::get('sortcol', 'none');
221+
222+
$template->addGlobal('sortdir', $dir);
223+
$template->addGlobal('sortcol', $column);
224+
225+
if (strtolower($dir) === 'none' || strtolower($column) === 'none') {
226+
return $keys;
227+
}
228+
229+
usort($keys, static function ($a, $b) use ($dir, $column) {
230+
$a_val = (string) $a['items'][$column];
231+
$b_val = (string) $b['items'][$column];
232+
$comparison = strnatcmp($a_val, $b_val);
233+
234+
return strtolower($dir) === 'desc' ? -$comparison : $comparison;
235+
});
236+
237+
return $keys;
238+
}
212239
}

src/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function stopRedirect(): void {
2222
* @param array<int|string, int|string> $additional Additional parameters with their new value.
2323
*/
2424
public static function queryString(array $preserve = [], array $additional = []): string {
25-
$keep = ['dashboard', 'server'];
25+
$keep = ['dashboard', 'server', 's', 'sortdir', 'sortcol'];
2626
$preserve = array_flip(array_merge($keep, $preserve));
2727
$query = [];
2828

0 commit comments

Comments
 (0)