All files / si-map/components/si-map-tooltip si-map-tooltip.component.ts

100% Statements 19/19
84.61% Branches 22/26
100% Functions 6/6
100% Lines 15/15

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                                              73x                 24x 24x               24x     21x     24x 24x 24x                   8x 8x 12x   8x       12x 1x     11x      
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  inject,
  input,
  signal
} from '@angular/core';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
import { TOOLTIP_FEATURES_TO_DISPLAY } from '../../models/constants';
 
@Component({
  selector: 'si-map-tooltip',
  imports: [SiTranslatePipe],
  templateUrl: './si-map-tooltip.component.html',
  styleUrl: './si-map-tooltip.component.scss',
  changeDetection: ChangeDetectionStrategy.Default
})
export class SiMapTooltipComponent {
  /**
   * Cutoff text for tooltips, when cluster combines more than 4 features
   *
   * @defaultValue
   * ```
   * t(() =>$localize`:@@SI_MAPS.TOOLTIP_MORE_TEXT:and {{length}} more...`)
   * ```
   */
  readonly moreText = input(
    t(() => $localize`:@@SI_MAPS.TOOLTIP_MORE_TEXT:and {{length}} more...`)
  );
 
  /**
   * Adds the maximum length of a string thats allowed in a tooltip and if its longer it cuts off the rest and adds 3 dots at the end.
   * If set to -1, it will not cut off the string.
   * @defaultValue 50
   */
  readonly maxLabelLength = input(50);
 
  get nativeElement(): HTMLElement {
    return this.elementRef.nativeElement;
  }
 
  protected readonly hiddenEntries = signal(0);
  protected readonly tooltipLabels = signal<string[]>([]);
  private readonly elementRef = inject(ElementRef);
 
  /**
   * Method sets tooltip content. If parameter is of type
   * string, it will directly set it as tooltip content.
   * If it is array of string, it will render it as list of those strings.
   *
   * @param labels - array of strings or string itself
   */
  setTooltip(labels: string[] | string): void {
    const labelList = typeof labels === 'string' ? [labels] : labels.length > 1 ? labels : [];
    this.tooltipLabels.set(
      labelList.slice(0, TOOLTIP_FEATURES_TO_DISPLAY).map(label => this.getLabelText(label))
    );
    this.hiddenEntries.set(Math.max(labelList.length - TOOLTIP_FEATURES_TO_DISPLAY, 0));
  }
 
  private getLabelText(label: string): string {
    if (this.maxLabelLength() != -1 && label.length > this.maxLabelLength()) {
      return `${label.substring(0, this.maxLabelLength())}…`;
    }
 
    return label;
  }
}