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 | 1x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 64x 64x 100x 167x 166x 166x 1x 165x 65x 167x 39x 164x 164x 164x 129x 129x 168x 160x 147x 147x 129x 309x 436x 337x 129x 262x 129x 169x 169x 169x 169x 169x 222x 222x 163x 163x 59x 2x 2x 57x 57x 169x 169x 169x 386x 169x 169x 101x 101x 101x 147x 147x 101x 101x 2x 2x 2x 64x 64x 12x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { AfterViewInit, booleanAttribute, ChangeDetectorRef, contentChild, contentChildren, Directive, ElementRef, inject, INJECTOR, input, OnChanges, OnDestroy, SimpleChanges } from '@angular/core'; import { toObservable } from '@angular/core/rxjs-interop'; import { ResizeObserverService } from '@siemens/element-ng/resize-observer'; import { BehaviorSubject, combineLatest, of, Subscription } from 'rxjs'; import { auditTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators'; import { SiAutoCollapsableListAdditionalContentDirective } from './si-auto-collapsable-list-additional-content.directive'; import { SiAutoCollapsableListItemDirective } from './si-auto-collapsable-list-item.directive'; import { SiAutoCollapsableListOverflowItemDirective } from './si-auto-collapsable-list-overflow-item.directive'; @Directive({ selector: '[siAutoCollapsableList]', host: { style: 'position: relative', '[style.overflow]': 'siAutoCollapsableList() ? "hidden" : ""' }, exportAs: 'siAutoCollapsableList' }) export class SiAutoCollapsableListDirective implements AfterViewInit, OnChanges, OnDestroy { /** * The items which are displayed in the siAutoCollapsableList. */ readonly items = contentChildren(SiAutoCollapsableListItemDirective); private readonly overflowItem = contentChild(SiAutoCollapsableListOverflowItemDirective); private readonly additionalContent = contentChildren( SiAutoCollapsableListAdditionalContentDirective ); /** @defaultValue true */ readonly siAutoCollapsableList = input(true, { transform: booleanAttribute }); /** * The (flex) gap in pixels, will automatically be set if a pixel value is used in CSS. */ readonly gap = input<number>(); /** * The element which size is available for the content of the siAutoCollapsableList. * * @defaultValue undefined */ readonly containerElement = input<HTMLElement | undefined | null>(undefined, { alias: 'siAutoCollapsableListContainerElement' }); private resizeSubscription?: Subscription; private disableInitSubscription?: Subscription; private readonly elementRef = inject(ElementRef); private readonly resizeObserverService = inject(ResizeObserverService); private readonly changeDetectorRef = inject(ChangeDetectorRef); private readonly containerElementSubject = new BehaviorSubject<HTMLElement | null>( this.elementRef.nativeElement ); /** * The same as {@link gap}, but automatically read from the computed styles. * Used if not set by user. */ private computedGap = 0; private injector = inject(INJECTOR); ngAfterViewInit(): void { if (this.siAutoCollapsableList()) { this.readGapSize(); this.setupResizeListener(); } else { this.reset(); } } ngOnChanges(changes: SimpleChanges): void { if (changes.siAutoCollapsableList) { const siAutoCollapsableList = this.siAutoCollapsableList(); if (!siAutoCollapsableList && this.resizeSubscription) { this.reset(); } else if (siAutoCollapsableList && !this.resizeSubscription && this.items()) { this.setupResizeListener(); } } if (changes.containerElement) { this.containerElementSubject.next(this.containerElement() ?? this.elementRef.nativeElement); } } ngOnDestroy(): void { this.resizeSubscription?.unsubscribe(); this.disableInitSubscription?.unsubscribe(); this.containerElementSubject.complete(); } private setupResizeListener(): void { this.disableInitSubscription?.unsubscribe(); const containerSize$ = this.containerElementSubject.pipe( switchMap(element => this.resizeObserverService.observe(element!, 0, true, true)), map(size => size.width), distinctUntilChanged(), map(size => { const { paddingInlineStart, paddingInlineEnd } = getComputedStyle( this.containerElement() ?? this.elementRef.nativeElement ); return size - parseFloat(paddingInlineStart) - parseFloat(paddingInlineEnd); }) ); const itemSizes$ = toObservable(this.items, { injector: this.injector }).pipe( startWith(this.items()), switchMap(items => !items.length ? of([]) : combineLatest( items.map(item => item.size$.pipe( map(size => ({ size, directive: item })) ) ) ) ) ); const additionalContentSizes$ = toObservable(this.additionalContent, { injector: this.injector }).pipe( startWith(this.additionalContent()), switchMap(additionalContent => combineLatest(additionalContent.map(item => item.size$))), startWith([]) ); this.resizeSubscription = combineLatest([ containerSize$, this.overflowItem()?.size$ ?? of(0), itemSizes$, additionalContentSizes$ ]) .pipe(auditTime(0)) .subscribe(([containerSize, overflowItemSize, items, additionalContentSizes]) => this.updateItemVisibility(containerSize, overflowItemSize, items, additionalContentSizes) ); } private updateItemVisibility( containerSize: number, overflowItemSize: number, items: { size: number; directive: SiAutoCollapsableListItemDirective }[], additionalContent: number[] ): void { let remainingSpace = containerSize - additionalContent.reduce((a, b) => a + b, 0); const itemsRemaining = items.slice(); const gap = this.gap() ?? this.computedGap; while (remainingSpace > 0 && itemsRemaining.length) { const item = itemsRemaining.shift()!; if (remainingSpace - item.size - gap - overflowItemSize >= 0) { // There is space for the item and an overflow item. item.directive.canBeVisible.set(true); remainingSpace -= item.size + gap; } else if ( !itemsRemaining.length && (remainingSpace - item.size >= 0 || overflowItemSize >= item.size) ) { // There are no other items remaining and there is enough space or the overflow item is biggger than the current one. item.directive.canBeVisible.set(true); remainingSpace = 0; } else { // There is no space for the item. remainingSpace = 0; item.directive.canBeVisible.set(false); } } itemsRemaining.forEach(item => item.directive.canBeVisible.set(false)); const overflowItem = this.overflowItem(); if (overflowItem) { overflowItem.hiddenItemCount = this.items().filter(item => !item.canBeVisible()).length; overflowItem.canBeVisible.set(overflowItem.hiddenItemCount !== 0); } this.changeDetectorRef.markForCheck(); } private reset(): void { this.resizeSubscription?.unsubscribe(); this.resizeSubscription = undefined; this.disableInitSubscription = toObservable(this.items, { injector: this.injector }).subscribe( items => queueMicrotask(() => { items.forEach(item => item.canBeVisible.set(true)); }) ); const overflowItem = this.overflowItem(); if (overflowItem) { queueMicrotask(() => { overflowItem!.canBeVisible.set(false); overflowItem!.hiddenItemCount = 0; }); } } private readGapSize(): void { const { gap } = getComputedStyle(this.containerElement() ?? this.elementRef.nativeElement); if (gap.endsWith('px') || gap === '0') { this.computedGap = parseFloat(gap); } } } |