From 982a1fa8dbd0d11f00c6eaec9a8f9dced5773e71 Mon Sep 17 00:00:00 2001 From: Gregoo Date: Fri, 24 Jan 2025 15:55:15 +0100 Subject: [PATCH] Simplify the way javascript is loaded (we don't have that many scripts running) and use data attribute to instantiate grid and tables --- static/scripts/changer.js | 18 ++++++++---------- static/scripts/grid.js | 19 ++++++++++++------- static/scripts/table.js | 24 +++++++++++++++++++++--- templates/add.html | 4 ---- templates/admin.html | 4 ---- templates/admin/checkbox.html | 5 ----- templates/base.html | 16 +++++++++++++++- templates/bulk.html | 4 ---- templates/delete.html | 9 --------- templates/instructions.html | 4 ---- templates/instructions/delete.html | 5 ----- templates/instructions/rename.html | 5 ----- templates/instructions/table.html | 13 +++---------- templates/macro/table.html | 12 ++++++------ templates/minifigure.html | 5 ----- templates/minifigure/table.html | 9 +-------- templates/minifigures.html | 4 ---- templates/missing.html | 4 ---- templates/part.html | 5 ----- templates/part/table.html | 9 +-------- templates/parts.html | 4 ---- templates/set.html | 11 ----------- templates/sets.html | 13 +------------ templates/wish/table.html | 13 +++---------- templates/wishes.html | 4 ---- 25 files changed, 71 insertions(+), 152 deletions(-) diff --git a/static/scripts/changer.js b/static/scripts/changer.js index 31177d5..224e24b 100644 --- a/static/scripts/changer.js +++ b/static/scripts/changer.js @@ -115,13 +115,11 @@ class BrickChanger { } // Helper to setup the changer -const setup_changers = () => { - document.querySelectorAll("*[data-changer-id]").forEach(el => { - new BrickChanger( - el.dataset.changerPrefix, - el.dataset.changerId, - el.dataset.changerUrl, - el.dataset.changerParent - ); - }); -} \ No newline at end of file +const setup_changers = () => document.querySelectorAll("*[data-changer-id]").forEach( + el => new BrickChanger( + el.dataset.changerPrefix, + el.dataset.changerId, + el.dataset.changerUrl, + el.dataset.changerParent + ) +); \ No newline at end of file diff --git a/static/scripts/grid.js b/static/scripts/grid.js index 6246817..42b8ac3 100644 --- a/static/scripts/grid.js +++ b/static/scripts/grid.js @@ -50,15 +50,15 @@ class BrickGridSortButton { // Grid class class BrickGrid { - constructor(id) { - this.id = id; + constructor(grid) { + this.id = grid.id; // Grid elements (built based on the initial id) - this.html_grid = document.getElementById(id); - this.html_sort = document.getElementById(`${id}-sort`); - this.html_search = document.getElementById(`${id}-search`); - this.html_filter = document.getElementById(`${id}-filter`); - this.html_theme = document.getElementById(`${id}-theme`); + this.html_grid = document.getElementById(this.id); + this.html_sort = document.getElementById(`${this.id}-sort`); + this.html_search = document.getElementById(`${this.id}-search`); + this.html_filter = document.getElementById(`${this.id}-filter`); + this.html_theme = document.getElementById(`${this.id}-theme`); // Sort buttons this.html_sort_buttons = {}; @@ -251,3 +251,8 @@ class BrickGrid { } } } + +// Helper to setup the grids +const setup_grids = () => document.querySelectorAll('*[data-grid="true"]').forEach( + el => new BrickGrid(el) +); diff --git a/static/scripts/table.js b/static/scripts/table.js index 68179af..669afc5 100644 --- a/static/scripts/table.js +++ b/static/scripts/table.js @@ -1,6 +1,19 @@ class BrickTable { - constructor(id, per_page, no_sort = [], number = []) { - const columns = []; + constructor(table, per_page) { + const columns = [] + const no_sort = []; + const number = []; + + // Read the table header for parameters + table.querySelectorAll('th').forEach((th, index) => { + if (th.dataset.tableNoSort) { + no_sort.push(index); + } + + if (th.dataset.tableNumber) { + number.push(index); + } + }); if (no_sort.length) { columns.push({ select: no_sort, sortable: false, searchable: false }); @@ -10,7 +23,7 @@ class BrickTable { columns.push({ select: number, type: "number", searchable: false }); } - this.table = new simpleDatatables.DataTable(`#${id}`, { + this.table = new simpleDatatables.DataTable(`#${table.id}`, { columns: columns, pagerDelta: 1, perPage: per_page, @@ -67,3 +80,8 @@ class BrickTable { return search; } } + +// Helper to setup the tables +const setup_tables = (per_page) => document.querySelectorAll('table[data-table="true"]').forEach( + el => new BrickTable(el, per_page) +); diff --git a/templates/add.html b/templates/add.html index 8fff618..140eec6 100644 --- a/templates/add.html +++ b/templates/add.html @@ -70,7 +70,3 @@ {% include 'set/socket.html' %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/admin.html b/templates/admin.html index 860d99f..dc87256 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -42,7 +42,3 @@ {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/admin/checkbox.html b/templates/admin/checkbox.html index 7cbd59d..b71cc2a 100644 --- a/templates/admin/checkbox.html +++ b/templates/admin/checkbox.html @@ -60,8 +60,3 @@ {{ accordion.footer() }} - diff --git a/templates/base.html b/templates/base.html index 23badab..1132f2b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -78,11 +78,25 @@ + + + + + + + + - {% block scripts %}{% endblock %} \ No newline at end of file diff --git a/templates/bulk.html b/templates/bulk.html index 1974e1b..6e6e5d8 100644 --- a/templates/bulk.html +++ b/templates/bulk.html @@ -62,7 +62,3 @@ {% include 'set/socket.html' %} {% endwith %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/delete.html b/templates/delete.html index d61821a..07c2b08 100644 --- a/templates/delete.html +++ b/templates/delete.html @@ -12,13 +12,4 @@ - {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/instructions.html b/templates/instructions.html index 7959796..70cd1eb 100644 --- a/templates/instructions.html +++ b/templates/instructions.html @@ -23,7 +23,3 @@ {% endif %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/instructions/delete.html b/templates/instructions/delete.html index 9443dd0..247e703 100644 --- a/templates/instructions/delete.html +++ b/templates/instructions/delete.html @@ -28,8 +28,3 @@ - diff --git a/templates/instructions/rename.html b/templates/instructions/rename.html index 0be3dbe..a3269c1 100644 --- a/templates/instructions/rename.html +++ b/templates/instructions/rename.html @@ -34,8 +34,3 @@ - diff --git a/templates/instructions/table.html b/templates/instructions/table.html index 8f507aa..8ad9f3c 100644 --- a/templates/instructions/table.html +++ b/templates/instructions/table.html @@ -1,14 +1,14 @@ {% import 'macro/table.html' as table %}
- +
- + {% if g.login.is_authenticated() %} - + {% endif %} @@ -46,10 +46,3 @@
Filename Set Image Image Actions Actions
-{% if all %} - -{% endif %} diff --git a/templates/macro/table.html b/templates/macro/table.html index d4246f2..91bb1c9 100644 --- a/templates/macro/table.html +++ b/templates/macro/table.html @@ -1,25 +1,25 @@ {% macro header(color=false, quantity=false, missing=false, missing_parts=false, sets=false, minifigures=false) %} - Image + Image Name {% if color %} Color {% endif %} {% if quantity %} - Quantity + Quantity {% endif %} {% if missing %} - Missing + Missing {% endif %} {% if missing_parts %} - Missing parts + Missing parts {% endif %} {% if sets %} - Sets + Sets {% endif %} {% if minifigures %} - Minifigures + Minifigures {% endif %} diff --git a/templates/minifigure.html b/templates/minifigure.html index 3359897..52ed7a1 100644 --- a/templates/minifigure.html +++ b/templates/minifigure.html @@ -12,9 +12,4 @@ - {% endblock %} diff --git a/templates/minifigure/table.html b/templates/minifigure/table.html index 6315fec..94ccef7 100644 --- a/templates/minifigure/table.html +++ b/templates/minifigure/table.html @@ -1,7 +1,7 @@ {% import 'macro/table.html' as table %}
- +
{{ table.header(quantity=true, missing_parts=true, sets=true) }} {% for item in table_collection %} @@ -21,10 +21,3 @@
-{% if all %} - -{% endif %} diff --git a/templates/minifigures.html b/templates/minifigures.html index b28b600..ce42f18 100644 --- a/templates/minifigures.html +++ b/templates/minifigures.html @@ -9,7 +9,3 @@ {% endwith %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/missing.html b/templates/missing.html index de36f35..d7c82a5 100644 --- a/templates/missing.html +++ b/templates/missing.html @@ -9,7 +9,3 @@ {% endwith %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/part.html b/templates/part.html index 969b676..40899d9 100644 --- a/templates/part.html +++ b/templates/part.html @@ -12,9 +12,4 @@ - {% endblock %} diff --git a/templates/part/table.html b/templates/part/table.html index 3603392..1fca264 100644 --- a/templates/part/table.html +++ b/templates/part/table.html @@ -1,7 +1,7 @@ {% import 'macro/table.html' as table %}
- +
{{ table.header(color=true, quantity=not no_quantity, missing=not no_missing, sets=all, minifigures=all) }} {% for item in table_collection %} @@ -51,10 +51,3 @@
-{% if all %} - -{% endif %} \ No newline at end of file diff --git a/templates/parts.html b/templates/parts.html index d2cb4b3..1c3e417 100644 --- a/templates/parts.html +++ b/templates/parts.html @@ -9,7 +9,3 @@ {% endwith %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file diff --git a/templates/set.html b/templates/set.html index d5eeef9..69ba6dd 100644 --- a/templates/set.html +++ b/templates/set.html @@ -12,15 +12,4 @@ - {% endblock %} - -{% block scripts %} - - -{% endblock %} \ No newline at end of file diff --git a/templates/sets.html b/templates/sets.html index 2517c96..ea08c2d 100644 --- a/templates/sets.html +++ b/templates/sets.html @@ -58,7 +58,7 @@ -
+
{% for item in collection %}
{% with index=loop.index0 %} @@ -67,19 +67,8 @@
{% endfor %}
-
{% else %} {% include 'set/empty.html' %} {% endif %} {% endblock %} - -{% block scripts %} - - -{% endblock %} \ No newline at end of file diff --git a/templates/wish/table.html b/templates/wish/table.html index 83536e3..73a5eb8 100644 --- a/templates/wish/table.html +++ b/templates/wish/table.html @@ -2,10 +2,10 @@ {% import 'macro/badge.html' as badge %}
- +
- + @@ -13,7 +13,7 @@ {% if g.login.is_authenticated() %} - + {% endif %} @@ -40,10 +40,3 @@
Image Image Set Name Theme Parts Retirement Actions Actions
-{% if all %} - -{% endif %} diff --git a/templates/wishes.html b/templates/wishes.html index cc9a242..eac8255 100644 --- a/templates/wishes.html +++ b/templates/wishes.html @@ -29,7 +29,3 @@ {% endwith %} {% endblock %} - -{% block scripts %} - -{% endblock %} \ No newline at end of file