From cea1c2b7175321c09053e803b664dc47dc983d0b Mon Sep 17 00:00:00 2001 From: RubenPX Date: Thu, 12 May 2022 18:44:02 +0200 Subject: [PATCH 1/5] full table replacament --- .../static/js/main_inventory.build.js | 94 +++++++++++++------ ereuse_devicehub/static/js/main_inventory.js | 20 ++-- 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/ereuse_devicehub/static/js/main_inventory.build.js b/ereuse_devicehub/static/js/main_inventory.build.js index 09006905..a58a8260 100644 --- a/ereuse_devicehub/static/js/main_inventory.build.js +++ b/ereuse_devicehub/static/js/main_inventory.build.js @@ -2,7 +2,7 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { _classCheckPrivateStaticAccess(receiver, classConstructor); _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); return _classApplyDescriptorGet(receiver, descriptor); } -function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { if (descriptor === undefined) { throw new TypeError(`attempted to ${ action } private static field before its declaration`); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { if (descriptor === undefined) { throw new TypeError("attempted to " + action + " private static field before its declaration"); } } function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } } @@ -100,22 +100,32 @@ window.addEventListener("DOMContentLoaded", () => { const alertInfoDevices = document.getElementById("select-devices-info"); function itemListCheckChanged() { - const listDevices = TableController.getAllDevicesInCurrentPage(); - const isAllChecked = listDevices.map(itm => itm.checked); + alertInfoDevices.innerHTML = "Selected devices: ".concat(TableController.getSelectedDevices().length, "\n ").concat(TableController.getAllDevices().length != TableController.getSelectedDevices().length ? "Select all devices (".concat(TableController.getAllDevices().length, ")") : "Cancel selection"); - if (isAllChecked.every(bool => bool == true)) { - btnSelectAll.checked = true; - btnSelectAll.indeterminate = false; - alertInfoDevices.innerHTML = "Selected devices: ".concat(TableController.getSelectedDevices().length, "\n ").concat(TableController.getAllDevices().length != TableController.getSelectedDevices().length ? "Select all devices (".concat(TableController.getAllDevices().length, ")") : "Cancel selection"); - alertInfoDevices.classList.remove("d-none"); - } else if (isAllChecked.every(bool => bool == false)) { - btnSelectAll.checked = false; - btnSelectAll.indeterminate = false; + if (TableController.getSelectedDevices().length <= 0) { alertInfoDevices.classList.add("d-none"); } else { - btnSelectAll.indeterminate = true; - alertInfoDevices.classList.add("d-none"); + alertInfoDevices.classList.remove("d-none"); } + + if (TableController.getAllDevices().length == TableController.getSelectedDevices().length) { + btnSelectAll.checked = true; + btnSelectAll.indeterminate = false; + } else if (TableController.getAllSelectedDevicesInCurrentPage().length > 0) { + btnSelectAll.indeterminate = true; + } else { + btnSelectAll.checked = false; + btnSelectAll.indeterminate = false; + } + + if (TableController.getAllDevices().length == 0) { + btnSelectAll.checked = false; + btnSelectAll.disabled = true; + } else { + btnSelectAll.disabled = false; + } + + get_device_list(); } TableController.getAllDevices().forEach(item => { @@ -139,6 +149,7 @@ window.addEventListener("DOMContentLoaded", () => { table.on("datatable.page", () => itemListCheckChanged()); table.on("datatable.perpage", () => itemListCheckChanged()); table.on("datatable.update", () => itemListCheckChanged()); + itemListCheckChanged(); }); function deviceSelect() { @@ -327,25 +338,44 @@ async function processSelectedDevices() { const lotID = lot.id; const srcElement = event.srcElement.parentElement.children[0]; const checked = !srcElement.checked; + const { + indeterminate + } = srcElement; const found = this.list.filter(list => list.lot.id == lotID)[0]; if (checked) { if (found && found.type == "Remove") { - found.type = "Add"; + const affectedDevices = found.devices.filter(dev => found.lot.devices.includes(dev.id)); + + if (affectedDevices.length > 0 && found.indeterminate == false) { + // Remove action from list + actions.list = actions.list.filter(x => x.lot.id != found.lot.id); + } else { + found.type = "Add"; + } } else { this.list.push({ type: "Add", lot, - devices: selectedDevices + devices: selectedDevices, + indeterminate }); } } else if (found && found.type == "Add") { - found.type = "Remove"; + const affectedDevices = found.devices.filter(dev => !found.lot.devices.includes(dev.id)); + + if (affectedDevices.length > 0 && found.indeterminate == false) { + // Remove action from list + actions.list = actions.list.filter(x => x.lot.id != found.lot.id); + } else { + found.type = "Remove"; + } } else { this.list.push({ type: "Remove", lot, - devices: selectedDevices + devices: selectedDevices, + indeterminate }); } @@ -430,19 +460,23 @@ async function processSelectedDevices() { const newRequest = await Api.doRequest(window.location); const tmpDiv = document.createElement("div"); tmpDiv.innerHTML = newRequest; - const newTable = Array.from(tmpDiv.querySelectorAll("table.table > tbody > tr .deviceSelect")).map(x => x.attributes["data-device-dhid"].value); // https://github.com/fiduswriter/Simple-DataTables/wiki/rows()#removeselect-arraynumber - - const rowsToRemove = []; - - for (let i = 0; i < table.activeRows.length; i++) { - const row = table.activeRows[i]; - - if (!newTable.includes(row.querySelector("input").attributes["data-device-dhid"].value)) { - rowsToRemove.push(i); - } - } - - table.rows().remove(rowsToRemove); // Restore state of checkbox + const newTable = document.createElement("table"); + newTable.innerHTML = tmpDiv.querySelector("table").innerHTML; + newTable.classList = "table"; + const oldTable = document.querySelector(".dataTable-wrapper"); + oldTable.parentElement.replaceChild(newTable, oldTable); + table = new simpleDatatables.DataTable(newTable, { + perPage: 20 + }); // https://github.com/fiduswriter/Simple-DataTables/wiki/rows()#removeselect-arraynumber + // const rowsToRemove = [] + // for (let i = 0; i < table.activeRows.length; i++) { + // const row = table.activeRows[i]; + // if (!newTable.includes(row.querySelector("input").attributes["data-device-dhid"].value)) { + // rowsToRemove.push(i) + // } + // } + // table.rows().remove(rowsToRemove); + // // Restore state of checkbox const selectAllBTN = document.getElementById("SelectAllBTN"); selectAllBTN.checked = false; diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index bec04232..00b90cd4 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -425,19 +425,17 @@ async function processSelectedDevices() { const tmpDiv = document.createElement("div") tmpDiv.innerHTML = newRequest - const newTable = Array.from(tmpDiv.querySelectorAll("table.table > tbody > tr .deviceSelect")).map(x => x.attributes["data-device-dhid"].value) + + const newTable = document.createElement("table") + newTable.innerHTML = tmpDiv.querySelector("table").innerHTML + newTable.classList = "table" - // https://github.com/fiduswriter/Simple-DataTables/wiki/rows()#removeselect-arraynumber - const rowsToRemove = [] - for (let i = 0; i < table.activeRows.length; i++) { - const row = table.activeRows[i]; - if (!newTable.includes(row.querySelector("input").attributes["data-device-dhid"].value)) { - rowsToRemove.push(i) - } - } - table.rows().remove(rowsToRemove); + const oldTable = document.querySelector(".dataTable-wrapper") + oldTable.parentElement.replaceChild(newTable, oldTable) - // Restore state of checkbox + table = new simpleDatatables.DataTable(newTable, {perPage: 20}) + + // // Restore state of checkbox const selectAllBTN = document.getElementById("SelectAllBTN"); selectAllBTN.checked = false; selectAllBTN.indeterminate = false; From 6a408a08c782ea41c4663110866f1753a764dc3d Mon Sep 17 00:00:00 2001 From: RubenPX Date: Thu, 12 May 2022 18:44:26 +0200 Subject: [PATCH 2/5] set const table to var This allow modify variable --- ereuse_devicehub/templates/inventory/device_list.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 0f022fd6..84294c48 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -357,6 +357,14 @@ {{ dev.verbose_name }} + {% if dev.lots | length > 0 %} +
+
+ {% for lot in dev.lots %} + {{ lot.name }} + {% endfor %} +
+ {% endif %} @@ -430,7 +438,7 @@ From c7e050182885543033ab61129ef73b8f59669519 Mon Sep 17 00:00:00 2001 From: RubenPX Date: Thu, 12 May 2022 18:58:06 +0200 Subject: [PATCH 3/5] add soft init to reassign events --- .../static/js/main_inventory.build.js | 48 +++++++++++-------- ereuse_devicehub/static/js/main_inventory.js | 38 ++++++++++----- 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/ereuse_devicehub/static/js/main_inventory.build.js b/ereuse_devicehub/static/js/main_inventory.build.js index a58a8260..48ab00a7 100644 --- a/ereuse_devicehub/static/js/main_inventory.build.js +++ b/ereuse_devicehub/static/js/main_inventory.build.js @@ -95,10 +95,27 @@ var _tableRowsPage = { writable: true, value: () => table.pages[table.rows().dt.currentPage - 1] }; -window.addEventListener("DOMContentLoaded", () => { + +const selectorController = action => { const btnSelectAll = document.getElementById("SelectAllBTN"); const alertInfoDevices = document.getElementById("select-devices-info"); + function softInit() { + TableController.getAllDevices().forEach(item => { + item.addEventListener("click", itemListCheckChanged); + }); // https://github.com/fiduswriter/Simple-DataTables/wiki/Events + + table.on("datatable.page", () => itemListCheckChanged()); + table.on("datatable.perpage", () => itemListCheckChanged()); + table.on("datatable.update", () => itemListCheckChanged()); + } + + if (action == "softInit") { + softInit(); + itemListCheckChanged(); + return; + } + function itemListCheckChanged() { alertInfoDevices.innerHTML = "Selected devices: ".concat(TableController.getSelectedDevices().length, "\n ").concat(TableController.getAllDevices().length != TableController.getSelectedDevices().length ? "Select all devices (".concat(TableController.getAllDevices().length, ")") : "Cancel selection"); @@ -128,9 +145,6 @@ window.addEventListener("DOMContentLoaded", () => { get_device_list(); } - TableController.getAllDevices().forEach(item => { - item.addEventListener("click", itemListCheckChanged); - }); btnSelectAll.addEventListener("click", event => { const checkedState = event.target.checked; TableController.getAllDevicesInCurrentPage().forEach(ckeckbox => { @@ -144,13 +158,12 @@ window.addEventListener("DOMContentLoaded", () => { ckeckbox.checked = !checkState; }); itemListCheckChanged(); - }); // https://github.com/fiduswriter/Simple-DataTables/wiki/Events - - table.on("datatable.page", () => itemListCheckChanged()); - table.on("datatable.perpage", () => itemListCheckChanged()); - table.on("datatable.update", () => itemListCheckChanged()); + }); + softInit(); itemListCheckChanged(); -}); +}; + +window.addEventListener("DOMContentLoaded", () => selectorController()); function deviceSelect() { const devices_count = TableController.getSelectedDevices().length; @@ -467,20 +480,13 @@ async function processSelectedDevices() { oldTable.parentElement.replaceChild(newTable, oldTable); table = new simpleDatatables.DataTable(newTable, { perPage: 20 - }); // https://github.com/fiduswriter/Simple-DataTables/wiki/rows()#removeselect-arraynumber - // const rowsToRemove = [] - // for (let i = 0; i < table.activeRows.length; i++) { - // const row = table.activeRows[i]; - // if (!newTable.includes(row.querySelector("input").attributes["data-device-dhid"].value)) { - // rowsToRemove.push(i) - // } - // } - // table.rows().remove(rowsToRemove); - // // Restore state of checkbox + }); // // Restore state of checkbox const selectAllBTN = document.getElementById("SelectAllBTN"); selectAllBTN.checked = false; - selectAllBTN.indeterminate = false; + selectAllBTN.indeterminate = false; // Re-init SelectorController + + selectorController("softInit"); } } diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 00b90cd4..7a2ecc93 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -78,10 +78,28 @@ class TableController { /** * Select all functionality */ -window.addEventListener("DOMContentLoaded", () => { + +const selectorController = (action) => { const btnSelectAll = document.getElementById("SelectAllBTN"); const alertInfoDevices = document.getElementById("select-devices-info"); + function softInit() { + TableController.getAllDevices().forEach(item => { + item.addEventListener("click", itemListCheckChanged); + }) + + // https://github.com/fiduswriter/Simple-DataTables/wiki/Events + table.on("datatable.page", () => itemListCheckChanged()); + table.on("datatable.perpage", () => itemListCheckChanged()); + table.on("datatable.update", () => itemListCheckChanged()); + } + + if (action == "softInit") { + softInit(); + itemListCheckChanged(); + return; + } + function itemListCheckChanged() { alertInfoDevices.innerHTML = `Selected devices: ${TableController.getSelectedDevices().length} ${TableController.getAllDevices().length != TableController.getSelectedDevices().length @@ -114,11 +132,7 @@ window.addEventListener("DOMContentLoaded", () => { get_device_list(); } - - TableController.getAllDevices().forEach(item => { - item.addEventListener("click", itemListCheckChanged); - }) - + btnSelectAll.addEventListener("click", event => { const checkedState = event.target.checked; TableController.getAllDevicesInCurrentPage().forEach(ckeckbox => { ckeckbox.checked = checkedState }); @@ -131,13 +145,12 @@ window.addEventListener("DOMContentLoaded", () => { itemListCheckChanged() }) - // https://github.com/fiduswriter/Simple-DataTables/wiki/Events - table.on("datatable.page", () => itemListCheckChanged()); - table.on("datatable.perpage", () => itemListCheckChanged()); - table.on("datatable.update", () => itemListCheckChanged()); + softInit(); itemListCheckChanged(); -}) +} + +window.addEventListener("DOMContentLoaded", () => selectorController()); function deviceSelect() { const devices_count = TableController.getSelectedDevices().length; @@ -439,6 +452,9 @@ async function processSelectedDevices() { const selectAllBTN = document.getElementById("SelectAllBTN"); selectAllBTN.checked = false; selectAllBTN.indeterminate = false; + + // Re-init SelectorController + selectorController("softInit"); } } From 300edac5a8205694faca7f7e701bab34e2e87bda Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 13 May 2022 10:35:11 +0200 Subject: [PATCH 4/5] Dropped .badge-pill use the .rounded-pill utility instead. Breaking change on Boostrap 5 https://getbootstrap.com/docs/5.0/migration/#badges --- ereuse_devicehub/templates/inventory/device_list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 84294c48..5729ccbc 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -361,7 +361,7 @@
{% for lot in dev.lots %} - {{ lot.name }} + {{ lot.name }} {% endfor %}
{% endif %} From 56ccb7591acdef552017eae61c992557cbd72a5c Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 13 May 2022 10:39:37 +0200 Subject: [PATCH 5/5] Cosmetics: display lots inline & use ligh bg Lower visual weight because it's secundary information --- ereuse_devicehub/templates/inventory/device_list.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 5729ccbc..3d5367fc 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -358,10 +358,9 @@ {{ dev.verbose_name }} {% if dev.lots | length > 0 %} -
-
+
{% for lot in dev.lots %} - {{ lot.name }} + {{ lot.name }} {% endfor %}
{% endif %}