@roussos/pathway-router
Version:
A lightweight client-side router that manages navigation, history, caching, and rendering of content within a container element.
100 lines (99 loc) • 3.01 kB
TypeScript
export default Pathway;
export type CacheState = {
title: string;
url: string;
content: HTMLElement;
};
export type HistoryState = {
title: string;
url: string;
state: Record | null;
scroll: number;
};
export type OnLoadingChangeCallback = (router: Pathway, state: boolean) => any;
export type OnNavigateCallback = (router: Pathway, url: string) => any;
export type OnBeforeLeaveCallback = (router: Pathway) => any;
export type OnBeforeRenderCallback = (router: Pathway) => any;
export type OnAfterRenderCallback = (router: Pathway) => any;
export type OnErrorCallback = (router: Pathway, error: ErrorEvent) => any;
export type PathwayOptions = {
containerSelector?: string | undefined;
preloadLinkSelector?: string | undefined;
excludeLinkSelector?: string | undefined;
defaultLinkSelector?: string | undefined;
historyStackSize?: number | undefined;
cacheCapacity?: number | undefined;
transitionDuration?: number | undefined;
updateRouterHistory?: boolean | undefined;
popstateEvent?: boolean | undefined;
clickEvent?: boolean | undefined;
mutationObserver?: boolean | undefined;
scrollRestoration?: boolean | undefined;
onNavigate?: OnNavigateCallback | undefined;
onLoadingChange?: OnLoadingChangeCallback | undefined;
onBeforeLeave?: OnBeforeLeaveCallback | undefined;
onBeforeRender?: OnBeforeRenderCallback | undefined;
onAfterRender?: OnAfterRenderCallback | undefined;
onError?: OnErrorCallback | undefined;
};
/**
*
* @param {PathwayOptions} params
*/
declare function Pathway(params: PathwayOptions): void;
declare class Pathway {
/**
*
* @param {PathwayOptions} params
*/
constructor(params: PathwayOptions);
options: PathwayOptions;
history: HistoryState[];
cache: Map<string, CacheState>;
linkElements: Map<HTMLAnchorElement, Function>;
scrolls: Map<string, number>;
isLoading: boolean;
container: HTMLElement;
mutation: {
observer: MutationObserver | null;
};
private initEvents;
/**
*
* @param {PointerEvent} event
*/
clickEventCallback(event: PointerEvent): void;
private isUnusableLink;
private addClickListeners;
private removeClickListeners;
private cacheContainerLinks;
private fetchLink;
private navigate;
private updateDocument;
private mutationHandler;
private waitFetch;
private cacheResponse;
private parseResponse;
private __get;
private __set;
/**
* LRU retrieval method
*
* @returns {[string, {title:string, url:string,content:HTMLElement }]}
*/
getLeastRecent(): [string, {
title: string;
url: string;
content: HTMLElement;
}];
/**
* MRU retrieval method
*
* @returns {[string, {title:string, url:string,content:HTMLElement }]}}
*/
getMostRecent(): [string, {
title: string;
url: string;
content: HTMLElement;
}];
}