nise/nise-frontend/src/corelib/components/query/query.component.ts

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-02-24 13:59:17 +00:00
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";
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,
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];
}
getOperators(fieldType: FieldType | undefined): Operator[] {
2024-02-24 13:59:17 +00:00
switch (fieldType) {
case 'number':
return ['=', '>', '<', '>=', '<=', '!=']
.map((operatorType: String) => ({ operatorType: operatorType, acceptsValues: 'any'}) as Operator);
2024-02-24 13:59:17 +00:00
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);
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();
}
}