All files / status-toggle si-status-toggle.component.ts

91.66% Statements 88/96
73.46% Branches 36/49
88.88% Functions 24/27
93.25% Lines 83/89

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                                                                          3x           1x   7x         7x   7x     7x   7x 7x   7x 7x 7x     7x 7x   7x 7x 7x 7x     7x 4x         12x 6x 3x         7x         3x         6x         6x         3x       2x     2x 2x 2x                             2x 2x 2x   2x 2x     2x 2x 2x 2x   2x 2x       9x 9x 9x       6x 6x   6x 6x 6x       6x 6x 6x 6x 6x       9x 2x 2x 2x         4x 2x 2x   4x 4x         8x 8x       7x 8x             12x 2x     10x 10x 10x 10x   10x 10x 10x 10x   10x 2x 1x       10x 2x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { NgClass } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  ElementRef,
  forwardRef,
  input,
  model,
  OnChanges,
  OnDestroy,
  OnInit,
  output,
  signal,
  SimpleChanges,
  viewChild
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { isRTL, listenGlobal } from '@siemens/element-ng/common';
import { SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe } from '@siemens/element-translate-ng/translate';
 
import { StatusToggleItem } from './status-toggle.model';
 
@Component({
  selector: 'si-status-toggle',
  imports: [NgClass, SiIconComponent, SiTranslatePipe],
  templateUrl: './si-status-toggle.component.html',
  styleUrl: './si-status-toggle.component.scss',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      // eslint-disable-next-line @angular-eslint/no-forward-ref
      useExisting: forwardRef(() => SiStatusToggleComponent),
      multi: true
    }
  ],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiStatusToggleComponent implements ControlValueAccessor, OnInit, OnDestroy, OnChanges {
  /** List of status items. */
  readonly items = input.required<StatusToggleItem[]>();
  /**
   * Disabled state for the whole component.
   * @defaultValue false
   **/
  readonly disabled = input(false);
  /** Value of currently selected status item. */
  readonly value = model<string | number>();
 
  /** Emitted when an item is clicked. */
  readonly itemClick = output<string | number>();
 
  private readonly containerElement = viewChild.required<ElementRef>('container');
  private readonly draggableElement = viewChild.required<ElementRef>('draggable');
 
  private boundingX = 0;
  private x0 = 0;
  private offset0 = 0;
  private onChange?: (value: string | number) => void;
  private onTouched?: () => void;
  private unlistenDragEvents: (() => void)[] = [];
  private readonly internalDisabled = signal(false);
 
  protected readonly selectedIndex = signal<number | undefined>(undefined);
  protected readonly draggablePosition = signal('');
  protected readonly animated = signal(false);
  protected readonly isDisabled = computed(() => this.disabled() || this.internalDisabled());
 
  ngOnChanges(changes: SimpleChanges): void {
    if (changes.value || changes.values) {
      this.setValue(this.value(), true, false);
    }
  }
 
  ngOnInit(): void {
    const selectedIndex = this.items().findIndex(v => v.value === this.value());
    if (selectedIndex >= 0) {
      this.selectItem(selectedIndex, false, false);
    }
  }
 
  ngOnDestroy(): void {
    this.handleDragEnd();
  }
 
  /** @internal */
  writeValue(value: string | number): void {
    this.setValue(value, true, false);
  }
 
  /** @internal */
  registerOnChange(fn: (value: string | number) => void): void {
    this.onChange = fn;
  }
 
  /** @internal */
  registerOnTouched(fn: () => void): void {
    this.onTouched = fn;
  }
 
  /** @internal */
  setDisabledState(isDisabled: boolean): void {
    this.internalDisabled.set(isDisabled);
  }
 
  protected handleMouseDown(event: MouseEvent): void {
    Iif (this.isDisabled() || event.buttons !== 1) {
      return;
    }
    if (this.handleDragStart(event)) {
      this.unlistenDragEvents.push(listenGlobal('mousemove', e => this.handleDragMove(e)));
      this.unlistenDragEvents.push(listenGlobal('mouseup', e => this.handleDragEnd(e)));
    }
  }
 
  protected handleTouchStart(event: TouchEvent): void {
    Iif (event.touches.length !== 1 || this.disabled()) {
      return;
    }
    Iif (this.handleDragStart(event)) {
      this.unlistenDragEvents.push(listenGlobal('touchmove', e => this.handleDragMove(e), true));
      this.unlistenDragEvents.push(listenGlobal('touchend', e => this.handleDragEnd(e)));
    }
  }
 
  private handleDragStart(event: Event): boolean {
    const rect = this.containerElement().nativeElement.getBoundingClientRect();
    this.boundingX = rect.x;
    const draggable = this.draggableElement().nativeElement;
 
    this.offset0 = draggable.offsetLeft;
    this.x0 = this.getX(event);
 
    // adjust for click outside current selection
    const clickIndex = this.getUnderlyingIndex(this.x0, true);
    const currentIndex = this.getUnderlyingIndex(this.offset0);
    const factor = isRTL() ? -1 : 1;
    this.offset0 += (clickIndex - currentIndex) * draggable.offsetWidth * factor;
 
    this.handleDragMove(event);
    return true;
  }
 
  private handleDragEnd(event?: Event): void {
    this.arrangeToUnderlyingItem(event);
    this.unlistenDragEvents.forEach(handler => handler());
    this.unlistenDragEvents.length = 0;
  }
 
  private getOffset(event: Event, isStart = false): number {
    const containerWidth = this.containerElement().nativeElement.clientWidth;
    const draggableWidth = this.draggableElement().nativeElement.clientWidth;
 
    const dX = this.getX(event) - this.x0;
    const left = Math.min(Math.max(0, this.offset0 + dX), containerWidth - draggableWidth);
    return isStart && isRTL() ? containerWidth - draggableWidth - left : left;
  }
 
  private getUnderlyingIndex(offsetLeft: number, floor = false): number {
    const pos = offsetLeft / this.draggableElement().nativeElement.clientWidth;
    const element = floor ? Math.floor(pos) : Math.round(pos);
    const len = this.items().length;
    const index = isRTL() ? len - element - 1 : element;
    return Math.max(0, Math.min(len - 1, index));
  }
 
  private arrangeToUnderlyingItem(event?: Event): void {
    if (event && !this.isDisabled()) {
      const index = this.getUnderlyingIndex(this.getOffset(event));
      this.selectItem(this.items()[index].disabled ? this.selectedIndex() : index);
      this.onTouched?.();
    }
  }
 
  private handleDragMove(event: Event): void {
    if (event.cancelable) {
      event.preventDefault();
      event.stopPropagation();
    }
    this.draggablePosition.set(`${this.getOffset(event, true)}px`);
    this.animated.set(false);
  }
 
  private getX(event: Event): number {
    const clientX =
      (event as MouseEvent).clientX ?? (event as TouchEvent).changedTouches[0]?.clientX ?? '';
    return clientX - this.boundingX;
  }
 
  private setValue(val?: string | number, animated = true, emit = false): void {
    this.selectItem(
      this.items().findIndex(v => v.value === val),
      animated,
      emit
    );
  }
 
  protected selectItem(index: number | undefined, animated = true, emit = true): void {
    if (index === undefined || index === -1 || this.isDisabled()) {
      return;
    }
 
    const values = this.items();
    const value = values[index];
    if (!value.disabled) {
      const prevIndex = this.selectedIndex();
 
      this.value.set(value.value);
      this.animated.set(animated);
      this.selectedIndex.set(index);
      this.draggablePosition.set(`${(100 / values.length) * index}%`);
 
      if (emit && index !== prevIndex) {
        if (this.onChange) {
          this.onChange(this.value()!);
        }
      }
 
      if (emit) {
        this.itemClick.emit(this.value()!);
      }
    }
  }
}