71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
|
|
import {Component, EventEmitter, Input, Output} from '@angular/core';
|
||
|
|
import {Field, FieldType, OperatorType, Predicate, Query} from "../query-builder/query-builder.component";
|
||
|
|
import {NgForOf} from "@angular/common";
|
||
|
|
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
|
||
|
|
import { v4 as uuidv4 } from 'uuid';
|
||
|
|
|
||
|
|
@Component({
|
||
|
|
selector: 'app-query',
|
||
|
|
standalone: true,
|
||
|
|
imports: [
|
||
|
|
NgForOf,
|
||
|
|
ReactiveFormsModule,
|
||
|
|
FormsModule
|
||
|
|
],
|
||
|
|
templateUrl: './query.component.html',
|
||
|
|
styleUrl: './query.component.css'
|
||
|
|
})
|
||
|
|
export class QueryComponent {
|
||
|
|
|
||
|
|
@Input() query!: Query;
|
||
|
|
@Input() fields!: Field[];
|
||
|
|
|
||
|
|
@Output() removeQuery = new EventEmitter<void>();
|
||
|
|
@Output() queryChanged = new EventEmitter<void>();
|
||
|
|
|
||
|
|
queryId = uuidv4();
|
||
|
|
|
||
|
|
constructor() {}
|
||
|
|
|
||
|
|
onFieldChange(predicate: Predicate, selectedField: any): void {
|
||
|
|
predicate.field = selectedField;
|
||
|
|
predicate.operator = this.getOperators(selectedField.type)[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
getOperators(fieldType: FieldType | undefined): OperatorType[] {
|
||
|
|
switch (fieldType) {
|
||
|
|
case 'number':
|
||
|
|
return ['=', '>', '<', '>=', '<=', '!='];
|
||
|
|
case 'string':
|
||
|
|
return ['=', 'contains', 'like'];
|
||
|
|
default:
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
addPredicate(): void {
|
||
|
|
this.query.predicates.push({ field: null, operator: null, value: null });
|
||
|
|
this.queryChanged.emit();
|
||
|
|
}
|
||
|
|
|
||
|
|
removePredicate(index: number): void {
|
||
|
|
this.query.predicates.splice(index, 1);
|
||
|
|
this.queryChanged.emit();
|
||
|
|
}
|
||
|
|
|
||
|
|
addSubQuery(): void {
|
||
|
|
if (!this.query.childQueries) {
|
||
|
|
this.query.childQueries = [];
|
||
|
|
}
|
||
|
|
this.query.childQueries.push({ predicates: [], logicalOperator: 'AND' });
|
||
|
|
this.queryChanged.emit();
|
||
|
|
}
|
||
|
|
|
||
|
|
removeSubQuery(index: number): void {
|
||
|
|
// @ts-ignore
|
||
|
|
this.query.childQueries.splice(index, 1);
|
||
|
|
this.queryChanged.emit();
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|