import { Scope } from '@aurelia/runtime'; import { CustomAttributeDefinition } from '../resources/custom-attribute'; import { CustomElementDefinition } from '../resources/custom-element'; import type { IContainer, IDisposable } from '@aurelia/kernel'; import type { INodeSequence, IRenderLocation } from '../dom'; import type { INode } from '../dom.node'; import type { ISSRScope, ISSRScopeChild, ISSRTemplateController } from './ssr'; import type { IInstruction, AttrSyntax } from '@aurelia/template-compiler'; import type { PartialCustomElementDefinition } from '../resources/custom-element'; import type { LifecycleHooksLookup } from './lifecycle-hooks'; import type { IViewFactory } from './view'; import { IBinding } from '../binding/interfaces-bindings'; export declare class Controller implements IController { container: IContainer; readonly vmKind: ViewModelKind; readonly definition: CustomElementDefinition | CustomAttributeDefinition | null; /** * The viewFactory. Only present for synthetic views. */ viewFactory: IViewFactory | null; /** * The physical host dom node. * * For containerless elements, this node will be removed from the DOM and replaced by a comment, which is assigned to the `location` property. * * For ShadowDOM elements, this will be the original declaring element, NOT the shadow root (the shadow root is stored on the `shadowRoot` property) */ host: HTMLElement | null; head: IHydratedController | null; tail: IHydratedController | null; next: IHydratedController | null; parent: IHydratedController | null; bindings: IBinding[] | null; children: Controller[] | null; scope: Scope | null; isBound: boolean; mountTarget: MountTarget; shadowRoot: ShadowRoot | null; nodes: INodeSequence | null; location: IRenderLocation | null; /** * SSR manifest scope for this controller. * - For CEs: ISSRScope with children array * - For TCs: ISSRTemplateController with views array * Set during hydration, consumed once in attaching(). */ ssrScope?: ISSRScopeChild; get lifecycleHooks(): LifecycleHooksLookup> | null; state: State; get isActive(): boolean; get name(): string; private logger; private debug; get viewModel(): ControllerBindingContext | null; set viewModel(v: ControllerBindingContext | null); get strict(): boolean | undefined; constructor(container: IContainer, vmKind: ViewModelKind, definition: CustomElementDefinition | CustomAttributeDefinition | null, /** * The viewFactory. Only present for synthetic views. */ viewFactory: IViewFactory | null, /** * The backing viewModel. Only present for custom attributes and elements. */ viewModel: ControllerBindingContext | null, /** * The physical host dom node. * * For containerless elements, this node will be removed from the DOM and replaced by a comment, which is assigned to the `location` property. * * For ShadowDOM elements, this will be the original declaring element, NOT the shadow root (the shadow root is stored on the `shadowRoot` property) */ host: HTMLElement | null, /** * The render location replacement for the host on containerless elements */ location: IRenderLocation | null); static getCached(viewModel: C): ICustomElementController | undefined; static getCachedOrThrow(viewModel: C): ICustomElementController; /** * Create a controller for a custom element based on a given set of parameters * * @param ctn - The own container of the custom element * @param viewModel - The view model object (can be any object if a definition is specified) * * Semi private API */ static $el(ctn: IContainer, viewModel: C, host: HTMLElement, hydrationInst: IControllerElementHydrationInstruction | null, definition?: CustomElementDefinition | undefined, location?: IRenderLocation | null, ssrScope?: ISSRScope | null): ICustomElementController; /** * Create a controller for a custom attribute based on a given set of parameters * * @param ctn - own container associated with the custom attribute object * @param viewModel - the view model object * @param host - host element where this custom attribute is used * @param flags - todo(comment) * @param definition - the definition of the custom attribute, * will be used to override the definition associated with the view model object contructor if given */ static $attr(ctn: IContainer, viewModel: C, host: HTMLElement, /** * The definition that will be used to hydrate the custom attribute view model * * If not given, will be the one associated with the constructor of the attribute view model given. */ definition?: CustomAttributeDefinition, /** * SSR manifest entry for template controllers. * Contains views array with nodeCount for DOM partitioning. */ ssrScope?: ISSRTemplateController): ICustomAttributeController; /** * Create a synthetic view (controller) for a given factory * * @param viewFactory - todo(comment) * @param flags - todo(comment) * @param parentController - the parent controller to connect the created view with. Used in activation * @param host - when it's desirable to associate a synthetic view with a host node during hydration, * it's possible to do so if a host is given here. * * Semi private API */ static $view(viewFactory: IViewFactory, parentController?: ISyntheticView | ICustomElementController | ICustomAttributeController | undefined, host?: HTMLElement | null): ISyntheticView; /** * Create a synthetic view (controller) that adopts existing DOM nodes. * * Used for SSR hydration of template controller views. Instead of cloning * from a template, the view wraps pre-existing DOM nodes. * * @param viewFactory - The view factory * @param parentController - Parent controller * @param adoptedNodes - Pre-existing DOM nodes to adopt * @param ssrScope - SSR manifest scope for nested hydration */ static $viewAdopted(viewFactory: IViewFactory, parentController: ISyntheticView | ICustomElementController | ICustomAttributeController | undefined, adoptedNodes: INodeSequence, ssrScope?: ISSRScope): ISyntheticView; private $initiator; activate(initiator: IHydratedController, parent: IHydratedController | null, scope?: Scope | null): void | Promise; private bind; deactivate(initiator: IHydratedController, _parent: IHydratedController | null): void | Promise; private removeNodes; private unbind; private $resolve; private $reject; private $promise; addBinding(binding: IBinding): void; addChild(controller: Controller): void; is(name: string): boolean; setHost(host: HTMLElement): this; setShadowRoot(shadowRoot: ShadowRoot): this; setLocation(location: IRenderLocation): this; release(): void; dispose(): void; accept(visitor: ControllerVisitor): void | true; } export type ControllerBindingContext = Required & Required> & C; /** * Describes the type of the host node/location of a controller * - `none` / 1: no host * - `host` / 2: an HTML element is the host of a controller * - `shadowRoot` / 3: a shadow root is the host of a controller * - `location` / 4: a render location is the location of a controller, this is often used for template controllers */ export declare const MountTarget: Readonly<{ none: 0; host: 1; shadowRoot: 2; location: 3; }>; export type MountTarget = typeof MountTarget[keyof typeof MountTarget]; export declare function isCustomElementController(value: unknown): value is ICustomElementController; export declare function isCustomElementViewModel(value: unknown): value is ICustomElementViewModel; declare const vmkSynth: "synthetic"; export type ViewModelKind = typeof vmkCe | typeof vmkCa | typeof vmkSynth; /** * A controller that is ready for activation. It can be `ISyntheticView`, `ICustomElementController` or `ICustomAttributeController`. * * In terms of specificity this is identical to `IController`. The only difference is that this * type is further initialized and thus has more properties and APIs available. */ export type IHydratedController = ISyntheticView | ICustomElementController | ICustomAttributeController; /** * A controller that is ready for activation. It can be `ICustomElementController` or `ICustomAttributeController`. * * This type of controller is backed by a real component (hence the name) and therefore has ViewModel and may have lifecycle hooks. * * In contrast, `ISyntheticView` has neither a view model nor lifecycle hooks (but its child controllers, if any, may). */ export type IHydratedComponentController = ICustomElementController | ICustomAttributeController; /** * A controller that is ready for activation. It can be `ISyntheticView` or `ICustomElementController`. * * This type of controller may have child controllers (hence the name) and bindings directly placed on it during hydration. * * In contrast, `ICustomAttributeController` has neither child controllers nor bindings directly placed on it (but the backing component may). * * Note: the parent of a `ISyntheticView` is always a `IHydratedComponentController` because views cannot directly own other views. Views may own components, and components may own views or components. */ export type IHydratedParentController = ISyntheticView | ICustomElementController; /** * A callback that is invoked on each controller in the component tree. * * Return `true` to stop traversal. */ export type ControllerVisitor = (controller: IHydratedController) => void | true; /** * The base type for all controller types. * * Every controller, regardless of their type and state, will have at least the properties/methods in this interface. */ export interface IController extends IDisposable { /** * The container associated with this controller. * By default, CE should have their own container while custom attribute & synthetic view * will use the parent container one, since they do not need to manage one */ readonly name: string; readonly container: IContainer; readonly vmKind: ViewModelKind; readonly definition: CustomElementDefinition | CustomAttributeDefinition | null; readonly host: HTMLElement | null; readonly state: State; readonly isActive: boolean; readonly parent: IHydratedController | null; readonly isBound: boolean; readonly bindings: readonly IBinding[] | null; addBinding(binding: IBinding): void; /** * Return `true` to stop traversal. */ accept(visitor: ControllerVisitor): void | true; } /** * The base type for `ICustomAttributeController` and `ICustomElementController`. * * Both of those types have the `viewModel` property which represent the user instance containing the bound properties and hooks for this component. */ export interface IComponentController extends IController { readonly vmKind: 'customAttribute' | 'customElement'; readonly definition: CustomElementDefinition | CustomAttributeDefinition; /** * The user instance containing the bound properties. This is always an instance of a class, which may either be user-defined, or generated by a view locator. */ readonly viewModel: C; } /** * The base type for `ISyntheticView` and `ICustomElementController`. * * Both of those types can: * - Have `children` which are populated during hydration (hence, 'Hydratable'). * - Have physical DOM nodes that can be mounted. */ export interface IHydratableController extends IController { readonly vmKind: 'customElement' | 'synthetic'; readonly mountTarget: MountTarget; readonly definition: CustomElementDefinition | null; readonly strict: boolean | undefined | null; readonly children: readonly IHydratedController[] | null; /** * SSR manifest scope for tree-based hydration. * Set during controller creation, consumed by TCs in attaching(). */ ssrScope?: ISSRScopeChild; addChild(controller: IController): void; } export declare const State: Readonly<{ none: 0; activating: 1; activated: 2; deactivating: 4; deactivated: 8; released: 16; disposed: 32; }>; export type State = typeof State[keyof typeof State]; export declare function stringifyState(state: State): string; /** * The controller for a synthetic view, that is, a controller created by an `IViewFactory`. * * A synthetic view, typically created when composing a template controller (`if`, `repeat`, etc), is a hydratable component with mountable DOM nodes that has no user view model. * * It has either its own synthetic binding context or is locked to some externally sourced scope (in the case of `au-compose`) */ export interface ISyntheticView extends IHydratableController { readonly vmKind: 'synthetic'; readonly definition: null; readonly viewModel: null; /** * The physical DOM nodes that will be appended during the attach operation. */ readonly nodes: INodeSequence; activate(initiator: IHydratedController, parent: IHydratedController, scope: Scope): void | Promise; deactivate(initiator: IHydratedController, parent: IHydratedController): void | Promise; /** * The scope that belongs to this view. This property will always be defined when the `state` property of this view indicates that the view is currently bound. * * The `scope` may be set during `activate()` and unset during `deactivate()` */ readonly scope: Scope; /** * Set the render location that this view will be inserted before. */ setLocation(location: IRenderLocation): this; /** * The DOM node that this view will be inserted before (if set). */ readonly location: IRenderLocation | null; /** * Set the host that this view will be appended to. */ setHost(host: Node & ParentNode): this; /** * The DOM node that this view will be appended to (if set). */ readonly host: HTMLElement | null; /** * Set the `ShadowRoot` that this view will be appended to. */ setShadowRoot(shadowRoot: ShadowRoot): this; /** * The ShadowRoot that this view will be appended to (if set). */ readonly shadowRoot: ShadowRoot | null; /** * Mark this view as not-in-use, so that it can either be disposed or returned to cache after finishing the deactivate lifecycle. * * If this view is cached and later retrieved from the cache, it will be marked as in-use again before starting the activate lifecycle, so this method must be called each time. * * If this method is *not* called before `deactivate()`, this view will neither be cached nor disposed. */ release(): void; } export interface ICustomAttributeController extends IComponentController { readonly vmKind: 'customAttribute'; readonly definition: CustomAttributeDefinition; /** * @inheritdoc */ readonly viewModel: C; readonly lifecycleHooks: LifecycleHooksLookup; /** * The scope that belongs to this custom attribute. This property will always be defined when the `state` property of this view indicates that the view is currently bound. * * The `scope` will be set during `activate()` and unset during `deactivate()`. * * The scope's `bindingContext` will be the same instance as this controller's `viewModel` property. */ readonly scope: Scope; readonly children: null; readonly bindings: null; /** * SSR manifest scope for template controllers during hydration. * Contains views array for repeat, if/else branches, etc. * Set during hydration, consumed once in attaching(). */ ssrScope?: ISSRScopeChild; activate(initiator: IHydratedController, parent: IHydratedController, scope: Scope): void | Promise; deactivate(initiator: IHydratedController, parent: IHydratedController): void | Promise; } /** * A representation of `IController` specific to a custom element whose `create` hook is about to be invoked (if present). * * It is not yet hydrated (hence 'dry') with any render-specific information. */ export interface IDryCustomElementController extends IComponentController, IHydratableController { readonly vmKind: 'customElement'; readonly definition: CustomElementDefinition; readonly strict: boolean | undefined | null; /** * The scope that belongs to this custom element. This property is set immediately after the controller is created and is always guaranteed to be available. * * It may be overwritten by end user during the `create()` hook. * * By default, the scope's `bindingContext` will be the same instance as this controller's `viewModel` property. */ scope: Scope; /** * The original host dom node. * * For containerless elements, this node will be removed from the DOM and replaced by a comment, which is assigned to the `location` property. * * For ShadowDOM elements, this will be the original declaring element, NOT the shadow root (the shadow root is stored on the `shadowRoot` property) */ readonly host: HTMLElement; } /** * A representation of `IController` specific to a custom element whose `hydrating` hook is about to be invoked (if present). * * It has the same properties as `IDryCustomElementController`, as well as a render context (hence 'contextual'). */ export interface IContextualCustomElementController extends IDryCustomElementController { } /** * A representation of `IController` specific to a custom element whose `hydrated` hook is about to be invoked (if present). * * It has the same properties as `IContextualCustomElementController`, except the context is now compiled (hence 'compiled'), as well as the nodes, and projector. */ export interface ICompiledCustomElementController extends IContextualCustomElementController { /** * The ShadowRoot, if this custom element uses ShadowDOM. */ readonly shadowRoot: ShadowRoot | null; /** * The renderLocation, if this is a `containerless` custom element. */ readonly location: IRenderLocation | null; /** * The physical DOM nodes that will be appended during the `mount()` operation. */ readonly nodes: INodeSequence; } /** * A fully hydrated custom element controller. */ export interface ICustomElementController extends ICompiledCustomElementController { /** * @inheritdoc */ readonly viewModel: C; readonly lifecycleHooks: LifecycleHooksLookup; activate(initiator: IHydratedController, parent: IHydratedController | null, scope?: Scope): void | Promise; deactivate(initiator: IHydratedController, parent: IHydratedController | null): void | Promise; } export declare const IController: import("@aurelia/kernel").InterfaceSymbol>; export declare const IHydrationContext: import("@aurelia/kernel").InterfaceSymbol>; export interface IHydrationContext { readonly controller: ICustomElementController; readonly instruction: IControllerElementHydrationInstruction | null; readonly parent: IHydrationContext | undefined; } export interface IActivationHooks { binding?(initiator: IHydratedController, parent: TParent): void | Promise; bound?(initiator: IHydratedController, parent: TParent): void | Promise; attaching?(initiator: IHydratedController, parent: TParent): void | Promise; attached?(initiator: IHydratedController): void | Promise; detaching?(initiator: IHydratedController, parent: TParent): void | Promise; unbinding?(initiator: IHydratedController, parent: TParent): void | Promise; dispose?(): void; /** * If this component controls the instantiation and lifecycles of one or more controllers, * implement this hook to enable component tree traversal for plugins that use it (such as the router). * * Return `true` to stop traversal. */ accept?(visitor: ControllerVisitor): void | true; } export interface ICompileHooks { define?(controller: IDryCustomElementController, /** * The context where this element is hydrated. * * This is created by the controller associated with the CE creating this this controller */ hydrationContext: IHydrationContext | null, definition: CustomElementDefinition): PartialCustomElementDefinition | void; hydrating?(controller: IContextualCustomElementController): void; hydrated?(controller: ICompiledCustomElementController): void; created?(controller: ICustomElementController | ICustomAttributeController): void; } /** * Defines optional lifecycle hooks that will be called only when they are implemented. */ export interface IViewModel { constructor: Function; readonly $controller?: IController; } export interface ICustomElementViewModel extends IViewModel, IActivationHooks, ICompileHooks { readonly $controller?: ICustomElementController; created?(controller: ICustomElementController): void; propertyChanged?(key: PropertyKey, newValue: unknown, oldValue: unknown): void; propertiesChanged?(changes: Record): void; } export interface ICustomAttributeViewModel extends IViewModel, IActivationHooks { readonly $controller?: ICustomAttributeController; link?(controller: IHydratableController, childController: ICustomAttributeController, target: INode, instruction: IInstruction): void; created?(controller: ICustomAttributeController): void; propertyChanged?(key: PropertyKey, newValue: unknown, oldValue: unknown): void; propertiesChanged?(changes: Record): void; } export interface IHydratedCustomElementViewModel extends ICustomElementViewModel { readonly $controller: ICustomElementController; } export interface IHydratedCustomAttributeViewModel extends ICustomAttributeViewModel { readonly $controller: ICustomAttributeController; } export interface IControllerElementHydrationInstruction { readonly projections: Record | null; /** * A list of captured attributes/binding in raw format */ readonly captures?: AttrSyntax[]; /** * Indicates whether the custom element was used with "containerless" attribute */ readonly containerless?: boolean; } export {}; //# sourceMappingURL=controller.d.ts.map