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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | 1x 107x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 39x 15x 15x 15x 15x 15x 14x 126x 126x 14x 15x 4x 4x 15x 15x 15x 15x 40x 27x 40x 1x 1x 66x 66x 64x 66x 25x 28x 28x 28x 28x 28x 28x 28x 28x 28x 15x 13x 13x 59x 16x 16x 16x 3x 3x 7x 4x 4x 3x 3x 1x 1x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 17x 2x 2x 15x 15x 15x 5x 5x 1x 1x 4x 5x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { CdkTrapFocus } from '@angular/cdk/a11y'; import { MediaMatcher } from '@angular/cdk/layout'; import { CdkListbox, CdkOption } from '@angular/cdk/listbox'; import { OverlayModule } from '@angular/cdk/overlay'; import { DatePipe, NgTemplateOutlet } from '@angular/common'; import { booleanAttribute, ChangeDetectionStrategy, Component, computed, inject, input, model, OnChanges, output, Pipe, PipeTransform, signal, SimpleChanges } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { DatepickerConfig, DatepickerInputConfig, DateRange, isValid, SiCalendarButtonComponent, SiDatepickerComponent, SiDatepickerDirective } from '@siemens/element-ng/datepicker'; import { addIcons, elementDown2, SiIconComponent } from '@siemens/element-ng/icon'; import { BOOTSTRAP_BREAKPOINTS } from '@siemens/element-ng/resize-observer'; import { SiSearchBarComponent } from '@siemens/element-ng/search-bar'; import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate'; import { SiDateRangeCalculationService } from './si-date-range-calculation.service'; import { DateRangeFilter, DateRangePreset, ONE_DAY, ResolvedDateRange } from './si-date-range-filter.types'; import { SiRelativeDateComponent } from './si-relative-date.component'; @Pipe({ name: 'presetMatchFilter', pure: true }) export class PresetMatchFilterPipe implements PipeTransform { transform(value: string, term: string): boolean { return !term ? true : value.toLowerCase().includes(term.toLowerCase()); } } @Component({ selector: 'si-date-range-filter', imports: [ CdkOption, CdkListbox, CdkTrapFocus, DatePipe, FormsModule, NgTemplateOutlet, OverlayModule, PresetMatchFilterPipe, SiCalendarButtonComponent, SiDatepickerComponent, SiDatepickerDirective, SiIconComponent, SiRelativeDateComponent, SiSearchBarComponent, SiTranslatePipe ], templateUrl: './si-date-range-filter.component.html', styleUrl: './si-date-range-filter.component.scss', changeDetection: ChangeDetectionStrategy.OnPush, host: { '[class.mobile]': 'smallScreen' } }) export class SiDateRangeFilterComponent implements OnChanges { private readonly service = inject(SiDateRangeCalculationService); private readonly mediaMatcher = inject(MediaMatcher); protected readonly smallScreen = this.mediaMatcher.matchMedia( `(max-width: ${BOOTSTRAP_BREAKPOINTS.mdMinimum}px)` ).matches; /** The filter range object */ readonly range = model.required<DateRangeFilter>(); /** List of preset time ranges. When not present or empty, the preset section won't show */ readonly presetList = input<DateRangePreset[]>(); /** * Determines if there's a search field for the preset list * * @defaultValue true */ readonly presetSearch = input(true, { transform: booleanAttribute }); /** * Determines if time is selectable or only dates * * @defaultValue false */ readonly enableTimeSelection = input(false, { transform: booleanAttribute }); /** * Determines whether to show input fields or a date-range calendar in basic mode. * When time selection is enabled, this has no effect and input fields are always shown. * * @defaultValue 'calendar' */ readonly basicMode = input<'input' | 'calendar'>('calendar'); /** * Reverses the order of the from/to fields * * @defaultValue false */ readonly reverseInputFields = input(false, { transform: booleanAttribute }); /** * Determines whether to show the 'Apply' button * * @defaultValue false */ readonly showApplyButton = input(false, { transform: booleanAttribute }); /** * Hides the advanced mode if input allows * * @defaultValue false */ readonly hideAdvancedMode = input(false, { transform: booleanAttribute }); /** * label for the "Reference point" title * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.REF_POINT:Reference point`) * ``` */ readonly refLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.REF_POINT:Reference point`)); /** * label for the "Reference point" title * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.FROM:From`) * ``` */ readonly fromLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.FROM:From`)); /** * label for the "Reference point" title * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.TO:To`) * ``` */ readonly toLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.TO:To`)); /** * label for the "Range" title * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.RANGE:Range`) * ``` */ readonly rangeLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.RANGE:Range`)); /** * label for the "Today" checkbox * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.TODAY:Today`) * ``` */ readonly todayLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.TODAY:Today`)); /** * label for the "Now" checkbox * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.NOW:Now`) * ``` */ readonly nowLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.NOW:Now`)); /** * label for "Date" field / radio button * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.DATE:Date`) * ``` */ readonly dateLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.DATE:Date`)); /** * label for the "Preview" title * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.PREVIEW:Preview`) * ``` */ readonly previewLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.PREVIEW:Preview`)); /** * Placeholder for date fields * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.DATE_PLACEHOLDER:Select date`) * ``` */ readonly datePlaceholder = input( t(() => $localize`:@@SI_DATE_RANGE_FILTER.DATE_PLACEHOLDER:Select date`) ); /** * label for the "Before" toggle * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.BEFORE:Before`) * ``` */ readonly beforeLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.BEFORE:Before`)); /** * label for the "After" toggle * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.AFTER:After`) * ``` */ readonly afterLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.AFTER:After`)); /** * label for the "Within" toggle * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.WITHIN:Within`) * ``` */ readonly withinLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.WITHIN:Within`)); /** * label for the "value" number input * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.VALUE:Value`) * ``` */ readonly valueLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.VALUE:Value`)); /** * label for the "Unit" select * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.UNIT:Unit`) * ``` */ readonly unitLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.UNIT:Unit`)); /** * label for the "search" input * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.SEARCH:Search`) * ``` */ readonly searchLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.SEARCH:Search`)); /** * label for the "search" input * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.PRESETS:Presets`) * ``` */ readonly presetLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.PRESETS:Presets`)); /** * label for the "advanced" switch * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.ADVANCED:Advanced`) * ``` */ readonly advancedLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.ADVANCED:Advanced`)); /** * label for the "apply" switch * * @defaultValue * ``` * t(() => $localize`:@@SI_DATE_RANGE_FILTER.APPLY:Apply`) * ``` */ readonly applyLabel = input(t(() => $localize`:@@SI_DATE_RANGE_FILTER.APPLY:Apply`)); /** Event fired when the apply button has been clicked */ readonly applyClicked = output<void>(); /** Base configuration on how the dates should be displayed, parts of it may be overwritten internally. */ readonly datepickerConfig = input<DatepickerInputConfig>(); protected readonly icons = addIcons({ elementDown2 }); protected advancedMode = false; protected dateRange: DateRange = { start: undefined, end: undefined }; protected point1Now = true; protected point2Mode: 'duration' | 'date' = 'duration'; protected point1date = this.getDateNow(); protected point2date = this.getDateNow(); protected point2offset = 0; protected point2range: 'before' | 'after' | 'within' = 'before'; protected readonly calculatedRange = computed<ResolvedDateRange>(() => this.resolve(this.range()) ); protected readonly pipeFormat = computed<string>(() => this.enableTimeSelection() ? (this.datepickerConfig()?.dateTimeFormat ?? 'short') : (this.datepickerConfig()?.dateFormat ?? 'shortDate') ); protected readonly datepickerConfigInternal = computed<DatepickerConfig>(() => ({ ...(this.datepickerConfig() ?? {}), enableDateRange: false, showTime: this.enableTimeSelection(), mandatoryTime: this.enableTimeSelection() })); protected readonly dateRangeConfig = computed<DatepickerConfig>(() => ({ ...(this.datepickerConfig() ?? {}), enableDateRange: true })); protected readonly filteredPresetList = computed<DateRangePreset[]>(() => { const timeFilter = (item: DateRangePreset): boolean => { const timeOnly = item.type === 'custom' ? item.timeOnly : item.offset < ONE_DAY; return this.enableTimeSelection() || !timeOnly; }; return (this.presetList() ?? []).filter(timeFilter); }); protected readonly focusedDate = computed(() => { const date = this.dateRange.end ?? this.dateRange.start; return isValid(date) ? date : undefined; }); protected readonly presetFilter = signal(''); protected readonly presetOpen = signal(false); protected readonly inputMode = computed( () => this.basicMode() === 'input' || this.enableTimeSelection() ); ngOnChanges(changes: SimpleChanges): void { if ( changes.enableTimeSelection || changes.datepickerConfig || (changes.range && this.rangeChanged(changes.range.previousValue, changes.range.currentValue)) ) { this.updateFromRange(); } if ( (changes.basicMode || changes.enableTimeSelection) && this.inputMode() && !this.advancedMode ) { this.point2Mode = 'date'; this.point2Changed(); } } private getDateNow(): Date { const now = new Date(); if (!this.enableTimeSelection()) { this.service.removeTime(now); } return now; } private rangeChanged(oldRange: DateRangeFilter | undefined, newRange: DateRangeFilter): boolean { return ( !oldRange || oldRange.point1 !== newRange.point1 || oldRange.point2 !== newRange.point2 || oldRange.range !== newRange.range ); } private updateFromRange(): void { const rangeVal = this.range(); this.point1Now = rangeVal.point1 === 'now'; this.point1date = rangeVal.point1 === 'now' ? this.getDateNow() : rangeVal.point1; if (rangeVal.point2) { this.point2Mode = rangeVal.point2 instanceof Date ? 'date' : 'duration'; this.point2date = rangeVal.point2 instanceof Date ? rangeVal.point2 : this.getDateNow(); this.point2range = rangeVal.range ?? 'before'; this.point2offset = rangeVal.point2 instanceof Date ? Math.round(this.point1date.getTime() - rangeVal.point2.getTime()) : rangeVal.point2; } if ((this.point1Now && this.basicMode() !== 'input') || this.point2Mode !== 'date') { this.advancedMode = true; } else { this.advancedMode = false; this.updateDateRange(); } } private resolve(range: DateRangeFilter, skipNormalization?: boolean): ResolvedDateRange { return this.service.resolveDateRangeFilter(range, { withTime: this.enableTimeSelection(), skipNormalization }); } protected updateDateRange(range = this.range()): void { const calculatedRange = this.resolve(range); this.dateRange.start = calculatedRange.start; this.dateRange.end = calculatedRange.end; } protected updateOnModeChange(): void { Iif (this.advancedMode) { const calculatedRange = this.resolve(this.range()); this.point2Mode = 'duration'; this.point2range = 'after'; this.point2offset = Math.abs(calculatedRange.end.getTime() - calculatedRange.start.getTime()); } else { this.updateSimpleMode(this.range()); } } private updateSimpleMode(newRange: DateRangeFilter): void { if (this.inputMode()) { this.range.update(oldRange => ({ ...oldRange, point1: newRange.point1 })); // input mode supports `now`, so point1 needs to remain unchanged if (newRange.point1 === 'now') { this.point1Now = true; this.point1date = this.getDateNow(); } else { this.point1Now = false; this.point1date = newRange.point1; } const calculatedRange = this.resolve(newRange, true); this.point2Mode = 'date'; this.point2date = calculatedRange.end; this.range.update(oldRange => ({ ...oldRange, range: undefined, point2: calculatedRange.end })); } else { this.point1Now = false; this.updateDateRange(newRange); this.updateFromDateRange(); } } protected updateFromDateRange(): void { const startDate = this.dateRange.start ?? this.getDateNow(); const endDate = this.dateRange.end ?? this.getDateNow(); this.point1date = startDate; this.point2date = startDate; this.range.set({ point1: startDate, point2: endDate, range: undefined }); this.point2Mode = 'date'; this.point2offset = 0; } protected point1Changed(): void { Iif (this.point1Now) { this.range.update(oldRange => ({ ...oldRange, point1: 'now' })); this.point1date = this.getDateNow(); Iif (this.point2Mode !== 'date') { this.point2range ??= 'before'; } } else { this.range.update(oldRange => ({ ...oldRange, point1: this.point1date ?? new Date(NaN) })); } } protected point2Changed(): void { if (this.point2Mode === 'date') { Iif (!(this.range().point2 instanceof Date)) { const calculatedRange = this.resolve(this.range()); Iif (calculatedRange.valid) { this.point2date = this.point1date < calculatedRange.end ? calculatedRange.start : calculatedRange.end; } } this.range.update(oldRange => ({ ...oldRange, range: undefined, point2: this.point2date })); } else { this.range.update(oldRange => ({ ...oldRange, range: this.point2range })); Iif (this.range().point2 instanceof Date) { const calculatedRange = this.resolve(this.range()); this.point2offset = Math.round( calculatedRange.end.getTime() - calculatedRange.start.getTime() ); } this.range.update(oldRange => ({ ...oldRange, point2: this.point2offset })); } } protected selectPresetItem(event: Event, item: DateRangePreset): void { const newRange: DateRangeFilter = item.type === 'custom' ? item.calculate(item, this.range()) : { point1: 'now', range: 'before', point2: item.offset }; if (this.advancedMode) { Object.assign(this.range(), newRange); this.updateFromRange(); } else { this.updateSimpleMode(newRange); } Iif (this.smallScreen) { // Prevent re-opening the dropdown when pressing enter on the selected item event.preventDefault(); this.presetOpen.set(false); } } } |