nts-ng2-components
Version:
Paquete de componentes para Angular2 desarrollado por NITSNETS.
64 lines (56 loc) • 1.84 kB
text/typescript
import {
AfterContentInit,
Component,
ComponentRef,
ElementRef,
EventEmitter,
HostListener,
Input,
Output,
Type,
} from '@angular/core';
export class NtsModalComponent implements AfterContentInit {
component: Type<any>;
componentRef: ComponentRef<any>;
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;
}
componentLoaded(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(); } }
}