projects/commons/src/lib/elements/dialog/components/dialog.component.ts
| encapsulation | ViewEncapsulation.None |
| selector | cmn-dialog |
| templateUrl | ./dialog.component.html |
Properties |
|
Methods |
|
HostListeners |
constructor(componentFactoryResolver: ComponentFactoryResolver)
|
||||||
|
Parameters :
|
| document:keydown.escape |
document:keydown.escape()
|
| Public _setComponent | ||||||
_setComponent(componentOrTemplateRef: ComponentType
|
||||||
|
Parameters :
Returns :
void
|
| Private actionHandler | ||||||||||||
actionHandler(action: "okHandler" | "cancelHandler", isDismiss: boolean)
|
||||||||||||
|
Parameters :
Returns :
void
|
| Public afterClosed |
afterClosed()
|
|
Returns :
Observable<boolean>
|
| Public animationDone | ||||||
animationDone(event: AnimationEvent)
|
||||||
|
Parameters :
Returns :
void
|
| Public backdropHandler |
backdropHandler()
|
|
Returns :
void
|
| Public cancelHandler |
cancelHandler()
|
|
Returns :
void
|
| Public dismiss | ||||||
dismiss(fromOkButton: boolean)
|
||||||
|
Parameters :
Returns :
void
|
| Public ngOnInit |
ngOnInit()
|
|
Returns :
void
|
| Public okHandler |
okHandler()
|
|
Returns :
void
|
| Private afterClosed$ |
Type : Subject<any>
|
Default value : new Subject()
|
| Public Readonly backdropButtonEl |
Type : ElementRef
|
Decorators :
@ViewChild('backdropButton')
|
| Public Readonly cancelButtonEl |
Type : ElementRef
|
Decorators :
@ViewChild('cancelButton')
|
| Public componentInstance |
Type : T
|
| Public Readonly componentSection |
Type : ViewContainerRef
|
Decorators :
@ViewChild('componentSection', {read: ViewContainerRef})
|
| Public config |
Type : IDialogConfig
|
| Public Readonly footerEl |
Type : ElementRef
|
Decorators :
@ViewChild('footer')
|
| Private fromOkButton |
Default value : false
|
| Public hasComponent |
Default value : false
|
| Public Readonly headerEl |
Type : ElementRef
|
Decorators :
@ViewChild('header')
|
| Public Readonly okButtonEl |
Type : ElementRef
|
Decorators :
@ViewChild('okButton')
|
| Public open |
Default value : false
|
| Public ready |
Default value : new EventEmitter<boolean>()
|
import { Observable, Subject } from 'rxjs';
import { animate, keyframes, style, transition, trigger } from '@angular/animations';
import {
Component, ComponentFactoryResolver, ElementRef, EventEmitter, HostListener, OnInit,
TemplateRef, ViewChild, ViewContainerRef, ViewEncapsulation
} from '@angular/core';
import { IDialogConfig } from '../interfaces';
interface ComponentType<T> {
new(...args: any[]): T;
}
@Component({
selector: 'cmn-dialog',
templateUrl: './dialog.component.html',
encapsulation: ViewEncapsulation.None,
animations: [
trigger('modalState', [
transition(
'void => active',
animate('200ms', keyframes([
style({ opacity: '0', top: '5%', offset: 0 }),
style({ opacity: '1', top: '0', offset: 1 })
]))
),
transition(
'active => inactive',
animate('200ms', keyframes([
style({ opacity: '1', offset: 0 }),
style({ opacity: '0', top: '5%', offset: 1 })
]))
)
])
]
})
export class DialogComponent<T> implements OnInit {
@ViewChild('header') public readonly headerEl: ElementRef;
@ViewChild('footer') public readonly footerEl: ElementRef;
@ViewChild('okButton') public readonly okButtonEl: ElementRef;
@ViewChild('cancelButton') public readonly cancelButtonEl: ElementRef;
@ViewChild('backdropButton') public readonly backdropButtonEl: ElementRef;
@ViewChild('componentSection', { read: ViewContainerRef }) public readonly componentSection: ViewContainerRef;
public open = false;
public componentInstance: T;
public hasComponent = false;
public config: IDialogConfig;
public ready = new EventEmitter<boolean>();
private fromOkButton = false;
private afterClosed$: Subject<any> = new Subject();
constructor(private readonly componentFactoryResolver: ComponentFactoryResolver) {}
public ngOnInit() {
this.open = true;
this.ready.next(true);
}
@HostListener('document:keydown.escape')
public keypress() {
this.backdropHandler();
}
public backdropHandler() {
if (this.config.hasBackdrop) {
this.dismiss(false);
}
}
public cancelHandler() {
this.actionHandler('cancelHandler');
}
public okHandler() {
this.actionHandler('okHandler', true);
}
public dismiss(fromOkButton: boolean) {
this.open = false;
this.fromOkButton = fromOkButton;
}
public animationDone(event: AnimationEvent) {
if ((event as any).fromState === 'active' && (event as any).toState === 'inactive') {
this.afterClosed$.next(this.fromOkButton);
this.afterClosed$.complete();
}
}
public afterClosed(): Observable<boolean> {
return this.afterClosed$.asObservable();
}
public _setComponent(componentOrTemplateRef: ComponentType<T> | TemplateRef<T>) {
if (componentOrTemplateRef instanceof TemplateRef) {
this.componentSection.createEmbeddedView(componentOrTemplateRef);
} else {
const factory = this.componentFactoryResolver.resolveComponentFactory(componentOrTemplateRef);
const componentRef = this.componentSection.createComponent(factory);
this.componentInstance = componentRef.instance as T;
}
this.hasComponent = true;
}
private actionHandler(action: 'okHandler' | 'cancelHandler', isDismiss: boolean = false) {
if (this.config[action]) {
this.config[action]();
} else {
this.dismiss(isDismiss);
}
}
}
<div class="modal is-active">
<div class="modal-background"
(click)="backdropHandler()">
</div>
<div class="modal-card"
[@modalState]="open ? 'active' : 'inactive'"
(@modalState.done)="animationDone($event)"
[ngStyle]="{ 'width': config.width }">
<header class="modal-card-head"
*ngIf="config.title"
#header>
<p class="modal-card-title">
{{ config.title }}
</p>
<button *ngIf="config.hasBackdrop"
class="delete"
(click)="backdropHandler()"
#backdropButton>
</button>
</header>
<section *ngIf="config.loading"
class="modal-card-body">
loading ...
</section>
<section class="modal-card-body"
*ngIf="!config.loading && config.message"
[innerHTML]="config.message">
</section>
<section class="modal-card-body"
[ngClass]="{ 'is-hidden': !hasComponent || config.loading}">
<div #componentSection></div>
</section>
<footer class="modal-card-foot"
*ngIf="!config.loading && (config.cancelButtonText || config.okButtonText)"
#footer>
<button class="button is-medium is-danger"
*ngIf="config.cancelButtonText"
(click)="cancelHandler()"
#cancelButton>
{{ config.cancelButtonText }}
</button>
<button class="button is-medium is-primary"
*ngIf="config.okButtonText"
(click)="okHandler()"
#okButton>
{{ config.okButtonText }}
</button>
</footer>
</div>
</div>