import {Component, Input, Output, EventEmitter, ElementRef, ViewChild} from "@angular/core"; @Component({ selector: "modal-header", template: `` }) export class ModalHeader { } @Component({ selector: "modal-content", template: `` }) export class ModalContent { } @Component({ selector: "modal-footer", template: `` }) export class ModalFooter { } @Component({ selector: "modal", template: ` ` }) export class Modal { // ------------------------------------------------------------------------- // Inputs // ------------------------------------------------------------------------- @Input() public modalClass: string; @Input() public closeOnEscape: boolean = true; @Input() public closeOnOutsideClick: boolean = true; @Input() public title: string; @Input() public hideCloseButton = false; @Input() public cancelButtonLabel: string; @Input() public submitButtonLabel: string; // ------------------------------------------------------------------------- // Outputs // ------------------------------------------------------------------------- @Output() public onOpen = new EventEmitter(false); @Output() public onClose = new EventEmitter(false); @Output() public onSubmit = new EventEmitter(false); // ------------------------------------------------------------------------- // Public properties // ------------------------------------------------------------------------- public isOpened = false; // ------------------------------------------------------------------------- // Private properties // ------------------------------------------------------------------------- @ViewChild("modalRoot") public modalRoot: ElementRef; private backdropElement: HTMLElement; // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- constructor() { this.createBackDrop(); } // ------------------------------------------------------------------------- // Lifecycle Methods // ------------------------------------------------------------------------- ngOnDestroy() { document.body.className = document.body.className.replace(/modal-open\b/, ""); if (this.backdropElement && this.backdropElement.parentNode === document.body) document.body.removeChild(this.backdropElement); } // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- open(...args: any[]) { if (this.isOpened) return; this.isOpened = true; document.body.appendChild(this.backdropElement); window.setTimeout(() => { this.modalRoot.nativeElement.focus(); this.onOpen.emit(args); }, 0); document.body.className += " modal-open"; } close(...args: any[]) { if (!this.isOpened) return; document.body.removeChild(this.backdropElement); document.body.className = document.body.className.replace(/modal-open\b/, ""); this.isOpened = false; this.onClose.emit(args); } // ------------------------------------------------------------------------- // Private Methods // ------------------------------------------------------------------------- public preventClosing(event: MouseEvent) { event.stopPropagation(); } private createBackDrop() { this.backdropElement = document.createElement("div"); this.backdropElement.classList.add("modal-backdrop"); this.backdropElement.classList.add("fade"); this.backdropElement.classList.add("in"); } }