Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(geo): ensure ogc filter accept today and now #1622

Merged
merged 9 commits into from
May 15, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class OgcFilterTimeComponent implements OnInit {
@ViewChild('beginTime') beginTime: HTMLInputElement;
@ViewChild('endTime') endTime: HTMLInputElement;

private filterOriginConfig: OgcFilterDuringOptions;

get step(): string {
return this.datasource.options.stepDate
? this.datasource.options.stepDate
Expand Down Expand Up @@ -115,6 +117,8 @@ export class OgcFilterTimeComponent implements OnInit {
constructor(public ogcFilterTimeService: OGCFilterTimeService) {}

ngOnInit() {
this.filterOriginConfig = this.datasource.options.ogcFilters
.filters as OgcFilterDuringOptions;
if (this.currentFilter.sliderOptions) {
this.currentFilter.sliderOptions.enabled =
this.currentFilter.sliderOptions.enabled !== undefined
Expand Down Expand Up @@ -143,14 +147,14 @@ export class OgcFilterTimeComponent implements OnInit {
const interval = filter.match(/years|months|weeks|days|hours|seconds/);
if (filter.match(/\+/)) {
const intervalInt = parseInt(
filter.substring(filter.search('+') + 1, interval.index),
filter.substring(filter.indexOf('+') + 1, interval.index),
10
);
return moment().add(intervalInt, interval[0]).toDate();
}
if (filter.match(/\-/)) {
const intervalInt = parseInt(
filter.substring(filter.search('-') + 1, interval.index),
filter.substring(filter.indexOf('-') + 1, interval.index),
10
);
return moment().subtract(intervalInt, interval[0]).toDate();
Expand All @@ -162,14 +166,14 @@ export class OgcFilterTimeComponent implements OnInit {
const interval = filter.match(/years|months|weeks|days|hours|seconds/);
if (filter.match(/\+/)) {
const intervalInt = parseInt(
filter.substring(filter.search('+') + 1, interval.index),
filter.substring(filter.indexOf('+') + 1, interval.index),
10
);
return moment(_now).add(intervalInt, interval[0]).toDate();
}
if (filter.match(/\-/)) {
const intervalInt = parseInt(
filter.substring(filter.search('-') + 1, interval.index),
filter.substring(filter.indexOf('-') + 1, interval.index),
10
);
return moment(_now).subtract(intervalInt, interval[0]).toDate();
Expand All @@ -186,9 +190,13 @@ export class OgcFilterTimeComponent implements OnInit {
}
}

changeTemporalProperty(value, position?, refreshFilter = true) {
if (!this.isValidDate(value)) {
return;
changeTemporalProperty(
value: string | Date,
position?,
refreshFilter = true
) {
if (typeof value === 'string') {
value = new Date(value);
}
let valueTmp = this.getDateTime(value, position);
if (this.isCalendarYearMode()) {
Expand Down Expand Up @@ -631,16 +639,16 @@ export class OgcFilterTimeComponent implements OnInit {
return this.currentFilter.begin
? this.currentFilter.begin
: this.datasource.options.minDate
? this.datasource.options.minDate
: this._defaultMin;
? this.datasource.options.minDate
: this._defaultMin;
}

public handleMax() {
return this.currentFilter.end
? this.currentFilter.end
: this.datasource.options.maxDate
? this.datasource.options.maxDate
: this._defaultMax;
? this.datasource.options.maxDate
: this._defaultMax;
}

changePropertyByPass(event) {
Expand Down Expand Up @@ -702,27 +710,24 @@ export class OgcFilterTimeComponent implements OnInit {
}

resetFilter() {
let filterOriginConfig = this.datasource.options.ogcFilters
.filters as OgcFilterDuringOptions;

let minDefaultDate;
let maxDefaultDate;
let minDefaultISOString;
let maxDefaultISOString;

if (this.calendarTypeYear) {
if (filterOriginConfig.end === 'today') {
if (this.filterOriginConfig.end === 'today') {
let todayDateStringNoTime = new Date().toLocaleDateString('en-CA'); // '2022-02-13'
maxDefaultISOString = `${todayDateStringNoTime.substring(0, 4)}-01-01`;
} else {
maxDefaultISOString = `${filterOriginConfig.end.substring(0, 4)}-01-01`;
maxDefaultISOString = `${this.filterOriginConfig.end.substring(0, 4)}-01-01`;
}
minDefaultISOString = `${filterOriginConfig.begin.substring(0, 4)}-01-01`;
minDefaultISOString = `${this.filterOriginConfig.begin.substring(0, 4)}-01-01`;
minDefaultDate = this.getDateFromStringWithoutTime(minDefaultISOString);
maxDefaultDate = this.getDateFromStringWithoutTime(maxDefaultISOString);
} else {
minDefaultDate = this.parseFilter(filterOriginConfig.begin);
maxDefaultDate = this.parseFilter(filterOriginConfig.end);
minDefaultDate = this.parseFilter(this.filterOriginConfig.begin);
maxDefaultDate = this.parseFilter(this.filterOriginConfig.end);
minDefaultISOString = minDefaultDate.toISOString();
maxDefaultISOString = maxDefaultDate.toISOString();
}
Expand All @@ -746,8 +751,4 @@ export class OgcFilterTimeComponent implements OnInit {
this.setFilterStateDisable();
this.updateValues();
}

private isValidDate(date: Date) {
return date instanceof Date && !isNaN(date.getTime());
}
}
58 changes: 54 additions & 4 deletions packages/geo/src/lib/filter/shared/ogc-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ export class OgcFilterWriter {
const srsName = igoOgcFilterObject.hasOwnProperty('srsName')
? igoOgcFilterObject.srsName
: proj
? proj.getCode()
: 'EPSG:3857';
? proj.getCode()
: 'EPSG:3857';

return Object.assign(
{},
Expand Down Expand Up @@ -913,12 +913,62 @@ export class OgcFilterWriter {
public parseFilterOptionDate(value: string, defaultValue?: string): string {
if (!value) {
return defaultValue;
} else if (value === 'today') {
return undefined;
} else if (
value.toLowerCase().includes('now') ||
value.toLowerCase().includes('today')
) {
return this.parseDateOperation(value);
} else if (moment(value).isValid()) {
return value;
} else {
return undefined;
}
}
/**
* this function to parse date with specific format
* exemple 'today + 1 days' or 'now + 1 years'
* @param value string date
* @returns date
*/
private parseDateOperation(value: string): string {
const operationSplitted = value.toLowerCase().split(' ');
const leftOperand = operationSplitted[0];
const operator = ['+', '-'].includes(operationSplitted[1])
? operationSplitted[1]
: undefined;
const rightOperand = /^[0-9]*$/.test(operationSplitted[2])
? operationSplitted[2]
: undefined;
const rightUnitOperand = (
['years', 'months', 'weeks', 'days', 'hours', 'seconds'].includes(
operationSplitted[3]
)
? operationSplitted[3]
: undefined
) as moment.DurationInputArg2;
alecarn marked this conversation as resolved.
Show resolved Hide resolved

if (!operator || !rightUnitOperand || !rightOperand) {
return leftOperand === 'now'
? moment().format()
: moment().endOf('day').format();
}

if (operator === '+') {
return leftOperand === 'now'
? moment().add(parseInt(rightOperand, 10), rightUnitOperand).format()
: moment()
.endOf('day')
.add(parseInt(rightOperand, 10), rightUnitOperand)
.format();
} else {
return leftOperand === 'now'
? moment()
.subtract(parseInt(rightOperand, 10), rightUnitOperand)
.format()
: moment()
.endOf('day')
.subtract(parseInt(rightOperand, 10), rightUnitOperand)
.format();
}
}
}
Loading