• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / 772d086c-2b84-41d9-a1da-d9c1f65d7d9c

11 Apr 2025 08:16AM UTC coverage: 90.209% (-0.03%) from 90.236%
772d086c-2b84-41d9-a1da-d9c1f65d7d9c

Pull #3335

circleci

wangyuan-ky
fix: error
Pull Request #3335: feat(date-picker): add timezone support to date picker components and utilities #TINFR-1734

5606 of 6877 branches covered (81.52%)

Branch coverage included in aggregate %.

39 of 41 new or added lines in 11 files covered. (95.12%)

48 existing lines in 8 files now uncovered.

13364 of 14152 relevant lines covered (94.43%)

992.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.74
/src/date-picker/lib/popups/date-popup.component.ts
1
import {
2
    endOfDay,
3
    FunctionProp,
4
    helpers,
5
    isFunction,
6
    isUndefinedOrNull,
7
    sortRangeValue,
8
    startOfDay,
9
    TinyDate,
10
    TinyDateCompareGrain
11
} from 'ngx-tethys/util';
12

13
import {
14
    ChangeDetectionStrategy,
15
    ChangeDetectorRef,
16
    Component,
17
    EventEmitter,
18
    inject,
1✔
19
    Input,
20
    OnChanges,
152✔
21
    OnInit,
152✔
22
    Output,
152✔
23
    Signal,
152✔
24
    SimpleChanges,
152✔
25
    TemplateRef
152✔
26
} from '@angular/core';
152✔
27

152✔
28
import { NgTemplateOutlet } from '@angular/common';
152✔
29
import { FormsModule } from '@angular/forms';
152✔
30
import { ThyButtonIcon } from 'ngx-tethys/button';
152✔
31
import { injectLocale, ThyDatePickerLocale } from 'ngx-tethys/i18n';
152✔
32
import { ThyNav, ThyNavItemDirective } from 'ngx-tethys/nav';
152✔
33
import { ThyDatePickerConfigService } from '../../date-picker.service';
152✔
34
import { CompatibleValue, DatePickerFlexibleTab, RangeAdvancedValue, RangePartType } from '../../inner-types';
152✔
35
import { dateAddAmount, getShortcutValue, hasValue, makeValue, setValueByTimestampPrecision, transformDateValue } from '../../picker.util';
152✔
36
import {
37
    CompatibleDate,
38
    CompatiblePresets,
1✔
39
    DisabledDateFn,
40
    SupportTimeOptions,
41
    ThyDateChangeEvent,
1✔
42
    ThyDateGranularity,
1✔
43
    ThyPanelMode,
44
    ThyShortcutPosition,
45
    ThyShortcutPreset,
152✔
46
    ThyShortcutValue
152✔
47
} from '../../standard-types';
152!
48
import { CalendarFooter } from '../calendar/calendar-footer.component';
×
49
import { DateCarousel } from '../date-carousel/date-carousel.component';
50
import { InnerPopup } from './inner-popup.component';
152!
51

×
52
/**
×
53
 * @private
54
 */
152✔
55
@Component({
152✔
56
    changeDetection: ChangeDetectionStrategy.OnPush,
152✔
57
    // eslint-disable-next-line @angular-eslint/component-selector
8✔
58
    selector: 'date-popup',
59
    exportAs: 'datePopup',
60
    templateUrl: './date-popup.component.html',
61
    imports: [ThyNav, ThyNavItemDirective, ThyButtonIcon, DateCarousel, FormsModule, NgTemplateOutlet, InnerPopup, CalendarFooter]
314✔
62
})
152✔
63
export class DatePopup implements OnChanges, OnInit {
61✔
64
    private cdr = inject(ChangeDetectorRef);
65
    private datePickerConfigService = inject(ThyDatePickerConfigService);
66
    locale: Signal<ThyDatePickerLocale> = injectLocale('datePicker');
91✔
67

68
    @Input() isRange: boolean;
69
    @Input() showWeek: boolean;
314✔
70

152✔
71
    @Input() format: string;
72
    @Input() disabledDate: DisabledDateFn;
314✔
73
    @Input() minDate: Date | number;
204✔
74
    @Input() maxDate: Date | number;
75
    @Input() showToday: boolean;
76

77
    /**
152✔
78
     * 是否支持设置时间(时、分)
152✔
79
     */
421✔
80
    @Input() showTime: SupportTimeOptions | boolean;
81

82
    /**
152✔
83
     * 是否展示时间(时、分)
94✔
84
     */
91✔
85
    @Input() mustShowTime: boolean;
86

94✔
87
    @Input() dateRender: FunctionProp<TemplateRef<Date> | string>;
94!
88
    @Input() className: string;
94✔
89
    @Input() panelMode: ThyPanelMode | ThyPanelMode[];
94✔
90
    @Input() value: CompatibleValue;
94✔
91
    @Input() defaultPickerValue: CompatibleDate | number;
94✔
92

94✔
93
    @Input() showShortcut: boolean;
30✔
94

114✔
95
    @Input() shortcutPresets: CompatiblePresets;
114✔
96

114✔
97
    @Input() shortcutPosition: ThyShortcutPosition;
114✔
98

114✔
99
    @Input() flexible: boolean;
8✔
100

101
    @Input() flexibleDateGranularity: ThyDateGranularity;
102

106✔
103
    @Input() timestampPrecision: 'seconds' | 'milliseconds';
104

105
    @Input() timeZone: string;
106

107
    @Output() readonly panelModeChange = new EventEmitter<ThyPanelMode | ThyPanelMode[]>();
64✔
108
    @Output() readonly calendarChange = new EventEmitter<CompatibleValue>();
133✔
109
    @Output() readonly valueChange = new EventEmitter<CompatibleValue | RangeAdvancedValue>();
133✔
110
    @Output() readonly resultOk = new EventEmitter<void>(); // Emitted when done with date selecting
133✔
111
    @Output() readonly showTimePickerChange = new EventEmitter<boolean>();
11✔
112
    @Output() readonly dateValueChange = new EventEmitter<ThyDateChangeEvent>();
113

114
    prefixCls = 'thy-calendar';
122✔
115
    showTimePicker = false;
116
    timeOptions: SupportTimeOptions | SupportTimeOptions[] | null;
117
    activeDate: TinyDate | TinyDate[];
118
    selectedValue: TinyDate[] = []; // Range ONLY
119
    hoverValue: TinyDate[] = []; // Range ONLY
120

121
    advancedSelectedValue: RangeAdvancedValue; // advanced ONLY
122

509✔
123
    flexibleActiveTab: DatePickerFlexibleTab = 'advanced';
509✔
124

143✔
125
    get hasTimePicker(): boolean {
143✔
126
        return !!this.showTime;
127
    }
509✔
128
    private partTypeMap: { [key: string]: number } = { left: 0, right: 1 };
246✔
129

208✔
130
    [property: string]: any;
131

246✔
132
    endPanelMode: ThyPanelMode | ThyPanelMode[];
133

134
    innerShortcutPresets: ThyShortcutPreset[];
263✔
135

136
    disableTimeConfirm = false;
509✔
137

138
    setProperty<T extends keyof DatePopup>(key: T, value: this[T]): void {
139
        this[key] = value;
152!
UNCOV
140
        this.cdr.markForCheck();
×
UNCOV
141
    }
×
142

143
    ngOnInit(): void {
UNCOV
144
        this.initShortcutPresets();
×
145
        this.initPanelMode();
146
        if (this.flexible && this.flexibleDateGranularity === 'day') {
147
            this.flexibleActiveTab = 'custom';
148
        }
152✔
149
        if (this.defaultPickerValue && !hasValue(this.value)) {
61✔
150
            const { value } = transformDateValue(this.defaultPickerValue);
151
            this.value = makeValue(value, this.isRange, this.timeZone);
152
        }
91✔
153
        this.updateActiveDate();
154
        this.initDisabledDate();
155
        if (this.isRange && this.flexible && this.value) {
156
            this.advancedSelectedValue = { begin: this.value[0], end: this.value[1], dateGranularity: this.flexibleDateGranularity };
157
        }
158
    }
159

160
    ngOnChanges(changes: SimpleChanges): void {
152✔
161
        if (changes.panelMode) {
15✔
162
            if (helpers.isArray(this.panelMode)) {
15✔
163
                this.endPanelMode = [...this.panelMode];
164
            } else {
152✔
165
                this.endPanelMode = this.panelMode;
13✔
166
            }
13✔
167
        }
168
        if (changes.defaultPickerValue) {
152✔
169
            this.updateActiveDate();
6✔
170
        }
171
        if (changes.value && changes.value.currentValue) {
152✔
172
            this.updateActiveDate();
21,722✔
173
        }
21,722✔
174
    }
2,310✔
175

176
    initShortcutPresets(): void {
21,722✔
177
        const { shortcutRangesPresets, shortcutDatePresets, showShortcut } = this.datePickerConfigService;
2,025✔
178

179
        this.showShortcut =
21,722✔
180
            ['date', 'date,date'].includes(this.panelMode.toString()) && isUndefinedOrNull(this.showShortcut)
630✔
181
                ? showShortcut
182
                : this.showShortcut;
21,722✔
183

184
        if (this.showShortcut) {
185
            if (!this.shortcutPresets) {
186
                this.shortcutPresets = this.isRange ? shortcutRangesPresets : shortcutDatePresets;
66✔
187
            }
66✔
188

189
            this.innerShortcutPresets = isFunction(this.shortcutPresets) ? this.shortcutPresets() : this.shortcutPresets;
190
            if (this.innerShortcutPresets.length) {
5✔
191
                const minDate: TinyDate = this.getMinTinyDate();
5✔
192
                const maxDate: TinyDate = this.getMaxTinyDate();
5✔
193

194
                const minTime = minDate ? minDate.getTime() : null;
195
                const maxTime = maxDate ? maxDate.getTime() : null;
2!
196

2✔
197
                if (this.isRange) {
198
                    this.innerShortcutPresets.forEach((preset: ThyShortcutPreset) => {
199
                        const begin: number | Date = getShortcutValue(preset.value[0]);
1!
200
                        const beginTime: number = new TinyDate(startOfDay(begin), this.timeZone).getTime();
UNCOV
201

×
UNCOV
202
                        const end: number | Date = getShortcutValue(preset.value[1]);
×
NEW
203
                        const endTime: number = new TinyDate(endOfDay(end), this.timeZone).getTime();
×
204

205
                        if ((minDate && endTime < minTime) || (maxDate && beginTime > maxTime)) {
UNCOV
206
                            preset.disabled = true;
×
207
                        } else {
208
                            preset.disabled = false;
209
                        }
210
                    });
211
                } else {
14✔
212
                    this.innerShortcutPresets.forEach((preset: ThyShortcutPreset) => {
1✔
213
                        const singleValue: number | Date = getShortcutValue(preset.value as ThyShortcutValue);
214
                        const singleTime: number = new TinyDate(singleValue, this.timeZone).getTime();
215

13✔
216
                        if ((minDate && singleTime < minTime) || (maxDate && singleTime > maxTime)) {
217
                            preset.disabled = true;
14✔
218
                        } else {
219
                            preset.disabled = false;
220
                        }
42✔
221
                    });
10✔
222
                }
10✔
223
            }
224
        }
225
    }
32✔
226

227
    updateActiveDate() {
228
        this.clearHoverValue();
229
        if (!this.value) {
26!
230
            const { value } = transformDateValue(this.defaultPickerValue);
231
            this.value = makeValue(value, this.isRange, this.timeZone);
232
        }
233
        if (this.isRange) {
26✔
234
            if (!this.flexible || this.flexibleDateGranularity === 'day') {
235
                this.selectedValue = this.value as TinyDate[];
236
            }
237
            this.activeDate = this.normalizeRangeValue(this.value as TinyDate[], this.getPanelMode(this.endPanelMode) as ThyPanelMode);
2✔
238
        } else {
239
            this.activeDate = this.value as TinyDate;
240
        }
3✔
241
        this.isDisableTimeConfirm();
3!
242
    }
3✔
243

244
    initPanelMode() {
UNCOV
245
        if (!this.endPanelMode) {
×
246
            if (helpers.isArray(this.panelMode)) {
247
                this.endPanelMode = [...this.panelMode];
3✔
248
            } else {
249
                this.endPanelMode = this.panelMode;
250
            }
5✔
251
        } else {
252
            if (helpers.isArray(this.endPanelMode)) {
5✔
253
                this.panelMode = [...this.endPanelMode];
5✔
254
            } else {
255
                this.panelMode = this.endPanelMode;
256
            }
38✔
257
        }
258
    }
24✔
259

24✔
260
    initDisabledDate(): void {
24✔
261
        let minDate: TinyDate;
262
        let maxDate: TinyDate;
12✔
263
        let disabledDateFn: DisabledDateFn;
12✔
264
        if (this.minDate) {
12✔
265
            const { value } = transformDateValue(this.minDate);
266
            minDate = new TinyDate(value as Date, this.timeZone);
12!
267
        }
268
        if (this.maxDate) {
12✔
269
            const { value } = transformDateValue(this.maxDate);
12✔
270
            maxDate = new TinyDate(value as Date, this.timeZone);
12✔
271
        }
12✔
272
        if (this.disabledDate) {
12✔
273
            disabledDateFn = this.disabledDate;
12✔
274
        }
12✔
275
        this.disabledDate = d => {
12✔
276
            let expression = false;
277
            if (minDate) {
278
                expression = d < minDate.startOfDay().nativeDate;
279
            }
14✔
280
            if (maxDate && !expression) {
14✔
281
                expression = d > maxDate.endOfDay().nativeDate;
14✔
282
            }
283
            if (disabledDateFn && typeof disabledDateFn === 'function' && !expression) {
284
                expression = disabledDateFn(d);
285
            }
12✔
286
            return expression;
12✔
287
        };
1✔
288
    }
289

11✔
290
    onShowTimePickerChange(show: boolean): void {
1✔
291
        this.showTimePicker = show;
292
        this.showTimePickerChange.emit(show);
10!
UNCOV
293
    }
×
294

295
    onClickOk(): void {
10!
UNCOV
296
        this.setValue(this.value);
×
297
        this.valueChange.emit(this.value);
298
        this.resultOk.emit();
299
    }
10✔
300

301
    onClickRemove(): void {
302
        this.value = this.isRange ? [] : null;
303
        this.valueChange.emit(this.value);
25✔
304
    }
12✔
305

306
    onDayHover(value: TinyDate): void {
13✔
307
        if (this.isRange && this.selectedValue[0] && !this.selectedValue[1]) {
13✔
308
            // When right value is selected, don't do hover
13✔
309
            const base = this.selectedValue[0]; // Use the left of selected value as the base to decide later hoverValue
13✔
310
            if (base.isBeforeDay(value)) {
13✔
311
                this.hoverValue = [base, value];
11✔
312
            } else {
313
                this.hoverValue = [value, base];
314
            }
2✔
315
        }
316
    }
317

318
    onPanelModeChange(mode: ThyPanelMode, partType?: RangePartType): void {
1,024✔
319
        if (this.isRange) {
4✔
320
            (this.panelMode as ThyPanelMode[])[this.getPartTypeIndex(partType)] = mode;
4✔
321
        } else {
4✔
322
            this.panelMode = mode;
2✔
323
        }
324
        this.panelModeChange.emit(this.panelMode);
2✔
325
    }
326

327
    onHeaderChange(value: TinyDate, partType?: RangePartType): void {
1,020✔
328
        if (this.isRange) {
329
            this.activeDate[this.getPartTypeIndex(partType)] = value;
330
            this.activeDate = this.normalizeRangeValue(
331
                this.activeDate as TinyDate[],
1,302✔
332
                this.getPanelMode(this.endPanelMode, partType) as ThyPanelMode
848✔
333
            );
334
        } else {
335
            this.activeDate = value;
454✔
336
        }
337
    }
338

339
    onSelectTime(value: TinyDate, partType?: RangePartType): void {
511✔
340
        if (this.isRange) {
284✔
341
            // TODO:range picker set time
284✔
342
        } else {
343
            this.setValue(new TinyDate(value.nativeDate, this.timeZone));
344
        }
227✔
345
    }
346

347
    selectTab(active: DatePickerFlexibleTab) {
348
        this.flexibleActiveTab = active;
511✔
349
    }
284✔
350

351
    clearFlexibleValue() {
352
        this.flexibleDateGranularity = null;
227✔
353
        if (this.flexibleActiveTab === 'advanced') {
354
            this.advancedSelectedValue = {};
355
        } else {
379✔
356
            this.selectedValue = [];
1,439✔
357
        }
358
        this.valueChange.emit({ begin: null, end: null, dateGranularity: this.flexibleDateGranularity });
359
    }
213✔
360

361
    changeValueFromAdvancedSelect(value: RangeAdvancedValue) {
362
        this.valueChange.emit(value);
213✔
363
        // clear custom date when select a advanced date
364
        this.selectedValue = [];
365
        this.dateValueChange.emit({ value: [value.begin, value.end] });
521✔
366
    }
367

368
    changeValueFromSelect(value: TinyDate, partType?: RangePartType): void {
78✔
369
        if (this.isRange) {
78✔
370
            // clear advanced date when select a custom date
1✔
371
            this.advancedSelectedValue = {};
1✔
372

373
            const [left, right] = this.selectedValue as TinyDate[];
374

77✔
375
            if ((!left && !right) || (left && right)) {
43✔
376
                // If totally full or empty, clean up && re-assign left first
377
                this.hoverValue = this.selectedValue = [value];
378
                this.selectedValue = [this.selectedValue[0].startOfDay()];
78✔
379
                this.calendarChange.emit([this.selectedValue[0].clone()]);
380
            } else if (left && !right) {
×
381
                // If one of them is empty, assign the other one and sort, then set the final values
268✔
382
                this.clearHoverValue(); // Clean up
383
                this.setRangeValue('right', value);
384
                this.selectedValue = sortRangeValue(this.selectedValue); // Sort
385
                this.selectedValue = this.getSelectedRangeValueByMode(this.selectedValue);
386
                this.activeDate = this.normalizeRangeValue(
387
                    this.selectedValue,
388
                    this.getPanelMode(this.endPanelMode, partType) as ThyPanelMode
268✔
389
                );
268✔
390
                this.setValue(this.cloneRangeDate(this.selectedValue));
268✔
391
                this.calendarChange.emit(this.cloneRangeDate(this.selectedValue));
268✔
392
                this.dateValueChange.emit({ value: this.cloneRangeDate(this.selectedValue) });
268✔
393
            }
214✔
394
        } else {
395
            const updatedValue = this.updateHourMinute(value);
268✔
396
            this.setValue(updatedValue);
397
            this.dateValueChange.emit({ value: updatedValue });
398
        }
12✔
399
    }
12✔
400

401
    private getSelectedRangeValueByMode(value: TinyDate[]): TinyDate[] {
402
        const panelMode = this.getPanelMode(this.endPanelMode);
58✔
403
        if (panelMode === 'year') {
404
            return [value[0].startOfYear(), value[1].endOfYear()];
405
        } else if (panelMode === 'quarter') {
587✔
406
            return [value[0].startOfQuarter(), value[1].endOfQuarter()];
489✔
407
        } else if (panelMode === 'month') {
408
            return [value[0].startOfMonth(), value[1].endOfMonth()];
98✔
409
        } else if (panelMode === 'week') {
98✔
410
            return [value[0].startOfISOWeek(), value[1].endOfISOWeek()];
98✔
411
        } else {
98✔
412
            return [value[0].startOfDay(), value[1].endOfDay()];
16✔
413
        }
414
    }
415

82✔
416
    private updateHourMinute(value: TinyDate): TinyDate {
417
        if (!this.value) {
418
            return value;
419
        }
21✔
420
        const originDate = this.value as TinyDate;
21✔
421
        const dateTime = [value.getHours(), value.getMinutes(), value.getSeconds()];
21✔
422
        const originDateTime = [originDate.getHours(), originDate.getMinutes(), originDate.getSeconds()];
21✔
423

21✔
424
        const isEqualTime = dateTime.toString() === originDateTime.toString();
10✔
425
        if (isEqualTime) {
10✔
426
            return value;
10✔
427
        } else {
10✔
428
            return value.setHms(originDateTime[0], originDateTime[1], originDateTime[2]);
10!
UNCOV
429
        }
×
430
    }
431

10✔
432
    enablePrevNext(direction: 'prev' | 'next', partType?: RangePartType): boolean {
1✔
433
        if (this.isRange && this.panelMode === this.endPanelMode) {
434
            const [start, end] = this.activeDate as TinyDate[];
9✔
435
            const showMiddle = !start.addMonths(1).isSame(end, 'month'); // One month diff then don't show middle prev/next
1✔
436
            if ((partType === 'left' && direction === 'next') || (partType === 'right' && direction === 'prev')) {
437
                return showMiddle;
8✔
438
            }
1✔
439
            return true;
440
        } else {
7✔
441
            return true;
442
        }
443
    }
11✔
444

11!
UNCOV
445
    getPanelMode(panelMode: ThyPanelMode | ThyPanelMode[], partType?: RangePartType): ThyPanelMode {
×
446
        if (this.isRange) {
447
            return panelMode[this.getPartTypeIndex(partType)] as ThyPanelMode;
11✔
448
        } else {
449
            return panelMode as ThyPanelMode;
450
        }
451
    }
24✔
452

2✔
453
    getValueBySelector(partType?: RangePartType): TinyDate {
454
        if (this.isRange) {
22✔
455
            const valueShow = this.selectedValue; // Use the real time value that without decorations when timepicker is shown up
22✔
456
            return (valueShow as TinyDate[])[this.getPartTypeIndex(partType)];
1✔
457
        } else {
458
            return this.value as TinyDate;
459
        }
21✔
460
    }
10✔
461

10✔
462
    getActiveDate(partType?: RangePartType): TinyDate {
10!
463
        if (this.isRange) {
10✔
464
            return this.activeDate[this.getPartTypeIndex(partType)];
465
        } else {
466
            return this.activeDate as TinyDate;
467
        }
10✔
468
    }
469

470
    getPartTypeIndex(partType: RangePartType = 'left'): number {
471
        return this.partTypeMap[partType];
11✔
472
    }
11✔
473

11✔
474
    private getMinTinyDate() {
11✔
475
        return this.minDate ? new TinyDate(transformDateValue(this.minDate).value as Date, this.timeZone) : null;
476
    }
21✔
477

21✔
478
    private getMaxTinyDate() {
21✔
479
        return this.maxDate ? new TinyDate(transformDateValue(this.maxDate).value as Date, this.timeZone) : null;
21✔
480
    }
481

482
    private clearHoverValue(): void {
21✔
483
        this.hoverValue = [];
1✔
484
    }
485

486
    private setValue(value: CompatibleValue): void {
487
        this.value = value;
11✔
488
        if (this.isRange && this.flexible) {
489
            this.flexibleDateGranularity = 'day';
1✔
490
            this.valueChange.emit({ begin: value[0], end: value[1], dateGranularity: this.flexibleDateGranularity });
491
        } else {
492
            if (!this.showTime || !this.showTimePicker) {
493
                this.valueChange.emit(this.value);
494
            }
495
        }
496
        this.isDisableTimeConfirm();
497
    }
498

499
    private normalizeRangeValue(value: TinyDate[], mode: ThyPanelMode = 'month'): TinyDate[] {
500
        const headerModes: { [key in ThyPanelMode]?: ThyPanelMode } = {
501
            week: 'month',
502
            date: 'month',
503
            month: 'year',
504
            quarter: 'year',
505
            year: 'decade'
506
        };
507
        const headerMode = headerModes[mode];
508
        const [start, end] = value;
509
        const newStart = start || new TinyDate(undefined, this.timeZone);
510
        let newEnd = end;
511
        if (!newEnd || newStart.isSame(end, headerMode as TinyDateCompareGrain)) {
512
            newEnd = dateAddAmount(newStart, 1, headerMode);
513
        }
514
        return [newStart, newEnd];
515
    }
516

517
    private setRangeValue(partType: RangePartType, value: TinyDate): void {
518
        const ref = (this.selectedValue = this.cloneRangeDate(this.selectedValue as TinyDate[]));
519
        ref[this.getPartTypeIndex(partType)] = value;
1✔
520
    }
521

522
    private cloneRangeDate(value: TinyDate[]): TinyDate[] {
523
        return [value[0] && value[0].clone(), value[1] && value[1].clone()] as TinyDate[];
524
    }
525

526
    private isDisableTimeConfirm() {
527
        if (this.isRange || !this.showTime) {
528
            return;
529
        }
530

531
        const date: TinyDate = this.value ? (this.value as TinyDate) : new TinyDate(undefined, this.timeZone);
532
        const minDate: TinyDate = this.getMinTinyDate();
533
        const maxDate: TinyDate = this.getMaxTinyDate();
534

535
        if ((minDate && date.getTime() < minDate.getTime()) || (maxDate && date.getTime() > maxDate.getTime())) {
536
            this.disableTimeConfirm = true;
537
        } else {
538
            this.disableTimeConfirm = false;
539
        }
540
    }
541

542
    private getSelectedShortcutPreset(date: CompatibleValue): CompatibleValue {
543
        const minDate: TinyDate = this.getMinTinyDate();
544
        const maxDate: TinyDate = this.getMaxTinyDate();
545

546
        const minTime: number = (minDate && minDate.getTime()) || null;
547
        const maxTime: number = (maxDate && maxDate.getTime()) || null;
548

549
        if (helpers.isArray(date)) {
550
            const startDate: TinyDate = date[0];
551
            const endDate: TinyDate = date[1];
552

553
            const startTime: number = startDate.getTime();
554
            const endTime: number = endDate.getTime();
555

556
            if ((maxDate && startTime > maxTime) || (minDate && endTime < minTime)) {
557
                return [];
558
            }
559

560
            if (minDate && startTime < minTime && maxDate && endTime > maxTime) {
561
                return [minDate, maxDate];
562
            }
563

564
            if (minDate && startTime < minTime) {
565
                return [minDate, endDate];
566
            }
567

568
            if (maxDate && endTime > maxTime) {
569
                return [startDate, maxDate];
570
            }
571

572
            return date;
573
        } else {
574
            const singleTime: number = date.getTime();
575

576
            if ((minDate && singleTime < minTime) || (maxDate && singleTime > maxTime)) {
577
                return null;
578
            }
579

580
            return date;
581
        }
582
    }
583

584
    shortcutSetValue(shortcutPresets: ThyShortcutPreset) {
585
        if (shortcutPresets.disabled) {
586
            return;
587
        }
588

589
        const { value } = shortcutPresets;
590
        if (!value) {
591
            return;
592
        }
593

594
        let selectedPresetValue: CompatibleValue;
595
        if (helpers.isArray(value)) {
596
            const begin: number | Date = getShortcutValue(value[0]);
597
            const end: number | Date = getShortcutValue(value[1]);
598

599
            if (begin && end) {
600
                this.selectedValue = this.getSelectedShortcutPreset([
601
                    new TinyDate(begin, this.timeZone).startOfDay(),
602
                    new TinyDate(end, this.timeZone).endOfDay()
603
                ]) as TinyDate[];
604
                selectedPresetValue = this.cloneRangeDate(this.selectedValue);
605
            }
606
        } else {
607
            const originDate = this.value as TinyDate;
608
            const zonedTime = this.createInZoneTime(
609
                new TinyDate(getShortcutValue(value), this.timeZone),
610
                originDate?.getHours() ?? 0,
611
                originDate?.getMinutes() ?? 0,
612
                originDate?.getSeconds() ?? 0
613
            );
614
            const singleTinyDate: TinyDate = this.updateHourMinute(new TinyDate(zonedTime, this.timeZone));
615
            selectedPresetValue = this.getSelectedShortcutPreset(singleTinyDate) as TinyDate;
616
        }
617
        this.setValue(selectedPresetValue);
618
        const shortcutPresetsValue = setValueByTimestampPrecision(
619
            shortcutPresets?.value,
620
            this.isRange,
621
            this.timestampPrecision,
622
            this.timeZone
623
        ) as number;
624
        this.dateValueChange.emit({
625
            value: helpers.isArray(value) ? this.selectedValue : selectedPresetValue,
626
            triggerPreset: Object.assign({}, shortcutPresets, { value: shortcutPresetsValue })
627
        });
628
        if (!helpers.isArray(value) && this.showTime && this.showTimePicker) {
629
            this.updateActiveDate();
630
        }
631
    }
632

633
    private createInZoneTime(date: TinyDate, hours?: number, minutes?: number, seconds?: number): Date {
634
        return TinyDate.createDateInTimeZone(date.getYear(), date.getMonth(), date.getDate(), hours, minutes, seconds, this.timeZone);
635
    }
636
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc