UNPKG

@osaedasia/oresume

Version:

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

98 lines (97 loc) 3.94 kB
import { ParsedHtml } from "../domain/models/ParsedHtml"; export type ContentFunction<TAppRoute extends string> = (props: { navigate: (url: TAppRoute) => void; }) => ParsedHtml; /** * Represents a modular, reusable part of the DOM that supports dynamic content updates, * event management, and state observation using registered listeners and observers. */ export declare class Fragment<TAppRoute extends string> { /** * In-memory DOM container for efficient batch operations. */ private _virtualFragment; /** * Unique identifier for the fragment. */ private readonly _fragmentUUID; /** * Name identifier for the fragment. */ private readonly _fragmentName; /** * Collection of registered event listeners. */ private _registeredEvents; /** * Collection of registered observers. */ private _registeredObservers; /** * Creates a new Fragment instance. * @param {string} fragmentName Identifier name for the fragment. */ constructor(fragmentName: string); /** * Updates the fragment's content and resets all event listeners and observers. * @param {ParsedHtml} value The new HTML content in parsed format. */ set content(value: ParsedHtml | ContentFunction<TAppRoute>); /** * @returns {DocumentFragment} A deep clone of the virtual fragment. */ get fragment(): DocumentFragment; /** * Registers an event listener for specified elements within the fragment. * @template K Type extending keyof HTMLElementEventMap. * @param {string} selector CSS selector for target element(s). * @param {K} type DOM event type to listen for. * @param {EventListenerOrEventListenerObject} callback Event handler function. * @throws {ElementNotFoundInFragmentError} If there's no element in dom with the specified selector */ addEventListener<K extends keyof HTMLElementEventMap>(selector: string, type: K, callback: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any): void; /** * Removes all registered event listeners and unsubscribes from observers. */ detachAllEvents(): void; /** * Registers and activates all event listeners and observers. * @throws {ElementNotFoundInFragmentError} When target elements cannot be found with the selector. */ attachAllEvents(): void; /** * Prepares the virtual fragment with current observer states. * @throws {ElementNotFoundInFragmentError} When a required element is not found in the fragment. */ private _preLoadFragment; /** * Converts observer state to string representation. * @param {unknown} state State value to convert. * @returns {string} HTML string representation of the state. */ private _getObserverStateString; /** * Processes registered event listeners across fragment elements. * @param {HTMLElement | DocumentFragment} parent Container element for event delegation. * @param {Function} onFound Handler for found elements. * @param {Function} onNotFound Handler for missing elements. */ private _iterateListeners; /** * Creates and processes an HTML fragment with unique identifiers and pattern handlers. * @param {ParsedHtml} html HTML content and associated patterns. * @returns {DocumentFragment} Processed document fragment. */ private _createHTMLFragment; /** * Retrieves the active root element of the fragment. * @returns {HTMLElement | DocumentFragment} Active fragment root or virtual fragment. */ private _getActiveFragmentRoot; /** * Locates the fragment's root element in the DOM using its UUID. * @returns {HTMLElement} The fragment's root element. * @throws {FragmentRootNotFoundError} When the root element cannot be found in the DOM. */ private _findRootElementInDOMByUUID; }