arch-repo-manager/srv/static/js/singlepage.js

75 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2022-01-23 01:56:17 +01:00
import * as GlobalStatusPage from './globalstatuspage.js';
import * as Utils from './utils.js';
export let sections = {};
export let sectionNames = [];
2021-01-25 00:24:31 +01:00
/// \brief 'main()' function which initializes the single page app.
2022-01-23 01:56:17 +01:00
export function initPage(pageSections)
2021-01-25 00:24:31 +01:00
{
2022-01-23 01:56:17 +01:00
sections = pageSections;
sectionNames = Object.keys(sections);
2021-01-25 00:24:31 +01:00
handleHashChange();
2022-01-23 01:56:17 +01:00
document.body.onhashchange = handleHashChange;
document.getElementById('logo-link').onclick = function () {
document.getElementById('about-dialog').style.display = 'block';
return false;
};
GlobalStatusPage.queryGlobalStatus();
2021-01-25 00:24:31 +01:00
}
2022-01-23 01:56:17 +01:00
let preventHandlingHashChange = false;
let preventSectionInitializer = false;
2021-01-25 00:24:31 +01:00
/// \brief Shows the current section and hides other sections.
function handleHashChange()
{
2022-01-23 01:56:17 +01:00
if (preventHandlingHashChange) {
2021-01-25 00:24:31 +01:00
return;
}
2022-01-23 01:56:17 +01:00
const hashParts = Utils.splitHashParts();
2021-01-25 00:24:31 +01:00
const currentSectionName = hashParts.shift() || 'global-section';
if (!currentSectionName.endsWith('-section')) {
return;
}
sectionNames.forEach(function (sectionName) {
const sectionData = sections[sectionName];
const sectionElement = document.getElementById(sectionName + '-section');
if (sectionElement.id === currentSectionName) {
const sectionInitializer = sectionData.initializer;
2022-01-23 01:56:17 +01:00
if (sectionInitializer === undefined || preventSectionInitializer || sectionInitializer(sectionElement, sectionData, hashParts)) {
2021-01-25 00:24:31 +01:00
sectionElement.style.display = 'block';
}
} else {
const sectionDestructor = sectionData.destructor;
if (sectionDestructor === undefined || sectionDestructor(sectionElement, sectionData, hashParts)) {
sectionElement.style.display = 'none';
}
}
const navLinkElement = document.getElementById(sectionName + '-nav-link');
if (sectionElement.id === currentSectionName) {
navLinkElement.classList.add('active');
} else {
navLinkElement.classList.remove('active');
}
});
}
2021-03-22 16:51:57 +01:00
/// \brief Updates the #hash without triggering the handler.
2022-01-23 01:56:17 +01:00
export function updateHashPreventingChangeHandler(newHash)
2021-03-22 16:51:57 +01:00
{
2022-01-23 01:56:17 +01:00
preventHandlingHashChange = true;
2021-03-22 16:51:57 +01:00
window.location.hash = newHash;
2022-01-23 01:56:17 +01:00
preventHandlingHashChange = false;
2021-03-22 16:51:57 +01:00
}
/// \brief Updates the #hash without triggering the section initializer.
2022-01-23 01:56:17 +01:00
export function updateHashPreventingSectionInitializer(newHash)
2021-03-22 16:51:57 +01:00
{
2022-01-23 01:56:17 +01:00
preventSectionInitializer = true;
2021-03-22 16:51:57 +01:00
window.location.hash = newHash;
2022-01-23 01:56:17 +01:00
preventSectionInitializer = false;
2021-03-22 16:51:57 +01:00
}