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 | 1x 56x 56x 69x 1x 1x 8x 8x 7x 7x 32x 10x 22x 28x 29x 29x 22x 22x 22x 22x 22x 11x 2x 9x 9x 2x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { AfterViewInit, booleanAttribute, ComponentRef, Directive, ElementRef, HostListener, inject, input } from '@angular/core'; import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms'; import { SI_FORM_ITEM_CONTROL } from '@siemens/element-ng/form'; import { Subscription } from 'rxjs'; import { SiDateInputDirective } from './si-date-input.directive'; import { SiDatepickerOverlayComponent } from './si-datepicker-overlay.component'; import { SiDatepickerOverlayDirective } from './si-datepicker-overlay.directive'; import { getDatepickerFormat } from './si-datepicker.model'; @Directive({ selector: '[siDatepicker]', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: SiDatepickerDirective, multi: true }, { provide: NG_VALIDATORS, useExisting: SiDatepickerDirective, multi: true }, { provide: SI_FORM_ITEM_CONTROL, useExisting: SiDatepickerDirective } ], hostDirectives: [ { directive: SiDatepickerOverlayDirective, outputs: ['siDatepickerClose'] } ], exportAs: 'siDatepicker' }) export class SiDatepickerDirective extends SiDateInputDirective implements AfterViewInit { /** * Automatically close overlay on date selection. * Do not use this behavior with config showTime = true, because it * will close the overlay when the user change one of the time units. * * @defaultValue false */ readonly autoClose = input(false, { transform: booleanAttribute }); /** * During focus on close the datepicker will not show since we recover the focus on element. * The focus on close is only relevant when the directive is configured without a calendar button. */ private overlaySubscriptions?: Subscription[]; private externalTrigger?: ElementRef<HTMLElement>; private readonly overlayToggle = inject(SiDatepickerOverlayDirective); ngAfterViewInit(): void { // Update datepicker with new date value this.dateChange.subscribe(date => this.overlayToggle.setInputs({ date })); } /** @internal */ touch(): void { this.onTouched(); } /** * On click shall show datepicker. */ @HostListener('click') protected onClick(): void { if (!this.externalTrigger) { this.show(); } } /** * Focus out shall close the datepicker except we are moving the focus to the datepicker. * @param event - focus out event with the related target */ protected override onBlur(event: FocusEvent): void { const target = event.relatedTarget as HTMLElement; if (!this.externalTrigger && !this.overlayToggle.contains(target)) { this.overlayToggle.closeOverlay(); this.onTouched(); } } @HostListener('keydown.tab') protected onTab(): void { Iif (this.overlayToggle.isShown()) { this.overlayToggle.closeOverlay(); } } /** * @internal */ public show(initialFocus = false): void { if (this.disabled() || this.readonly() || this.overlayToggle.isShown()) { return; } this.subscribeDateChanges( this.overlayToggle.showOverlay(initialFocus, { config: this.siDatepickerConfig(), date: this.date, time12h: this.getTime12h() }) ); } /** * @internal */ public useExternalTrigger(element: ElementRef<HTMLElement>): void { this.externalTrigger = element; } @HostListener('focus') protected focusChange(): void { if (!this.externalTrigger) { this.show(); } } private getTime12h(): boolean | undefined { const dateFormat = getDatepickerFormat(this.locale, this.siDatepickerConfig(), true); return dateFormat?.includes('a'); } private subscribeDateChanges(overlay?: ComponentRef<SiDatepickerOverlayComponent>): void { this.overlaySubscriptions?.forEach(s => s.unsubscribe()); overlay?.instance.date.subscribe(d => this.onDateChanged(d)); overlay?.instance.disabledTimeChange.subscribe(d => this.onDisabledTime(d)); } /** * Callback when the datepicker changes his value. * @param date - updated date */ protected override onDateChanged(date?: Date): void { // While typing a date can be invalid and the datepicker component will automatically change the date to undefined. // Since we don't want to reset the current input value it is necessary to ignore those updates. if (!date) { return; } super.onDateChanged(date); if (this.autoClose()) { // a tick later so the event won't end on the wrong element setTimeout(() => this.overlayToggle.closeAfterSelection()); } } } |