modal.service.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { Injectable } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3. @Injectable({
  4. providedIn: 'root',
  5. })
  6. export class ModalService {
  7. private _modalSubject = new Subject<any>();
  8. modal$ = this._modalSubject.asObservable();
  9. private _closeModalSubject = new Subject<any>();
  10. closeModal$ = this._closeModalSubject.asObservable();
  11. private _resultSubject = new Subject<any>();
  12. result$ = this._resultSubject.asObservable();
  13. /**
  14. * Opens the modal with the specified component and provides the data as input for the component.
  15. * @param component The component to open in the modal.
  16. * @param data The data for the input of the component.
  17. */
  18. public openModal(component: any, data: any) {
  19. console.log('ModalService: openModal');
  20. this._modalSubject.next({ component, data });
  21. }
  22. /**
  23. * This function closes the modal and sends a result state and an optinal data object to the host component.
  24. * @param result The result state. Can be 'cancel', 'update' etc.
  25. * @param data The oprional data object that eg contains a new or modifed weapon object.
  26. */
  27. public handleModalClosing(result: any, data?: any) {
  28. //The host components subscribe to the result$ Observable to get the result of the modal
  29. this._resultSubject.next({ state: result, data: data });
  30. this._closeModalSubject.next('close');
  31. }
  32. }