UNPKG

simpa

Version:

Lightweight library for prototyping Single Page Applications.

93 lines (92 loc) 3.01 kB
/** * Base class for all components. */ export declare class Component { /** * Unique component identifier, initialized during component construction. */ private readonly _componentId; /** * HTML element that is the root element for a component. */ private readonly _componentRoot; /** * Creates a new component. * * @param className Name of the class for styling the root element of the component. * @param tagName Name of the HTML tag that is the root element for this component. */ constructor(className: string, tagName?: string); /** * Returns the identifier of the component. * * @return Component identifier. */ get componentId(): string; /** * Returns the HTML root element for this component. * * @return Root element for this component. */ get componentRoot(): HTMLElement; /** * Creates the content of this component basing on the template. */ doCreate(): void; /** * Builds the DOM structure for this component. * Derived classes should buildNumber the DOM structure for * the whole content that constitutes the component. * * @return HTML element that is the root of the component. */ doBuild(): void; /** * Initializes the component. * Derived classes may process additional initialization * in this method which is called when the DOM tree * is already fully constructed. */ doInit(): void; /** * Prepares the HTML template for a component. * Derived classes may override this method * to prepare custom HTML template. * * @return HTML template for a component. */ doTemplate(): string; /** * Utility method for adding child elements to the root element of this component. * * @param child Child HTML element. */ protected appendChild(child: HTMLElement): void; /** * Initializes all properties with names ending with 'Id', * having type string and value equal to empty string. * Each component property that meets these criteria is * initialized with a unique identifier in form of UUID. */ private initializeIdentifiers; /** * Modifies HTML template for this component. * All identifier names that appear in 'id=' or 'for=' attributes * will be replaced with values assigned to component's properties with the name * defined in this attribute. * Only component properties with names ending with 'Id' are taken into consideration. * * Example: * * If there is <div> element defined in HTML template: * * <div id="componentId">Hello<div> * * then it's value will be replaced with UUID assigned to property named 'componentId': * * <div id="03cbcaa8-017f-47fc-a602-e6e34941c193">Hello<div> * * @param template Modified HTML template. */ private replaceIdentifiersInTemplate; }