Files
theoldfitz/admin/index.js
T

56 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Announcements admin page: intercepts the status badge's toggle form so
// clicking it updates in place via fetch instead of reloading the whole
// page — same interaction as the menu item's status toggle. Everything
// else here (create/edit/delete) is still a plain form POST + reload.
document.addEventListener('submit', async (e) => {
const form = e.target;
if (!form.classList.contains('status-toggle-form')) return;
e.preventDefault();
const btn = form.querySelector('button');
btn.disabled = true;
try {
const response = await fetch('index.php', {
method: 'POST',
body: new FormData(form),
headers: { 'X-Requested-With': 'XMLHttpRequest' }
});
const data = await response.json();
if (data.success) {
btn.className = `status-badge ${data.status_class}`;
btn.textContent = data.status_label;
btn.title = `Click to turn ${data.is_active ? 'off' : 'on'}`;
} else {
alert(data.error || 'Failed to update status.');
}
} catch {
alert('Network error. Please try again.');
} finally {
btn.disabled = false;
}
});
// Asks before submitting any form marked .js-confirm-submit (e.g. delete),
// using the message in its data-confirm attribute.
document.addEventListener('submit', (e) => {
const form = e.target;
if (!form.classList.contains('js-confirm-submit')) return;
if (!confirm(form.dataset.confirm || 'Are you sure?')) {
e.preventDefault();
}
});
// Announcement editor dialog: the server flags it for ?new / ?edit URLs.
// Closing it any way (× link, Esc) drops the query string, so a reload
// doesn't reopen it.
const annModal = document.getElementById('modal-ann');
if (annModal?.hasAttribute('data-open-on-load')) {
annModal.showModal();
annModal.addEventListener('close', () => {
history.replaceState(null, '', 'index.php');
});
}