import { ViewContainerRef, Injector, Compiler, ComponentRef, ReflectiveInjector, Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { WptDialog } from '@arrow/warpaint/dialog'; // import {AppModule} from '../app.module'; @Injectable() export class ModalService { private viewContainerRef: ViewContainerRef; private injector: Injector; private text: any; //TODO: Refactor this into a tooltip/infoModal service private setInfoModalConfigSource = new Subject(); private closeAllInfoModalsSource = new Subject(); private tooltipConfigChangedSource = new Subject(); private showMessageSource = new Subject(); private hideMessagesSource = new Subject(); private noModalConfirmationSource = new Subject(); setInfoModalConfig$ = this.setInfoModalConfigSource.asObservable(); closeAllInfoModals$ = this.closeAllInfoModalsSource.asObservable(); tooltipConfigChanged$ = this.tooltipConfigChangedSource.asObservable(); showMessage$ = this.showMessageSource.asObservable(); hideMessages$ = this.hideMessagesSource.asObservable(); noModalConfirmation$ = this.noModalConfirmationSource.asObservable(); constructor(private compiler: Compiler, private dialog: WptDialog) {} showMessage(data?: any) { this.showMessageSource.next(data); } // Closes all messages hideMessages() { this.hideMessagesSource.next(); } noModalConfirmation() { this.noModalConfirmationSource.next(); } /** * Sets the toolitp config * @param config */ setTooltipConfig(config: any) { this.tooltipConfigChangedSource.next(config); } /** * Sets info modal x & y position coordinates + VISIBILITY * @param position Object { x: ***, y: ***, isOpen: true/false, item: item data } */ setInfoModalConfig(config: any) { this.setInfoModalConfigSource.next(config); } /** * closes all info modals */ closeAllInfoModals() { this.closeAllInfoModalsSource.next(); } /** * Registers the modal placeholder ViewContainerRef with the service. * @param vcRef = ViewContainerRef to the modal placeholder element. */ regiterViewContainerRef(vcRef: ViewContainerRef): void { this.viewContainerRef = vcRef; } /** * Registers the modal placeholder Injector with the service. * @param injector = Injector for modal placeholder element. */ registerInjector(injector: Injector) { this.injector = injector; } /** * Dynamically creates a modal instance that will then be injected into the placeholder * @param component - the modal component type to be created. * @param parameters - an optional object that will be assigned to the component instance * after it is created. Allows params to be dynamically added to the modal instsance. * @returns an observable instance ref to the newly created modal. Calling code that * subscribes will be notified when the modal is created and passed a ref to the modal instance. */ // create(component: any, parameters: Object): Observable> { // // we return a stream so we can access the componentref // let componentRef$ = new Subject(); // // compile the component and create a component factory // this.compiler.compileModuleAndAllComponentsAsync(AppModule) // .then((factory: any) => { // // find the component factory // let componentFactory = factory.componentFactories // .filter((item: any) => item.componentType === component)[0]; // // the injector is required to enable DI in the component // const childInjector = ReflectiveInjector.resolveAndCreate([], this.injector); // // create the component instance // let componentRef = this.viewContainerRef.createComponent(componentFactory, 0, childInjector); // // // add text to parameters // parameters['text'] = this.text; // // // pass parameters to the instance // Object.assign(componentRef.instance, parameters); // // // add a destroy method to the modal instance // componentRef.instance['destroy'] = () => { // componentRef.destroy(); // }; // // // update subscribers with the componentRef and complete the strem // componentRef$.next(componentRef); // componentRef$.complete(); // }); // // return >> componentRef$.asObservable(); // } create(component: any, parameters: any): any { return this.dialog.open(component, { data: parameters }); } }