2022-10-25 13:46:30 +02:00
|
|
|
/*
|
|
|
|
* 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(
|
2022-10-25 15:46:06 +02:00
|
|
|
response => response.text()
|
2022-10-25 13:46:30 +02:00
|
|
|
).then(
|
2022-10-28 17:02:16 +02:00
|
|
|
json => {
|
|
|
|
let jsonObject = JSON.parse(json)
|
|
|
|
if (jsonObject.status === 200) {
|
|
|
|
setInfo('Data successfully saved.')
|
|
|
|
} else {
|
|
|
|
setError(jsonObject.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-10-25 13:46:30 +02:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:46:06 +02:00
|
|
|
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(
|
2022-10-28 17:02:16 +02:00
|
|
|
json => {
|
|
|
|
let jsonObject = JSON.parse(json)
|
|
|
|
if (jsonObject.status === 200) {
|
|
|
|
setInfo('Data successfully saved.')
|
|
|
|
} else {
|
|
|
|
setError(jsonObject.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-10-25 15:46:06 +02:00
|
|
|
let row = document.getElementById('row_' + id)
|
|
|
|
row.parentNode.removeChild(row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 11:58:27 +02:00
|
|
|
function upCase(text) {
|
|
|
|
return text[0].toUpperCase() + text.substring(1);
|
|
|
|
}
|
2022-10-26 12:12:34 +02:00
|
|
|
|
2022-10-25 15:46:06 +02:00
|
|
|
function sortBy(column) {
|
2022-10-26 11:58:27 +02:00
|
|
|
// clear titles
|
|
|
|
const titles = ['first', 'last', 'street', 'zip', 'city', 'phone']
|
|
|
|
titles.forEach((title) =>
|
|
|
|
document.getElementById(title).innerHTML = upCase(title)
|
|
|
|
)
|
|
|
|
|
2022-10-25 18:25:38 +02:00
|
|
|
// switch direction on every call
|
2022-10-26 11:58:27 +02:00
|
|
|
let currentTitleElement = document.getElementById(column)
|
|
|
|
let currentTitle = currentTitleElement.innerHTML
|
|
|
|
|
2022-10-26 12:12:34 +02:00
|
|
|
let newTitle
|
2022-10-29 14:06:56 +02:00
|
|
|
if (currentSortOrder === 'asc') {
|
|
|
|
currentSortOrder = 'desc'
|
2022-10-26 11:58:27 +02:00
|
|
|
newTitle = currentTitle[0] + currentTitle.substring(1) + ' ⬇'
|
2022-10-25 18:25:38 +02:00
|
|
|
} else {
|
2022-10-29 14:06:56 +02:00
|
|
|
currentSortOrder = 'asc'
|
2022-10-26 11:58:27 +02:00
|
|
|
newTitle = currentTitle[0] + currentTitle.substring(1) + ' ⬆'
|
2022-10-25 18:25:38 +02:00
|
|
|
}
|
2022-10-26 11:58:27 +02:00
|
|
|
currentTitleElement.innerHTML = newTitle
|
2022-10-25 18:25:38 +02:00
|
|
|
|
2022-10-25 15:46:06 +02:00
|
|
|
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
|
2022-10-27 12:13:06 +02:00
|
|
|
let rowXNumber = rowXId.match(/\d+/)
|
2022-10-25 15:46:06 +02:00
|
|
|
let valueX = document.getElementById(column + '_' + rowXNumber).value
|
|
|
|
|
|
|
|
let y = rows[i + 1]
|
|
|
|
let rowYId = y.id
|
2022-10-27 12:13:06 +02:00
|
|
|
let rowYNumber = rowYId.match(/\d+/)
|
2022-10-25 15:46:06 +02:00
|
|
|
let valueY = document.getElementById(column + '_' + rowYNumber).value
|
|
|
|
|
2022-10-26 12:46:03 +02:00
|
|
|
let sortOrder
|
2022-10-29 14:06:56 +02:00
|
|
|
if (currentSortOrder === 'asc') {
|
2022-10-25 18:25:38 +02:00
|
|
|
sortOrder = 1
|
|
|
|
} else {
|
|
|
|
sortOrder = -1
|
|
|
|
}
|
2022-10-25 15:46:06 +02:00
|
|
|
if (valueX.localeCompare(valueY) === sortOrder) {
|
|
|
|
x.parentNode.insertBefore(y, x);
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 13:46:30 +02:00
|
|
|
}
|
2022-10-26 12:12:34 +02:00
|
|
|
|
2022-10-28 17:02:16 +02:00
|
|
|
|
|
|
|
function setInfo(info) {
|
|
|
|
const infoBox = document.getElementById('info_box')
|
|
|
|
infoBox.innerHTML = info
|
|
|
|
infoBox.style.display = 'block'
|
|
|
|
infoBox.classList.add('panel_float')
|
|
|
|
setTimeout(() => {
|
|
|
|
infoBox.style.display = 'none'
|
|
|
|
}, 2500)
|
|
|
|
}
|
|
|
|
|
|
|
|
function setError(error) {
|
|
|
|
const errorBox = document.getElementById('error_box')
|
|
|
|
const errorText = document.getElementById('error_text')
|
|
|
|
const infoButton = document.getElementById('info_button')
|
|
|
|
if (errorBox.style.display === 'block') {
|
|
|
|
errorBox.style.display = 'none'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (infoButton != null) {
|
|
|
|
infoButton.disabled = true
|
|
|
|
}
|
|
|
|
errorText.innerHTML = error
|
|
|
|
errorBox.style.display = 'block'
|
|
|
|
errorBox.classList.add('panel_float')
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeError() {
|
|
|
|
const errorBox = document.getElementById('error_box')
|
|
|
|
const infoButton = document.getElementById('info_button')
|
|
|
|
if (infoButton) {
|
|
|
|
infoButton.disabled = false
|
|
|
|
}
|
|
|
|
errorBox.style.display = 'none'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:06:56 +02:00
|
|
|
// global scope
|
|
|
|
let currentSortOrder = 'desc'
|
|
|
|
|
2022-10-26 12:12:34 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
2022-10-26 12:33:38 +02:00
|
|
|
const table = document.getElementById('address_table') || false
|
|
|
|
if (table) {
|
|
|
|
sortBy('last')
|
|
|
|
}
|
2022-10-26 12:12:34 +02:00
|
|
|
})
|