All files / tree-view si-tree-view-converter.service.ts

95.65% Statements 22/23
85.71% Branches 12/14
100% Functions 2/2
95.45% Lines 21/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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                  1x           103x             103x             103x   103x                   410x 377x 377x 10x   367x     33x 33x   410x               466x       466x   466x 4903x 4903x 1006x   4903x 89x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { inject, Injectable } from '@angular/core';
 
import { TreeItem } from './si-tree-view.model';
import { SiTreeViewService } from './si-tree-view.service';
 
/**
 * Provides the functionality to flatten the hierarchical tree structure into a flat one dimensional array; called flattenedTrees.
 * The TreeViewComponent uses this flattened trees (the array) to bind the contained tree/list items to the GUI into a list.
 * In addition the service provides the number of 'normal' list items and the number of group items.
 * Important:
 * The coupling to the TreeViewComponent is high, thus the service is provided only by the TreeViewComponent.
 */
@Injectable()
export class SiTreeViewConverterService {
  /**
   * The flattened tree array; filled upon calling the method 'fillFlattenedTree'
   *
   * @defaultValue []
   */
  flattenedTrees: TreeItem[] = [];
 
  /**
   * The number of group items of the flattened tree.
   *
   * @defaultValue 0
   */
  groupItemsCount = 0;
 
  /**
   * The total items count of the flattened tree.
   *
   * @defaultValue 0
   */
  itemsTotal = 0;
 
  private siTreeViewService = inject(SiTreeViewService);
 
  /**
   * Flattens the tree handed over as parameter.
   * Takes the mode of the tree into consideration:
   * In case of a flat tree: only the first level of the items parameter is copied into the flattenedTrees array.
   * In case of a grouped tree: only the first two levels are copied into the flattenedTrees array
   * In the other cases all tree nodes are copied into the flattenedTrees array.
   */
  fillFlattenedTree(items: TreeItem[], isFlatTree: boolean): void {
    if (isFlatTree === false) {
      this.flattenedTrees = [];
      if (this.siTreeViewService.groupedList) {
        this.fillFlattenedTreeRecursive(items, 2, 0);
      } else {
        this.fillFlattenedTreeRecursive(items);
      }
    } else {
      this.flattenedTrees = items;
      this.groupItemsCount = 0;
    }
    this.itemsTotal = items.length;
  }
 
  private fillFlattenedTreeRecursive(
    items: TreeItem[],
    noOfLevels?: number,
    stackLevel?: number
  ): void {
    Iif (noOfLevels !== undefined && stackLevel !== undefined && noOfLevels <= stackLevel) {
      return;
    }
 
    stackLevel = stackLevel ?? 0;
 
    for (const current of items) {
      this.flattenedTrees.push(current);
      if (this.siTreeViewService.isGroupedItem(current)) {
        this.groupItemsCount++;
      }
      if (current.state === 'expanded') {
        this.fillFlattenedTreeRecursive(current.children ?? [], noOfLevels, stackLevel + 1);
      }
    }
  }
}