All files / app/services modal.service.ts

95.23% Statements 40/42
80% Branches 16/20
100% Functions 10/10
94.73% Lines 36/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                                  45x 45x 45x 45x 45x     45x 45x 45x             15x   15x           15x 15x   15x 15x               2x 2x 2x   2x             4x 4x 4x   4x             2x 2x 1x               2x 2x 1x         6x   6x 6x 6x 6x   6x         21x 21x                
import { Injectable, signal, computed, Type, ComponentRef } from '@angular/core';
 
export interface ModalConfig<T = unknown> {
  component: Type<unknown>;
  data?: T;
  closeOnBackdropClick?: boolean;
  closeOnEscape?: boolean;
}
 
export interface ModalRef<R = unknown> {
  close: (result?: R) => void;
  dismiss: () => void;
}
 
@Injectable({
  providedIn: 'root'
})
export class ModalService {
  private readonly _isOpen = signal(false);
  private readonly _config = signal<ModalConfig | null>(null);
  private _resolvePromise: ((value: unknown) => void) | null = null;
  private _cleanupTimer: ReturnType<typeof setTimeout> | null = null;
 
  // Public readonly signals
  readonly isOpen = this._isOpen.asReadonly();
  readonly config = this._config.asReadonly();
  readonly hasModal = computed(() => this._config() !== null);
 
  /**
   * Öffnet ein Modal mit der angegebenen Konfiguration
   * @returns Promise das resolved wenn das Modal geschlossen wird
   */
  open<T, R = unknown>(config: ModalConfig<T>): Promise<R | undefined> {
    this.clearCleanupTimer();
 
    const fullConfig: ModalConfig<T> = {
      closeOnBackdropClick: true,
      closeOnEscape: true,
      ...config
    };
 
    this._config.set(fullConfig as ModalConfig);
    this._isOpen.set(true);
 
    return new Promise<R | undefined>((resolve) => {
      this._resolvePromise = resolve as (value: unknown) => void;
    });
  }
 
  /**
   * Schließt das aktuelle Modal mit einem optionalen Result
   */
  close<R>(result?: R): void {
    Eif (this._resolvePromise) {
      this._resolvePromise(result);
      this._resolvePromise = null;
    }
    this._cleanup();
  }
 
  /**
   * Schließt das Modal ohne Result (abbrechen)
   */
  dismiss(): void {
    Eif (this._resolvePromise) {
      this._resolvePromise(undefined);
      this._resolvePromise = null;
    }
    this._cleanup();
  }
 
  /**
   * Handler für Backdrop-Click
   */
  onBackdropClick(): void {
    const config = this._config();
    if (config?.closeOnBackdropClick) {
      this.dismiss();
    }
  }
 
  /**
   * Handler für Escape-Taste
   */
  onEscapeKey(): void {
    const config = this._config();
    if (config?.closeOnEscape) {
      this.dismiss();
    }
  }
 
  private _cleanup(): void {
    this._isOpen.set(false);
    // Kurze Verzögerung für Animation (aber sicher für direktes Re-Open)
    this.clearCleanupTimer();
    this._cleanupTimer = setTimeout(() => {
      Eif (!this._isOpen()) {
        this._config.set(null);
      }
      this._cleanupTimer = null;
    }, 200);
  }
 
  private clearCleanupTimer(): void {
    Eif (this._cleanupTimer === null) {
      return;
    }
 
    clearTimeout(this._cleanupTimer);
    this._cleanupTimer = null;
  }
}