diff --git a/ereuse_devicehub/static/js/main_inventory.js b/ereuse_devicehub/static/js/main_inventory.js index 2a55e757..84097ded 100644 --- a/ereuse_devicehub/static/js/main_inventory.js +++ b/ereuse_devicehub/static/js/main_inventory.js @@ -193,9 +193,9 @@ async function processSelectedDevices() { * @returns get lots */ async get_lots() { - var request = await this.doRequest(API_URLS.lots, "GET", null) - if (request != undefined) return request.items - throw request + var request = await this.doRequest(API_URLS.lots, "GET", null); + if (request != undefined) return request.items; + throw request; }, /** @@ -204,9 +204,9 @@ async function processSelectedDevices() { * @returns full detailed device list */ async get_devices(ids) { - var request = await this.doRequest(API_URLS.devices + '?filter={"id": [' + ids.toString() + ']}', "GET", null) - if (request != undefined) return request.items - throw request + var request = await this.doRequest(API_URLS.devices + '?filter={"id": [' + ids.toString() + ']}', "GET", null); + if (request != undefined) return request.items; + throw request; }, /** @@ -215,8 +215,8 @@ async function processSelectedDevices() { * @param {number[]} listDevices list devices id */ async devices_add(lotID, listDevices) { - var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&") - return await Api.doRequest(queryURL, "POST", null) + var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&"); + return await Api.doRequest(queryURL, "POST", null); }, /** @@ -225,8 +225,8 @@ async function processSelectedDevices() { * @param {number[]} listDevices list devices id */ async devices_remove(lotID, listDevices) { - var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&") - return await Api.doRequest(queryURL, "DELETE", null) + var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&"); + return await Api.doRequest(queryURL, "DELETE", null); }, /** @@ -242,23 +242,21 @@ async function processSelectedDevices() { result = await $.ajax({ url: url, type: type, - headers: { - "Authorization": API_URLS.Auth_Token - }, + headers: { "Authorization": API_URLS.Auth_Token }, body: body }); - return result + return result; } catch (error) { - console.error(error) - throw error + console.error(error); + throw error; } } } class Actions { - constructor () { - this.list = [] // list of petitions of requests @item --> {type: ["Remove" | "Add"], "LotID": string, "devices": number[]} + constructor() { + this.list = []; // list of petitions of requests @item --> {type: ["Remove" | "Add"], "LotID": string, "devices": number[]} } /** @@ -268,41 +266,41 @@ async function processSelectedDevices() { * @param {number} deviceID device id */ manage(event, lotID, deviceListID) { - event.preventDefault() - const indeterminate = event.srcElement.indeterminate - const checked = !event.srcElement.checked + event.preventDefault(); + const indeterminate = event.srcElement.indeterminate; + const checked = !event.srcElement.checked; - var found = this.list.filter(list => list.lotID == lotID)[0] - var foundIndex = found != undefined ? this.list.findLastIndex(x => x.lotID == found.lotID) : -1 + var found = this.list.filter(list => list.lotID == lotID)[0]; + var foundIndex = found != undefined ? this.list.findLastIndex(x => x.lotID == found.lotID) : -1; if (checked) { if (found != undefined && found.type == "Remove") { if (found.isFromIndeterminate == true) { - found.type = "Add" - this.list[foundIndex] = found + found.type = "Add"; + this.list[foundIndex] = found; } else { - this.list = this.list.filter(list => list.lotID != lotID) + this.list = this.list.filter(list => list.lotID != lotID); } } else { - this.list.push({ type: "Add", lotID: lotID, devices: deviceListID, isFromIndeterminate: indeterminate }) + this.list.push({ type: "Add", lotID: lotID, devices: deviceListID, isFromIndeterminate: indeterminate }); } } else { if (found != undefined && found.type == "Add") { if (found.isFromIndeterminate == true) { - found.type = "Remove" - this.list[foundIndex] = found + found.type = "Remove"; + this.list[foundIndex] = found; } else { - this.list = this.list.filter(list => list.lotID != lotID) + this.list = this.list.filter(list => list.lotID != lotID); } } else { - this.list.push({ type: "Remove", lotID: lotID, devices: deviceListID, isFromIndeterminate: indeterminate }) + this.list.push({ type: "Remove", lotID: lotID, devices: deviceListID, isFromIndeterminate: indeterminate }); } } if (this.list.length > 0) { - document.getElementById("ApplyDeviceLots").classList.remove("disabled") + document.getElementById("ApplyDeviceLots").classList.remove("disabled"); } else { - document.getElementById("ApplyDeviceLots").classList.add("disabled") + document.getElementById("ApplyDeviceLots").classList.add("disabled"); } } @@ -313,21 +311,21 @@ async function processSelectedDevices() { * @param {boolean} isError defines if a toast is a error */ notifyUser(title, toastText, isError) { - let toast = document.createElement("div") - toast.classList = "alert alert-dismissible fade show " + (isError ? "alert-danger" : "alert-success") - toast.attributes["data-autohide"] = !isError - toast.attributes["role"] = "alert" - toast.style = "margin-left: auto; width: fit-content;" - toast.innerHTML = `${title}` + let toast = document.createElement("div"); + toast.classList = "alert alert-dismissible fade show " + (isError ? "alert-danger" : "alert-success"); + toast.attributes["data-autohide"] = !isError; + toast.attributes["role"] = "alert"; + toast.style = "margin-left: auto; width: fit-content;"; + toast.innerHTML = `${title}`; if (toastText && toastText.length > 0) { - toast.innerHTML += `
${toastText}` + toast.innerHTML += `
${toastText}`; } - document.getElementById("NotificationsContainer").appendChild(toast) + document.getElementById("NotificationsContainer").appendChild(toast); if (!isError) { - setTimeout(() => toast.classList.remove("show"), 3000) + setTimeout(() => toast.classList.remove("show"), 3000); } - setTimeout(() => document.getElementById("NotificationsContainer").innerHTML == "", 3500) + setTimeout(() => document.getElementById("NotificationsContainer").innerHTML == "", 3500); } /** @@ -337,17 +335,17 @@ async function processSelectedDevices() { this.list.forEach(async action => { if (action.type == "Add") { try { - await Api.devices_add(action.lotID, action.devices) - this.notifyUser("Devices sucefully aded to selected lot/s", "", false) + await Api.devices_add(action.lotID, action.devices); + this.notifyUser("Devices sucefully aded to selected lot/s", "", false); } catch (error) { - this.notifyUser("Failed to add devices to selected lot/s", error.responseJSON.message, true) + this.notifyUser("Failed to add devices to selected lot/s", error.responseJSON.message, true); } } else if (action.type == "Remove") { try { - await Api.devices_remove(action.lotID, action.devices) - this.notifyUser("Devices sucefully removed from selected lot/s", "", false) + await Api.devices_remove(action.lotID, action.devices); + this.notifyUser("Devices sucefully removed from selected lot/s", "", false); } catch (error) { - this.notifyUser("Fail to remove devices from selected lot/s", error.responseJSON.message, true) + this.notifyUser("Fail to remove devices from selected lot/s", error.responseJSON.message, true); } } }) @@ -369,60 +367,58 @@ async function processSelectedDevices() { var htmlTemplate = ` `; - var existLotList = selectedDevicesIDs.map(selected => lot.devices.includes(selected)) + var existLotList = selectedDevicesIDs.map(selected => lot.devices.includes(selected)); - var doc = document.createElement('li') - doc.innerHTML = htmlTemplate + var doc = document.createElement('li'); + doc.innerHTML = htmlTemplate; if (selectedDevicesIDs.length <= 0) { - doc.children[0].disabled = true + doc.children[0].disabled = true; } else if (existLotList.every(value => value == true)) { - doc.children[0].checked = true + doc.children[0].checked = true; } else if (existLotList.every(value => value == false)) { - doc.children[0].checked = false + doc.children[0].checked = false; } else { doc.children[0].indeterminate = true; } - doc.children[0].addEventListener('mouseup', (ev) => actions.manage(ev, lotID, selectedDevicesIDs)) - elementTarget.append(doc) + doc.children[0].addEventListener('mouseup', (ev) => actions.manage(ev, lotID, selectedDevicesIDs)); + elementTarget.append(doc); } var listHTML = $("#LotsSelector") // Get selected devices - var selectedDevicesIDs = $.map($(".deviceSelect").filter(':checked'), function (x) { return parseInt($(x).attr('data')) }) + var selectedDevicesIDs = $.map($(".deviceSelect").filter(':checked'), function (x) { return parseInt($(x).attr('data')) }); if (selectedDevicesIDs.length <= 0) { - listHTML.html('
  • No devices selected
  • ') - return + listHTML.html('
  • No devices selected
  • '); + return; } // Initialize Actions list, and set checkbox triggers - var actions = new Actions() + var actions = new Actions(); if (eventClickActions) { - document.getElementById("ApplyDeviceLots").removeEventListener(eventClickActions) + document.getElementById("ApplyDeviceLots").removeEventListener(eventClickActions); } - eventClickActions = document.getElementById("ApplyDeviceLots").addEventListener("click", () => actions.doActions()) - document.getElementById("ApplyDeviceLots").classList.add("disabled") + eventClickActions = document.getElementById("ApplyDeviceLots").addEventListener("click", () => actions.doActions()); + document.getElementById("ApplyDeviceLots").classList.add("disabled"); try { listHTML.html('
  • ') - var devices = await Api.get_devices(selectedDevicesIDs) - var lots = await Api.get_lots() + var devices = await Api.get_devices(selectedDevicesIDs); + var lots = await Api.get_lots(); lots = lots.map(lot => { lot.devices = devices .filter(device => device.lots.filter(devicelot => devicelot.id == lot.id).length > 0) - .map(device => parseInt(device.id)) - return lot + .map(device => parseInt(device.id)); + return lot; }) - listHTML.html('') - lots.forEach(lot => templateLot(lot.id, lot, selectedDevicesIDs, listHTML, actions)) + listHTML.html(''); + lots.forEach(lot => templateLot(lot.id, lot, selectedDevicesIDs, listHTML, actions)); } catch (error) { - console.log(error) - listHTML.html('
  • Error feching devices and lots
    (see console for more details)
  • ') + console.log(error); + listHTML.html('
  • Error feching devices and lots
    (see console for more details)
  • '); } - - -} \ No newline at end of file +}