All files / lib/breadcrumbs breadcrumbs.component.ts

97.15% Statements 205/211
92.1% Branches 35/38
91.66% Functions 11/12
97.15% Lines 205/211

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 2121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 1x 1x 1x 63x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 48x 48x 48x 48x 48x 26x 26x 26x 48x 48x 48x 48x 48x 48x 48x 48x 14x 14x 1x 1x 1x 1x 1x     1x 1x 1x 1154x 1154x 1x 1x 1x 2308x 2308x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1694x 1694x 1x 1x 1x 15x 1x 1x 1x 1x 1x 15x 1x 1x 1x 168x 168x 1x 1x 1x 1x 1x 63x 63x 63x 63x 63x 49x 49x 63x 14x 14x 14x 63x 63x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 63x 356x 356x 356x 356x       356x 356x 356x 356x 356x 356x 343x 343x 13x 13x 356x 356x 63x 63x 63x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  NgZone,
  OnDestroy,
  computed,
  effect,
  input,
  signal,
  untracked,
  viewChild,
  viewChildren,
} from '@angular/core';
 
import { LinkEvent } from '../utils/event.types';
import { observeElementSize } from '../utils/internal-utils';
import { Callback, Consumer, LinkItem } from '../utils/util.types';
import { itemTrack } from '../utils/utilities';
 
/**
 * Breadcrumbs component is a graphical control element used as a navigational aid on a web page.
 */
@Component({
  selector: 'gc-breadcrumbs',
  templateUrl: './breadcrumbs.component.html',
  styleUrls: ['./breadcrumbs.component.css'],
  changeDetection: ChangeDetectionStrategy.Default,
  standalone: false,
})
export class BreadcrumbsComponent implements OnDestroy {
  /**
   * Indicates that breadcrumbs is in loading state.
   */
  public isLoading = input(false);
  /**
   * Optional handler to customize handling of navigation requests
   */
  public linkHandler = input<Consumer<LinkEvent<LinkItem>>>();
  /**
   * Items that will be rendered as breadcrumbs.
   */
  public items = input.required<readonly LinkItem[]>();
 
  /**
   * @ignore
   */
  protected _visibleItems = signal<ReadonlySet<string>>(new Set());
 
  /**
   * @ignore
   */
  protected _computeLastItemText = computed<string>(() => {
    return this.items().at(-1)?.label ?? '';
  });
 
  protected _getPoppedItems = computed<LinkItem[]>(() => {
    return this.items().filter(i => !this._isItemVisible(i));
  });
 
  /** @ignore */
  private menuItem = viewChild<ElementRef<HTMLElement>>('collapsedMenuItem');
  /**
   * @ignore
   */
  private renderedItems =
    viewChildren<ElementRef<HTMLElement>>('breadcrumbItem');
 
  /**
   * @ignore
   */
  private resizeObserver: Callback;
 
  /** @ignore */
  private _items: readonly LinkItem[] = [];
 
  constructor(
    private el: ElementRef<HTMLElement>,
    zone: NgZone,
  ) {
    this.resizeObserver = observeElementSize(
      el.nativeElement,
      zone,
      this.computeVisibleItems.bind(this),
    );
    effect(() => {
      // recomputation required when menuItem is added/removed because of change to isLoading
      this.menuItem();
      const items = this.items();
      untracked(() => {
        if (this._items.length === this._visibleItems().size) {
          // avoid immediate change of the visibility of the 'collapsedMenuItem' to avoid visible layout jumps
          // in case the following full recalculation determines no change is needed
          this._visibleItems.set(new Set(items.map(i => i.key)));
        }
      });
 
      this._items = items;
 
      untracked(() => {
        this.computeVisibleItems();
      });
    });
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this.resizeObserver();
  }
 
  /** @ignore */
  public _isFirstItem(item: LinkItem): boolean {
    return this.items().at(0)?.key === item.key;
  }
 
  /** @ignore */
  public _isLastItem(item: LinkItem): boolean {
    return this.items().at(-1)?.key === item.key;
  }
 
  /** @ignore */
  public _onMenuAction(item: LinkItem): void {
    const linkHandler = this.linkHandler();
    if (linkHandler) {
      linkHandler({ linkInfo: item, replace: true });
    } else {
      window.open(item.link, '_self');
    }
  }
 
  /** @ignore */
  public _isItemVisible(item: LinkItem): boolean {
    return this._visibleItems().has(item.key);
  }
 
  /** @ignore */
  protected _createLinkHandler(item: LinkItem): Consumer<LinkEvent> {
    return evt => {
      const linkHandler = this.linkHandler();
      if (linkHandler) {
        linkHandler({ linkInfo: item, replace: evt.replace });
      }
    };
  }
 
  /** @ignore */
  protected _itemTrackBy(item: LinkItem): string {
    return itemTrack(item);
  }
 
  /**
   * @ignore
   */
  private computeVisibleItems(): void {
    const items = this.items();
    const renderedItems = this.renderedItems();
    const menuItem = this.menuItem();
    if (
      menuItem === undefined ||
      items.length === 0 ||
      renderedItems.length === 0
    ) {
      this._visibleItems.set(new Set());
      return;
    }
 
    const hostBounds = this.el.nativeElement.getBoundingClientRect();
    const collapsedMenuItemWidth =
      menuItem.nativeElement.getBoundingClientRect().width;
    let availableWidth = hostBounds.width;
 
    // last item is always visible
    const visibleItems = new Set([items[items.length - 1].key]);
 
    availableWidth -=
      renderedItems[
        renderedItems.length - 1
      ].nativeElement.getBoundingClientRect().width;
 
    // add remaining items until no space is left
    for (let i = items.length - 2; i >= 0 && availableWidth > 0; i--) {
      const itemWidth = renderedItems
        .at(i)
        ?.nativeElement.getBoundingClientRect().width;
      if (itemWidth === undefined) {
        console.warn('failed to find rendered item for item index', i);
        break;
      }
 
      // include the width of the collapsed menu item in the calculation, as it needs to also fit
      // in front of the current item when it'd be the last one to show
      const requiredWidth = itemWidth + (i > 0 ? collapsedMenuItemWidth : 0);
 
      if (availableWidth >= requiredWidth) {
        visibleItems.add(items[i].key);
        availableWidth -= itemWidth;
      } else {
        availableWidth = 0;
      }
    }
 
    this._visibleItems.set(visibleItems);
  }
}