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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 21x 17x 21x 21x 21x 19x 126x 20x 20x 126x 126x 126x 126x 126x 20x 20x 20x 10x 10x 126x 126x 20x 20x 20x 20x 20x 20x 2x 2x 2x 2x 2x 2x 5x 6x 5x 1x 30x 30x 30x 30x 30x 5x 5x 5x 30x 30x 30x 30x 30x 216x 30x 30x 30x 30x 30x 30x 48x 47x 47x 2x 2x 45x 45x 30x 30x 30x 113x 41x 41x 12x 12x 2x 1x 1x 29x 29x 30x 14x 14x 14x 14x 30x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { NgTemplateOutlet } from '@angular/common'; import { booleanAttribute, ChangeDetectorRef, Component, ElementRef, HostListener, inject, input, OnChanges, OnDestroy, viewChild, viewChildren } from '@angular/core'; import { addIcons, elementBreadcrumbRoot, elementRight2, SiIconComponent } from '@siemens/element-ng/icon'; import { SiLinkDirective } from '@siemens/element-ng/link'; import { SiResizeObserverDirective } from '@siemens/element-ng/resize-observer'; 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 how long a display item can be without it being shortened. * Cannot be lower than 4. * If this is 0, titles will not be shortened */ const ITEM_MAX_LENGTH = 30; /** * Defines how many characters of an item are always displayed in the beginning. * Must be at least 2 less than ITEM_MAX_LENGTH */ const ITEM_CHARACTERS_ALWAYS_DISPLAYED_IN_BEGINNING = 10; /** * Defines the width of the root icon in pixels. */ const ROOT_ICON_WIDTH = 24; let controlIdCounter = 1; @Component({ selector: 'si-breadcrumb', imports: [ NgTemplateOutlet, SiIconComponent, SiLinkDirective, SiResizeObserverDirective, SiTranslatePipe, SiBreadcrumbItemTemplateDirective ], templateUrl: './si-breadcrumb.component.html', styleUrl: './si-breadcrumb.component.scss' }) 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:Breadcrumbs`) * ``` */ readonly ariaLabel = input(t(() => $localize`:@@SI_BREADCRUMB:Breadcrumbs`)); private translationSubscription?: Subscription; private itemsProcessed = false; private numberOfItems = 0; protected itemsShown: EnumeratedBreadcrumbItem[] = []; protected itemsHidden: EnumeratedBreadcrumbItem[] = []; protected breadcrumbShortened = false; protected ellipsesLevel = 0; // Record to allow for -1 (root). protected breadcrumbDropdownOpen: number | undefined = undefined; protected addExpandDropdown = false; protected controlId = `__si-breadcrumb-${controlIdCounter++}-`; protected readonly icons = addIcons({ elementBreadcrumbRoot, elementRight2 }); private readonly breadcrumbElement = viewChild.required<ElementRef>('breadcrumb'); private readonly breadcrumbElements = viewChildren<ElementRef>('breadcrumbItem'); 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 to the items and check if they need to be shortened. // If they need to be shortened, shorten them at a convenient place. // Set the lastItem tag to true for the last item let counter = -1; const enumeratedItems = this.items().map(item => { counter++; const title = translatedTitles[item.title]; let shortened = false; let shortenedTitle = title; // If this is not the last item and the title too long, shorten the title if (counter !== this.numberOfItems - 1 && title && title.length > ITEM_MAX_LENGTH) { shortened = true; // This regex gets the last space, dash or underscore. const regexMatch = title .slice(ITEM_CHARACTERS_ALWAYS_DISPLAYED_IN_BEGINNING, ITEM_MAX_LENGTH - 2) .match(/^.*[- _](?=.*?$)/); if (regexMatch) { shortenedTitle = title.slice( 0, ITEM_CHARACTERS_ALWAYS_DISPLAYED_IN_BEGINNING + regexMatch[0].length - 1 ); } else { shortenedTitle = title.slice(0, ITEM_MAX_LENGTH - 3); } } // 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, shortened, shortenedTitle, lastItem: counter === this.numberOfItems - 1 }; }); this.itemsShown = enumeratedItems; this.itemsHidden = []; this.breadcrumbShortened = false; this.breadcrumbDropdownOpen = undefined; this.itemsProcessed = true; this.resetBreadcrumb(); }); } else { this.itemsShown = []; this.itemsHidden = []; this.breadcrumbShortened = false; this.breadcrumbDropdownOpen = undefined; this.itemsProcessed = true; this.resetBreadcrumb(); } } /* * Toggle dropdown (on click of ellipses), either for * the general dropdown list if itemLevel is at ellipsesLevel * or otherwise the name expansion at the specified item level. * Close any open dropdown before opening a new one. */ protected toggleBreadcrumbDropdown(itemLevel: number): void { this.breadcrumbDropdownOpen = this.breadcrumbDropdownOpen === itemLevel ? undefined : itemLevel; } // Close dropdown on click anywhere else @HostListener('document:click', ['$event.target']) protected documentClick(targetElement: any): void { if (this.breadcrumbDropdownOpen) { if (!this.breadcrumbElement().nativeElement.contains(targetElement)) { // Close all dropdowns. this.breadcrumbDropdownOpen = undefined; } } } protected resetBreadcrumb(): void { if (this.itemsProcessed) { this.numberOfItems = this.items().length; // Add an additional the ellipses item to the end of the shownItems (breadcrumb items). // Disable addExpandDropdown for now, to make every item a proper SiBreadcrumbItemComponent. const ellipsesItem = { title: '...', level: this.numberOfItems, shortenedTitle: '' }; this.itemsShown.push(ellipsesItem); if (this.breadcrumbShortened) { // If the breadcrumb was shortened before, remove the ellipses and add back itemsHidden (breadcrumb dropdown items). this.breadcrumbShortened = false; this.itemsShown.splice(this.ellipsesLevel, 1, ...this.itemsHidden); this.itemsHidden = []; } this.addExpandDropdown = false; // Wait for the next change detection cycle to measure the updated item length. this.changeDetector.detectChanges(); this.calculateBreadcrumb(); } } private calculateBreadcrumb(): void { this.addExpandDropdown = true; 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 { reverseCounter++; } } } else { currentWidth += currentItemWidth; counter++; } } }); // If breadcrumbShortened is true, move the items that do not fit to itemsHidden (breadcrumb dropdown 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, shortenedTitle: '' }; 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(); } } |