All files / side-panel si-side-panel.service.ts

94.11% Statements 32/34
75% Branches 3/4
100% Functions 8/8
93.93% Lines 31/33

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                      1x 15x   15x   15x           15x   15x   15x       15x     2x         14x 14x         2x     2x         1x 1x         14x             21x         2x 2x   2x 2x 2x             20x 19x   1x 1x 1x 1x 1x   1x 1x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Portal } from '@angular/cdk/portal';
import { Injectable, signal } from '@angular/core';
import { BehaviorSubject, EMPTY, Observable, Subject } from 'rxjs';
 
@Injectable({
  providedIn: 'root'
})
export class SiSidePanelService {
  private contentSubject = new BehaviorSubject<Portal<any> | undefined>(undefined);
  /** @internal */
  readonly content$ = this.contentSubject.asObservable();
 
  private openSubject = new BehaviorSubject<boolean>(false);
  /**
   * Emits on side panel is open or close.
   *
   * @defaultValue this.openSubject.asObservable()
   */
  readonly isOpen$ = this.openSubject.asObservable();
 
  private tempContentSubject = new BehaviorSubject<Portal<any> | undefined>(undefined);
  /** @internal */
  readonly tempContent$ = this.tempContentSubject.asObservable();
 
  private tempContentClosed?: Subject<void>;
  /** @internal */
  readonly enableMobile = signal(false);
  /** Set or update displayed content. */
  setSidePanelContent(portal: Portal<any> | undefined): void {
    this.contentSubject.next(portal);
  }
 
  /** Open side panel. */
  open(): void {
    this.hideTemporaryContent();
    this.openSubject.next(true);
  }
 
  /** Close side panel. */
  close(): void {
    Iif (this.hideTemporaryContent()) {
      return;
    }
    this.openSubject.next(false);
  }
 
  /** Toggle side panel open/close. */
  toggle(): void {
    this.hideTemporaryContent();
    this.openSubject.next(!this.openSubject.value);
  }
 
  /** Indicate is side panel open. */
  isOpen(): boolean {
    return this.openSubject.value;
  }
 
  /**
   * Indicate that the side panel is open with temporary content.
   */
  isTemporaryOpen(): boolean {
    return !!this.tempContentSubject.value;
  }
 
  /** Show side panel temporary content, opening the side panel when necessary. */
  showTemporaryContent(portal: Portal<any> | undefined): Observable<void> {
    this.hideTemporaryContent();
    this.tempContentSubject.next(portal);
 
    if (portal) {
      this.tempContentClosed = new Subject();
      return this.tempContentClosed.asObservable();
    }
    return EMPTY;
  }
 
  /** Hide side panel temporary content, reverting to state before showing temporary content. */
  hideTemporaryContent(): boolean {
    if (!this.isTemporaryOpen()) {
      return false;
    }
    if (this.tempContentClosed) {
      const sub = this.tempContentClosed;
      this.tempContentClosed = undefined;
      sub.next();
      sub.complete();
    }
    this.tempContentSubject.next(undefined);
    return true;
  }
}