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

93.47% Statements 43/46
50% Branches 5/10
91.66% Functions 11/12
93.02% Lines 40/43

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                                                                                                                            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 2x   3x 3x     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,
  inject,
  input,
  OnInit,
  output,
  signal
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
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,
  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 { SiSidePanelService } from './si-side-panel.service';
 
/**
 * 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,
    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()'
  }
})
export class SiSidePanelContentComponent implements OnInit {
  /**
   * @defaultValue false
   */
  readonly collapsible = input(false, { 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`));
 
  /**
   * Show a badge on the mobile drawer indicating a new alert or notification
   *
   * @defaultValue false
   */
  readonly showMobileDrawerBadge = input(false, { transform: booleanAttribute });
 
  /**
   * Output for search bar input
   */
  readonly searchEvent = output<string>();
 
  protected readonly isCollapsed = signal(false);
  protected readonly isExpanded = signal(true);
  protected readonly enableMobile = computed(() => this.service?.enableMobile() ?? false);
  protected readonly mobileSize = signal(false);
  protected readonly focusable = computed(
    () => !this.mobileSize() || !this.enableMobile() || !this.isCollapsed()
  );
  protected readonly icons = addIcons({ 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 service = inject(SiSidePanelService);
  private readonly breakpointObserver = inject(BreakpointObserver);
 
  private expandedTimeout: any;
 
  constructor() {
    const accordionHcollapse = inject(SiAccordionHCollapseService);
    this.service.isOpen$.pipe(takeUntilDestroyed(this.destroyRef)).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);
      }
      accordionHcollapse.hcollapsed.set(!state);
    });
    accordionHcollapse.open$
      .pipe(takeUntilDestroyed(this.destroyRef))
      .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);
      });
  }
 
  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();
    }
  }
}