Files
desktop/public/assets/apps/cron-management/app.js
Lars Gebhardt-Kusche d121f74bd0
All checks were successful
Deploy / deploy-staging (push) Successful in 25s
Deploy / deploy-production (push) Has been skipped
Main update
2026-06-24 02:24:39 +02:00

104 lines
3.5 KiB
JavaScript

(() => {
const normalizeUrl = (url) => {
const next = new URL(url, window.location.origin);
next.searchParams.set('partial', '1');
return next.toString();
};
const renderError = (host, message) => {
host.innerHTML = `
<div class="window-app-shell cm-shell">
<div class="window-app-frame cm-frame">
<main class="window-app-main cm-main">
<div class="window-app-panel cm-panel">
<section class="window-app-card cm-card">
<p class="cm-message is-error">${String(message || 'Cron Tool konnte nicht geladen werden.')}</p>
</section>
</div>
</main>
</div>
</div>
`;
};
const bindInteractions = (host, loadRoute) => {
host.querySelectorAll('form[action]').forEach((form) => {
if (form.dataset.cmBound === '1') {
return;
}
const action = String(form.getAttribute('action') || '');
if (!action.startsWith('/admin/cron/')) {
return;
}
form.dataset.cmBound = '1';
form.addEventListener('submit', async (event) => {
event.preventDefault();
try {
host.classList.add('is-loading');
const response = await fetch(normalizeUrl(action), {
method: 'POST',
credentials: 'same-origin',
body: new FormData(form),
});
const html = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
host.innerHTML = html;
bindInteractions(host, loadRoute);
} catch (error) {
renderError(host, error?.message || 'Cron Tool konnte nicht geladen werden.');
} finally {
host.classList.remove('is-loading');
}
});
});
};
window.initCronManagementApp = function initCronManagementApp(host, options = {}) {
if (!(host instanceof Element)) {
return;
}
if (host.dataset.cmAppBound === '1') {
return;
}
host.dataset.cmAppBound = '1';
const entryRoute = String(options.entryRoute || '/admin/cron/');
const loadRoute = async (route) => {
try {
host.classList.add('is-loading');
const response = await fetch(normalizeUrl(route), {
credentials: 'same-origin',
headers: { Accept: 'text/html' },
});
const html = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
host.innerHTML = html;
bindInteractions(host, loadRoute);
} catch (error) {
renderError(host, error?.message || 'Cron Tool konnte nicht geladen werden.');
} finally {
host.classList.remove('is-loading');
}
};
if (options.standalone) {
bindInteractions(host, loadRoute);
return;
}
loadRoute(entryRoute);
};
})();