src/app/core/basic-datatypes/date/date-range-filter/date-range-filter-panel/date-range-filter-panel.component.ts
| changeDetection | ChangeDetectionStrategy.OnPush |
| providers |
MatRangeDateSelectionModel
RangePreviewStrategy
|
| selector | app-date-range-filter-panel |
| standalone | true |
| imports |
MatDialogModule
MatButtonModule
MatDatepickerModule
|
| styleUrls | ./date-range-filter-panel.component.scss |
| templateUrl | ./date-range-filter-panel.component.html |
No results matching.
MatDialogModule
MatButtonModule
MatDatepickerModule
FormsModule
MatRangeDateSelectionModel
RangePreviewStrategy
Properties |
|
Methods |
| preselectAllRange |
preselectAllRange()
|
|
Returns :
void
|
| preselectRange | ||||||
preselectRange(dateRangeOption: DateRangeOption)
|
||||||
|
Parameters :
Returns :
void
|
| selectedRangeChange | ||||||
selectedRangeChange(selectedDate: Date)
|
||||||
|
Parameters :
Returns :
void
|
| selectRangeAndClose | ||||||
selectRangeAndClose(index: number | "all" | "empty")
|
||||||
|
Parameters :
Returns :
void
|
| unselectRange |
unselectRange()
|
|
Returns :
void
|
| comparisonRange |
Type : unknown
|
Default value : signal<DateRange<Date>>(this.highlightRange)
|
| Readonly emptyFilterOptionKey |
Type : unknown
|
Default value : EMPTY_FILTER_OPTION_KEY
|
| rangeOptions |
Type : unknown
|
Default value : signal<DateRangeOption[]>(this.data?.rangeOptions ?? [])
|
| selectedOption |
Type : unknown
|
Default value : signal<DateRangeFilterConfigOption | undefined>(
this.data?.selectedOption,
)
|
| selectedOptionValues |
Type : unknown
|
Default value : signal<string[]>(
this.data?.selectedOptionValues ?? [],
)
|
| Readonly showAllOption |
Type : unknown
|
Default value : !this.data?.hideAllOption
|
import {
Component,
inject,
Injectable,
ChangeDetectionStrategy,
signal,
} from "@angular/core";
import {
DateRange,
DefaultMatCalendarRangeStrategy,
MAT_DATE_RANGE_SELECTION_STRATEGY,
MatDatepickerModule,
MatDateSelectionModel,
MatRangeDateSelectionModel,
} from "@angular/material/datepicker";
import {
MAT_DIALOG_DATA,
MatDialogModule,
MatDialogRef,
} from "@angular/material/dialog";
import { MatButtonModule } from "@angular/material/button";
import { DateRangeFilterConfigOption } from "../../../../entity-list/EntityListConfig";
import { FormsModule } from "@angular/forms";
import { dateToString } from "../../../../../utils/utils";
import { DateFilter } from "app/core/filter/filters/dateFilter";
import { calculateDateRange, DateRangeOption } from "./date-range-utils";
import { EMPTY_FILTER_OPTION_KEY } from "app/core/filter/filters/filters";
export const defaultDateFilters: DateRangeFilterConfigOption[] = [
{
label: $localize`:Filter label:Today`,
},
{
startOffsets: [{ amount: 0, unit: "weeks" }],
endOffsets: [{ amount: 0, unit: "weeks" }],
label: $localize`:Filter label:This week`,
},
{
startOffsets: [{ amount: -1, unit: "weeks" }],
label: $localize`:Filter label:Since last week`,
},
{
startOffsets: [{ amount: 0, unit: "months" }],
endOffsets: [{ amount: 0, unit: "months" }],
label: $localize`:Filter label:This month`,
},
{
startOffsets: [{ amount: -1, unit: "months" }],
endOffsets: [{ amount: -1, unit: "months" }],
label: $localize`:Filter label:Last month`,
},
];
/**
* Shows the range up to the day that is hovered, after the start date was picked in the calendar.
* If that day is before the start date, the range is shown the other way round, as the panel orders the picked dates.
*
* Dragging the ends of a selected range is not supported, so that is not previewed either.
*/
@Injectable()
export class RangePreviewStrategy extends DefaultMatCalendarRangeStrategy<Date> {
override createPreview(
activeDate: Date | null,
currentRange: DateRange<Date>,
): DateRange<Date> {
if (!currentRange.start || currentRange.end || !activeDate) {
return new DateRange(null, null);
}
return activeDate < currentRange.start
? new DateRange(activeDate, currentRange.start)
: new DateRange(currentRange.start, activeDate);
}
override createDrag(): null {
return null;
}
}
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "app-date-range-filter-panel",
templateUrl: "./date-range-filter-panel.component.html",
styleUrls: ["./date-range-filter-panel.component.scss"],
providers: [
{ provide: MatDateSelectionModel, useClass: MatRangeDateSelectionModel },
{
provide: MAT_DATE_RANGE_SELECTION_STRATEGY,
useClass: RangePreviewStrategy,
},
],
imports: [MatDialogModule, MatButtonModule, MatDatepickerModule, FormsModule],
})
export class DateRangeFilterPanelComponent {
readonly emptyFilterOptionKey = EMPTY_FILTER_OPTION_KEY;
// Dialog input data (plain DTO) instead of full DateFilter instance
private readonly data = inject<any>(MAT_DIALOG_DATA) as {
selectedOptionValues?: string[];
selectedOption?: DateRangeFilterConfigOption;
dateRange?: DateRange<Date>;
rangeOptions?: DateRangeOption[];
/**
* For selecting the dates of a field rather than a filter:
* no "All" option, and without given dates nothing is selected initially.
*/
hideAllOption?: boolean;
/** a range shaded in the calendar while no predefined range is hovered (e.g. the period of a parent record) */
highlightRange?: DateRange<Date>;
};
private dialogRef =
inject<MatDialogRef<DateRangeFilterPanelComponent>>(MatDialogRef);
readonly showAllOption = !this.data?.hideAllOption;
selectedRangeValue = signal<DateRange<Date>>(
this.showAllOption
? new DateRange(
this.data?.dateRange?.start ?? new Date("1900-01-01"),
this.data?.dateRange?.end ?? new Date("2999-12-31"),
)
: new DateRange(
this.data?.dateRange?.start ?? null,
this.data?.dateRange?.end ?? null,
),
);
selectedOption = signal<DateRangeFilterConfigOption | undefined>(
this.data?.selectedOption,
);
selectedOptionValues = signal<string[]>(
this.data?.selectedOptionValues ?? [],
);
rangeOptions = signal<DateRangeOption[]>(this.data?.rangeOptions ?? []);
private readonly highlightRange =
this.data?.highlightRange ?? new DateRange<Date>(null, null);
comparisonRange = signal<DateRange<Date>>(this.highlightRange);
/** the month shown first: for a field its start date, else the highlighted range (a filter starts at today) */
readonly startAt: Date | null = this.showAllOption
? null
: (this.data?.dateRange?.start ?? this.highlightRange.start ?? null);
preselectRange(dateRangeOption: DateRangeOption): void {
this.comparisonRange.set(calculateDateRange(dateRangeOption));
}
preselectAllRange(): void {
this.comparisonRange.set(
new DateRange(new Date("1900-01-01"), new Date("2999-12-31")),
);
}
unselectRange() {
this.comparisonRange.set(this.highlightRange);
}
selectRangeAndClose(index: number | "all" | "empty"): void {
let result: string[] = [];
if (typeof index === "number") {
result = [index.toString()];
} else if (index === "empty") {
result = [EMPTY_FILTER_OPTION_KEY];
} else {
result = [];
}
this.selectedOptionValues.set(result);
this.dialogRef.close({ selectedOptionValues: result });
}
selectedRangeChange(selectedDate: Date) {
const current = this.selectedRangeValue();
if (!current?.start || current?.end) {
this.selectedRangeValue.set(new DateRange(selectedDate, null));
} else {
const start: Date = current.start as Date;
const resultValues =
start < selectedDate
? [dateToString(start), dateToString(selectedDate)]
: [dateToString(selectedDate), dateToString(start)];
this.selectedOptionValues.set(resultValues);
this.dialogRef.close({ selectedOptionValues: resultValues });
}
}
}
<div mat-dialog-title i18n="Title of dialog">Select a date range</div>
<mat-dialog-content>
<div class="container">
<mat-calendar
class="calendar"
(selectedChange)="selectedRangeChange($event)"
[selected]="selectedRangeValue()"
[startAt]="startAt"
[comparisonStart]="comparisonRange().start"
[comparisonEnd]="comparisonRange().end"
>
</mat-calendar>
<div class="panel">
<button
class="button"
mat-button
color="primary"
(click)="selectRangeAndClose('empty')"
[class.selected-option]="
selectedOptionValues()[0] === emptyFilterOptionKey
"
>
<span class="not-defined-label" i18n="Filter option label">
no date defined
</span>
</button>
@if (showAllOption) {
<button
class="button"
mat-button
color="primary"
(mouseenter)="preselectAllRange()"
(mouseleave)="unselectRange()"
(click)="selectRangeAndClose('all')"
[class.selected-option]="selectedOptionValues().length === 0"
i18n
>
All
</button>
}
@for (
item of rangeOptions();
track item;
let selectedIndexOfDateRanges = $index
) {
<button
class="button"
mat-button
color="primary"
[class.selected-option]="item === selectedOption()"
(mouseenter)="preselectRange(item)"
(mouseleave)="unselectRange()"
(click)="selectRangeAndClose(selectedIndexOfDateRanges)"
>
{{ item.label }}
</button>
}
</div>
</div>
</mat-dialog-content>
./date-range-filter-panel.component.scss
@use "../../../../common-components/special-option-labels";
.container {
display: flex;
}
.calendar {
width: 60%;
}
.panel {
width: 40%;
margin: auto;
}
.button {
display: block;
text-align: start;
}
.selected-option {
font-weight: bold;
}