2024-02-24 13:59:17 +00:00
|
|
|
import {Component, EventEmitter, Input, Output} from '@angular/core';
|
2024-02-24 19:31:15 +00:00
|
|
|
import {Field, FieldType, Operator, Predicate, Query} from "../query-builder/query-builder.component";
|
2024-02-24 16:58:10 +00:00
|
|
|
import {JsonPipe, NgForOf, NgIf} from "@angular/common";
|
2024-02-24 13:59:17 +00:00
|
|
|
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
|
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-query',
|
|
|
|
|
standalone: true,
|
|
|
|
|
imports: [
|
|
|
|
|
NgForOf,
|
|
|
|
|
ReactiveFormsModule,
|
2024-02-24 16:58:10 +00:00
|
|
|
FormsModule,
|
|
|
|
|
NgIf,
|
|
|
|
|
JsonPipe
|
2024-02-24 13:59:17 +00:00
|
|
|
],
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 16:58:10 +00:00
|
|
|
getOperators(fieldType: FieldType | undefined): Operator[] {
|
2024-02-24 13:59:17 +00:00
|
|
|
switch (fieldType) {
|
|
|
|
|
case 'number':
|
2024-02-24 16:58:10 +00:00
|
|
|
return ['=', '>', '<', '>=', '<=', '!=']
|
|
|
|
|
.map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'any'}) as Operator);
|
2024-02-24 13:59:17 +00:00
|
|
|
case 'string':
|
2024-02-24 16:58:10 +00:00
|
|
|
return ['=', 'contains', 'like']
|
|
|
|
|
.map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'any'}) as Operator);
|
|
|
|
|
case 'boolean':
|
|
|
|
|
return ['=', '!=']
|
|
|
|
|
.map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'boolean'}) as Operator);
|
2024-02-24 19:31:15 +00:00
|
|
|
case 'flag':
|
|
|
|
|
return ['=', '!=']
|
|
|
|
|
.map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'flag'}) as Operator);
|
|
|
|
|
case 'grade':
|
|
|
|
|
return ['=', '!=']
|
|
|
|
|
.map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'grade'}) as Operator);
|
2024-02-24 13:59:17 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|