mirror of
https://github.com/litruv/Docs-Viewer.git
synced 2026-07-24 10:46:09 +10:00
Split up monolith index.js into multiple files
This commit is contained in:
44
js/NavigationService.js
Normal file
44
js/NavigationService.js
Normal 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 });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user