2025-01-17 11:03:00 +01:00
|
|
|
// Grid class
|
|
|
|
class BrickGrid {
|
2025-01-30 22:07:09 +01:00
|
|
|
constructor(grid, target = "div#grid>div") {
|
2025-01-24 15:55:15 +01:00
|
|
|
this.id = grid.id;
|
2025-01-30 22:07:09 +01:00
|
|
|
this.target = target;
|
2025-01-17 11:03:00 +01:00
|
|
|
|
|
|
|
// Grid elements (built based on the initial id)
|
2025-01-24 15:55:15 +01:00
|
|
|
this.html_grid = document.getElementById(this.id);
|
|
|
|
this.html_search = document.getElementById(`${this.id}-search`);
|
2025-01-30 21:57:42 +01:00
|
|
|
this.html_status = document.getElementById(`${this.id}-status`);
|
2025-01-24 15:55:15 +01:00
|
|
|
this.html_theme = document.getElementById(`${this.id}-theme`);
|
2025-01-17 11:03:00 +01:00
|
|
|
|
2025-01-30 22:30:14 +01:00
|
|
|
// Sort setup
|
|
|
|
this.sort = new BrickGridSort(this);
|
2025-01-17 11:03:00 +01:00
|
|
|
|
|
|
|
// Filter setup
|
|
|
|
if (this.html_search) {
|
|
|
|
this.html_search.addEventListener("keyup", ((grid) => () => {
|
|
|
|
grid.filter();
|
|
|
|
})(this));
|
|
|
|
}
|
|
|
|
|
2025-01-30 21:57:42 +01:00
|
|
|
if (this.html_status) {
|
|
|
|
this.html_status.addEventListener("change", ((grid) => () => {
|
2025-01-17 11:03:00 +01:00
|
|
|
grid.filter();
|
|
|
|
})(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.html_theme) {
|
|
|
|
this.html_theme.addEventListener("change", ((grid) => () => {
|
|
|
|
grid.filter();
|
|
|
|
})(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter
|
|
|
|
filter() {
|
|
|
|
var filters = {};
|
|
|
|
|
|
|
|
// Check if there is a search filter
|
|
|
|
if (this.html_search && this.html_search.value != "") {
|
|
|
|
filters["search"] = this.html_search.value.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there is a set filter
|
2025-01-30 21:57:42 +01:00
|
|
|
if (this.html_status && this.html_status.value != "") {
|
|
|
|
if (this.html_status.value.startsWith("-")) {
|
|
|
|
filters["filter"] = this.html_status.value.substring(1);
|
2025-01-17 11:03:00 +01:00
|
|
|
filters["filter-target"] = "0";
|
|
|
|
} else {
|
2025-01-30 21:57:42 +01:00
|
|
|
filters["filter"] = this.html_status.value;
|
2025-01-17 11:03:00 +01:00
|
|
|
filters["filter-target"] = "1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there is a theme filter
|
|
|
|
if (this.html_theme && this.html_theme.value != "") {
|
|
|
|
filters["theme"] = this.html_theme.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter all cards
|
|
|
|
if (this.html_grid) {
|
|
|
|
const cards = this.html_grid.querySelectorAll("div > div.card");
|
|
|
|
cards.forEach(current => {
|
|
|
|
// Set filter
|
|
|
|
if ("filter" in filters) {
|
|
|
|
if (current.getAttribute("data-" + filters["filter"]) != filters["filter-target"]) {
|
|
|
|
current.parentElement.classList.add("d-none");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Theme filter
|
|
|
|
if ("theme" in filters) {
|
|
|
|
if (current.getAttribute("data-theme") != filters["theme"]) {
|
|
|
|
current.parentElement.classList.add("d-none");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check all searchable fields for a match
|
|
|
|
if ("search" in filters) {
|
|
|
|
for (let attribute of ["data-name", "data-number", "data-parts", "data-theme", "data-year"]) {
|
|
|
|
if (current.getAttribute(attribute).includes(filters["search"])) {
|
|
|
|
current.parentElement.classList.remove("d-none");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no match, we need to hide it
|
|
|
|
current.parentElement.classList.add("d-none");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we passed all filters, we need to display it
|
|
|
|
current.parentElement.classList.remove("d-none");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2025-01-24 15:55:15 +01:00
|
|
|
|
|
|
|
// Helper to setup the grids
|
|
|
|
const setup_grids = () => document.querySelectorAll('*[data-grid="true"]').forEach(
|
|
|
|
el => new BrickGrid(el)
|
|
|
|
);
|