Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x | import {
Directive,
ElementRef,
EventEmitter,
Input,
OnChanges,
OnDestroy,
SimpleChanges,
TemplateRef,
Type,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import {
IRenderedModel,
AngularDynamicOutput,
IBaseCustomEvent,
ICrudFormEvent,
} from './interfaces';
import { CrudEvent, FormParent, KeyValue } from './types';
import { NgxRenderingEngine } from './NgxRenderingEngine';
import { shareReplay, takeUntil } from 'rxjs';
import { NgxModelPageDirective } from './NgxModelPageDirective';
import { Model, ModelKeys } from '@decaf-ts/decorator-validation';
@Directive()
export class NgxRenderableComponentDirective
extends NgxModelPageDirective
implements OnChanges, OnDestroy, IRenderedModel
{
/**
* @description Reference to the container where the dynamic component will be rendered.
* @summary This ViewContainerRef provides the container where the dynamically created
* component will be inserted into the DOM. It's marked as static to ensure it's available
* during the ngOnInit lifecycle hook when the component is created.
*
* @type {ViewContainerRef}
* @memberOf NgxRenderableComponentDirective
*/
@ViewChild('componentOuter', { static: true, read: ViewContainerRef })
protected vcr!: ViewContainerRef;
@ViewChild('componentInner', { read: TemplateRef, static: true })
inner?: TemplateRef<unknown>;
/**
* @description Global properties to pass to the rendered component.
* @summary This input property allows passing a set of properties to the dynamically
* rendered component. These properties will be mapped to the component's inputs if they
* match. Properties that don't match any input on the target component will be filtered out
* with a warning.
*
* @type {Record<string, unknown>}
* @default {}
* @memberOf NgxComponentDirective
*/
@Input()
override globals: Record<string, unknown> = {};
/**
* @description Repository model for data operations.
* @summary The data model repository that this component will use for CRUD operations.
* This provides a connection to the data layer for retrieving and manipulating data.
*
* @type {Model| undefined}
* @memberOf NgxComponentDirective
*/
@Input()
parentForm!: FormParent | ElementRef<unknown> | undefined;
@Input()
projectable: boolean = true;
@Input()
rendererId?: string;
protected output?: AngularDynamicOutput;
protected instance!: KeyValue | undefined;
protected readonly JSON = JSON;
// @Output()
// listenEvent: EventEmitter<IBaseCustomEvent> =
// new EventEmitter<IBaseCustomEvent>();
/**
* @description Cleans up resources when the component is destroyed.
* @summary Performs cleanup operations when the component is being destroyed by Angular.
* This includes unsubscribing from all event emitters of the dynamic component and
* destroying the rendering engine instance to prevent memory leaks.
*
* @mermaid
* sequenceDiagram
* participant A as Angular Lifecycle
* participant C as ComponentRendererComponent
* participant R as NgxRenderingEngine
*
* A->>C: ngOnDestroy()
* alt component exists
* C->>C: unsubscribeEvents()
* C->>R: destroy()
* end
*
* @return {Promise<void>} A promise that resolves when cleanup is complete
* @memberOf NgxComponentDirective
*/
override async ngOnDestroy(): Promise<void> {
super.ngOnDestroy();
Iif (this.instance) {
this.unsubscribeEvents();
await NgxRenderingEngine.destroy();
}
this.output = undefined;
}
/**
* @description Lifecycle hook that is called when data-bound properties of a directive change
* @param {SimpleChanges} changes - Object containing changes
*/
override async ngOnChanges(changes: SimpleChanges): Promise<void> {
Iif (changes[ModelKeys.MODEL]) {
const { currentValue } = changes[ModelKeys.MODEL];
Iif (currentValue) {
this.render(currentValue);
}
}
}
/**
* @description Subscribes to events emitted by the dynamic component.
* @summary This method sets up subscriptions to all EventEmitter properties of the
* dynamically created component. When an event is emitted by the dynamic component,
* it is captured and re-emitted through the listenEvent output property with additional
* metadata about the event source.
*
* @mermaid
* sequenceDiagram
* participant C as ComponentRendererComponent
* participant D as Dynamic Component
* participant P as Parent Component
*
* C->>C: subscribeEvents()
* C->>D: Get instance properties
* loop For each property
* C->>C: Check if property is EventEmitter
* alt is EventEmitter
* C->>D: Subscribe to event
* D-->>C: Event emitted
* C->>P: Re-emit event with metadata
* end
* end
*
* @private
* @return {void}
* @memberOf NgxComponentDirective
*/
protected async subscribeEvents<M extends Model>(component?: Type<unknown>): Promise<void> {
Iif (!component) component = this?.output?.component;
Iif (!this.instance && component) this.instance = component;
Iif (this.instance && component) {
const componentKeys = Object.keys(this.instance);
for (const key of componentKeys) {
const value = this.instance[key];
Iif (value instanceof EventEmitter)
(this.instance as KeyValue)[key]
.pipe(shareReplay(1), takeUntil(this.destroySubscriptions$))
.subscribe(async (event: Event) => {
await this.handleEvent({
component: component.name || '',
name: key,
...event,
} as CrudEvent<M>);
});
}
}
}
/**
* @description Unsubscribes from all events of the dynamic component.
* @summary This method cleans up event subscriptions when the component is being destroyed.
* It iterates through all properties of the dynamic component instance and unsubscribes
* from any EventEmitter properties to prevent memory leaks and unexpected behavior after
* the component is destroyed.
*
* @mermaid
* sequenceDiagram
* participant C as ComponentRendererComponent
* participant D as Dynamic Component
*
* C->>C: unsubscribeEvents()
* C->>D: Get instance properties
* loop For each property
* C->>C: Check if property is EventEmitter
* alt is EventEmitter
* C->>D: Unsubscribe from event
* end
* end
*
* @private
* @return {void}
* @memberOf NgxComponentDirective
*/
protected unsubscribeEvents(): void {
Iif (this.instance) {
const componentKeys = Object.keys(this.instance);
for (const key of componentKeys) {
const value = this.instance[key];
Iif (value instanceof EventEmitter) this.instance[key].unsubscribe();
}
}
}
}
|