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 | 45x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 90x 24x 24x 24x 24x 24x 24x 24x 24x 95x 95x 95x 95x 22x 22x 22x 24x 1x 2x 1x 1x 24x 31x 24x 24x 24x | /**
* Copyright (c) Siemens 2016 - 2026
* SPDX-License-Identifier: MIT
*/
import { ScrollStrategy } from '@angular/cdk/overlay';
import {
booleanAttribute,
Component,
computed,
input,
model,
OnChanges,
signal,
SimpleChanges
} from '@angular/core';
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: [
SiNumberInputComponent,
SiSelectComponent,
SiSelectSingleValueDirective,
SiSelectSimpleOptionsDirective
],
templateUrl: './si-relative-date.component.html',
styleUrl: './si-relative-date.component.scss'
})
export class SiRelativeDateComponent implements OnChanges {
/**
* Relative offset in milliseconds. Changes when the user selects a different offset or unit.
*
* @defaultValue 0
*/
readonly value = model(0);
/**
* Whether minute- and hour-based offsets are available.
*
* @defaultValue false
*/
readonly enableTimeSelection = input(false, { transform: booleanAttribute });
/** Label for the numeric offset input. */
readonly valueLabel = input.required<string>();
/** Label for the offset unit selector. */
readonly unitLabel = input.required<string>();
/** CDK scroll strategy used for the offset unit select overlay. */
readonly scrollStrategy = input.required<ScrollStrategy>();
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<this>): void {
Eif (changes.value) {
this.calculateOffset();
}
}
private calculateOffset(): void {
const offsetList = this.offsetList();
let unit = '';
Eif (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.value() / 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())!;
const nextValue = offset * item.offset;
this.value.set(nextValue);
}
protected changeUnit(newUnit: string): void {
this.unit.set(newUnit);
const item = this.offsetList().find(x => x.value === this.unit())!;
const roundedOffset = Math.max(1, Math.round(this.value() / item.offset));
this.offset.set(roundedOffset);
this.value.set(roundedOffset * item.offset);
}
}
|