Files
taxbaik/TaxBaik.Web/wwwroot/js/admin-session.js
T

103 lines
3.6 KiB
JavaScript

window.taxbaikAdminSession = {
syncRouteClass: function () {
document.documentElement.classList.toggle(
'admin-login-route',
window.location.pathname.toLowerCase().endsWith('/admin/login'));
},
getViewportWidth: function () {
return window.innerWidth || document.documentElement.clientWidth || 0;
},
clearAuthToken: function () {
try {
localStorage.removeItem('auth_token');
} catch {
// Ignore storage errors; redirect still recovers the session.
}
},
showLoading: function () {
const overlay = document.getElementById('blazor-loading');
if (!overlay) return;
// Show overlay immediately
overlay.classList.add('show');
// Check if page is already ready (cached state on fast nav)
const pageReady =
document.querySelector('.admin-page-hero') !== null ||
document.querySelector('.admin-login-page') !== null;
if (pageReady) {
// Page already rendered, hide immediately
window.taxbaikAdminSession.hideLoading();
return;
}
// Start observer to catch future mutations
if (window._taxbaikLoadingObserver) {
window._taxbaikLoadingObserver.disconnect();
}
window._taxbaikLoadingObserver = new MutationObserver(function () {
const pageReady =
document.querySelector('.admin-page-hero') !== null ||
document.querySelector('.admin-login-page') !== null;
if (pageReady) {
window.taxbaikAdminSession.hideLoading();
}
});
window._taxbaikLoadingObserver.observe(document.body, {
childList: true,
subtree: true
});
// Safety fallback: hide after 3 seconds regardless.
if (window._taxbaikLoadingTimeout) {
clearTimeout(window._taxbaikLoadingTimeout);
}
window._taxbaikLoadingTimeout = setTimeout(function () {
window.taxbaikAdminSession.hideLoading();
}, 3000);
},
hideLoading: function () {
const overlay = document.getElementById('blazor-loading');
if (overlay) {
overlay.classList.remove('show');
}
if (window._taxbaikLoadingTimeout) {
clearTimeout(window._taxbaikLoadingTimeout);
window._taxbaikLoadingTimeout = null;
}
if (window._taxbaikLoadingObserver) {
window._taxbaikLoadingObserver.disconnect();
window._taxbaikLoadingObserver = null;
}
},
watchReconnect: function () {
window.taxbaikAdminSession.syncRouteClass();
window.addEventListener('popstate', window.taxbaikAdminSession.syncRouteClass);
// Show loading on initial page load — overlay has 'show' from HTML,
// but we still need to set up the observer to detect when to hide it.
window.taxbaikAdminSession.showLoading();
const modal = document.getElementById('components-reconnect-modal');
if (!modal) return;
const reloadOnRejectedCircuit = function () {
const className = modal.className || '';
if (className.includes('components-reconnect-failed') ||
className.includes('components-reconnect-rejected')) {
window.setTimeout(function () { window.location.reload(); }, 1500);
}
};
new MutationObserver(reloadOnRejectedCircuit)
.observe(modal, { attributes: true, attributeFilter: ['class'] });
}
};