sort the code in meaningful way: const DATAAPIENDPOINT = '/data.json'; const…
- User
sort the code in meaningful way: const DATA_API_ENDPOINT = '/data.json'; const HEADER_ELEMENT = document.querySelector('header'); const ACTIONS_CONTAINER = document.querySelector('.actions-container'); const FORM_ELEMENT = document.querySelector('#form'); const TABLE_ELEMENT = document.querySelector('#table');
let BACKUP_DATA = []; let CURRENT_DATA = []; let HAS_UNSAVED_CHANGES = false; const ACTIVE_ROW = { data: null, html: null };
const CHANGE_OBSERVER = new MutationObserver(function () { HAS_UNSAVED_CHANGES = true; }); const OBSERVER_SETTINGS = { attributes: false, childList: true, subtree: true };
async function loadDataFromServer(url) { try { const response = await fetch(url); const data = await response.json(); return data; } catch (error) { console.log(error); return []; } }
function findDataItemIndexById(id) { return CURRENT_DATA.findIndex(item => item.id === id); }
function exchangeArrayElements(arr, index1, index2) { const temp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = temp; }
function exchangeRowIndices(row1, row2) { const index1 = row1.children[1].textContent; const index2 = row2.children[1].textContent;
row1.children[1].textContent = index2; row2.children[1].textContent = index1;}
function backupCurrentData() { BACKUP_DATA = JSON.parse(JSON.stringify(CURRENT_DATA)); }
function clearActiveRowSelection() { ACTIVE_ROW.data = null; ACTIVE_ROW.html = null; }
function restoreBackupData() { CURRENT_DATA = JSON.parse(JSON.stringify(BACKUP_DATA)); }
function sortTableData(property, direction, type) { CURRENT_DATA.sort((a, b) => { if (type === 'string') { return direction === 'asc' ? a[property].localeCompare(b[property], undefined, { numeric: true }) : b[property].localeCompare(a[property], undefined, { numeric: true }); } else if (type === 'number') { return direction === 'asc' ? a[property] - b[property] : b[property] - a[property]; } else { console.log(
Unsupported type: ${type}); return 0; } }); }function fillFormFields(data) { FORM_ELEMENT.chemical_name.value = data.chemical_name; FORM_ELEMENT.vendor.value = data.vendor; FORM_ELEMENT.density.value = data.density; FORM_ELEMENT.viscosity.value = data.viscosity; FORM_ELEMENT.packaging.value = data.packaging; FORM_ELEMENT.pack_size.value = data.pack_size; FORM_ELEMENT.unit.value = data.unit; FORM_ELEMENT.quantity.value = data.quantity; }
function toggleFormVisibility() { HEADER_ELEMENT.classList.toggle('show-form'); }
function createTableCell(text) { const cell = document.createElement('td'); cell.textContent = text; return cell; }
function createTableRow(obj, index) { const row = document.createElement('tr');
const selectBox = createTableCell(`\u2714`); selectBox.classList.add('select-box'); row.appendChild(selectBox); row.dataset.id = obj['id']; const rowIndexCell = createTableCell(index); row.appendChild(rowIndexCell); for (const key in obj) { if (key === 'id') continue; const cell = createTableCell(obj[key]); cell.dataset.property = key; row.appendChild(cell); } return row; - Assistant