All files / tree-view/si-tree-view-item si-tree-view-item.directive.ts

80.43% Statements 37/46
68.75% Branches 11/16
77.77% Functions 7/9
78.04% Lines 32/41

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                                                        1x 97x 97x 97x 97x 97x 97x   97x     97x 97x 97x 97x 2164x     97x   97x 122x         97x       219x 219x 219x         146x 146x           1159x 914x                                           245x 245x                   1159x       146x 1272x 1272x 1272x     146x                                        
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  AfterViewInit,
  ChangeDetectorRef,
  Directive,
  EmbeddedViewRef,
  inject,
  Injector,
  IterableChangeRecord,
  IterableChanges,
  IterableDiffer,
  IterableDiffers,
  OnDestroy,
  OutputRefSubscription,
  TemplateRef,
  ViewContainerRef
} from '@angular/core';
 
import { TREE_ITEM_CONTEXT } from '../si-tree-view-item-context';
import { SiTreeViewComponent } from '../si-tree-view.component';
import { TreeItem, TreeItemContext } from '../si-tree-view.model';
 
@Directive({
  selector: '[siTreeViewItem]'
})
export class SiTreeViewItemDirective implements AfterViewInit, OnDestroy {
  private templateRef = inject(TemplateRef);
  private viewContainerRef = inject(ViewContainerRef);
  private parent = inject(SiTreeViewComponent);
  private differs = inject(IterableDiffers);
  private cdRef = inject(ChangeDetectorRef);
  private differ: IterableDiffer<TreeItem> | null = null;
  private subscription!: OutputRefSubscription;
  private itemsVirtualizedDirty = true;
 
  ngAfterViewInit(): void {
    if (this.itemsVirtualizedDirty) {
      this.itemsVirtualizedDirty = false;
      const value = this.parent.itemsVirtualized;
      if (!this.differ && value) {
        this.differ = this.differs.find(value).create((_index, item) => item);
      }
    }
    this.evaluateDiffer();
 
    this.subscription = this.parent.evaluateForTreeItemsDiffer.subscribe(_ =>
      this.evaluateDiffer()
    );
  }
 
  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
 
  private evaluateDiffer(): void {
    if (this.differ) {
      const changes = this.differ.diff(this.parent.itemsVirtualized);
      if (changes) this.applyChanges(changes);
    }
  }
 
  private applyChanges(changes: IterableChanges<TreeItem>): void {
    const viewContainer = this.viewContainerRef;
    changes.forEachOperation(
      (
        record: IterableChangeRecord<TreeItem>,
        adjustedPreviousIndex: number | null,
        currentIndex: number | null
      ) => {
        if (record.previousIndex == null) {
          viewContainer.createEmbeddedView(
            this.templateRef,
            {
              treeItem: record.item,
              index: record.currentIndex,
              parent: this.parent
            },
            {
              injector: Injector.create({
                providers: [
                  {
                    provide: TREE_ITEM_CONTEXT,
                    useValue: {
                      record,
                      parent: this.parent
                    }
                  }
                ]
              }),
              index: currentIndex ?? undefined
            }
          );
        } else if (currentIndex == null) {
          viewContainer.remove(adjustedPreviousIndex ?? undefined);
        } else IEif (adjustedPreviousIndex !== null) {
          const view = viewContainer.get(adjustedPreviousIndex)!;
          viewContainer.move(view, currentIndex);
          (view as EmbeddedViewRef<TreeItemContext>).context = {
            treeItem: record.item,
            index: record.currentIndex!,
            parent: this.parent
          };
        }
        this.cdRef.detectChanges();
      }
    );
 
    for (let i = 0, ilen = viewContainer.length; i < ilen; i++) {
      const viewRef = viewContainer.get(i) as EmbeddedViewRef<TreeItemContext>;
      const context = viewRef.context;
      context.index = i;
    }
 
    changes.forEachIdentityChange((record: IterableChangeRecord<TreeItem>) => {
      Iif (record.currentIndex) {
        const viewRef = viewContainer.get(record.currentIndex) as EmbeddedViewRef<TreeItemContext>;
        viewRef.context = {
          treeItem: record.item,
          index: record.currentIndex,
          parent: this.parent
        };
        this.cdRef.detectChanges();
      }
    });
  }
 
  static ngTemplateContextGuard(
    _dir: SiTreeViewItemDirective,
    ctx: unknown
  ): ctx is TreeItemContext {
    return true;
  }
}