unicorn-components
Version:
<a target="_blank" href="https://getunicorn.io"><img src="https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2017/Jul/07/2615006260-5-nitsnetsstudios-ondemand-UNI_avatar.png" align="left"></a>
66 lines (58 loc) • 1.92 kB
text/typescript
import {
AfterContentInit,
Component,
ComponentRef,
ElementRef,
EventEmitter,
HostBinding,
HostListener,
Input,
Output,
Type,
} from '@angular/core';
export class UniModalComponent implements AfterContentInit {
component: Type<any>;
componentRef: ComponentRef<any>;
componentClass = true;
options = {};
loaded = new EventEmitter();
cancel = new EventEmitter();
submit = new EventEmitter();
leaving = false;
onKeyup(e: KeyboardEvent) {
if (e.key === 'Escape') { this.onCancel(); }
}
constructor(private elementRef: ElementRef) { }
ngAfterContentInit() { }
initContent(component: Type<any>, options) {
this.component = component;
this.options = options;
}
onComponentLoaded(componentRef: ComponentRef<any>) {
this.componentRef = componentRef;
if (this.componentRef.instance.submitModal) {
this.componentRef.instance.submitModal.subscribe(ev => this.onSubmit(ev));
}
if (this.componentRef.instance.cancelModal) {
this.componentRef.instance.cancelModal.subscribe(ev => this.onCancel());
}
if (this.componentRef.instance.initModalOptions) {
this.componentRef.instance.initModalOptions(this.options);
}
this.loaded.emit();
}
onCancel() {
this.leaving = true;
setTimeout(_ => this.cancel.emit(true), 200);
}
onSubmit(obj) {
this.leaving = true;
setTimeout(_ => this.submit.emit(obj), 200);
}
onClickOutside(event) { if (this.elementRef.nativeElement.contains(event.target)) { this.onCancel(); } }
}