All files / side-panel si-side-panel-content.component.ts

86.56% Statements 58/67
47.61% Branches 10/21
85% Functions 17/20
87.3% Lines 55/63

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288                                                                                                                                          1x           5x                   5x             5x             5x             5x             5x                   5x 5x                     5x                   5x                   5x 5x                     5x 5x               5x         5x         5x         5x   5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x   5x         5x 5x 5x         5x   5x 5x     5x 5x 5x 5x 5x 2x   3x 3x         5x 5x         5x 5x                 5x       5x       5x                             1x       1x     1x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { BreakpointObserver } from '@angular/cdk/layout';
import {
  booleanAttribute,
  Component,
  computed,
  DestroyRef,
  effect,
  inject,
  input,
  OnInit,
  output,
  signal
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { SiAccordionHCollapseService } from '@siemens/element-ng/accordion';
import { MenuItem as MenuItemLegacy } from '@siemens/element-ng/common';
import {
  ContentActionBarMainItem,
  SiContentActionBarComponent
} from '@siemens/element-ng/content-action-bar';
import {
  addIcons,
  elementCancel,
  elementDoubleLeft,
  elementDoubleRight,
  SiIconComponent
} from '@siemens/element-ng/icon';
import { SiLinkDirective } from '@siemens/element-ng/link';
import { MenuItem } from '@siemens/element-ng/menu';
import { BOOTSTRAP_BREAKPOINTS } from '@siemens/element-ng/resize-observer';
import { SiSearchBarComponent } from '@siemens/element-ng/search-bar';
import { SiTranslatePipe, t, TranslatableString } from '@siemens/element-translate-ng/translate';
import { timer } from 'rxjs';
 
import { SiSidePanelService } from './si-side-panel.service';
import { SidePanelDisplayMode, SidePanelNavigateConfig } from './side-panel.model';
 
/**
 * An extension of MenuItem to support combined icons
 */
export interface StatusItem extends MenuItemLegacy {
  overlayIcon?: string;
}
 
@Component({
  selector: 'si-side-panel-content',
  imports: [
    SiContentActionBarComponent,
    SiIconComponent,
    SiLinkDirective,
    RouterLink,
    SiSearchBarComponent,
    SiTranslatePipe
  ],
  templateUrl: './si-side-panel-content.component.html',
  styleUrl: './si-side-panel-content.component.scss',
  providers: [SiAccordionHCollapseService],
  host: {
    '[class.collapsed]': 'isCollapsed()',
    '[class.expanded]': 'isExpanded()',
    '[class.enable-mobile]': 'enableMobile()',
    '[class.rpanel-fullscreen-overlay]': 'isFullscreen()'
  }
})
export class SiSidePanelContentComponent implements OnInit {
  /**
   * @deprecated This input is no longer used. The collapsible state is managed by the SiSidePanelService.
   * This input will be removed in a future major version.
   * @defaultValue undefined
   */
  readonly collapsibleInput = input(undefined, {
    // eslint-disable-next-line @angular-eslint/no-input-rename
    alias: 'collapsible',
    transform: booleanAttribute
  });
  /**
   * Header of side panel
   *
   * @defaultValue ''
   */
  readonly heading = input<TranslatableString>('');
 
  /**
   * Input list of primary action items
   *
   * @defaultValue []
   */
  readonly primaryActions = input<(MenuItemLegacy | ContentActionBarMainItem)[]>([]);
 
  /**
   * Input list of secondary action items.
   *
   * @defaultValue []
   */
  readonly secondaryActions = input<(MenuItemLegacy | MenuItem)[]>([]);
 
  /**
   * Status icons/actions
   *
   * @defaultValue []
   */
  readonly statusActions = input<StatusItem[]>([]);
 
  /**
   * Toggles search bar
   *
   * @defaultValue false
   */
  readonly searchable = input(false, { transform: booleanAttribute });
 
  /**
   * Placeholder text for search
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_SIDE_PANEL.SEARCH_PLACEHOLDER:Search...`)
   * ```
   */
  readonly searchPlaceholder = input(
    t(() => $localize`:@@SI_SIDE_PANEL.SEARCH_PLACEHOLDER:Search...`)
  );
 
  /**
   * Aria label for close button. Needed for a11y
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_SIDE_PANEL.CLOSE:Close`)
   * ```
   */
  readonly closeButtonLabel = input(t(() => $localize`:@@SI_SIDE_PANEL.CLOSE:Close`));
 
  /**
   * Toggle icon aria-label, required for a11y
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_SIDE_PANEL.TOGGLE:Toggle`)
   * ```
   */
  readonly toggleItemLabel = input(t(() => $localize`:@@SI_SIDE_PANEL.TOGGLE:Toggle`));
 
  /**
   * Enter fullscreen aria-label, required for a11y
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_SIDE_PANEL.ENTER_FULLSCREEN:Enter fullscreen`)
   * ```
   */
  readonly enterFullscreenLabel = input(
    t(() => $localize`:@@SI_SIDE_PANEL.ENTER_FULLSCREEN:Enter fullscreen`)
  );
 
  /**
   * Exit fullscreen aria-label, required for a11y
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_SIDE_PANEL.EXIT_FULLSCREEN:Exit fullscreen`)
   * ```
   */
  readonly exitFullscreenLabel = input(
    t(() => $localize`:@@SI_SIDE_PANEL.EXIT_FULLSCREEN:Exit fullscreen`)
  );
 
  /**
   * Show a badge on the mobile drawer indicating a new alert or notification
   *
   * @defaultValue false
   */
  readonly showMobileDrawerBadge = input(false, { transform: booleanAttribute });
 
  /**
   * Display mode for side panel - enables navigate or overlay functionality
   */
  readonly displayMode = input<SidePanelDisplayMode>();
 
  /**
   * Configuration for navigate mode
   */
  readonly navigateConfig = input<SidePanelNavigateConfig>();
 
  /**
   * Output for search bar input
   */
  readonly searchEvent = output<string>();
 
  protected readonly activatedRoute = inject(ActivatedRoute, { optional: true });
  protected readonly service = inject(SiSidePanelService);
  protected readonly isCollapsed = signal(false);
  protected readonly isExpanded = signal(true);
  protected readonly isFullscreen = signal(false);
  protected readonly enableMobile = computed(() => this.service?.enableMobile() ?? false);
  protected readonly collapsible = computed(() => {
    return this.collapsibleInput() ?? this.service?.collapsible() ?? false;
  });
  protected readonly mobileSize = signal(false);
  protected readonly focusable = computed(
    () => !this.mobileSize() || !this.enableMobile() || !this.isCollapsed()
  );
  protected readonly icons = addIcons({ elementCancel, elementDoubleLeft, elementDoubleRight });
  /**
   * The $rpanel-transition-duration in the style is 0.5 seconds.
   * For the animation we need to wait until the resize is done.
   */
  private readonly resizeAnimationDelay = 500;
  private readonly destroyRef = inject(DestroyRef);
  private readonly breakpointObserver = inject(BreakpointObserver);
 
  private expandedTimeout: any;
 
  constructor() {
    const accordionHcollapse = inject(SiAccordionHCollapseService);
 
    this.service.isFullscreen$.pipe(takeUntilDestroyed()).subscribe(fullscreen => {
      this.isFullscreen.set(fullscreen);
    });
 
    this.service.isOpen$.pipe(takeUntilDestroyed()).subscribe(state => {
      this.isCollapsed.set(!state);
      clearTimeout(this.expandedTimeout);
      this.expandedTimeout = undefined;
      if (!state) {
        this.isExpanded.set(false);
      } else {
        this.expandedTimeout = setTimeout(() => {
          this.isExpanded.set(true);
        }, this.resizeAnimationDelay / 2);
      }
    });
 
    effect(() => {
      Iif (this.collapsible()) {
        accordionHcollapse.hcollapsed.set(this.isCollapsed());
      }
    });
 
    effect(() => {
      Iif (this.isCollapsed() && !this.service.isTemporaryOpen() && this.isFullscreen()) {
        timer(this.resizeAnimationDelay)
          .pipe(takeUntilDestroyed(this.destroyRef))
          .subscribe(() => {
            this.service.setFullscreen(false);
          });
      }
    });
 
    accordionHcollapse.open$.pipe(takeUntilDestroyed()).subscribe(() => this.service.open());
  }
 
  ngOnInit(): void {
    this.breakpointObserver
      .observe('(max-width: ' + BOOTSTRAP_BREAKPOINTS.smMinimum + 'px)')
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(({ matches }) => {
        this.mobileSize.set(matches);
      });
  }
 
  /**
   * Toggle fullscreen overlay mode
   */
  toggleFullscreen(): void {
    Iif (this.isCollapsed() && !this.service.isTemporaryOpen()) {
      return;
    }
    this.service.toggleFullscreen();
  }
 
  protected toggleSidePanel(event?: MouseEvent): void {
    Iif (event?.detail !== 0) {
      // Blur except if triggered by keyboard
      (document?.activeElement as HTMLElement)?.blur();
    }
    Iif (this.service.isTemporaryOpen()) {
      this.service.hideTemporaryContent();
    } else {
      this.service.toggle();
    }
  }
}