Split up monolith index.js into multiple files

This commit is contained in:
2026-01-02 13:18:20 +11:00
parent 5764e41db3
commit 1d978e3244
10 changed files with 1460 additions and 1041 deletions

44
js/NavigationService.js Normal file
View File

@@ -0,0 +1,44 @@
/**
* Service responsible for handling navigation events and browser history.
* @class NavigationService
*/
export class NavigationService {
/**
* Creates a new NavigationService instance.
* @param {EventBus} eventBus - The event bus for communication.
* @param {DocumentService} documentService - The document service.
*/
constructor(eventBus, documentService) {
/** @type {EventBus} */
this.eventBus = eventBus;
/** @type {DocumentService} */
this.documentService = documentService;
this.setupEventListeners();
}
/**
* Sets up browser history and internal link event listeners.
* @private
*/
setupEventListeners() {
window.addEventListener('popstate', async () => {
const search = window.location.search;
const hash = window.location.hash;
const slug = (search === '' || search === '?') ?
window._indexData.defaultPage :
search.replace(/^\?/, '').split('#')[0];
this.eventBus.emit('navigation:requested', { slug, hash });
});
document.addEventListener('click', async (e) => {
const target = e.target.closest('a[data-internal="true"]');
if (target) {
e.preventDefault();
const slug = target.href.split('?').pop();
history.pushState(null, '', `?${slug}`);
this.eventBus.emit('navigation:requested', { slug });
}
});
}
}