diff --git a/web/assets/js/main.js b/web/assets/js/main.js index f37fd88..a277791 100644 --- a/web/assets/js/main.js +++ b/web/assets/js/main.js @@ -220,11 +220,45 @@ document.getElementsByName("filter")[0].addEventListener("input", e => filter(e. //// SORT BY COLUMN -const units = {"B": 0, "KiB": 1, "MiB": 2, "GiB": 3, "TiB": 4}; const [thead_name, thead_date, thead_size] = document.getElementsByTagName('thead')[0] .children[0].children; -let g_sort_reverse = false; +if (localStorage.getItem("sort_reverse") == null) + localStorage.setItem("sort_reverse", false); + +function sortTable(compareFn, filterFn, filterNegFn, target, other) { + let records = Array.from(g_tbody.children); + + let dirs = []; + if (filterFn != null) { + dirs = records.filter(filterFn); + records = records.filter(filterNegFn); + } + + records.sort(compareFn); + + g_tbody.textContent = ""; + + other.forEach(v => { + v.classList.remove("sort-up"); + v.classList.remove("sort-down"); + }); + + if (filterFn != null) + g_tbody.append(...dirs); + + if (localStorage.getItem("sort_reverse") == "true") { + g_tbody.append(...records.reverse()); + target.classList.add("sort-up"); + target.classList.remove("sort-down"); + } else { + g_tbody.append(...records); + target.classList.add("sort-down"); + target.classList.remove("sort-up"); + } + + localStorage.setItem("sort_reverse", !(localStorage.getItem("sort_reverse") == "true")); +} thead_name.classList.toggle("clickable"); thead_name.addEventListener('click', e => { @@ -236,6 +270,7 @@ thead_name.addEventListener('click', e => { }, null, null, thead_name, [thead_date, thead_size]); g_first_row = g_tbody.firstChild; g_last_row = g_tbody.lastChild; + localStorage["sort_column"] = "name"; }); thead_date.classList.toggle("clickable"); @@ -248,6 +283,7 @@ thead_date.addEventListener('click', e => { }, null, null, thead_date, [thead_name, thead_size]); g_first_row = g_tbody.firstChild; g_last_row = g_tbody.lastChild; + localStorage["sort_column"] = "date"; }); @@ -276,38 +312,12 @@ thead_size.addEventListener('click', e => { thead_size, [thead_name, thead_date]); g_first_row = g_tbody.firstChild; g_last_row = g_tbody.lastChild; + localStorage["sort_column"] = "size"; }); -function sortTable(compareFn, filterFn, filterNegFn, target, other) { - let records = Array.from(g_tbody.children); - - let dirs = []; - if (filterFn != null) { - dirs = records.filter(filterFn); - records = records.filter(filterNegFn); - } - - records.sort(compareFn); - - g_tbody.textContent = ""; - - other.forEach(v => { - v.classList.remove("sort-up"); - v.classList.remove("sort-down"); - }); - - if (filterFn != null) - g_tbody.append(...dirs); - - if (g_sort_reverse) { - g_tbody.append(...records.reverse()); - target.classList.add("sort-up"); - target.classList.remove("sort-down"); - } else { - g_tbody.append(...records); - target.classList.add("sort-down"); - target.classList.remove("sort-up"); - } - - g_sort_reverse = !g_sort_reverse; +localStorage.setItem("sort_reverse", !(localStorage.getItem("sort_reverse") == "true")); +switch (localStorage["sort_column"]) { + case "name": thead_name.click(); break; + case "date": thead_date.click(); break; + case "size": thead_size.click(); break; } \ No newline at end of file