All files / lib/breadcrumbs breadcrumbs.component.ts

97.19% Statements 208/214
92.68% Branches 38/41
94.73% Functions 18/19
97.19% Lines 208/214

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 2151x 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 1x 1x 1x 1x 1x 9734x 9734x 9734x 1x 1x 29x 22x 22x 22x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x     1x 1x 1x 111x 111x 111x 111x 9x 9x 111x 111x 1x 1x 1x 1086x 1086x 1x 1x 1x 3258x 3258x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 2172x 2172x 1x 1x 1x 1x 1x 111x 111x 1x 1x 1x 1x 1x 111x 111x 1x 1x 1x 15x 1x 1x 15x 15x 15x 1x 1x 1x 1x 1x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 347x 347x 347x 347x       347x 347x 347x 347x 335x 335x 335x 335x 12x 12x 347x 347x 39x 39x 39x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewChecked,
  Component,
  ElementRef,
  Input,
  NgZone,
  OnDestroy,
  QueryList,
  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 { TooltipComponent } from '../internal/tooltip/tooltip.component';
 
/**
 * 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'],
})
export class BreadcrumbsComponent implements OnDestroy, AfterViewChecked {
  /**
   * Optional handler to customize handling of navigation requests
   */
  @Input()
  public linkHandler?: Consumer<LinkEvent<LinkItem>>;
 
  /** @ignore */
  @ViewChild('collapsedMenuItem')
  private menuItem!: ElementRef<HTMLElement>;
  /**
   * @ignore
   */
  @ViewChildren('breadcrumbItem')
  private renderedItems?: QueryList<ElementRef<HTMLElement>>;
 
  /**
   * @ignore
   */
  @ViewChild('lastItemTooltip')
  private lastBreadcrumbItemTooltip?: TooltipComponent;
 
  /**
   * @ignore
   */
  @ViewChild('lastBreadcrumbItemText')
  private lastBreadcrumbItemText?: ElementRef<HTMLElement>;
 
  /**
   * Items that will be rendered as breadcrumbs.
   */
  @Input()
  public get items(): readonly LinkItem[] {
    return this._items;
  }
 
  public set items(items: readonly LinkItem[]) {
    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
   */
  public _visibleItems: ReadonlySet<string> = new Set();
 
  /**
   * @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),
    );
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this.resizeObserver();
  }
 
  /** @ignore */
  ngAfterViewChecked(): void {
    if (
      this.lastBreadcrumbItemTooltip &&
      this.lastBreadcrumbItemTooltip.tooltipOwner !==
        this.lastBreadcrumbItemText
    ) {
      this.lastBreadcrumbItemTooltip.tooltipOwner = this.lastBreadcrumbItemText;
    }
  }
 
  /** @ignore */
  public _isFirstItem(item: LinkItem): boolean {
    return this.items[0]?.key === item.key;
  }
 
  /** @ignore */
  public _isLastItem(item: LinkItem): boolean {
    return this.items[this.items.length - 1]?.key === item.key;
  }
 
  /** @ignore */
  public _onMenuAction(item: LinkItem): void {
    if (this.linkHandler) {
      this.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 _computeLastItemText(): string {
    return this.items[this.items.length - 1]?.label ?? '';
  }
 
  /** @ignore */
  protected _createLinkHandler(item: LinkItem): Consumer<LinkEvent> {
    return evt => {
      if (this.linkHandler) {
        this.linkHandler({ linkInfo: item, replace: evt.replace });
      }
    };
  }
 
  /**
   * @ignore
   */
  private computeVisibleItems(): void {
    if (this.items.length === 0 || this.renderedItems === undefined) {
      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([this.items[this.items.length - 1].key]);
    availableWidth -=
      this.renderedItems.last.nativeElement.getBoundingClientRect().width;
 
    // add remaining items until no space is left
    for (let i = this.items.length - 2; i >= 0 && availableWidth > 0; i--) {
      const itemWidth = this.renderedItems
        .get(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(this.items[i].key);
        availableWidth -= itemWidth;
      } else {
        availableWidth = 0;
      }
    }
 
    this._visibleItems = visibleItems;
  }
}