All files / si-map/models grouping.model.ts

72.72% Statements 16/22
77.77% Branches 7/9
50% Functions 3/6
72.72% Lines 16/22

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                    2x         42x   42x 1x 1x   1x       1x 1x     41x 123x 123x     123x                                       18x 3x 3x   15x        
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { themeElement } from '../shared/themes/element';
import { ColorPalette } from './color-palette.type';
 
/**
 * Class for grouping functionality
 */
export class Grouping {
  private readonly _colors: string[];
  private readonly _palette?: ColorPalette;
 
  constructor(groupColors: Record<number, string> | ColorPalette = 'status') {
    this._colors = [];
 
    if (typeof groupColors === 'string') {
      const colorPalette = groupColors;
      this._colors = [colorPalette];
 
      Iif (colorPalette === 'element') {
        this._palette = colorPalette as ColorPalette;
        this._colors = themeElement.style().colorPalette.element;
      } else {
        this._palette = 'status';
        this._colors = themeElement.style().colorPalette.status;
      }
    } else {
      Object.keys(groupColors).forEach((key: string) => {
        const numKey = Number(key);
        Iif (numKey <= 0) {
          throw new Error('Key is out of range: keys should start from 1 and do not repeat');
        }
        this._colors[Number(key) - 1] = groupColors[numKey];
      });
    }
  }
 
  get colors(): string[] {
    return Object.values(this._colors);
  }
 
  get palette(): string | undefined {
    return this._palette;
  }
 
  get length(): number {
    return this._colors.length;
  }
 
  getColor(groupId: number): string {
    // Group has higher index than available colors,
    // cycle trough them.
    if (groupId > this._colors.length) {
      const index = groupId % this._colors.length;
      return this._colors[index];
    } else {
      return this._colors[groupId - 1];
    }
  }
}