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

90.9% Statements 50/55
83.33% Branches 10/12
84.61% Functions 11/13
90.74% Lines 49/54

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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                        1x 15x   15x   15x           15x   15x   15x       15x     15x   15x       15x     15x   15x 15x       2x         14x 14x         2x     2x         1x 1x         14x                   1x                       23x         2x 2x   2x 2x 2x             20x 19x   1x 1x 1x 1x 1x   1x 1x             11x       11x 11x 11x   11x             12x 1x     11x 11x 11x 11x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Portal } from '@angular/cdk/portal';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
import { inject, Injectable, PLATFORM_ID, 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);
 
  /** @internal */
  readonly collapsible = signal(false);
 
  private fullscreenSubject = new BehaviorSubject<boolean>(false);
  /**
   * Emits when fullscreen overlay mode is toggled.
   */
  readonly isFullscreen$ = this.fullscreenSubject.asObservable();
 
  // Body scroll management
  private bodyScrollLocks = 0;
  private originalBodyOverflow?: string;
  private readonly document = inject(DOCUMENT);
  private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
 
  /** 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;
  }
 
  /** Toggle fullscreen overlay mode. */
  toggleFullscreen(): void {
    this.fullscreenSubject.next(!this.fullscreenSubject.value);
  }
 
  /** Set fullscreen overlay mode. */
  setFullscreen(fullscreen: boolean): void {
    this.fullscreenSubject.next(fullscreen);
  }
 
  /** Indicate if side panel is in fullscreen overlay mode. */
  isFullscreen(): boolean {
    return this.fullscreenSubject.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;
  }
 
  /**
   * Request body scroll lock. Uses reference counting to handle multiple panels.
   */
  requestBodyScrollLock(): void {
    Iif (!this.isBrowser) {
      return;
    }
 
    if (this.bodyScrollLocks === 0) {
      this.originalBodyOverflow = this.document.body.style.overflow;
      this.document.body.style.overflow = 'hidden';
    }
    this.bodyScrollLocks++;
  }
 
  /**
   * Release body scroll lock. Restores scroll when no more locks exist.
   */
  releaseBodyScrollLock(): void {
    if (!this.isBrowser || this.bodyScrollLocks === 0) {
      return;
    }
 
    this.bodyScrollLocks--;
    if (this.bodyScrollLocks === 0 && this.originalBodyOverflow !== undefined) {
      this.document.body.style.overflow = this.originalBodyOverflow;
      this.originalBodyOverflow = undefined;
    }
  }
}