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 | 2x 2x 2x 2x 178x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 21x 19x 21x 21x 21x 21x 123x 22x 22x 123x 123x 123x 123x 22x 22x 22x 22x 22x 35x 35x 35x 35x 35x 35x 11x 11x 11x 35x 35x 35x 273x 35x 35x 35x 35x 35x 35x 63x 62x 62x 4x 4x 58x 58x 35x 35x 35x 141x 53x 53x 15x 15x 1x 1x 38x 38x 35x 19x 19x 19x 19x 35x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import { CdkMenuTrigger } from '@angular/cdk/menu';
import { NgTemplateOutlet } from '@angular/common';
import {
booleanAttribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
inject,
input,
OnChanges,
OnDestroy,
viewChild,
viewChildren
} from '@angular/core';
import { elementBreadcrumbRoot, elementRight2 } from '@siemens/element-icons';
import { addIcons, SiIconComponent } from '@siemens/element-ng/icon';
import { SiLinkDirective } from '@siemens/element-ng/link';
import { SiMenuDirective, SiMenuItemComponent } from '@siemens/element-ng/menu';
import { SiResizeObserverDirective } from '@siemens/element-ng/resize-observer';
import { SiTooltipOverflowDirective } from '@siemens/element-ng/tooltip';
import {
injectSiTranslateService,
SiTranslatePipe,
t
} from '@siemens/element-translate-ng/translate';
import { merge, of, Subscription } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { BreadcrumbItem, EnumeratedBreadcrumbItem } from './breadcrumb-item.model';
import { SiBreadcrumbItemTemplateDirective } from './si-breadcrumb-item-template.directive';
/**
* Defines how many items should be displayed at the end of the breadcrumb if possible.
*/
const NUMBER_OF_SHOWN_ITEMS_AT_END = 2;
/**
* Defines the width of the root icon in pixels.
*/
const ROOT_ICON_WIDTH = 24;
@Component({
selector: 'si-breadcrumb',
imports: [
CdkMenuTrigger,
NgTemplateOutlet,
SiIconComponent,
SiLinkDirective,
SiMenuDirective,
SiMenuItemComponent,
SiResizeObserverDirective,
SiTooltipOverflowDirective,
SiTranslatePipe,
SiBreadcrumbItemTemplateDirective
],
templateUrl: './si-breadcrumb.component.html',
styleUrl: './si-breadcrumb.component.scss',
changeDetection: ChangeDetectionStrategy.Eager
})
export class SiBreadcrumbComponent implements OnChanges, OnDestroy {
/** Array of breadcrumb items. */
readonly items = input.required<BreadcrumbItem[]>();
/**
* Shows the "root" route as the provided title string instead of an icon.
*
* @defaultValue false
*/
readonly showRootAsText = input(false, { transform: booleanAttribute });
/**
* Aria label for the main breadcrumb navigation. Needed for a11y.
*
* @defaultValue
* ```
* t(() => $localize`:@@SI_BREADCRUMB:Breadcrumb navigation`)
* ```
*/
readonly ariaLabel = input(t(() => $localize`:@@SI_BREADCRUMB:Breadcrumb navigation`));
private translationSubscription?: Subscription;
private itemsProcessed = false;
private numberOfItems = 0;
protected itemsShown: EnumeratedBreadcrumbItem[] = [];
protected itemsHidden: EnumeratedBreadcrumbItem[] = [];
protected breadcrumbShortened = false;
protected ellipsesLevel = 0;
protected readonly hiddenItemsLabel = t(
() => $localize`:@@SI_BREADCRUMB.HIDDEN_ITEMS:Hidden breadcrumb items`
);
protected readonly icons = addIcons({ elementBreadcrumbRoot, elementRight2 });
private readonly breadcrumbElement = viewChild.required<ElementRef>('breadcrumb');
private readonly breadcrumbElements = viewChildren<ElementRef>('breadcrumbItem');
private readonly breadcrumbMenu = viewChild(CdkMenuTrigger);
private changeDetector = inject(ChangeDetectorRef);
private translate = injectSiTranslateService();
ngOnChanges(): void {
// Reprocess items on every change and on init
this.processItems();
}
ngOnDestroy(): void {
this.translationSubscription?.unsubscribe();
}
private processItems(): void {
this.numberOfItems = this.items().length;
this.translationSubscription?.unsubscribe();
if (this.numberOfItems) {
this.translationSubscription = merge(this.translate.translationChange, of(undefined))
.pipe(switchMap(() => this.translate.translateAsync(this.items().map(item => item.title))))
.subscribe(translatedTitles => {
// Add the level and mark the last item, keeping the full translated titles.
let counter = -1;
const enumeratedItems = this.items().map(item => {
counter++;
const title = translatedTitles[item.title];
// If the root element should be displayed as text, set level not to 0.
// This is used to distinguish in the template between icon and text.
const level = counter === 0 && this.showRootAsText() ? -1 : counter;
return {
...item,
title,
level,
hide: false,
lastItem: counter === this.numberOfItems - 1
};
});
this.itemsShown = enumeratedItems;
this.itemsHidden = [];
this.breadcrumbShortened = false;
this.itemsProcessed = true;
this.resetBreadcrumb();
});
} else E{
this.itemsShown = [];
this.itemsHidden = [];
this.breadcrumbShortened = false;
this.itemsProcessed = true;
this.resetBreadcrumb();
}
}
protected resetBreadcrumb(): void {
this.breadcrumbMenu()?.close();
Eif (this.itemsProcessed) {
this.numberOfItems = this.items().length;
// Add an additional the ellipses item to the end of the shownItems (breadcrumb items).
const ellipsesItem = { title: '...', level: this.numberOfItems };
this.itemsShown.push(ellipsesItem);
if (this.breadcrumbShortened) {
// If the breadcrumb was shortened before, remove the ellipses and add back itemsHidden (breadcrumb menu items).
this.breadcrumbShortened = false;
this.itemsShown.splice(this.ellipsesLevel, 1, ...this.itemsHidden);
this.itemsHidden = [];
}
// Wait for the next change detection cycle to measure the updated item length.
this.changeDetector.detectChanges();
this.calculateBreadcrumb();
}
}
private calculateBreadcrumb(): void {
const maxWidth = this.breadcrumbElement().nativeElement.clientWidth;
const breadcrumbElementsList = this.breadcrumbElements().map(item => item);
// Measure the length of the last additional ellipses item, then remove it from itemsShown (breadcrumb items).
const ellipsesWidth = breadcrumbElementsList[this.numberOfItems].nativeElement.offsetWidth;
this.itemsShown.splice(this.numberOfItems, 1);
let currentWidth = this.showRootAsText() ? 0 : ROOT_ICON_WIDTH;
const numberOfItemsKeptAtEnd = Math.min(NUMBER_OF_SHOWN_ITEMS_AT_END, this.numberOfItems - 1);
let reverseCounter = this.numberOfItems;
// Test for numberOfItemsKeptAtEnd items from the end if they still fit, if not, set breadcrumbShortened to true.
breadcrumbElementsList
.slice(this.numberOfItems - numberOfItemsKeptAtEnd, this.numberOfItems)
.reverse()
.map(item => {
if (!this.breadcrumbShortened) {
const currentItemWidth = item.nativeElement.offsetWidth;
if (currentWidth + currentItemWidth > maxWidth) {
this.breadcrumbShortened = true;
// Test if the ellipses item still fits, if not remove last (actually next in original order) item as well.
Iif (currentWidth + ellipsesWidth > maxWidth) {
reverseCounter++;
}
} else {
currentWidth += currentItemWidth;
reverseCounter--;
}
}
});
const start = this.showRootAsText() ? 0 : 1;
let counter = start;
// If breadcrumbShortened is not true yet, test for the rest of the items from the start
// Whether they still fit, if not, set breadcrumbShortened to true.
breadcrumbElementsList.slice(start, this.numberOfItems - numberOfItemsKeptAtEnd).map(item => {
if (!this.breadcrumbShortened) {
const currentItemWidth = item.nativeElement.offsetWidth;
if (currentWidth + currentItemWidth > maxWidth) {
this.breadcrumbShortened = true;
// Test if the ellipses item still fits, if not remove last item as well.
// If the counter is still on 1, instead remove last (actually next in original order) from
// the previous reverse calculation
if (currentWidth + ellipsesWidth > maxWidth) {
if (counter > 1) {
counter--;
} else E{
reverseCounter++;
}
}
} else {
currentWidth += currentItemWidth;
counter++;
}
}
});
// If breadcrumbShortened is true, move the items that do not fit to itemsHidden (breadcrumb menu items) and add ellipses item.
if (this.breadcrumbShortened) {
this.ellipsesLevel = counter;
this.itemsHidden = this.itemsShown.slice(this.ellipsesLevel, reverseCounter);
const ellipsesItem = { title: '...', level: this.ellipsesLevel };
this.itemsShown.splice(this.ellipsesLevel, reverseCounter - this.ellipsesLevel, ellipsesItem);
}
// Manually detect changes to prevent them from not being detected on language change
this.changeDetector.detectChanges();
}
}
|