@bespunky/angular-zen
Version:
The Angular tools you always wished were there.
152 lines (150 loc) • 7.62 kB
TypeScript
import { BehaviorSubject } from 'rxjs';
import { EventEmitter } from '@angular/core';
import { AnyObject } from '@bespunky/typescript-utils';
import * as i0 from "@angular/core";
/**
* Holds data related with a router outlet event.
*
* @export
* @class RouterOutletEventData
*/
export declare class RouterOutletEventData {
readonly outletName: string;
/**
* Creates an instance of RouterOutletEventData.
*
* @param {string} outletName The name of the outlet which triggered the event. For the primary unnamed outlet, this will be angular's PRIMARY_OUTLET.
*/
constructor(outletName: string);
/**
* `true` if the event was triggered by the primary unnamed outlet; otherwise `false`.
*
* @readonly
* @type {boolean}
*/
get isPrimaryOutlet(): boolean;
}
/**
* Holds data related with component publishing triggered by outlet activation.
*
* @export
* @class ComponentPublishEventData
* @extends {RouterOutletEventData}
*/
export declare class ComponentPublishEventData extends RouterOutletEventData {
readonly changes: BehaviorSubject<AnyObject | null>;
/**
* Creates an instance of ComponentPublishEventData.
*
* @param {BehaviorSubject<AnyObject | null>} changes The observable used to track changes to the activated component of the triggering outlet.
* @param {string} outletName The name of the outlet which triggered the event. For the primary unnamed outlet, this will be angular's PRIMARY_OUTLET.
*/
constructor(changes: BehaviorSubject<AnyObject | null>, outletName: string);
/**
* The instance of the last component activated by the outlet which triggered the event.
* This will be null if the outlet has deactivated the component.
*
* @readonly
* @type {(AnyObject | null)}
*/
get componentInstance(): AnyObject | null;
}
/**
* Provides a publish bus for the currently rendered component.
*
* **Why?**
* Angular's router only provides the type of component being rendered for a specific route, but not the instance it has created for it.
* This service is a bridge which allows other services to get a hold of the instance of a currently rendered component.
*
* **How to use:**
* Use the [`publishComponent`](/directives/PublishComponentDirective.html) directive on your `<router-outlet>` element. This will hook into the outlet's `activate` event and pass
* the activated component to the bus service:
* @example
* <!-- Component template -->
* <router-outlet publishComponent name="header"></router-outlet>
* <router-outlet publishComponent ></router-outlet>
* <router-outlet publishComponent name="footer"></router-outlet>
*
* @export
* @class RouterOutletComponentBus
*/
export declare class RouterOutletComponentBus {
private _outletsState;
/**
* Gets a shallow clone of the current state outlet state.
*
* @readonly
* @type {(Map<string, AnyObject | null>)}
*/
get outletsState(): Map<string, AnyObject | null>;
/**
* A map of the currently instantiated components by outlet name.
* Users can either subscribe to changes, or get the current value of a component.
*
* The primary unnamed outlet component will be accessible via PRIMARY_OUTLET, but for scalability it is better to access it via the `instance()` method.
*
* @private
*/
private readonly components;
/**
* Emits whenever a router outlet marked with the `publishComponent` directive activates a component.
* When an outlet deactivates a component, the published component instance will be `null`.
*
* @type {EventEmitter<ComponentPublishEventData>}
*/
readonly componentPublished: EventEmitter<ComponentPublishEventData>;
/**
* Emits whenever a router outlet marked with the `publishComponent` directive is removed from the DOM.
*
* @type {EventEmitter<ComponentPublishEventData>}
*/
readonly componentUnpublished: EventEmitter<RouterOutletEventData>;
/**
* Publishes the instance of a currently activated or deactivated component by the specified outlet.
* When an outlet first publishes, this will create an observable for tracking the outlet's changes.
* The observable can be fetched using the `changes()` method.
* Following calls to publish a component by the same outlet will subscribers.
*
* The last published component of an outlet can be fetched using the `instance()` method.
*
* @param {AnyObject | null} instance The instance of the activated component. For publishing deactivation of a component pass `null`.
* @param {string} [outletName=PRIMARY_OUTLET] (Optional) The name of the outlet which activated or deactivated the component. The primary unnamed outlet will be used when not specified.
*/
publishComponent(instance: AnyObject | null, outletName?: string): void;
/**
* Notifies any subscribers to the outlet's changes observable that the outlet is being removed by completing
* the observable and removes the observable from the service.
*
* @param {string} [outletName=PRIMARY_OUTLET] (Optional) The name of the outlet to unpublish. The primary unnamed outlet will be used when not specified.
*/
unpublishComponent(outletName?: string): void;
/**
* Checks whether the outlet by the given name is present in the DOM and has already activated at least one component.
* This will be `true` even if the outlet currently has no active component (component is `null`).
*
* A `false` value can either mean the outlet hasn't been marked with `publishComponent`, or that the outlet is not currently rendered (not present in the DOM).
*
* When `true`, the user can subscribe to changes of that outlet through the `changes()` method.
*
* @param {string} [outletName=PRIMARY_OUTLET] (Optional) The name of the outlet to check. The primary unnamed outlet will be checked if no name is provided.
* @returns {boolean} `true` if the outlet has published a component at least once; otherwise `false`.
*/
isComponentPublished(outletName?: string): boolean;
/**
* Gets an observable which can be used to track changes to the activated component of the specified outlet.
* If the outlet is not rendered (present in the DOM), or hasn't been marked with `publishComponent`, this will be `null`.
*
* @param {string} [outletName=PRIMARY_OUTLET] (Optional) The name of the outlet to track changes for. The primary unnamed outlet will be used when not specified.
* @returns {(BehaviorSubject<AnyObject | null> | null)} An observable to use for tracking changes to the activated component for the specified outlet, or `null` if no such outlet exists.
*/
changes(outletName?: string): BehaviorSubject<AnyObject | null> | null;
/**
* Gets the current instance of the component created by the specified outlet.
*
* @param {string} [outletName=PRIMARY_OUTLET] (Optional) The name of the outlet to fetch the component instance for. If not provided, the primary unnamed outlet's component will be fetched.
* @returns {(AnyObject | null)} The instance of the component created by the specified outlet. If the outlet doesn't exist, or there is no component instance for the requested outlet, returns `null`.
*/
instance(outletName?: string): AnyObject | null;
static ɵfac: i0.ɵɵFactoryDeclaration<RouterOutletComponentBus, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<RouterOutletComponentBus>;
}