Skip to content

Commit

Permalink
Merge pull request #14957 from vigalo/dynamicdialog-childcomponentref
Browse files Browse the repository at this point in the history
feat: Dynamic dialog. Child component reference to be able to
  • Loading branch information
cetincakiroglu authored Mar 22, 2024
2 parents e20391b + 67e1c46 commit bb64696
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/app/components/dynamicdialog/dialogservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ObjectUtils } from 'primeng/utils';
*/
@Injectable()
export class DialogService {
dialogComponentRefMap: Map<DynamicDialogRef, ComponentRef<DynamicDialogComponent>> = new Map();
dialogComponentRefMap: Map<DynamicDialogRef<any>, ComponentRef<DynamicDialogComponent>> = new Map();

constructor(private appRef: ApplicationRef, private injector: Injector, @Inject(DOCUMENT) private document: Document) {}
/**
Expand All @@ -22,12 +22,12 @@ export class DialogService {
* @returns {DynamicDialogRef} DynamicDialog instance.
* @group Method
*/
public open(componentType: Type<any>, config: DynamicDialogConfig): DynamicDialogRef {
public open<T>(componentType: Type<T>, config: DynamicDialogConfig): DynamicDialogRef<T> {
if (!this.duplicationPermission(componentType, config)) {
return null;
}

const dialogRef = this.appendDialogComponentToBody(config);
const dialogRef = this.appendDialogComponentToBody<T>(config, componentType);

this.dialogComponentRefMap.get(dialogRef).instance.childComponentType = componentType;

Expand All @@ -38,15 +38,15 @@ export class DialogService {
* @param {ref} DynamicDialogRef - DynamicDialog instance.
* @group Method
*/
public getInstance(ref: DynamicDialogRef) {
public getInstance(ref: DynamicDialogRef<any>) {
return this.dialogComponentRefMap.get(ref).instance;
}

private appendDialogComponentToBody(config: DynamicDialogConfig) {
private appendDialogComponentToBody<T>(config: DynamicDialogConfig, componentType: Type<T>): DynamicDialogRef<T> {
const map = new WeakMap();
map.set(DynamicDialogConfig, config);

const dialogRef = new DynamicDialogRef();
const dialogRef = new DynamicDialogRef<T>();
map.set(DynamicDialogRef, dialogRef);

const sub = dialogRef.onClose.subscribe(() => {
Expand Down Expand Up @@ -75,7 +75,7 @@ export class DialogService {
return dialogRef;
}

private removeDialogComponentFromBody(dialogRef: DynamicDialogRef) {
private removeDialogComponentFromBody(dialogRef: DynamicDialogRef<any>) {
if (!dialogRef || !this.dialogComponentRefMap.has(dialogRef)) {
return;
}
Expand Down
10 changes: 9 additions & 1 deletion src/app/components/dynamicdialog/dynamicdialog-ref.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Observable, Subject } from 'rxjs';
import { Output, EventEmitter, Type } from '@angular/core';
/**
* Dynamic Dialog instance.
* @group Components
*/
export class DynamicDialogRef {
export class DynamicDialogRef<ComponentType = any> {
constructor() {}
/**
* Closes dialog.
Expand Down Expand Up @@ -117,4 +118,11 @@ export class DynamicDialogRef {
* @group Events
*/
onMaximize: Observable<any> = this._onMaximize.asObservable();

/**
* Event triggered on child component load.
* @param {*} value - Chi.
* @group Events
*/
readonly onChildComponentLoaded = new Subject<ComponentType>();
}
1 change: 1 addition & 0 deletions src/app/components/dynamicdialog/dynamicdialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ export class DynamicDialogComponent implements AfterViewInit, OnDestroy {
viewContainerRef?.clear();

this.componentRef = viewContainerRef?.createComponent(componentType);
this.dialogRef.onChildComponentLoaded.next(this.componentRef!.instance);
}

moveOnTop() {
Expand Down

0 comments on commit bb64696

Please sign in to comment.