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 | 1x 9x 1x 1x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 958x 284x 72x 72x 72x 72x 23x 1x 22x 22x 22x 9x 22x 2x 22x 6x 22x 8x 22x 124x 102x 22x 1x 22x 22x 22x 9x 9x 9x 9x 9x 9x 9x 9x 9x 13x 22x 6x 22x 1x 21x 8x 21x 24x 1x 23x 81x 81x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { Overlay, OverlayRef } from '@angular/cdk/overlay'; import { TemplatePortal } from '@angular/cdk/portal'; import { Component, ComponentRef, Directive, ElementRef, EmbeddedViewRef, HostBinding, HostListener, inject, Injector, input, OnChanges, OnDestroy, OnInit, output, TemplateRef, ViewContainerRef } from '@angular/core'; import { of, Subject } from 'rxjs'; import { filter, skip, take, takeUntil } from 'rxjs/operators'; import { SI_HEADER_WITH_DROPDOWNS } from './si-header.model'; @Component({ template: '', host: { '[attr.aria-owns]': 'ariaOwns()' } }) class SiHeaderAnchorComponent { readonly ariaOwns = input<string>(); } /** * Trigger to open dropdowns in a navbar. * A dropdown will always be attached to the view, even if not visible. * * If a dropdown is opened in desktop mode, it will be reattached to an overlay while being opened. */ @Directive({ selector: '[siHeaderDropdownTriggerFor]', host: { class: 'dropdown-toggle' }, exportAs: 'siHeaderDropdownTrigger' }) export class SiHeaderDropdownTriggerDirective implements OnChanges, OnInit, OnDestroy { private static idCounter = 0; /** Template that be rendered inside the dropdown. */ readonly dropdown = input.required<TemplateRef<unknown>>({ alias: 'siHeaderDropdownTriggerFor' }); /** Data that should be passed as template context to the dropdown. */ readonly dropdownData = input<unknown>(); /** Emits whenever a dropdown is opened or closed. */ readonly openChange = output<boolean>(); private readonly dropdownClose = new Subject<void>(); /** Child triggers will set themselves here if they are open. */ private openSubmenu?: SiHeaderDropdownTriggerDirective; private readonly viewContainerRef = inject(ViewContainerRef); private readonly elementRef = inject<ElementRef<HTMLElement>>(ElementRef); private readonly overlay = inject(Overlay); private readonly parent = inject(SiHeaderDropdownTriggerDirective, { optional: true, skipSelf: true }); /** @internal */ readonly navbar = inject(SI_HEADER_WITH_DROPDOWNS, { optional: true }); // we need to create a new injector, so that the parent can be injected properly private readonly injector = Injector.create({ parent: inject(Injector), providers: [] }); private viewRef?: EmbeddedViewRef<unknown>; private portal?: TemplatePortal<unknown>; private overlayRef?: OverlayRef; /** @internal */ readonly level: number = this.parent ? this.parent.level + 1 : 1; // used to prevent immediate re-creation of the overlay if this trigger was clicked while overlay is open private destroying = false; /** @internal */ @HostBinding('id') readonly id = `si-navbar-dropdown-trigger-${SiHeaderDropdownTriggerDirective.idCounter++}`; // eslint-disable-next-line @typescript-eslint/naming-convention @HostBinding('class.show') @HostBinding('attr.aria-expanded') protected _isOpen = false; /** @internal */ @HostBinding('attr.aria-controls') readonly ariaControls = `si-navbar-dropdown-${SiHeaderDropdownTriggerDirective.idCounter}`; private headerAnchorComponentRef?: ComponentRef<SiHeaderAnchorComponent>; /** Whether the dropdown is open. */ get isOpen(): boolean { return this._isOpen; } /** @internal */ get isOverlay(): boolean { return !!this.overlayRef; } ngOnChanges(): void { Iif (this.portal) { this.portal.templateRef = this.dropdown(); this.portal.context = this.dropdownData(); } } ngOnInit(): void { // Always attach the dropdown, so that it can be used with routerLinkActive this.attachDropdownInline(); } ngOnDestroy(): void { this.close(); this.dropdownClose.complete(); } /** Opens the dropdown. */ open(): void { if (this.destroying || this._isOpen) { return; } (this.navbar?.inlineDropdown ?? of(false)).pipe(take(1)).subscribe(inline => { this._isOpen = true; if (!inline) { this.attachDropdownOverlay(); } this.navbar?.inlineDropdown ?.pipe(skip(1), takeUntil(this.dropdownClose)) .subscribe(() => this.close()); }); if (this.parent) { this.parent.openSubmenu = this; } if (this.navbar?.dropdownOpened) { this.navbar?.dropdownOpened(this); } this.openChange.emit(true); } /** Closes the dropdown. */ close(options?: { all?: boolean }): void { if (!this._isOpen) { return; } if (this.openSubmenu) { this.openSubmenu.close(); } this.dropdownClose.next(); this._isOpen = false; if (this.overlayRef) { this.destroying = true; this.overlayRef.detach(); this.overlayRef.dispose(); this.viewRef?.destroy(); this.portal = undefined; this.overlayRef = undefined; this.headerAnchorComponentRef?.destroy(); // do not use queueMicrotask, it executed to early setTimeout(() => (this.destroying = false)); this.attachDropdownInline(); } else { this.viewRef?.markForCheck(); } if (this.parent) { this.parent.openSubmenu = undefined; } if (options?.all && this.parent) { this.parent.close(options); } else { if (this.navbar?.dropdownClosed) { this.navbar?.dropdownClosed(this); } this.openChange.emit(false); } } @HostListener('click') protected click(): void { if (this._isOpen) { this.close(); } else { this.open(); } } private attachDropdownInline(): void { this.viewRef = this.viewContainerRef.createEmbeddedView(this.dropdown(), this.dropdownData(), { injector: this.injector }); this.viewRef.markForCheck(); } private attachDropdownOverlay(): void { this.viewRef?.destroy(); this.overlayRef = this.overlay.create({ positionStrategy: this.overlay .position() .flexibleConnectedTo(this.elementRef) .withPositions( this.navbar?.overlayPosition ?? (this.level > 1 ? [ { originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top', offsetX: 2 } ] : [ { originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top' }, { originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top', offsetX: -4 } ]) ) }); this.portal = new TemplatePortal( this.dropdown(), this.viewContainerRef, this.dropdownData(), this.injector ); this.viewRef = this.overlayRef.attach(this.portal); this.headerAnchorComponentRef = this.viewContainerRef.createComponent(SiHeaderAnchorComponent); this.headerAnchorComponentRef.setInput('ariaOwns', this.ariaControls); this.overlayRef .outsidePointerEvents() .pipe( filter(event => event.type === 'click'), filter(() => !this.openSubmenu), takeUntil(this.dropdownClose), take(1) ) .subscribe(() => this.close()); } } |