All files / split si-split-part.component.ts

90.32% Statements 56/62
87.5% Branches 21/24
81.81% Functions 9/11
90.16% Lines 55/61

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                                                                              1x   79x       79x               79x             79x                           79x                   79x               79x             79x             79x           79x                                         79x   79x 79x     79x             79x     79x     79x                     79x     79x 363x 7x   356x             79x 46x 41x   5x   5x     79x   79x                         10x       79x 1x   78x     79x             99x 91x 91x 22x   69x                 7x 7x 6x 6x   7x 7x 7x       5x 1x 1x 1x         3x 1x 1x 1x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
/* eslint-disable @angular-eslint/prefer-output-emitter-ref */
import { NgTemplateOutlet } from '@angular/common';
import {
  booleanAttribute,
  Component,
  computed,
  ElementRef,
  EventEmitter,
  inject,
  Input,
  numberAttribute,
  OnChanges,
  Output,
  signal,
  SimpleChanges,
  TemplateRef
} from '@angular/core';
import { SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe, TranslatableString } from '@siemens/element-translate-ng/translate';
 
import { Action, CollapseTo, PartState, Scale, SplitOrientation } from './si-split.interfaces';
 
@Component({
  selector: 'si-split-part',
  imports: [NgTemplateOutlet, SiIconComponent, SiTranslatePipe],
  templateUrl: './si-split-part.component.html',
  styleUrl: './si-split-part.component.scss',
  // Signals cannot be used directly with @HostBinding. See: https://github.com/angular/angular/issues/53888#issuecomment-1888935225
  // Having every binding here for consistency.
  host: {
    '[class.is-collapsed]': 'collapsedState()',
    '[class.collapse-start]': 'collapseDirection === "start"',
    '[style.grid-area]': '"p-" + this.index'
  }
})
export class SiSplitPartComponent implements OnChanges {
  /** @defaultValue [] */
  @Input() actions: Action[] = [];
  /**
   * @defaultValue 'start'
   */
  @Input() collapseDirection: CollapseTo = 'start';
 
  /**
   * Sets the icon class that is used in the buttons of split parts to
   * collapse and uncollapse the parts.
   *
   * @defaultValue 'element-double-right'
   */
  @Input() collapseIconClass = 'element-double-right';
 
  /**
   * Collapse only to the given min size.
   *
   * @defaultValue false
   */
  @Input({ transform: booleanAttribute }) collapseToMinSize = false;
 
  @Input() headerTemplate?: TemplateRef<any>;
 
  /**
   * Sets the title of the split part header.
   */
  @Input() heading!: TranslatableString;
 
  /**
   * Minimum size in px.
   *
   * @defaultValue 0
   */
  @Input({ transform: numberAttribute }) minSize = 0;
 
  /**
   * When a split part is collapsed, the content gets hidden but it will
   * still remain within the DOM. If you want to remove and destroy the component
   * in collapsed mode and recreate it on un-collapse, set this property to
   * true.
   *
   * @defaultValue false
   */
  @Input({ transform: booleanAttribute }) removeContentOnCollapse = false;
 
  /**
   * Defines the behavior of the split part during scaling.
   * E.g. when set to `none`, the spit part will keep its current size even when the parent container grows or shrinks.
   *
   * @defaultValue 'auto'
   */
  @Input() scale: Scale = 'auto';
 
  /**
   * Defines if the collapse button of a split part is displayed. Default value is true.
   *
   * @defaultValue true
   */
  @Input({ transform: booleanAttribute }) showCollapseButton = true;
 
  /**
   * Defines if the header of the split part is visible. Default is true.
   *
   * @defaultValue true
   */
  @Input({ transform: booleanAttribute }) showHeader = true;
  /**
   * Aria label for collapse button. Needed for a11y
   *
   * @defaultValue 'collapse'
   */
  @Input() collapseLabel: TranslatableString = 'collapse';
  /**
   * An optional stateId to uniquely identify a component instance.
   * Required for persistence of ui state.
   */
  @Input() stateId?: string;
 
  /**
   * Expanded size in px.
   */
  @Input({ transform: numberAttribute }) size?: number;
 
  /**
   * This toggles the behavior when collapsing this split part.
   * If enabled, all split parts between this one and the end of the split in the respective direction will be collapsed if this part is collapsed.
   * If disabled, only this split part will be collapsed.
   *
   * The default value will change to false in v48.
   *
   * @defaultValue true
   */
  @Input({ transform: booleanAttribute }) collapseOthers = true;
 
  @Output() readonly collapseChanged = new EventEmitter<boolean>();
  @Output() readonly stateChange = new EventEmitter<PartState>();
 
  /** @internal */
  index = 0;
  /** @internal */
  before?: SiSplitPartComponent;
  /** @internal */
  after?: SiSplitPartComponent;
 
  /** @internal */
  readonly fractionalSize = signal<number | undefined>(undefined);
 
  /** @internal */
  readonly collapsedSize = signal(0);
 
  /** @internal */
  readonly collapsedState = signal(false);
 
  /**
   * Get collapsed state.
   * @returns True if the split part is collapsed, false is expanded.
   */
  get collapsed(): boolean {
    return this.collapsedState();
  }
 
  /** @internal */
  readonly expandedSize = signal<number | undefined>(undefined);
 
  /** @internal */
  readonly actualSize = computed(() => {
    if (this.collapsedState()) {
      return this.collapsedSize();
    }
    return this.expandedSize() ?? 0;
  });
 
  /** @internal */
  saveUIState!: () => void;
 
  /** @internal */
  readonly nextExpandedAfter = computed(() => {
    if (!this.collapsedState()) {
      return this as SiSplitPartComponent;
    }
    const nextExpanded: SiSplitPartComponent = this.after ? this.after.nextExpandedAfter() : this;
 
    return nextExpanded;
  });
 
  private elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
 
  protected headerContext: { $implicit: SiSplitPartComponent } = {
    $implicit: this
  };
 
  /** @defaultValue true */
  @Input({ transform: booleanAttribute }) set expanded(value: boolean) {
    this.collapsedState.set(!value);
    Iif (this.collapseOthers) {
      this.before?.refreshCollapseToStart();
      this.after?.refreshCollapsedToEnd();
    }
  }
  get expanded(): boolean {
    return !this.collapsedState();
  }
 
  ngOnChanges(changes: SimpleChanges): void {
    if (changes.collapseToMinSize && this.collapseToMinSize) {
      this.collapsedSize.set(this.minSize ?? 40);
    } else {
      this.collapsedSize.set(40); // 40px is default size of the header
    }
 
    Iif (changes.size) {
      this.expandedSize.set(this.size);
    }
  }
 
  /** @internal */
  refreshSizePx(orientation: SplitOrientation): void {
    if (!this.collapsedState()) {
      const rect = this.elementRef.nativeElement.getBoundingClientRect();
      if (orientation === 'vertical') {
        this.expandedSize.set(rect.height);
      } else {
        this.expandedSize.set(rect.width);
      }
    }
  }
 
  /**
   * Toggles the collapsed or expanded state.
   */
  toggleCollapse(): void {
    this.collapsedState.update(v => !v);
    if (this.collapseOthers) {
      this.before?.refreshCollapseToStart();
      this.after?.refreshCollapsedToEnd();
    }
    this.collapseChanged.emit(this.collapsedState());
    this.stateChange.emit({ expanded: this.expanded, size: this.actualSize() });
    this.saveUIState();
  }
 
  private refreshCollapsedToEnd(): void {
    if (this.before?.collapsedState() && this.before.collapseDirection === 'end') {
      this.collapsedState.set(true);
      this.collapseDirection = 'end';
      this.after?.refreshCollapsedToEnd();
    }
  }
 
  private refreshCollapseToStart(): void {
    if (this.after?.collapsedState() && this.after.collapseDirection === 'start') {
      this.collapsedState.set(true);
      this.collapseDirection = 'start';
      this.before?.refreshCollapseToStart();
    }
  }
}