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 | 1x 1x 1x 1x 1x 1x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 38x 51x 21x 21x 51x 51x 38x 11x 11x 11x 38x 43x 156x 64x 117x 156x 3x 11x 5x 6x 38x 38x 38x 38x 38x 50x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 3x 5x 38x 25x 4x 4x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { BreakpointObserver, MediaMatcher } from '@angular/cdk/layout'; import { ConnectionPositionPair, Overlay, OverlayRef } from '@angular/cdk/overlay'; import { ComponentPortal } from '@angular/cdk/portal'; import { ComponentRef, DestroyRef, Directive, ElementRef, inject, OnDestroy, output, signal } from '@angular/core'; import { outputToObservable, takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { getOverlayPositions, isRTL, positions } from '@siemens/element-ng/common'; import { BOOTSTRAP_BREAKPOINTS } from '@siemens/element-ng/resize-observer'; import { merge, Observable, Subject } from 'rxjs'; import { filter, map, skip, takeUntil, tap } from 'rxjs/operators'; import { SiDatepickerOverlayComponent } from './si-datepicker-overlay.component'; import { DatepickerConfig, DateRange } from './si-datepicker.model'; // eslint-disable-next-line no-restricted-syntax export enum CloseCause { Backdrop = 'backdrop', Detach = 'detach', Escape = 'escape', Select = 'select' } export type DatepickerInput = { config: DatepickerConfig; date: Date; dateRange: DateRange; rangeType: 'START' | 'END'; time12h: boolean; showTime: true; }; /** Partial datepicker inputs */ export type DatepickerInputPartial = Partial<DatepickerInput>; /** * Directive with the responsibility to open/close datepicker overlay. */ @Directive({ selector: '[siDatepickerOverlay]', exportAs: 'siDatepickerOverlay' }) export class SiDatepickerOverlayDirective implements OnDestroy { /** * Position of the datepicker overlay. Accepts an array of positions or a single position. * The position will be chosen based on the first position that fits into the viewport. * The input is necessary since the positions between the siDatepicker directive and si-date-range * component are different. * @internal */ readonly placement = signal<keyof typeof positions | ConnectionPositionPair[]>([ { overlayX: 'start', overlayY: 'top', originX: 'start', originY: 'bottom' }, { overlayX: 'start', overlayY: 'bottom', originX: 'start', originY: 'top' } ]); /** * Output event on closing datepicker e.g. by clicking backdrop or escape key. */ readonly siDatepickerClose = output<CloseCause>(); private overlayRef?: OverlayRef; private datepickerRef?: ComponentRef<SiDatepickerOverlayComponent>; private autoCloseSelection = new Subject<void>(); /** Guard for siDatepickerClose event emitter to make sure the cause is emitter just once */ private ignoreClose = false; private readonly overlay = inject(Overlay); private readonly triggerElementRef = inject(ElementRef); private readonly mediaMatcher = inject(MediaMatcher); private readonly breakpointObserver = inject(BreakpointObserver); private readonly destroyRef = inject(DestroyRef); /** * When the media query matches on open the overlay is displayed like a modal dialog. * In case, users change the screen size to the matching media query we close the overlay * if it is open with a connected overlay strategy. */ private readonly smallScreenQuery = `(max-width: ${BOOTSTRAP_BREAKPOINTS.mdMinimum}px) or (max-height: ${BOOTSTRAP_BREAKPOINTS.smMinimum}px)`; ngOnDestroy(): void { this.overlayRef?.dispose(); this.datepickerRef = undefined; } /** * Show datepicker overlay. * @param focus - move focus to the datepicker. * @returns create datepicker overlay instance */ showOverlay( focus = false, inputs?: DatepickerInputPartial ): ComponentRef<SiDatepickerOverlayComponent> { return this.showDatepicker().setInputs(inputs).focus(focus).datepickerRef!; } /** * Close datepicker. */ closeOverlay(): undefined { if (this.overlayRef?.hasAttached()) { this.overlayRef?.detach(); this.overlayRef?.dispose(); } this.datepickerRef = undefined; return undefined; } /** * Focus active cell in datepicker. * @param focus - show transfer focus. * @returns current instance. */ focus(focus = true): this { if (focus) { this.datepickerRef?.setInput('initialFocus', true); this.datepickerRef?.instance.focusActiveCell(); this.datepickerRef?.changeDetectorRef.markForCheck(); } return this; } /** * Indicate the datepicker is visible. * @returns is visible. */ isShown(): ComponentRef<SiDatepickerOverlayComponent> | undefined { return this.datepickerRef; } /** Set datepicker inputs */ setInputs(inputs?: DatepickerInputPartial): this { if (this.datepickerRef && inputs) { Object.entries(inputs).forEach(([key, value]) => { this.datepickerRef!.setInput(key.toString(), value); }); } return this; } /** Close overlay with cause select, which will recover the focus */ closeAfterSelection(): void { this.autoCloseSelection.next(); } /** Indicate whether the HTML element is a child of the datepicker overlay. */ contains(element: HTMLElement): boolean { if (!element) { return false; } return this.overlayRef?.overlayElement?.contains(element) ?? false; } private showDatepicker(): this { Iif (this.overlayRef?.hasAttached()) { return this; } // Since the connected overlay strategy has some limitations in small screens e.g. // the overlay is moved via style top without recalculating the (max)height which // can result in a cut of time picker. // To overcome this issue the overlay use the global position strategy in small screens. const smallScreen = this.mediaMatcher.matchMedia(this.smallScreenQuery).matches; Iif (smallScreen) { this.createMobileOverlay(); } else { this.createDesktopOverlay(); } this.closeStream(this.overlayRef!) .pipe( takeUntilDestroyed(this.destroyRef), takeUntil(outputToObservable(this.siDatepickerClose)) ) .subscribe(cause => { // The handler is called multiple times since we need to listen to detach events if (this.datepickerRef && !this.ignoreClose) { this.ignoreClose = true; this.closeOverlay(); this.ignoreClose = false; this.siDatepickerClose.emit(cause); } }); const portal = new ComponentPortal(SiDatepickerOverlayComponent); this.datepickerRef = this.overlayRef!.attach(portal); Iif (smallScreen) { this.datepickerRef.setInput('isMobile', true); } // Automatically close the overlay if we reach the small screen breakpoint // and the picker does not use the global position strategy. this.breakpointObserver .observe(this.smallScreenQuery) .pipe( takeUntilDestroyed(this.destroyRef), takeUntil(outputToObservable(this.siDatepickerClose)), skip(1) ) .subscribe(() => this.closeOverlay()); return this; } protected createMobileOverlay(): void { this.overlayRef = this.overlay.create({ positionStrategy: this.overlay.position().global().centerHorizontally().centerVertically(), direction: isRTL() ? 'rtl' : 'ltr', hasBackdrop: true, backdropClass: 'modal-backdrop' }); } protected createDesktopOverlay(): void { const popoverPositions = getOverlayPositions(this.triggerElementRef, this.placement(), false); this.overlayRef = this.overlay.create({ positionStrategy: this.overlay .position() .flexibleConnectedTo(this.triggerElementRef) .withPositions(popoverPositions) .withPush(true) .withGrowAfterOpen(true) .withFlexibleDimensions(true) .withViewportMargin(4), direction: isRTL() ? 'rtl' : 'ltr', hasBackdrop: true, backdropClass: 'cdk-overlay-transparent-backdrop' }); } /** * Merge events which shall close the overlay * @param overlayRef - source for backdrop, detach or escape events. * @returns merged observable */ private closeStream(overlayRef: OverlayRef): Observable<CloseCause> { return merge( this.autoCloseSelection.pipe(map(() => CloseCause.Select)), overlayRef.backdropClick().pipe(map(() => CloseCause.Backdrop)), overlayRef.detachments().pipe(map(() => CloseCause.Detach)), overlayRef.keydownEvents().pipe( filter(event => event.key === 'Escape'), tap(event => event.stopPropagation()), // ESC handled, prevent closing modal, etc. map(() => CloseCause.Escape) ) ); } } |