All files / dashboard/widgets si-widget-base.component.ts

88.23% Statements 15/17
80% Branches 8/10
100% Functions 4/4
88.23% Lines 15/17

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                                                      1x           206x           206x           206x           206x   206x 221x                 206x   206x       239x         20x         20x         206x 109x 109x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  ChangeDetectorRef,
  Component,
  computed,
  inject,
  input,
  OnChanges,
  OnInit,
  signal,
  SimpleChanges
} from '@angular/core';
 
/**
 * The SiWidgetBaseComponent<T> implements the timing for the skeleton loading
 * indicator of widgets. It supports a generic value input property that represents
 * the main value to be displayed by a widget. When the value is not set, the `showLoadingIndicator`
 * changes after the `initialLoadingIndicatorDebounceTime` delay to `true` and subclasses
 * should show the skeleton loading indicator.
 */
@Component({
  template: ''
})
export abstract class SiWidgetBaseComponent<T> implements OnInit, OnChanges {
  /**
   * The main value to be displayed. If no value is set,
   * the skeleton indicates the loading of the value. Disable
   * the automatic loading mechanism by setting `SiWidgetBodyBaseComponent.disableAutoLoadingIndicator`.
   */
  readonly value = input.required<T | undefined>();
  /**
   * Option to disable automatic start of skeleton loading indication.
   *
   * @defaultValue false
   */
  readonly disableAutoLoadingIndicator = input(false, { transform: booleanAttribute });
  /**
   * Input to start and stop the skeleton loading indication.
   *
   * @defaultValue false
   */
  readonly showLoadingIndicatorInput = input(false, {
    // eslint-disable-next-line @angular-eslint/no-input-rename
    alias: 'showLoadingIndicator',
    transform: booleanAttribute
  });
 
  private readonly showLoadingIndicatorInternal = signal<boolean | undefined>(false);
 
  readonly showLoadingIndicator = computed(() => {
    return this.showLoadingIndicatorInternal() ?? this.showLoadingIndicatorInput();
  });
 
  /**
   * Initial delay time in milliseconds before enabling loading indicator.
   * Only used once initially during construction.
   *
   * @defaultValue 300
   */
  readonly initialLoadingIndicatorDebounceTime = input(300);
 
  protected cdRef = inject(ChangeDetectorRef);
  protected loadingTimer?: ReturnType<typeof setTimeout>;
 
  ngOnChanges(changes: SimpleChanges): void {
    if (
      !this.disableAutoLoadingIndicator() &&
      !changes.value?.firstChange &&
      changes.value?.currentValue
    ) {
      Iif (this.loadingTimer) {
        clearTimeout(this.loadingTimer);
        this.loadingTimer = undefined;
      }
 
      this.showLoadingIndicatorInternal.set(false);
    }
  }
 
  ngOnInit(): void {
    if (!this.disableAutoLoadingIndicator() && !this.value()) {
      this.loadingTimer = setTimeout(() => {
        this.showLoadingIndicatorInternal.set(!this.value());
      }, this.initialLoadingIndicatorDebounceTime());
    }
  }
}