BrickTracker/static/scripts/grid/sort.js

119 lines
3.8 KiB
JavaScript
Raw Normal View History

// Grid sort
class BrickGridSort {
constructor(grid) {
2025-01-30 22:10:47 +01:00
this.grid = grid;
// Grid sort elements (built based on the initial id)
this.html_sort = document.getElementById(`${this.grid.id}-sort`);
if (this.html_sort) {
// Cookie names
this.cookie_id = `${this.grid.id}-sort-id`;
this.cookie_order = `${this.grid.id}-sort-order`;
// Sort buttons
this.html_sort_buttons = {};
this.html_sort.querySelectorAll("button[data-sort-attribute]").forEach(button => {
this.html_sort_buttons[button.id] = new BrickGridSortButton(button, this);
});
// Clear button
this.html_clear = this.html_sort.querySelector("button[data-sort-clear]")
if (this.html_clear) {
this.html_clear.addEventListener("click", ((grid) => () => {
grid.clear();
})(this))
}
// Cookie setup
const cookies = document.cookie.split(";").reduce((acc, cookieString) => {
const [key, value] = cookieString.split("=").map(s => s.trim().replace(/^"|"$/g, ""));
if (key && value) {
acc[key] = decodeURIComponent(value);
}
return acc;
}, {});
// Initial sort
if (this.cookie_id in cookies && cookies[this.cookie_id] in this.html_sort_buttons) {
const current = this.html_sort_buttons[cookies[this.cookie_id]];
if(this.cookie_order in cookies) {
current.button.setAttribute("data-sort-order", cookies[this.cookie_order]);
}
2025-01-30 22:10:47 +01:00
this.sort(current, true);
}
}
2025-01-30 22:10:47 +01:00
}
// Clear sort
clear() {
// Cleanup all
for (const [id, button] of Object.entries(this.html_sort_buttons)) {
button.toggle();
button.inactive();
}
// Clear cookies
document.cookie = `${this.cookie_id}=""; Path=/; SameSite=strict`;
document.cookie = `${this.cookie_order}=""; Path=/; SameSite=strict`;
// Reset sorting
tinysort(this.grid.target, {
selector: "div",
attr: "data-index",
order: "asc",
});
2025-01-30 22:10:47 +01:00
}
// Sort
sort(current, no_flip=false) {
const attribute = current.data.sortAttribute;
const natural = current.data.sortNatural;
2025-01-30 22:10:47 +01:00
// Cleanup all
for (const [id, button] of Object.entries(this.html_sort_buttons)) {
if (button != current) {
button.toggle();
button.inactive();
}
2025-01-30 22:10:47 +01:00
}
// Sort
if (attribute) {
let order = current.data.sortOrder;
// First ordering
if (!no_flip) {
if(!order) {
if (current.data.sortDesc) {
order = "desc";
} else {
order = "asc";
}
} else {
// Flip the sorting order
order = (order == "desc") ? "asc" : "desc";
}
}
2025-01-30 22:10:47 +01:00
// Toggle the ordering
current.toggle(order);
2025-01-30 22:10:47 +01:00
// Store cookies
document.cookie = `${this.cookie_id}="${encodeURIComponent(current.button.id)}"; Path=/; SameSite=strict`;
document.cookie = `${this.cookie_order}="${encodeURIComponent(order)}"; Path=/; SameSite=strict`;
2025-01-30 22:10:47 +01:00
// Do the sorting
tinysort(this.grid.target, {
selector: "div",
attr: "data-" + attribute,
natural: natural == "true",
order: order,
});
2025-01-30 22:10:47 +01:00
}
}
}