import * as i0 from '@angular/core'; import { TemplateRef, Type, ViewRef, OnDestroy, OnInit, OnChanges, Injector, StaticProvider, ViewContainerRef, InjectionToken } from '@angular/core'; import { GetProperties, NgChanges } from '@odx/angular/utils'; import { Observable } from 'rxjs'; type DynamicTextContent = string | Observable; type DynamicContent = DynamicTextContent | TemplateRef | Type; type GetDynamicViewContext = Partial ? (T extends Type ? GetProperties : T extends TemplateRef ? C : { content: T; }) : never>; interface DynamicViewRef { detectChanges(): void; destroy(): void; getElement(): Element; update(context: GetDynamicViewContext): void; getContext(): GetDynamicViewContext; getView(): ViewRef | null; } declare class DynamicViewComponent implements OnDestroy { private readonly element; private readonly sanitizer; private currentSubscription; set content(value: DynamicTextContent); ngOnDestroy(): void; private setContent; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵcmp: i0.ɵɵComponentDeclaration; } /** * A directive that dynamically renders content based on the provided input. It supports rendering * Angular components, templates, and text content, allowing for dynamic and flexible content * management within Angular applications. * * @template T - A type parameter that extends `DynamicContent`, representing the type of content to be dynamically rendered. */ declare class DynamicViewDirective = DynamicContent> implements OnDestroy, OnInit, OnChanges { private readonly dynamicViewService; private readonly viewContainer; private readonly template; private viewRef; private contentViewRef; /** * The content to be dynamically rendered. This can be a component, template reference, or text content. * The content is wrapped in a `DynamicViewRef`, providing additional control over the dynamically rendered view. * * @type {T | DynamicViewRef | null} * @default null */ content?: T | DynamicViewRef | null; /** * An optional injector to be used for dynamically created components. * This allows for custom dependency injection * into dynamically rendered components. * * @type {Injector | undefined} */ injector?: Injector; /** * An optional context providing function for dynamically created views. This function returns the context * object to be passed to the dynamically rendered content, allowing for dynamic interaction with the content. * * @type {GetDynamicViewContext | undefined} */ context?: GetDynamicViewContext; ngOnInit(): void; ngOnChanges(changes: NgChanges>): void; ngOnDestroy(): void; /** * Retrieves the current `DynamicViewRef` associated with the rendered content, providing access * to the view reference for additional operations like updating context or manual destruction. * * @returns {DynamicViewRef | null} The current view reference for the dynamically rendered content, if available. */ getView(): DynamicViewRef | null; private render; private destroy; static ɵfac: i0.ɵɵFactoryDeclaration, never>; static ɵdir: i0.ɵɵDirectiveDeclaration, "ng-template[odxDynamicView]", never, { "content": { "alias": "odxDynamicView"; "required": false; }; "injector": { "alias": "odxDynamicViewInjector"; "required": false; }; "context": { "alias": "odxDynamicViewContext"; "required": false; }; }, {}, never, never, true, never>; } interface DynamicViewRenderingOptions { injector: Injector; providers?: StaticProvider[]; host?: ViewContainerRef | Element | null; } interface DynamicViewOptions extends DynamicViewRenderingOptions { context?: GetDynamicViewContext; projectableNodes?: Node[][]; } interface DynamicComponentOptions> extends DynamicViewOptions { component: T; } /** * The DynamicComponentRef class is a powerful utility for dynamically creating and managing * Angular components at runtime. It encapsulates the logic for component creation, input handling, * and lifecycle management, making it easier to work with dynamic components in complex applications. * * A reference to a dynamically created component, providing methods to manage its lifecycle, * detect changes, and update its inputs based on a context. * * @template T - The type of the component being managed. * @template C - The type of the context used to update the component's inputs. */ declare class DynamicComponentRef, C extends GetDynamicViewContext> implements DynamicViewRef { private readonly options; private readonly application; private componentRef; private componentInputs; constructor(options: DynamicComponentOptions); /** * Triggers change detection for the dynamically created component, ensuring that any updates to inputs * or internal state are reflected in the view. */ detectChanges(): void; /** * Destroys the dynamically created component, cleaning up resources and detaching it from the application view. */ destroy(): void; /** * Retrieves the native element associated with the dynamically created component's host view. * * @returns {Element} The native DOM element of the component. */ getElement(): Element; /** * Updates the component's inputs based on the provided context. Merges the new context with the existing * context and applies the combined context as inputs to the component. * * @param {C} context - The context containing new values for the component's inputs. */ update(context: C): void; /** * Retrieves the current context used for the component's inputs. * * @returns {GetDynamicViewContext} The current context object. */ getContext(): GetDynamicViewContext; /** * Retrieves the view reference associated with the dynamically created component. * * @returns {ViewRef | null} The view reference, or null if the component has not been created or has been destroyed. */ getView(): ViewRef | null; } interface DynamicTemplateOptions> extends DynamicViewOptions { template: T; } /** * A reference to a dynamically created template, offering methods to manage its lifecycle, * detect changes, and update its context. It encapsulates the logic for creating and manipulating * an `EmbeddedViewRef` from a given `TemplateRef`. * * @template T - The type of the `NgTemplateRef` being managed. */ declare class DynamicTemplateRef = TemplateRef> implements DynamicViewRef { private readonly options; private readonly application; private embeddedViewRef; constructor(options: DynamicTemplateOptions); /** * Triggers change detection on the embedded view created from the template, ensuring that any updates * to the context or internal state are reflected in the view. */ detectChanges(): void; /** * Destroys the embedded view associated with the template, cleaning up resources and detaching it from the * application view if necessary. */ destroy(): void; /** * Retrieves the native element associated with the template's embedded view's root node. * * @returns {Element} The native DOM element of the embedded view's root node, if available. */ getElement(): Element; /** * Updates the context of the embedded view based on the provided context object. Merges the new context * with the existing one and applies the combined context to the embedded view. * * @param {GetDynamicViewContext} context - The context containing new values for the template's view. */ update(context: GetDynamicViewContext): void; /** * Retrieves the current context used for the template's view. * * @returns {GetDynamicViewContext} The current context object. */ getContext(): GetDynamicViewContext; /** * Retrieves the view reference (`ViewRef`) associated with the dynamically created template's embedded view. * * @returns {ViewRef | null} The view reference, or null if the template has not been instantiated or has been destroyed. */ getView(): ViewRef | null; private create; } /** * A service for creating and managing dynamic views in an Angular application. * It supports rendering Angular components, templates, and text content dynamically, * allowing for flexible and dynamic content composition. */ declare class DynamicViewService { private readonly injector; private readonly defaultHost; /** * Creates and returns a reference to a dynamic view based on the provided content and options. * The method supports dynamic creation of components, template references, and text content, * providing a unified API for dynamic content rendering. * * @template T - The type of the content to be dynamically rendered. * @param {T} content - The content to be rendered dynamically. This can be a component class, * a template reference, or text content. * @param {Partial>} [options={}] - Optional configuration for the dynamic view creation, * including a custom injector, context, host element, and additional providers for the dynamic component's injector. * @returns {DynamicViewRef} A reference to the dynamically created view, encapsulating the rendered content * and providing methods for interaction and management. * @throws {Error} Throws an error if the content type is not supported. * @example * ```ts * // Create a dynamic view for a component * const componentRef = dynamicViewService.createView(MyComponent, { context: { ... } }); * * // Create a dynamic view for a template reference * const templateRef = dynamicViewService.createView(myTemplate, { context: { ... } }); * * // Create a dynamic view for text content * const textRef = dynamicViewService.createView('Dynamic text content', {}); * ``` */ createView(content: T, options?: Partial>): DynamicViewRef; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } /** * Prepares an array of node arrays suitable for projection into the `` slots * of a dynamically created Angular component based on its content selectors. * * The function inspects the component's content selectors and aligns the provided nodes * for projection according to these selectors. A selector of '*' indicates that the nodes * should be projected into this slot. If a content selector does not match '*', an empty * array is returned for that slot, indicating no nodes are projected into it. * * @param {Type} component - The component class for which the projectable nodes are being prepared. * @param {Node[]} nodes - The nodes intended for projection into the component's `` slots. * @returns {Node[][]} An array of node arrays, each corresponding to a `` slot in the component. * Each sub-array contains nodes to be projected into the respective `` slot, based on the component's content selectors. * * @example * ```ts * // Given a component with two `` slots, the first without a selector and the second with the selector ``, * const nodes = [document.createTextNode('Hello'), document.createElement('div')]; * const projectableNodes = createProjectableNodes(MyComponent, nodes); * // projectableNodes would be `[[TextNode, DivElement], []]` for a component with selectors ['*', '.selector-class'] * ``` */ declare function createProjectableNodes(component: Type, nodes: Node[]): Node[][]; /** * Type guard function to check if a given value is considered dynamic text content. * Dynamic text content can either be a plain string or an Observable. This function * facilitates type-safe handling of dynamic text content by checking the type of the value. * * @param {unknown} value - The value to be checked. * @returns {value is DynamicTextContent} Returns `true` if the value is a string or an Observable, * indicating it can be used as dynamic text content; otherwise, returns `false`. * * @example * ```ts * // Example usage of the isDynamicTextContent function * const textValue: unknown = getTextValue(); // This function may return either string or Observable * * if (isDynamicTextContent(textValue)) { * // The type of textValue is now narrowed to DynamicTextContent (string | Observable) * console.log('The value can be used as dynamic text content.'); * } else { * console.log('The value is not suitable as dynamic text content.'); * } * ``` */ declare function isDynamicTextContent(value: unknown): value is DynamicTextContent; /** * Type guard function to check if a given value is a reference to a dynamically created view. * This helps in distinguishing between dynamic view references (`DynamicComponentRef` or `DynamicTemplateRef`) * and other types of values, facilitating type-safe operations on dynamic views. * * @param {unknown} value - The value to check. * @returns {value is DynamicViewRef} `true` if the value is an instance of `DynamicComponentRef` * or `DynamicTemplateRef`, indicating it is a dynamic view reference; otherwise, `false`. * * @example * ```ts * // Given a value that could be a dynamic view reference or something else * const value: unknown = getSomeValue(); * * if (isDynamicView(value)) { * // TypeScript now knows `value` is a dynamic view reference, so you can access its methods safely * value.detectChanges(); * } else { * console.log('Value is not a dynamic view reference'); * } * ``` */ declare function isDynamicView(value: unknown): value is DynamicViewRef; interface DynamicViewContextWithImplicitTemplate { $implicit: TemplateRef; } /** * @internal * Injection token for the dynamic view context. */ declare const DYNAMIC_VIEW_CONTEXT: InjectionToken; /** * @internal * Injection token for the default host element for dynamic views. */ declare const ODX_DYNAMIC_VIEW_DEFAULT_HOST: InjectionToken; /** * @internal * Retrieves the default host for dynamic views. * * @returns {Element} The default host element for dynamic views. */ declare function getDynamicViewDefaultHost(): Element; export { DYNAMIC_VIEW_CONTEXT, DynamicComponentRef, DynamicTemplateRef, DynamicViewComponent, DynamicViewDirective, DynamicViewService, ODX_DYNAMIC_VIEW_DEFAULT_HOST, createProjectableNodes, getDynamicViewDefaultHost, isDynamicTextContent, isDynamicView }; export type { DynamicComponentOptions, DynamicContent, DynamicTemplateOptions, DynamicTextContent, DynamicViewContextWithImplicitTemplate, DynamicViewOptions, DynamicViewRef, DynamicViewRenderingOptions, GetDynamicViewContext };