adlerka.top/assets/script.js

168 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-02-03 22:44:01 +01:00
function doAccountAction(requestData, successMessage, failureMessage, silent=false) {
2024-02-03 17:01:52 +01:00
return fetch('/account', {
2024-02-03 16:08:26 +01:00
method: 'POST',
body: requestData,
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
2024-02-03 22:44:01 +01:00
if(!silent) {
handleResponse(data, successMessage, failureMessage);
}
2024-02-03 16:08:26 +01:00
return data; // Returning the response data for further processing
})
.catch((error) => {
console.error('Error:', error);
});
}
2024-02-05 21:21:04 +01:00
function handlePageResponse(data){
if(data.Navigation){
document.getElementById("navbar").innerHTML = data.Navigation;
}
if(data.Page){
document.getElementById("pagearea").innerHTML = data.Page;
if(data.PageLocation){
history.pushState({}, "", data.PageLocation);
}
}
}
function doPageAction(requestData){
return fetch('/page', {
method: 'POST',
body: requestData,
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
handlePageResponse(data);
})
.catch((error) => {
console.error('Error:', error);
});
}
2024-02-04 09:50:04 +01:00
function displayList(data, element_id, delete_function=null) {
const tableContainer = document.getElementById(element_id);
tableContainer.innerHTML = ""; // Clear previous content
const table = document.createElement("table");
table.classList.add("list-table");
// Create header row
const headerRow = table.insertRow(0);
for (const key in data[0]) {
const th = document.createElement("th");
th.appendChild(document.createTextNode(key));
headerRow.appendChild(th);
}
if(typeof delete_function === "function") {
const th = document.createElement("th");
th.appendChild(document.createTextNode("Delete"));
headerRow.appendChild(th);
}
// Create data rows
for (const line of data) {
const dataRow = table.insertRow();
for (const key in line) {
const td = document.createElement("td");
td.appendChild(document.createTextNode(line[key]));
dataRow.appendChild(td);
}
if(typeof delete_function === "function") {
const td = document.createElement("td");
let delete_button = document.createElement('button');
2024-02-04 09:55:47 +01:00
delete_button.textContent = "Delete"
2024-02-04 09:50:04 +01:00
delete_button.onclick = function (){
delete_function(line.ID);
}
td.appendChild(delete_button);
dataRow.appendChild(td);
}
}
tableContainer.appendChild(table);
}
2024-02-03 16:08:26 +01:00
function handleResponse(data, SuccessMessage, failureMessage) {
const StatusMessageElement = document.getElementById("StatusMessage");
if (data.Status === 'Success') {
StatusMessageElement.innerText = SuccessMessage;
} else {
StatusMessageElement.innerText = failureMessage;
}
2024-02-04 09:26:17 +01:00
// Show the status message
StatusMessageElement.style.display = "block";
setTimeout(() => {
// Hide the status message after 3 seconds
2024-02-04 09:50:04 +01:00
StatusMessageElement.style.opacity = "0";
2024-02-04 09:26:17 +01:00
setTimeout(() => {
StatusMessageElement.style.display = "none";
// Reset opacity for future messages
2024-02-04 09:50:04 +01:00
StatusMessageElement.style.opacity = "1";
2024-02-04 09:26:17 +01:00
}, 500);
}, 3000);
2024-02-03 16:08:26 +01:00
}
2024-02-05 21:21:04 +01:00
function navigateTo(site, page){
const data = new URLSearchParams();
data.append("action", "getPage");
data.append("site", site);
data.append("page", page);
doPageAction(data);
}
function refreshNavbar(){
const data = new URLSearchParams();
data.append("action", "getNavigation");
doPageAction(data);
}
2024-02-03 16:08:26 +01:00
function logout() {
const data = new URLSearchParams();
data.append("action", "logout");
2024-02-04 09:50:04 +01:00
doAccountAction(data, "Logout Successful!", "Logout failed.").then(() => {
2024-02-05 21:21:04 +01:00
refreshNavbar();
navigateTo("", pageData.defaultPage);
2024-02-04 09:50:04 +01:00
// Expected output: "Success!"
});
2024-02-03 16:08:26 +01:00
}
2024-02-05 21:23:21 +01:00
function initAjax() {
2024-02-05 22:02:16 +01:00
let links = document.querySelectorAll('.navsite_link, .navpage_link');
2024-02-05 21:21:04 +01:00
// Add click event listener to each link
2024-02-05 21:23:21 +01:00
links.forEach(function (link) {
link.addEventListener('click', function (e) {
e.preventDefault();
2024-02-05 21:21:04 +01:00
2024-02-05 21:23:21 +01:00
// Get page and site information
2024-02-05 21:25:41 +01:00
let site = this.dataset.site;
let page = this.dataset.page;
2024-02-05 21:21:04 +01:00
2024-02-05 21:23:21 +01:00
if (site && page) {
navigateTo(site, page);
}
2024-02-05 21:21:04 +01:00
2024-02-05 21:23:21 +01:00
// You can use this information to update the URL or perform other actions
});
2024-02-05 21:21:04 +01:00
});
2024-02-05 21:23:21 +01:00
}
document.addEventListener('DOMContentLoaded', initAjax);