import { Component, ComponentRef, EventEmitter, Input, OnInit, Output, Renderer, ViewChild, ViewContainerRef } from "@angular/core"; import {animate, style, transition, trigger} from "@angular/animations"; import {TitleIconDetails} from "./models/modal.model"; import {ModalType} from "./models/modal.type"; import {DomSanitizer, SafeHtml} from "@angular/platform-browser"; import {ModalCloseButtonComponent} from "./components/modalCloseButton/modal-close-button.component"; import {CustomModalButtonComponent} from "./components/modalButton/modal-button.component"; @Component({ selector: 'sdc-modal', templateUrl: './modal.component.html', animations: [ trigger('toggleBackground', [ transition('* => 1', [style({ opacity: 0 }), animate('.45s cubic-bezier(0.23, 1, 0.32, 1)')]), transition('1 => *', [animate('.35s cubic-bezier(0.23, 1, 0.32, 1)', style({ opacity: 0 }))]) ]), trigger('toggleModal', [ transition('* => 1', [style({ opacity: 0, transform: 'translateY(-80px)' }), animate('.45s cubic-bezier(0.23, 1, 0.32, 1)')]), transition('1 => *', [style({ opacity: 1, transform: 'translateY(0px)' }), animate('.35s ease-in-out', style({ opacity: 0, transform: 'translateY(-80px)' }))]) ]) ] }) export class ModalComponent implements OnInit { @Input() size: string; 'xl|l|md|sm|xsm'; @Input() title: string; @Input() titleIcon: TitleIconDetails; @Input() message: string; @Input() buttons: CustomModalButtonComponent[]; @Input() type: ModalType; @Input() testId: string; @Input() isDisabled: boolean; @Input() instanceRef: ComponentRef; // the component ref is injected to the component in order to destroy the componet from itself @Output() onClose : EventEmitter = new EventEmitter(); @ViewChild('modalCloseButton', {static: false}) set refCloseButton(_modalCloseButton: ModalCloseButtonComponent) { this.modalCloseButton = _modalCloseButton; } modalVisible: boolean; // Allows for custom component as body instead of simple message. @ViewChild('dynamicContentContainer', { read: ViewContainerRef, static: true }) dynamicContentContainer: ViewContainerRef; innerModalContent: ComponentRef; public calculatedTestId: string; public modalCloseButton: ModalCloseButtonComponent; public svgIconContentSafeHtml: SafeHtml; private infoSvg = ` `; private warningSvg = ``; private errorSvg = ` `; private successSvg = ``; private noSvg = ``; constructor(private renderer: Renderer, private domSanitizer: DomSanitizer) { this.modalVisible = true; } ngOnInit() { switch (this.type) { case ModalType.info: this.svgIconContentSafeHtml = this.domSanitizer.bypassSecurityTrustHtml(this.infoSvg); break; case ModalType.warning: this.svgIconContentSafeHtml = this.domSanitizer.bypassSecurityTrustHtml(this.warningSvg); break; case ModalType.error: this.svgIconContentSafeHtml = this.domSanitizer.bypassSecurityTrustHtml(this.errorSvg); break; case ModalType.success: this.svgIconContentSafeHtml = this.domSanitizer.bypassSecurityTrustHtml(this.successSvg); break; default: this.svgIconContentSafeHtml = this.domSanitizer.bypassSecurityTrustHtml(this.noSvg); } } public modalToggled = (toggleEvent: any) => { if (!toggleEvent.toState) { this.instanceRef.destroy(); } } public getCloseButton = (): ModalCloseButtonComponent => { return this.modalCloseButton; } public getButtonById = (id: string): CustomModalButtonComponent => { // Support ES5 // return this.buttons.find((button) => { return this.buttons.filter((button) => { return button.id && button.id === id; })[0]; } public getButtons = (): CustomModalButtonComponent[] => { return this.buttons; } public setButtons = (_buttons: CustomModalButtonComponent[]): void => { this.buttons = _buttons; } public getTitle = (): string => { return this.title; } public setTitle = (_title: string): void => { this.title = _title; } public hoverAnimation(evn: MouseEvent) { this.renderer.setElementClass(evn.target as HTMLElement, 'sdc-ripple-click__animated', true); // evn.taregt.classList.add('sdc-ripple-click__animated'); } public closeModal = (btnName : string): void => { this.onClose.emit(btnName); this.modalVisible = false; } public disabledModal = (isDisabled: boolean): void => { this.isDisabled = isDisabled; this.buttons.forEach((button: CustomModalButtonComponent) => { button.disabled = isDisabled; }); this.modalCloseButton.disabled = false; } }