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 | 1x 1x 8x 8x 8x 8x 54x 8x 8x 11x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 40x 11x 11x 8x 1x 8x 1x 8x 8x 40x 40x 52x 40x 40x 9x 40x 14x 11x 3x 14x 16x 6x 16x 16x 16x 5x 11x 2x 11x 11x 11x 1x 2x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 20x 2x 2x 2x 2x 2x 2x 2x 2x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { CdkPortalOutlet, DomPortal, PortalModule } from '@angular/cdk/portal'; import { ViewportScroller } from '@angular/common'; import { AfterViewInit, booleanAttribute, ChangeDetectorRef, Component, computed, DOCUMENT, ElementRef, inject, input, OnChanges, OnDestroy, signal, SimpleChanges, viewChild } from '@angular/core'; import { ScrollbarHelper } from '@siemens/element-ng/common'; import { BOOTSTRAP_BREAKPOINTS, ElementDimensions, ResizeObserverService } from '@siemens/element-ng/resize-observer'; import { SiTranslateModule } from '@siemens/element-translate-ng/translate'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { SiDashboardCardComponent } from './si-dashboard-card.component'; import { SiDashboardService } from './si-dashboard.service'; const FIX_SCROLL_PADDING_RESIZE_OBSERVER_THROTTLE = 10; @Component({ selector: 'si-dashboard', imports: [PortalModule, SiTranslateModule], templateUrl: './si-dashboard.component.html', styleUrl: './si-dashboard.component.scss', providers: [SiDashboardService], host: { class: 'si-layout-fixed-height' } }) export class SiDashboardComponent implements OnChanges, OnDestroy, AfterViewInit { /** * Heading for the dashboard page. */ readonly heading = input<string>(); /** * Opt-in to enable expand interaction for all cards. * * @defaultValue false */ readonly enableExpandInteractions = input(false, { transform: booleanAttribute }); /** * Option to turn off the sticky behavior of the heading and menu bar. * * @defaultValue true */ readonly sticky = input(true, { transform: booleanAttribute }); /** * Option to hide the menu bar. * * @defaultValue false */ readonly hideMenubar = input(false, { transform: booleanAttribute }); /** * Is `true` if a card is expanded. * @defaultref {@link _isExpanded} */ get isExpanded(): boolean { return this._isExpanded; } protected dashboardFrameEndPadding: number | null = null; protected readonly hideMenubarComputed = computed( () => this.hideMenubar() || this.hideMenubarInternal() ); private _isExpanded = false; private unsubscribe = new Subject<void>(); private scrollPosition: [number, number] = [0, 0]; private cards: SiDashboardCardComponent[] = []; private readonly expandedPortalOutlet = viewChild.required('expandedPortalOutlet', { read: CdkPortalOutlet }); private readonly dashboardFrame = viewChild.required<ElementRef<HTMLElement>>('dashboardFrame'); private readonly dashboard = viewChild.required<ElementRef<HTMLElement>>('dashboard'); private dashboardFrameDimensions?: ElementDimensions; private dashboardDimensions?: ElementDimensions; private scroller = inject(ViewportScroller); private dashboardService = inject(SiDashboardService); private resizeObserver = inject(ResizeObserverService); private scrollbarHelper = inject(ScrollbarHelper); private cdRef = inject(ChangeDetectorRef); private document = inject(DOCUMENT); private readonly hideMenubarInternal = signal(false); constructor() { this.dashboardService.cards$ .pipe(takeUntil(this.unsubscribe)) .subscribe(cards => this.subscribeToCards(cards)); } ngOnChanges(changes: SimpleChanges): void { if (changes.enableExpandInteractions) { this.initCards(); } } ngAfterViewInit(): void { this.resizeObserver .observe(this.dashboard().nativeElement, FIX_SCROLL_PADDING_RESIZE_OBSERVER_THROTTLE) .pipe(takeUntil(this.unsubscribe)) .subscribe(x => this.setDashboardFrameEndPadding(this.dashboardFrameDimensions, x)); this.resizeObserver .observe(this.dashboardFrame().nativeElement, FIX_SCROLL_PADDING_RESIZE_OBSERVER_THROTTLE) .pipe(takeUntil(this.unsubscribe)) .subscribe(dims => this.setDashboardFrameEndPadding(dims, this.dashboardDimensions)); } ngOnDestroy(): void { this.unsubscribe.next(); this.unsubscribe.complete(); } private subscribeToCards(cards: SiDashboardCardComponent[]): void { this.cards = cards; this.initCards(); } private initCards(): void { for (const card of this.cards) { // We only enforce expand if the dashboard value is true, otherwise we would remove the individual // card settings. const enableExpandInteractions = this.enableExpandInteractions(); if (enableExpandInteractions) { card.enableExpandInteractionInternal.set(enableExpandInteractions); } card.expandChange.subscribe((expand: boolean) => { if (expand) { this.expand(card); } else { this.restoreDashboard(); } this.cdRef.markForCheck(); }); } } /** * Expands the provided card to full size in the dashboard. * @param card - The card to be expanded. */ public expand(card: SiDashboardCardComponent): void { if (this.isExpanded) { this.restoreDashboard(); } if (this.sticky()) { this.scrollPosition = [ this.dashboardFrame().nativeElement.scrollLeft, this.dashboardFrame().nativeElement.scrollTop ]; } else E{ this.scrollPosition = this.scroller.getScrollPosition(); } // Make sure card.expand() is called first and prevent recursions. if (!card.isExpanded()) { card.expand(); } else { if (!card.showMenubar()) { this.hideMenubarInternal.set(true); } this._isExpanded = true; this.expandedPortalOutlet().detach(); this.expandedPortalOutlet().attach(new DomPortal(card.element.nativeElement)); } } /** * Restores the expanded card to it's previous position. */ public restore(): void { // Restore all cards for (const card of this.cards) { if (card.isExpanded()) { card.restore(); } } // Restore the dashboard and scroll to previous position this.restoreDashboard(); // Subscribe to cards events again this.initCards(); this.cdRef.markForCheck(); } /** * Restored the UI state of the dashboard. This method is only part * of restoring a card and needs to be invoked after the card.restore() * method. In general this is achieved by listening to card events. */ private restoreDashboard(): void { this.expandedPortalOutlet().detach(); this.hideMenubarInternal.set(false); this.toggleCardsHide(false); const scrollBehavior = this.document.documentElement.style.scrollBehavior; this.document.documentElement.style.scrollBehavior = 'auto'; setTimeout(() => { if (this.sticky()) { this.dashboardFrame().nativeElement.scrollTo({ left: this.scrollPosition[0], top: this.scrollPosition[1], behavior: 'auto' }); } else E{ this.scroller.scrollToPosition(this.scrollPosition); this.document.documentElement.style.scrollBehavior = scrollBehavior; } this.cdRef.markForCheck(); }); this._isExpanded = false; } private toggleCardsHide(expand: boolean): void { for (const card of this.cards) { card.hide = !card.isExpanded() && expand; } } private setDashboardFrameEndPadding( dashboardFrameDimensions?: ElementDimensions, dashboardDimensions?: ElementDimensions ): void { Iif (!this.sticky()) { return; } Iif ( this.dashboardDimensions === dashboardDimensions && this.dashboardFrameDimensions === dashboardFrameDimensions ) { return; } this.dashboardDimensions = dashboardDimensions; this.dashboardFrameDimensions = dashboardFrameDimensions; let padding = this.document.body.offsetWidth >= BOOTSTRAP_BREAKPOINTS.mdMinimum ? 32 : 16; Iif ( dashboardDimensions && dashboardFrameDimensions && dashboardDimensions.height > dashboardFrameDimensions.height ) { padding = padding - this.scrollbarHelper.width; } this.dashboardFrameEndPadding = padding; this.cdRef.markForCheck(); } } |