@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
83 lines (82 loc) • 3.9 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import { filterOperators, isListFilterOperator, isTwoSidedFilterOperator, isValuelessFilterOperator, operatorsByAttributeTypeGroup } from '../../models/filter.js';
import Geometry from 'ol/geom/Geometry.js';
import { GeoJSON } from 'ol/format.js';
/**
* Base class for all filter conditions.
*/
export class BaseFilterCondition {
property;
propertyType;
operator;
value;
value2;
propertyTypeGroup;
constructor(property, operator, value, value2, propertyType, propertyTypeGroup) {
this.property = property;
this.operator = operator;
this.value = value ?? '';
this.value2 = value2 ?? '';
this.propertyType = propertyType;
this.propertyTypeGroup = propertyTypeGroup ?? 'string';
if (isTwoSidedFilterOperator(this.operator) && !this.value2) {
throw new Error('Operator needs two filter values !');
}
}
/**
Returns the supported operators for the given filter class.
*/
static get supportedOperators() {
return [...filterOperators];
}
/**
* Returns the supported operators for the given attribute type. This function supports mapping for
* the three basic attribute types: string, number and date. More diverse types must be mapped in the subclass.
*/
static supportedOperatorsByAttributeType(attributeType) {
return BaseFilterCondition.supportedOperators.filter((op) => (operatorsByAttributeTypeGroup[attributeType] ?? []).includes(op.operator));
}
/**
* Generates a simple text representation of the filter, for example, "attributeName [attributeType] = value"
* Needs the context to translate the operator and other terms.
* If instead of the attribute name and type, an alias or translated name should be displayed,
* provide the string via the optional propertyDisplayName parameter
*/
toTextRepresentation(context, propertyDisplayName) {
const i18n = (key) => context.i18nManager.getTranslation(key);
if (!propertyDisplayName) {
propertyDisplayName = `${this.property} [${i18n(this.propertyType ?? '')}]`;
}
const operatorDisplayName = filterOperators.find((op) => op.operator === this.operator)?.displayName ?? this.operator;
const attributeAndOperator = `${propertyDisplayName} ${i18n(operatorDisplayName)}`;
const quotes = this.propertyTypeGroup === 'date' || this.propertyTypeGroup === 'string' ? '"' : '';
if (this.propertyTypeGroup === 'geometry') {
return `${attributeAndOperator} ${i18n(BaseFilterCondition.geometryToTextRepresentation(this.value))}`;
}
if (isValuelessFilterOperator(this.operator)) {
return attributeAndOperator;
}
if (isTwoSidedFilterOperator(this.operator)) {
return `${attributeAndOperator} ${quotes}${this.value}${quotes} ${i18n('AND')} ${quotes}${this.value2}${quotes}`;
}
if (isListFilterOperator(this.operator)) {
const items = (JSON.parse(this.value).items ?? []);
const itemsAsCommaList = items.map((val) => `${quotes}${val}${quotes}`).join(', ');
return `${attributeAndOperator} ${itemsAsCommaList}`;
}
return `${attributeAndOperator} ${quotes}${this.value}${quotes}`;
}
static geometryToTextRepresentation(geoJson) {
const feature = JSON.parse(geoJson);
if (feature?.properties?.tool) {
// If the drawing tool is specified (circle, rectangle, etc.), return it as the geometry type
return feature.properties.tool;
}
else {
// Fallback to default geometry type
const format = new GeoJSON();
const geometry = format.readGeometry(geoJson);
return (geometry ?? new Geometry()).getType();
}
}
}