All files / tree-view si-tree-view.utils.ts

81.92% Statements 145/177
72.82% Branches 67/92
80% Functions 28/35
81.65% Lines 138/169

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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329                        1x   827x   460x   1x 55x         1x 7x 7x             1x 8x 8x       1x             1x 13x 8x 8x 8x 8x     13x 3x 3x                   1x         22x 19x 3x   3x 3x 1x   3x       1x 20x 20x 10x       1x 10x 10x 5x       1x             1x         3x 1x   2x 2x 2x 12x 12x 2x     2x 10x 10x     2x     1x 304x 300x   304x 137x       1x                   1x 29x 64x 29x 29x     1x 339x 339x 215x             1x             1x                   1x           1x     1x           11x 11x   11x 1x   11x 11x 1x 1x 1x   10x 7x 7x   3x     1x         269x 269x 2124x 2124x 2124x 2124x 150x         1x 2246x 2246x 2246x 2246x 2246x 2246x 2246x 2214x       1x 32x 7x 24x 24x         1x             12x     12x         2x 2x 10x             10x 2x 2x 2x 2x 8x       8x 8x     10x             1x 12x 4x 4x   4x 6x         6x 4x   6x 2x   6x 1x 1x 1x       3x 3x       3x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import type { CheckboxState, TreeItem, TreeItemFolderState } from './si-tree-view.model';
 
interface InBetweenState {
  startIsFirst: boolean;
  inBetween: boolean;
  done: boolean;
}
 
export const ROOT_LEVEL = -1;
 
export const childrenLoaded = (item: TreeItem): boolean => (item.children?.length ?? 0) > 0;
 
export const hasChildren = (item: TreeItem): boolean => item.state !== 'leaf';
 
export const removeUndefinedState = (state?: TreeItemFolderState): TreeItemFolderState =>
  state ?? 'collapsed';
 
/**
 * Collapses the tree item except for leaf items.
 */
export const collapse = (item: TreeItem): void => {
  if (item?.state !== 'leaf') {
    item.state = 'collapsed';
  }
};
 
/**
 * Expands the tree item except for leaf items.
 */
export const expand = (item: TreeItem): void => {
  if (item?.state !== 'leaf') {
    item.state = 'expanded';
  }
};
 
export const expandRecursive = (item: TreeItem): void => {
  Iif (item.parent) {
    expand(item.parent);
    expandRecursive(item.parent);
  }
};
 
export const boxClicked = (treeItem: TreeItem, inheritChecked: boolean): void => {
  if (treeItem.showCheckbox) {
    treeItem.checked = treeItem.checked === 'checked' ? 'unchecked' : 'checked';
    if (inheritChecked) {
      checkAllChildren(treeItem, treeItem.checked);
      updateCheckStateOfParents(treeItem);
    }
  }
  if (treeItem.showOptionbox && treeItem.checked === 'unchecked') {
    treeItem.parent?.children?.forEach(item => (item.checked = 'unchecked'));
    treeItem.checked = 'checked';
  }
};
 
/**
 * Changes the folder state to a new state based on the old state.
 * If a client calls this method, the same is responsible to load the children in case the new state is 'expanding'!!!
 *
 * @param deleteChildrenOnCollapse - If set to true, the children are deleted upon collapsing the item.
 */
export const doFolderStateChange = (
  item: TreeItem,
  deleteChildrenOnCollapse: boolean,
  newState?: string
): void => {
  if (item?.state === 'collapsed' && (!newState || newState === 'expanded')) {
    item.state = childrenLoaded(item) ? 'expanded' : 'expanding';
  } else Iif (item?.state === 'expanding' && (!newState || newState === 'collapsed')) {
    item.state = 'collapsed';
  } else if (item?.state === 'expanded' && (!newState || newState === 'collapsed')) {
    if (deleteChildrenOnCollapse) {
      item.children = [];
    }
    item.state = 'collapsed';
  }
};
 
export const enableCheckboxRecursive = (item: TreeItem, enable: boolean): void => {
  item.showCheckbox = enable;
  if (childrenLoaded(item)) {
    item.children?.forEach(child => enableCheckboxRecursive(child, enable));
  }
};
 
export const enableOptionboxRecursive = (item: TreeItem, enable: boolean): void => {
  item.showOptionbox = enable;
  if (childrenLoaded(item)) {
    item.children?.forEach(child => enableOptionboxRecursive(child, enable));
  }
};
 
export const parentCountRecursive = (item: TreeItem, stackLevel: number): number => {
  Iif (item?.parent && item.parent.level !== ROOT_LEVEL) {
    return parentCountRecursive(item.parent, stackLevel + 1);
  }
  return stackLevel;
};
 
export const selectItemsBetween = <T = any>(
  roots: TreeItem<T>[],
  start: TreeItem<T>,
  end: TreeItem<T>
): TreeItem<T>[] | undefined => {
  if (start === end) {
    return;
  }
  const foundItems: TreeItem<T>[] = [];
  const inBetweenState: InBetweenState = { startIsFirst: false, inBetween: false, done: false };
  for (const root of roots) {
    getItemsBetweenRecursive(foundItems, root, start, end, inBetweenState);
    if (inBetweenState.done) {
      break;
    }
  }
  foundItems.forEach(item => {
    if (item.selectable) {
      item.selected = true;
    }
  });
  return foundItems;
};
 
export const selectRecursive = (item: TreeItem, select: boolean): void => {
  if (item.selectable) {
    item.selected = select;
  }
  if (childrenLoaded(item)) {
    item.children?.forEach(child => selectRecursive(child, select));
  }
};
 
export const setBoxStateRecursive = (item: TreeItem, state: CheckboxState): void => {
  item.checked = state;
  Iif (childrenLoaded(item)) {
    item.children?.forEach(child => setBoxStateRecursive(child, state));
  }
};
 
/**
 * Sets the item to be active or not; all other items of the tree are reset.
 */
export const setActive = (item: TreeItem, active: boolean): void => {
  const getRoot = (treeItem: TreeItem): TreeItem =>
    treeItem.parent ? getRoot(treeItem.parent) : treeItem;
  resetActive(getRoot(item));
  item.active = active;
};
 
export const resetActive = (item: TreeItem): void => {
  item.active = false;
  if (childrenLoaded(item)) {
    item.children?.forEach(childItem => resetActive(childItem));
  }
};
 
/**
 * Sets the treeItem as selectable or not
 */
export const setSelectable = (item: TreeItem, selectable: boolean): void => {
  item.selectable = selectable;
  Iif (item.selected && !item.selectable) {
    item.selected = selectable;
  }
};
 
export const deleteItem = (item: TreeItem): void => {
  Iif (item.parent) {
    const idx = item.parent.children?.indexOf(item) ?? -1;
    Iif (idx < 0) {
      return;
    }
    item.parent.children?.splice(idx, 1);
  }
};
 
export const addChildItem = (
  parent: TreeItem,
  child: TreeItem,
  position?: number,
  callback?: (item: TreeItem) => void
): void => {
  addChildItems(parent, [child], position, callback);
};
 
export const addChildItems = (
  parent: TreeItem,
  children: TreeItem[],
  position?: number,
  callback?: (item: TreeItem) => void
): void => {
  const itemCount = parent.children?.length ?? 0;
  position ??= itemCount;
  // Negative position insert items counting from the last item
  if (position < 0) {
    position = itemCount + (position % itemCount);
  }
  initializeTreeItemsRecursive(children, parent, callback);
  if (!parent.children) {
    parent.children = [];
    parent.children.push(...children);
    return;
  }
  if (position >= parent.children.length) {
    parent.children.push(...children);
    return;
  }
  parent.children.splice(position, 0, ...children);
};
 
export const initializeTreeItemsRecursive = (
  items: TreeItem[],
  parent: TreeItem,
  callback?: (item: TreeItem) => void
): void => {
  const level = (parent.level ?? -1) + 1;
  for (const item of items) {
    item.parent = parent;
    item.level = level;
    setTreeItemDefaults(item, callback);
    if (item.children?.length) {
      initializeTreeItemsRecursive(item.children, item, callback);
    }
  }
};
 
export const setTreeItemDefaults = (item: TreeItem, callback?: (item: TreeItem) => void): void => {
  item.children ??= [];
  item.state ??= 'collapsed';
  item.active ??= false;
  item.checked ??= 'unchecked';
  item.selected ??= false;
  item.selectable ??= true;
  if (callback) {
    callback(item);
  }
};
 
const checkAllChildren = (treeItem: TreeItem, check: CheckboxState): void => {
  if (childrenLoaded(treeItem)) {
    treeItem.children?.forEach(item => {
      item.checked = check;
      checkAllChildren(item, check);
    });
  }
};
 
const getItemsBetweenRecursive = (
  found: TreeItem[],
  current: TreeItem,
  start: TreeItem,
  end: TreeItem,
  inBetweenState: InBetweenState
): void => {
  Iif (inBetweenState.done) {
    return;
  }
  if (current === start && !inBetweenState.inBetween) {
    // This is the first found tree item within the selection, the start tree item (the item which is clicked first) is ordered
    // before the end tree item (the item which is clicked second).
    // The recursive function is from now on handling tree node which are in between the two selections.
    // Note, we don't need to select it, as it is already selected with previous click
    inBetweenState.startIsFirst = true;
    inBetweenState.inBetween = true;
  } else Iif (current === end && !inBetweenState.inBetween) {
    // This is the first found tree item within the selection, the end tree item (the item which is clicked second) is ordered
    // before the start tree item (the item which is clicked first).
    // The recursive function is from now on handling tree node which are in between the two selections.
    inBetweenState.startIsFirst = false;
    inBetweenState.inBetween = true;
    found.push(current);
  } else if (current === end && inBetweenState.inBetween) {
    found.push(current);
    inBetweenState.inBetween = false;
    inBetweenState.done = true;
    return; // the end of the search
  } else Iif (current === start && inBetweenState.inBetween) {
    inBetweenState.inBetween = false;
    inBetweenState.done = true;
    return; // the end of the search
  } else if (inBetweenState.inBetween) {
    found.push(current);
  }
 
  Iif (current.state === 'expanded') {
    current.children?.forEach(item =>
      getItemsBetweenRecursive(found, item, start, end, inBetweenState)
    );
  }
};
 
const updateCheckStateOfParents = (treeItem: TreeItem): void => {
  if (treeItem.parent) {
    let allChecked = true;
    let allUnchecked = true;
 
    for (const item of treeItem.parent?.children ?? []) {
      Iif (item.checked === 'indeterminate') {
        treeItem.parent.checked = 'indeterminate';
        updateCheckStateOfParents(treeItem.parent);
        return;
      }
      if (item.checked === 'checked') {
        allUnchecked = false;
      }
      if (item.checked === 'unchecked') {
        allChecked = false;
      }
      if (!allChecked && !allUnchecked) {
        treeItem.parent.checked = 'indeterminate';
        updateCheckStateOfParents(treeItem.parent);
        return;
      }
    }
 
    if (allChecked) {
      treeItem.parent.checked = 'checked';
    } else E{
      treeItem.parent.checked = 'unchecked';
    }
    updateCheckStateOfParents(treeItem.parent);
  }
};