Merge pull request #272 from RubenPX/Show-device-labels

Show lots on deviceList
This commit is contained in:
Santiago L 2022-05-13 10:41:55 +02:00 committed by GitHub
commit af833a673a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 67 deletions

View File

@ -2,7 +2,7 @@
function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { _classCheckPrivateStaticAccess(receiver, classConstructor); _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); return _classApplyDescriptorGet(receiver, descriptor); } 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"); } } function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } }
@ -95,32 +95,56 @@ var _tableRowsPage = {
writable: true, writable: true,
value: () => table.pages[table.rows().dt.currentPage - 1] value: () => table.pages[table.rows().dt.currentPage - 1]
}; };
window.addEventListener("DOMContentLoaded", () => {
const selectorController = action => {
const btnSelectAll = document.getElementById("SelectAllBTN"); const btnSelectAll = document.getElementById("SelectAllBTN");
const alertInfoDevices = document.getElementById("select-devices-info"); const alertInfoDevices = document.getElementById("select-devices-info");
function itemListCheckChanged() { function softInit() {
const listDevices = TableController.getAllDevicesInCurrentPage(); TableController.getAllDevices().forEach(item => {
const isAllChecked = listDevices.map(itm => itm.checked); item.addEventListener("click", itemListCheckChanged);
}); // https://github.com/fiduswriter/Simple-DataTables/wiki/Events
if (isAllChecked.every(bool => bool == true)) { table.on("datatable.page", () => itemListCheckChanged());
btnSelectAll.checked = true; table.on("datatable.perpage", () => itemListCheckChanged());
btnSelectAll.indeterminate = false; table.on("datatable.update", () => itemListCheckChanged());
alertInfoDevices.innerHTML = "Selected devices: ".concat(TableController.getSelectedDevices().length, "\n ").concat(TableController.getAllDevices().length != TableController.getSelectedDevices().length ? "<a href=\"#\" class=\"ml-3\">Select all devices (".concat(TableController.getAllDevices().length, ")</a>") : "<a href=\"#\" class=\"ml-3\">Cancel selection</a>"); }
alertInfoDevices.classList.remove("d-none");
} else if (isAllChecked.every(bool => bool == false)) { if (action == "softInit") {
btnSelectAll.checked = false; softInit();
btnSelectAll.indeterminate = false; itemListCheckChanged();
alertInfoDevices.classList.add("d-none"); return;
} else { }
btnSelectAll.indeterminate = true;
alertInfoDevices.classList.add("d-none"); function itemListCheckChanged() {
} alertInfoDevices.innerHTML = "Selected devices: ".concat(TableController.getSelectedDevices().length, "\n ").concat(TableController.getAllDevices().length != TableController.getSelectedDevices().length ? "<a href=\"#\" class=\"ml-3\">Select all devices (".concat(TableController.getAllDevices().length, ")</a>") : "<a href=\"#\" class=\"ml-3\">Cancel selection</a>");
if (TableController.getSelectedDevices().length <= 0) {
alertInfoDevices.classList.add("d-none");
} else {
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 => {
item.addEventListener("click", itemListCheckChanged);
});
btnSelectAll.addEventListener("click", event => { btnSelectAll.addEventListener("click", event => {
const checkedState = event.target.checked; const checkedState = event.target.checked;
TableController.getAllDevicesInCurrentPage().forEach(ckeckbox => { TableController.getAllDevicesInCurrentPage().forEach(ckeckbox => {
@ -134,12 +158,12 @@ window.addEventListener("DOMContentLoaded", () => {
ckeckbox.checked = !checkState; ckeckbox.checked = !checkState;
}); });
itemListCheckChanged(); itemListCheckChanged();
}); // https://github.com/fiduswriter/Simple-DataTables/wiki/Events });
softInit();
itemListCheckChanged();
};
table.on("datatable.page", () => itemListCheckChanged()); window.addEventListener("DOMContentLoaded", () => selectorController());
table.on("datatable.perpage", () => itemListCheckChanged());
table.on("datatable.update", () => itemListCheckChanged());
});
function deviceSelect() { function deviceSelect() {
const devices_count = TableController.getSelectedDevices().length; const devices_count = TableController.getSelectedDevices().length;
@ -327,25 +351,44 @@ async function processSelectedDevices() {
const lotID = lot.id; const lotID = lot.id;
const srcElement = event.srcElement.parentElement.children[0]; const srcElement = event.srcElement.parentElement.children[0];
const checked = !srcElement.checked; const checked = !srcElement.checked;
const {
indeterminate
} = srcElement;
const found = this.list.filter(list => list.lot.id == lotID)[0]; const found = this.list.filter(list => list.lot.id == lotID)[0];
if (checked) { if (checked) {
if (found && found.type == "Remove") { 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 { } else {
this.list.push({ this.list.push({
type: "Add", type: "Add",
lot, lot,
devices: selectedDevices devices: selectedDevices,
indeterminate
}); });
} }
} else if (found && found.type == "Add") { } 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 { } else {
this.list.push({ this.list.push({
type: "Remove", type: "Remove",
lot, lot,
devices: selectedDevices devices: selectedDevices,
indeterminate
}); });
} }
@ -430,23 +473,20 @@ async function processSelectedDevices() {
const newRequest = await Api.doRequest(window.location); const newRequest = await Api.doRequest(window.location);
const tmpDiv = document.createElement("div"); const tmpDiv = document.createElement("div");
tmpDiv.innerHTML = newRequest; 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 newTable = document.createElement("table");
newTable.innerHTML = tmpDiv.querySelector("table").innerHTML;
const rowsToRemove = []; newTable.classList = "table";
const oldTable = document.querySelector(".dataTable-wrapper");
for (let i = 0; i < table.activeRows.length; i++) { oldTable.parentElement.replaceChild(newTable, oldTable);
const row = table.activeRows[i]; table = new simpleDatatables.DataTable(newTable, {
perPage: 20
if (!newTable.includes(row.querySelector("input").attributes["data-device-dhid"].value)) { }); // // Restore state of checkbox
rowsToRemove.push(i);
}
}
table.rows().remove(rowsToRemove); // Restore state of checkbox
const selectAllBTN = document.getElementById("SelectAllBTN"); const selectAllBTN = document.getElementById("SelectAllBTN");
selectAllBTN.checked = false; selectAllBTN.checked = false;
selectAllBTN.indeterminate = false; selectAllBTN.indeterminate = false; // Re-init SelectorController
selectorController("softInit");
} }
} }

View File

@ -78,10 +78,28 @@ class TableController {
/** /**
* Select all functionality * Select all functionality
*/ */
window.addEventListener("DOMContentLoaded", () => {
const selectorController = (action) => {
const btnSelectAll = document.getElementById("SelectAllBTN"); const btnSelectAll = document.getElementById("SelectAllBTN");
const alertInfoDevices = document.getElementById("select-devices-info"); 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() { function itemListCheckChanged() {
alertInfoDevices.innerHTML = `Selected devices: ${TableController.getSelectedDevices().length} alertInfoDevices.innerHTML = `Selected devices: ${TableController.getSelectedDevices().length}
${TableController.getAllDevices().length != TableController.getSelectedDevices().length ${TableController.getAllDevices().length != TableController.getSelectedDevices().length
@ -114,11 +132,7 @@ window.addEventListener("DOMContentLoaded", () => {
get_device_list(); get_device_list();
} }
TableController.getAllDevices().forEach(item => {
item.addEventListener("click", itemListCheckChanged);
})
btnSelectAll.addEventListener("click", event => { btnSelectAll.addEventListener("click", event => {
const checkedState = event.target.checked; const checkedState = event.target.checked;
TableController.getAllDevicesInCurrentPage().forEach(ckeckbox => { ckeckbox.checked = checkedState }); TableController.getAllDevicesInCurrentPage().forEach(ckeckbox => { ckeckbox.checked = checkedState });
@ -131,13 +145,12 @@ window.addEventListener("DOMContentLoaded", () => {
itemListCheckChanged() itemListCheckChanged()
}) })
// https://github.com/fiduswriter/Simple-DataTables/wiki/Events softInit();
table.on("datatable.page", () => itemListCheckChanged());
table.on("datatable.perpage", () => itemListCheckChanged());
table.on("datatable.update", () => itemListCheckChanged());
itemListCheckChanged(); itemListCheckChanged();
}) }
window.addEventListener("DOMContentLoaded", () => selectorController());
function deviceSelect() { function deviceSelect() {
const devices_count = TableController.getSelectedDevices().length; const devices_count = TableController.getSelectedDevices().length;
@ -425,22 +438,23 @@ async function processSelectedDevices() {
const tmpDiv = document.createElement("div") const tmpDiv = document.createElement("div")
tmpDiv.innerHTML = newRequest 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 oldTable = document.querySelector(".dataTable-wrapper")
const rowsToRemove = [] oldTable.parentElement.replaceChild(newTable, oldTable)
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 table = new simpleDatatables.DataTable(newTable, {perPage: 20})
// // Restore state of checkbox
const selectAllBTN = document.getElementById("SelectAllBTN"); const selectAllBTN = document.getElementById("SelectAllBTN");
selectAllBTN.checked = false; selectAllBTN.checked = false;
selectAllBTN.indeterminate = false; selectAllBTN.indeterminate = false;
// Re-init SelectorController
selectorController("softInit");
} }
} }

View File

@ -361,6 +361,13 @@
<a href="{{ url_for('inventory.device_details', id=dev.devicehub_id)}}"> <a href="{{ url_for('inventory.device_details', id=dev.devicehub_id)}}">
{{ dev.verbose_name }} {{ dev.verbose_name }}
</a> </a>
{% if dev.lots | length > 0 %}
<h6 class="d-inline">
{% for lot in dev.lots %}
<span class="badge rounded-pill bg-light text-dark">{{ lot.name }}</span>
{% endfor %}
</h6>
{% endif %}
</td> </td>
<td> <td>
<a href="{{ url_for('inventory.device_details', id=dev.devicehub_id)}}"> <a href="{{ url_for('inventory.device_details', id=dev.devicehub_id)}}">
@ -434,7 +441,7 @@
<!-- Custom Code --> <!-- Custom Code -->
<script> <script>
const table = new simpleDatatables.DataTable("table", { let table = new simpleDatatables.DataTable("table", {
perPage: 20 perPage: 20
}) })
</script> </script>