import {Component, EventEmitter, Input, Output} from '@angular/core'; import {Field, FieldType, Operator, Predicate, Query} from "../query-builder/query-builder.component"; import {JsonPipe, NgForOf, NgIf} 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, NgIf, JsonPipe ], templateUrl: './query.component.html', styleUrl: './query.component.css' }) export class QueryComponent { @Input() query!: Query; @Input() fields!: Field[]; @Output() removeQuery = new EventEmitter(); @Output() queryChanged = new EventEmitter(); queryId = uuidv4(); constructor() {} onFieldChange(predicate: Predicate, selectedField: any): void { predicate.field = selectedField; predicate.operator = this.getOperators(selectedField.type)[0]; } getOperators(fieldType: FieldType | undefined): Operator[] { switch (fieldType) { case 'number': return ['=', '>', '<', '>=', '<=', '!='] .map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'any'}) as Operator); case 'string': return ['=', 'contains', 'like'] .map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'any'}) as Operator); case 'boolean': return ['=', '!='] .map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'boolean'}) as Operator); case 'flag': return ['=', '!='] .map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'flag'}) as Operator); case 'grade': return ['=', '!='] .map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'grade'}) as Operator); 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(); } }