UNPKG

@osaedasia/oresume

Version:

A user-friendly library for generating complete Single Page Applications (SPAs)

129 lines (128 loc) 5.47 kB
import { QueryParamTypeMap } from "../domain/definitions/types/QueryParam"; import { IPageConfig } from "./page/interfaces/IPageConfig"; import { PublicPageDefinition } from "./page/types/PageDefinition"; /** * Configuration interface for the application setup. * @template RelativeUrl The type for relative URL paths in the application. */ interface AppConfig<RelativeUrl extends string> { /** * The default page URL to load when no specific route is matched. */ defaultPage: RelativeUrl; /** * Flag to enable URL-based routing in the application. */ urlBasedRouting?: boolean; } /** * Core application class managing page navigation and rendering. * Implements the Singleton pattern for application state management. * @template RelativeUrl The type for relative URL paths in the application. */ export declare class App<RelativeUrl extends string> { /** * Singleton instance of the application. */ private static _instance; /** * Application configuration settings. */ private readonly _config; /** * Unique identifier for this application instance. */ private readonly _uuid; /** * Manager handling page-related operations. */ private readonly _pageManager; /** * Default page instance used when no specific route is matched. */ private _defaultPage?; /** * The currently active page. */ private _currentPage?; /** * Creates a new App instance with the specified configuration. * @param {AppConfig<RelativeUrl>} config The application configuration. */ private constructor(); /** * Retrieves the application's root DOM node. * @returns {HTMLDivElement} The root DOM element for the application. * @throws {AppRootElementNotFoundError} When the application root element is not found in the DOM. */ private get _appNode(); /** * Gets or creates the singleton instance of the application. * @param {AppConfig<RelativeUrl>} config The application configuration. * @returns {App<RelativeUrl>} The singleton instance of the application. * @throws AppInitializedError When attempting to register a page with a duplicate normalized URL or when there is no registered pages. * @throws PageNotFoundError When the default provided url does not match a registered page. */ static getInstance<RelativeUrl extends string>(config: AppConfig<RelativeUrl>): App<RelativeUrl>; /** * Creates a navigation function for the specified URL type. * @returns {(url: RelativeUrl) => void} A function that navigates to the specified URL. * @throws AppNotInitializedError When there is no app instance. */ static navigate<RelativeUrl extends string>(url: RelativeUrl): void; /** * Creates a new page definition. * @template RelativeUrl The type representing the relative URL of the page. * @template TParamTypeMap The type map for query parameters. * @param {IPageConfig<RelativeUrl, TParamTypeMap>} config Configuration object for the page. * @returns {PublicPageDefinition<RelativeUrl, TParamTypeMap>} A public interface for interacting with the page. */ static createPage<RelativeUrl extends string, TParamTypeMap extends QueryParamTypeMap = never>(config: IPageConfig<RelativeUrl, TParamTypeMap>): PublicPageDefinition<RelativeUrl, TParamTypeMap>; /** * Registers multiple pages with the application and initiates the application start sequence. * @template RelativeUrl The type representing the relative URL of the pages. * @template TParamTypeMap The type map for query parameters. * @param {PublicPageDefinition<RelativeUrl, TParamTypeMap>[]} pages Array of page definitions to register. * @throws {AppInitializedError} When an invalid page definition is provided. */ addPages<TParamTypeMap extends QueryParamTypeMap>(pages: PublicPageDefinition<RelativeUrl, TParamTypeMap>[]): void; /** * Creates and configures the application's DOM container. * @returns {HTMLDivElement} The configured application container element. */ private _createAppContainer; /** * Configures the provided DOM element as the application container. * @param {HTMLDivElement} div The DOM element to configure. */ private _configureAppContainer; /** * Initializes the application's DOM structure. * @throws {AppInitializedError} When the body node is not found in the HTML. */ private _initializeDom; /** * Initializes the default page if not already set by finding it using the configured default page URL. */ private _initializeDefaultPage; /** * Starts the application by initializing the default page and setting up routing based on configuration. */ private _start; /** * Sets up URL-based routing by initializing hash-based navigation and event listeners. * Only processes if the current pathname is root ("/"). */ private _initializeRoutingFromUrl; /** * Renders the page corresponding to the current URL hash. * Falls back to default page if an error occurs during rendering. */ private _renderPageFromHash; /** * Renders the specified page by updating the DOM and managing event listeners. * @param {IPage<RelativeUrl, any>} page The page to render. */ private _renderPage; } export {};