All files / lib/breadcrumbs breadcrumbs.component.ts

95.5% Statements 191/200
87.87% Branches 29/33
92.3% Functions 12/13
95.5% Lines 191/200

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 2011x 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 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 34x 34x 27x 27x 27x 34x 34x 34x 34x 34x 34x 34x 14x 14x 1x 1x 1x 1x 1x     1x 1x 1x 1472x 1472x 1x 1x 1x 2944x 2944x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 2944x 2944x 1x 1x 1x 1x 1x 292x 292x 1x 1x 1x 15x 1x 1x 1x 1x 1x 15x 1x 1x 1x 168x 168x 1x 1x 1x 1x 1x 49x 49x 49x       49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 366x 366x 366x 366x       366x 366x 366x 366x 366x 366x 354x 354x 12x 12x 366x 366x 49x 49x 49x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  ChangeDetectionStrategy,
  Component,
  ElementRef,
  NgZone,
  OnDestroy,
  computed,
  effect,
  input,
  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 {
  /**
   * 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: ReadonlySet<string> = new Set();
 
  /**
   * @ignore
   */
  protected _computeLastItemText = computed<string>(() => {
    return this.items().at(-1)?.label ?? '';
  });
 
  /** @ignore */
  private menuItem =
    viewChild.required<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(() => {
      const items = this.items();
      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 = new Set(items.map(i => i.key));
      }
 
      this._items = items;
 
      setTimeout(() => {
        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
   */
  _getPoppedItems(): LinkItem[] {
    return this.items().filter(i => !this._isItemVisible(i));
  }
 
  /** @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();
    if (items.length === 0 || renderedItems.length === 0) {
      this._visibleItems = new Set();
      return;
    }
 
    const hostBounds = this.el.nativeElement.getBoundingClientRect();
    const collapsedMenuItemWidth =
      this.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 = visibleItems;
  }
}