140 lines
4.6 KiB
JavaScript
140 lines
4.6 KiB
JavaScript
/*
|
|
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*
|
|
*/
|
|
|
|
function addAddress(url) {
|
|
location.href = url
|
|
}
|
|
|
|
function editAddress(id) {
|
|
if (document.getElementById('edit_button_' + id).value === 'Save') {
|
|
// save
|
|
const url = "/address/update";
|
|
fetch(url, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
id: id,
|
|
owner: document.getElementById('owner_' + id).value,
|
|
first: document.getElementById('first_' + id).value,
|
|
last: document.getElementById('last_' + id).value,
|
|
street: document.getElementById('street_' + id).value,
|
|
zip: document.getElementById('zip_' + id).value,
|
|
city: document.getElementById('city_' + id).value,
|
|
phone: document.getElementById('phone_' + id).value,
|
|
})
|
|
})
|
|
.then(
|
|
response => response.text()
|
|
).then(
|
|
html => console.log(html)
|
|
);
|
|
|
|
document.getElementById('first_' + id).disabled = true
|
|
document.getElementById('last_' + id).disabled = true
|
|
document.getElementById('street_' + id).disabled = true
|
|
document.getElementById('zip_' + id).disabled = true
|
|
document.getElementById('city_' + id).disabled = true
|
|
document.getElementById('phone_' + id).disabled = true
|
|
|
|
document.getElementById('edit_button_' + id).value = 'Edit'
|
|
} else {
|
|
//switch to edit
|
|
document.getElementById('first_' + id).disabled = false
|
|
document.getElementById('last_' + id).disabled = false
|
|
document.getElementById('street_' + id).disabled = false
|
|
document.getElementById('zip_' + id).disabled = false
|
|
document.getElementById('city_' + id).disabled = false
|
|
document.getElementById('phone_' + id).disabled = false
|
|
|
|
document.getElementById('edit_button_' + id).value = 'Save'
|
|
}
|
|
}
|
|
|
|
function deleteAddress(id) {
|
|
if (confirm('Are you sure?')) {
|
|
const url = "/address/delete";
|
|
fetch(url, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
id: id
|
|
})
|
|
})
|
|
.then(
|
|
response => response.text()
|
|
).then(
|
|
html => console.log(html)
|
|
);
|
|
let row = document.getElementById('row_' + id)
|
|
row.parentNode.removeChild(row)
|
|
}
|
|
}
|
|
|
|
function upCase(text) {
|
|
return text[0].toUpperCase() + text.substring(1);
|
|
}
|
|
|
|
function sortBy(column) {
|
|
// clear titles
|
|
const titles = ['first', 'last', 'street', 'zip', 'city', 'phone']
|
|
titles.forEach((title) =>
|
|
document.getElementById(title).innerHTML = upCase(title)
|
|
)
|
|
|
|
// switch direction on every call
|
|
let currentSortOrder = document.getElementById(column + '_sort')
|
|
let currentTitleElement = document.getElementById(column)
|
|
let currentTitle = currentTitleElement.innerHTML
|
|
|
|
let newTitle
|
|
if (currentSortOrder.innerHTML === 'asc') {
|
|
currentSortOrder.innerHTML = 'desc'
|
|
newTitle = currentTitle[0] + currentTitle.substring(1) + ' ⬇'
|
|
} else {
|
|
currentSortOrder.innerHTML = 'asc'
|
|
newTitle = currentTitle[0] + currentTitle.substring(1) + ' ⬆'
|
|
}
|
|
currentTitleElement.innerHTML = newTitle
|
|
|
|
const table = document.getElementById('address_table');
|
|
let dirty = true;
|
|
// loop until clean
|
|
while (dirty) {
|
|
// assume we are finished
|
|
dirty = false
|
|
const rows = table.rows;
|
|
for (let i = 1; i < (rows.length - 2); i++) {
|
|
let x = rows[i]
|
|
let rowXId = x.id
|
|
let rowXNumber = rowXId.charAt(rowXId.length -1)
|
|
let valueX = document.getElementById(column + '_' + rowXNumber).value
|
|
|
|
let y = rows[i + 1]
|
|
let rowYId = y.id
|
|
let rowYNumber = rowYId.charAt(rowYId.length -1)
|
|
let valueY = document.getElementById(column + '_' + rowYNumber).value
|
|
|
|
let currentSortOrder = document.getElementById(column + '_sort')
|
|
let sortOrder
|
|
if (currentSortOrder.innerHTML === 'asc') {
|
|
sortOrder = 1
|
|
} else {
|
|
sortOrder = -1
|
|
}
|
|
if (valueX.localeCompare(valueY) === sortOrder) {
|
|
x.parentNode.insertBefore(y, x);
|
|
dirty = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const table = document.getElementById('address_table') || false
|
|
if (table) {
|
|
sortBy('last')
|
|
}
|
|
}) |