fix lint javascript

This commit is contained in:
RubenPX 2022-04-08 12:37:10 +02:00
parent 5fbcc6ba05
commit 458e267b46
1 changed files with 71 additions and 75 deletions

View File

@ -193,9 +193,9 @@ async function processSelectedDevices() {
* @returns get lots * @returns get lots
*/ */
async get_lots() { async get_lots() {
var request = await this.doRequest(API_URLS.lots, "GET", null) var request = await this.doRequest(API_URLS.lots, "GET", null);
if (request != undefined) return request.items if (request != undefined) return request.items;
throw request throw request;
}, },
/** /**
@ -204,9 +204,9 @@ async function processSelectedDevices() {
* @returns full detailed device list * @returns full detailed device list
*/ */
async get_devices(ids) { async get_devices(ids) {
var request = await this.doRequest(API_URLS.devices + '?filter={"id": [' + ids.toString() + ']}', "GET", null) var request = await this.doRequest(API_URLS.devices + '?filter={"id": [' + ids.toString() + ']}', "GET", null);
if (request != undefined) return request.items if (request != undefined) return request.items;
throw request throw request;
}, },
/** /**
@ -215,8 +215,8 @@ async function processSelectedDevices() {
* @param {number[]} listDevices list devices id * @param {number[]} listDevices list devices id
*/ */
async devices_add(lotID, listDevices) { async devices_add(lotID, listDevices) {
var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&") var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&");
return await Api.doRequest(queryURL, "POST", null) return await Api.doRequest(queryURL, "POST", null);
}, },
/** /**
@ -225,8 +225,8 @@ async function processSelectedDevices() {
* @param {number[]} listDevices list devices id * @param {number[]} listDevices list devices id
*/ */
async devices_remove(lotID, listDevices) { async devices_remove(lotID, listDevices) {
var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&") var queryURL = API_URLS.devices_modify.replace("UUID", lotID) + "?" + listDevices.map(deviceID => "id=" + deviceID).join("&");
return await Api.doRequest(queryURL, "DELETE", null) return await Api.doRequest(queryURL, "DELETE", null);
}, },
/** /**
@ -242,23 +242,21 @@ async function processSelectedDevices() {
result = await $.ajax({ result = await $.ajax({
url: url, url: url,
type: type, type: type,
headers: { headers: { "Authorization": API_URLS.Auth_Token },
"Authorization": API_URLS.Auth_Token
},
body: body body: body
}); });
return result return result;
} catch (error) { } catch (error) {
console.error(error) console.error(error);
throw error throw error;
} }
} }
} }
class Actions { class Actions {
constructor () { constructor() {
this.list = [] // list of petitions of requests @item --> {type: ["Remove" | "Add"], "LotID": string, "devices": number[]} 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 * @param {number} deviceID device id
*/ */
manage(event, lotID, deviceListID) { manage(event, lotID, deviceListID) {
event.preventDefault() event.preventDefault();
const indeterminate = event.srcElement.indeterminate const indeterminate = event.srcElement.indeterminate;
const checked = !event.srcElement.checked const checked = !event.srcElement.checked;
var found = this.list.filter(list => list.lotID == lotID)[0] var found = this.list.filter(list => list.lotID == lotID)[0];
var foundIndex = found != undefined ? this.list.findLastIndex(x => x.lotID == found.lotID) : -1 var foundIndex = found != undefined ? this.list.findLastIndex(x => x.lotID == found.lotID) : -1;
if (checked) { if (checked) {
if (found != undefined && found.type == "Remove") { if (found != undefined && found.type == "Remove") {
if (found.isFromIndeterminate == true) { if (found.isFromIndeterminate == true) {
found.type = "Add" found.type = "Add";
this.list[foundIndex] = found this.list[foundIndex] = found;
} else { } else {
this.list = this.list.filter(list => list.lotID != lotID) this.list = this.list.filter(list => list.lotID != lotID);
} }
} else { } else {
this.list.push({ type: "Add", lotID: lotID, devices: deviceListID, isFromIndeterminate: indeterminate }) this.list.push({ type: "Add", lotID: lotID, devices: deviceListID, isFromIndeterminate: indeterminate });
} }
} else { } else {
if (found != undefined && found.type == "Add") { if (found != undefined && found.type == "Add") {
if (found.isFromIndeterminate == true) { if (found.isFromIndeterminate == true) {
found.type = "Remove" found.type = "Remove";
this.list[foundIndex] = found this.list[foundIndex] = found;
} else { } else {
this.list = this.list.filter(list => list.lotID != lotID) this.list = this.list.filter(list => list.lotID != lotID);
} }
} else { } 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) { if (this.list.length > 0) {
document.getElementById("ApplyDeviceLots").classList.remove("disabled") document.getElementById("ApplyDeviceLots").classList.remove("disabled");
} else { } 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 * @param {boolean} isError defines if a toast is a error
*/ */
notifyUser(title, toastText, isError) { notifyUser(title, toastText, isError) {
let toast = document.createElement("div") let toast = document.createElement("div");
toast.classList = "alert alert-dismissible fade show " + (isError ? "alert-danger" : "alert-success") toast.classList = "alert alert-dismissible fade show " + (isError ? "alert-danger" : "alert-success");
toast.attributes["data-autohide"] = !isError toast.attributes["data-autohide"] = !isError;
toast.attributes["role"] = "alert" toast.attributes["role"] = "alert";
toast.style = "margin-left: auto; width: fit-content;" toast.style = "margin-left: auto; width: fit-content;";
toast.innerHTML = `<strong>${title}</strong><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>` toast.innerHTML = `<strong>${title}</strong><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>`;
if (toastText && toastText.length > 0) { if (toastText && toastText.length > 0) {
toast.innerHTML += `<br>${toastText}` toast.innerHTML += `<br>${toastText}`;
} }
document.getElementById("NotificationsContainer").appendChild(toast) document.getElementById("NotificationsContainer").appendChild(toast);
if (!isError) { 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 => { this.list.forEach(async action => {
if (action.type == "Add") { if (action.type == "Add") {
try { try {
await Api.devices_add(action.lotID, action.devices) await Api.devices_add(action.lotID, action.devices);
this.notifyUser("Devices sucefully aded to selected lot/s", "", false) this.notifyUser("Devices sucefully aded to selected lot/s", "", false);
} catch (error) { } 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") { } else if (action.type == "Remove") {
try { try {
await Api.devices_remove(action.lotID, action.devices) await Api.devices_remove(action.lotID, action.devices);
this.notifyUser("Devices sucefully removed from selected lot/s", "", false) this.notifyUser("Devices sucefully removed from selected lot/s", "", false);
} catch (error) { } 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 = `<input class="form-check-input" type="checkbox" id="${lotID}" style="width: 20px; height: 20px; margin-right: 7px;"> var htmlTemplate = `<input class="form-check-input" type="checkbox" id="${lotID}" style="width: 20px; height: 20px; margin-right: 7px;">
<label class="form-check-label" for="${lotID}">${lot.name}</label>`; <label class="form-check-label" for="${lotID}">${lot.name}</label>`;
var existLotList = selectedDevicesIDs.map(selected => lot.devices.includes(selected)) var existLotList = selectedDevicesIDs.map(selected => lot.devices.includes(selected));
var doc = document.createElement('li') var doc = document.createElement('li');
doc.innerHTML = htmlTemplate doc.innerHTML = htmlTemplate;
if (selectedDevicesIDs.length <= 0) { if (selectedDevicesIDs.length <= 0) {
doc.children[0].disabled = true doc.children[0].disabled = true;
} else if (existLotList.every(value => value == 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)) { } else if (existLotList.every(value => value == false)) {
doc.children[0].checked = false doc.children[0].checked = false;
} else { } else {
doc.children[0].indeterminate = true; doc.children[0].indeterminate = true;
} }
doc.children[0].addEventListener('mouseup', (ev) => actions.manage(ev, lotID, selectedDevicesIDs)) doc.children[0].addEventListener('mouseup', (ev) => actions.manage(ev, lotID, selectedDevicesIDs));
elementTarget.append(doc) elementTarget.append(doc);
} }
var listHTML = $("#LotsSelector") var listHTML = $("#LotsSelector")
// Get selected devices // 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) { if (selectedDevicesIDs.length <= 0) {
listHTML.html('<li style="color: red; text-align: center">No devices selected</li>') listHTML.html('<li style="color: red; text-align: center">No devices selected</li>');
return return;
} }
// Initialize Actions list, and set checkbox triggers // Initialize Actions list, and set checkbox triggers
var actions = new Actions() var actions = new Actions();
if (eventClickActions) { if (eventClickActions) {
document.getElementById("ApplyDeviceLots").removeEventListener(eventClickActions) document.getElementById("ApplyDeviceLots").removeEventListener(eventClickActions);
} }
eventClickActions = document.getElementById("ApplyDeviceLots").addEventListener("click", () => actions.doActions()) eventClickActions = document.getElementById("ApplyDeviceLots").addEventListener("click", () => actions.doActions());
document.getElementById("ApplyDeviceLots").classList.add("disabled") document.getElementById("ApplyDeviceLots").classList.add("disabled");
try { try {
listHTML.html('<li style="text-align: center"><div class="spinner-border text-info" style="margin: auto" role="status"></div></li>') listHTML.html('<li style="text-align: center"><div class="spinner-border text-info" style="margin: auto" role="status"></div></li>')
var devices = await Api.get_devices(selectedDevicesIDs) var devices = await Api.get_devices(selectedDevicesIDs);
var lots = await Api.get_lots() var lots = await Api.get_lots();
lots = lots.map(lot => { lots = lots.map(lot => {
lot.devices = devices lot.devices = devices
.filter(device => device.lots.filter(devicelot => devicelot.id == lot.id).length > 0) .filter(device => device.lots.filter(devicelot => devicelot.id == lot.id).length > 0)
.map(device => parseInt(device.id)) .map(device => parseInt(device.id));
return lot return lot;
}) })
listHTML.html('') listHTML.html('');
lots.forEach(lot => templateLot(lot.id, lot, selectedDevicesIDs, listHTML, actions)) lots.forEach(lot => templateLot(lot.id, lot, selectedDevicesIDs, listHTML, actions));
} catch (error) { } catch (error) {
console.log(error) console.log(error);
listHTML.html('<li style="color: red; text-align: center">Error feching devices and lots<br>(see console for more details)</li>') listHTML.html('<li style="color: red; text-align: center">Error feching devices and lots<br>(see console for more details)</li>');
} }
} }