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 | 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 90x 23x 23x 20x 20x 20x 20x 20x 20x 20x 20x 76x 76x 76x 76x 19x 19x 19x 20x 1x 2x 1x 1x 20x 26x 20x 20x 20x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import {
booleanAttribute,
ChangeDetectionStrategy,
Component,
computed,
input,
OnChanges,
output,
signal,
SimpleChanges
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SiNumberInputComponent } from '@siemens/element-ng/number-input';
import {
SelectOption,
SiSelectComponent,
SiSelectSimpleOptionsDirective,
SiSelectSingleValueDirective
} from '@siemens/element-ng/select';
import { t } from '@siemens/element-translate-ng/translate';
import { ONE_DAY, ONE_MINUTE } from './si-date-range-filter.types';
interface OffsetOption extends SelectOption<string> {
offset: number;
}
@Component({
selector: 'si-relative-date',
imports: [
FormsModule,
SiNumberInputComponent,
SiSelectComponent,
SiSelectSingleValueDirective,
SiSelectSimpleOptionsDirective
],
templateUrl: './si-relative-date.component.html',
styleUrl: './si-relative-date.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiRelativeDateComponent implements OnChanges {
/** @defaultValue 0 */
// eslint-disable-next-line @angular-eslint/prefer-signal-model
readonly value = input(0);
/** @defaultValue false */
readonly enableTimeSelection = input(false, { transform: booleanAttribute });
readonly valueLabel = input.required<string>();
readonly unitLabel = input.required<string>();
readonly valueChange = output<number>();
protected readonly internalValue = signal(0);
protected readonly offset = signal(0);
protected readonly unit = signal('days');
private readonly fullOffsetList: OffsetOption[] = [
{
type: 'option',
value: 'minutes',
label: t(() => $localize`:@@SI_DATE_RANGE_FILTER.MINUTES:Minutes`),
offset: ONE_MINUTE
},
{
type: 'option',
value: 'hours',
label: t(() => $localize`:@@SI_DATE_RANGE_FILTER.HOURS:Hours`),
offset: 60 * ONE_MINUTE
},
{
type: 'option',
value: 'days',
label: t(() => $localize`:@@SI_DATE_RANGE_FILTER.DAYS:Days`),
offset: ONE_DAY
},
{
type: 'option',
value: 'weeks',
label: t(() => $localize`:@@SI_DATE_RANGE_FILTER.WEEKS:Weeks`),
offset: 7 * ONE_DAY
},
{
type: 'option',
value: 'months',
label: t(() => $localize`:@@SI_DATE_RANGE_FILTER.MONTHS:Months`),
offset: 30 * ONE_DAY
},
{
type: 'option',
value: 'years',
label: t(() => $localize`:@@SI_DATE_RANGE_FILTER.YEARS:Years`),
offset: 365 * ONE_DAY
}
];
protected readonly offsetList = computed(() =>
this.enableTimeSelection()
? this.fullOffsetList
: this.fullOffsetList.filter(item => item.offset >= ONE_DAY)
);
ngOnChanges(changes: SimpleChanges): void {
const value = this.value();
if (changes.value && value !== this.internalValue()) {
this.internalValue.set(value);
this.calculateOffset();
}
}
private calculateOffset(): void {
const offsetList = this.offsetList();
let unit = '';
if (offsetList.length) {
this.offset.set(0);
unit = offsetList[0].value;
}
for (let i = offsetList.length - 1; i >= 0; i--) {
const item = offsetList[i];
const raw = this.internalValue() / item.offset;
const rounded = Math.round(raw);
if (rounded > 0 && Math.abs(raw - rounded) < 0.001) {
this.offset.set(rounded);
unit = item.value;
break;
}
}
this.changeUnit(unit);
}
protected updateValue(offset: number): void {
this.offset.set(offset);
const item = this.offsetList().find(x => x.value === this.unit())!;
this.internalValue.set(offset * item.offset);
this.valueChange.emit(this.internalValue());
}
protected changeUnit(newUnit: string): void {
this.unit.set(newUnit);
const item = this.offsetList().find(x => x.value === this.unit())!;
this.offset.set(Math.max(1, Math.round(this.internalValue() / item.offset)));
this.internalValue.set(this.offset() * item.offset);
this.valueChange.emit(this.internalValue());
}
}
|