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

100% Statements 36/36
91.66% Branches 11/12
100% Functions 9/9
100% Lines 36/36

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                                                                                1x       9x     9x     9x                   9x 9x                 9x 9x                 9x 9x           9x             9x                                                 9x   33x 33x 33x                                                 9x   4x 4x                 9x       9x   9x 9x 9x       9x 8x           2x       8x 4x 4x   4x 4x   8x       2x 1x   1x        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { NgClass } from '@angular/common';
import { booleanAttribute, Component, computed, input, model, OnChanges } from '@angular/core';
import { SiCardComponent } from '@siemens/element-ng/card';
import { AccentLineType } from '@siemens/element-ng/common';
import { ContentActionBarMainItem } from '@siemens/element-ng/content-action-bar';
import {
  addIcons,
  SiIconComponent,
  elementRight2,
  elementSortDown,
  elementSortUp
} from '@siemens/element-ng/icon';
import { Link, SiLinkDirective } from '@siemens/element-ng/link';
import { SiTranslateModule, t, TranslatableString } from '@siemens/element-translate-ng/translate';
 
import { SiWidgetBaseComponent } from '../si-widget-base.component';
import { SiListWidgetBodyComponent, SortOrder } from './si-list-widget-body.component';
import { SiListWidgetItem } from './si-list-widget-item.component';
 
/**
 * The `<si-list-widget>` supports an easy composition of links and actions
 * with support for skeleton loading indicator, wrapped in a card.
 */
@Component({
  selector: 'si-list-widget',
  imports: [
    NgClass,
    SiCardComponent,
    SiIconComponent,
    SiLinkDirective,
    SiListWidgetBodyComponent,
    SiTranslateModule
  ],
  templateUrl: './si-list-widget.component.html',
  host: { class: 'si-list-widget' }
})
export class SiListWidgetComponent
  extends SiWidgetBaseComponent<SiListWidgetItem[]>
  implements OnChanges
{
  protected readonly icons = addIcons({ elementRight2, elementSortDown, elementSortUp });
 
  /** List widget heading. */
  readonly heading = input<TranslatableString>();
 
  /** Optional footer link for the list widget */
  readonly link = input<Link>();
 
  /**
   * label for the "search placeholder" name
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LIST_WIDGET.SEARCH_PLACEHOLDER:Search...`)
   * ```
   */
  readonly searchPlaceholderLabel = input(
    t(() => $localize`:@@SI_LIST_WIDGET.SEARCH_PLACEHOLDER:Search...`)
  );
 
  /**
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LIST_WIDGET.SORT_ASCENDING:Sort ascending`)
   * ```
   */
  readonly sortAscendingLabel = input(
    t(() => $localize`:@@SI_LIST_WIDGET.SORT_ASCENDING:Sort ascending`)
  );
 
  /**
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_LIST_WIDGET.SORT_DESCENDING:Sort descending`)
   * ```
   */
  readonly sortDescendingLabel = input(
    t(() => $localize`:@@SI_LIST_WIDGET.SORT_DESCENDING:Sort descending`)
  );
 
  /**
   * Set `ASC` of ascending sorting, `DSC` for descending sorting and `undefined` for no sorting.
   */
  readonly sort = model<SortOrder>();
 
  /**
   * Enables the search functionality.
   *
   * @defaultValue false
   */
  readonly search = input(false, { transform: booleanAttribute });
 
  /**
   * Compare function of for two `SiListWidgetItem`s that is used to sort
   * the array of items. The default compares the item labels by using the
   * string `localeCompare()` function.
   *
   * @param a - first `SiListWidgetItem` of the comparison.
   *
   * @param b - second `SiListWidgetItem` of the comparison.
   *
   * @returns A value `< 0` if `a` is smaller, `> 0` if `b` is smaller, otherwise `0`.
   *
   * @defaultValue
   * ```
   * (
   *     a: SiListWidgetItem,
   *     b: SiListWidgetItem
   *   ) => {
   *     const aLabel = typeof a.label === 'object' ? a.label.title! : a.label;
   *     const bLabel = typeof b.label === 'object' ? b.label.title! : b.label;
   *     return aLabel.localeCompare(bLabel);
   *   }
   * ```
   */
  readonly compareFn = input<(a: SiListWidgetItem, b: SiListWidgetItem) => number | undefined>(
    (a: SiListWidgetItem, b: SiListWidgetItem) => {
      const aLabel = typeof a.label === 'object' ? a.label.title! : a.label;
      const bLabel = typeof b.label === 'object' ? b.label.title! : b.label;
      return aLabel.localeCompare(bLabel);
    }
  );
 
  /**
   * Filter function that is used to filter down the list items when the user enters
   * a term in the search bar. Default function.
   *
   * @param item - The item be checked if it matches the searchTerm.
   *
   * @param searchTerm - The string the user typed in the search bar.
   *
   * @returns `true` if the `searchTerm` matches the `item` and the `item` shall be kept in the list.
   *
   * @defaultValue
   * ```
   * (
   *     item: SiListWidgetItem,
   *     searchTerm: string
   *   ) => {
   *     const aLabel = typeof item.label === 'object' ? item.label.title! : item.label;
   *     return aLabel.toLowerCase().includes(searchTerm.toLowerCase());
   *   }
   * ```
   */
  readonly filterFn = input<(item: SiListWidgetItem, searchTerm: string) => boolean | undefined>(
    (item: SiListWidgetItem, searchTerm: string) => {
      const aLabel = typeof item.label === 'object' ? item.label.title! : item.label;
      return aLabel.toLowerCase().includes(searchTerm.toLowerCase());
    }
  );
 
  /**
   * The number of skeleton progress indication rows.
   *
   * @defaultValue 6
   */
  readonly numberOfLinks = input(6);
  /**
   * Optional accent line
   */
  readonly accentLine = input<AccentLineType>();
 
  protected readonly accentClass = computed(() => {
    const accentLine = this.accentLine();
    return accentLine ? 'accent-' + accentLine : '';
  });
 
  /** The menu item array used to display the sort button. */
  protected readonly sortAction = computed(() => {
    const sortAction: ContentActionBarMainItem[] = [
      {
        label: this.sortDescendingLabel(),
        type: 'action',
        icon: this.icons.elementSortUp,
        iconOnly: true,
        action: () => this.doSort()
      }
    ];
 
    if (this.sort() === 'ASC') {
      sortAction[0].label = this.sortDescendingLabel();
      sortAction[0].icon = this.icons.elementSortUp;
    } else {
      sortAction[0].label = this.sortAscendingLabel();
      sortAction[0].icon = this.icons.elementSortDown;
    }
    return sortAction;
  });
 
  private doSort(): void {
    if (this.sort() === 'ASC') {
      this.sort.set('DSC');
    } else {
      this.sort.set('ASC');
    }
  }
}