Refactoring, dont error when changing atoms

This commit is contained in:
shamoon 2024-09-02 15:58:48 -07:00
parent b5691cfbc8
commit 36f33e59de
2 changed files with 38 additions and 20 deletions

View File

@ -9,23 +9,29 @@ export enum CustomFieldQueryLogicalOperator {
Not = 'NOT', Not = 'NOT',
} }
export enum CustomFieldQueryComponentType { export enum CustomFieldQueryElementType {
Atom = 'Atom', Atom = 'Atom',
Expression = 'Expression', Expression = 'Expression',
} }
export class CustomFieldQueryComponent { export class CustomFieldQueryElement {
public readonly type: CustomFieldQueryComponentType public readonly type: CustomFieldQueryElementType
public changed: Subject<CustomFieldQueryComponent> public changed: Subject<CustomFieldQueryElement>
constructor(type: CustomFieldQueryComponentType) { constructor(type: CustomFieldQueryElementType) {
this.type = type this.type = type
this.changed = new Subject<CustomFieldQueryComponent>() this.changed = new Subject<CustomFieldQueryElement>()
} }
public serialize() {} public serialize() {
throw new Error('Implemented in subclass')
}
protected _operator: string public get isValid(): boolean {
throw new Error('Implemented in subclass')
}
protected _operator: string = null
set operator(value: string) { set operator(value: string) {
this._operator = value this._operator = value
this.changed.next(this) this.changed.next(this)
@ -37,7 +43,7 @@ export class CustomFieldQueryComponent {
protected _value: protected _value:
| string | string
| CustomFieldQueryAtom[] | CustomFieldQueryAtom[]
| CustomFieldQueryExpression[] | CustomFieldQueryExpression[] = null
set value( set value(
value: string | CustomFieldQueryAtom[] | CustomFieldQueryExpression[] value: string | CustomFieldQueryAtom[] | CustomFieldQueryExpression[]
) { ) {
@ -49,34 +55,38 @@ export class CustomFieldQueryComponent {
} }
} }
export class CustomFieldQueryAtom extends CustomFieldQueryComponent { export class CustomFieldQueryAtom extends CustomFieldQueryElement {
protected _field: string protected _field: string
set field(value: string) { set field(value: string) {
this._field = value this._field = value
this.changed.next(this) if (this.isValid) this.changed.next(this)
} }
get field(): string { get field(): string {
return this._field return this._field
} }
constructor(queryArray: [string, string, string] = [null, null, null]) { constructor(queryArray: [string, string, string] = [null, null, null]) {
super(CustomFieldQueryComponentType.Atom) super(CustomFieldQueryElementType.Atom)
;[this._field, this._operator, this._value] = queryArray ;[this._field, this._operator, this._value] = queryArray
} }
public serialize() { public serialize() {
return [this._field, this._operator, this._value] return [this._field, this._operator, this._value]
} }
public get isValid(): boolean {
return !!(this._field && this._operator && this._value !== null)
}
} }
export class CustomFieldQueryExpression extends CustomFieldQueryComponent { export class CustomFieldQueryExpression extends CustomFieldQueryElement {
constructor( constructor(
expressionArray: [CustomFieldQueryLogicalOperator, any[]] = [ expressionArray: [CustomFieldQueryLogicalOperator, any[]] = [
CustomFieldQueryLogicalOperator.And, CustomFieldQueryLogicalOperator.And,
null, null,
] ]
) { ) {
super(CustomFieldQueryComponentType.Expression) super(CustomFieldQueryElementType.Expression)
;[this._operator] = expressionArray ;[this._operator] = expressionArray
let values = expressionArray[1] let values = expressionArray[1]
if (!values) { if (!values) {
@ -111,6 +121,14 @@ export class CustomFieldQueryExpression extends CustomFieldQueryComponent {
} }
return [this._operator, value] return [this._operator, value]
} }
public get isValid(): boolean {
return (
this._operator &&
this._value.length > 0 &&
(this._value as any[]).every((v) => v.isValid)
)
}
} }
export class CustomFieldQueriesModel { export class CustomFieldQueriesModel {
@ -133,7 +151,7 @@ export class CustomFieldQueriesModel {
]) ])
) { ) {
if (this.queries.length > 0) { if (this.queries.length > 0) {
if (this.queries[0].type === CustomFieldQueryComponentType.Expression) { if (this.queries[0].type === CustomFieldQueryElementType.Expression) {
;(this.queries[0].value as Array<any>).push(query) ;(this.queries[0].value as Array<any>).push(query)
} else { } else {
this.queries.push(query) this.queries.push(query)
@ -152,7 +170,7 @@ export class CustomFieldQueriesModel {
expression: CustomFieldQueryExpression = new CustomFieldQueryExpression() expression: CustomFieldQueryExpression = new CustomFieldQueryExpression()
) { ) {
if (this.queries.length > 0) { if (this.queries.length > 0) {
if (this.queries[0].type === CustomFieldQueryComponentType.Atom) { if (this.queries[0].type === CustomFieldQueryElementType.Atom) {
expression.value = this.queries as CustomFieldQueryAtom[] expression.value = this.queries as CustomFieldQueryAtom[]
this.queries = [] this.queries = []
} }
@ -171,7 +189,7 @@ export class CustomFieldQueriesModel {
if (components[i] === queryComponent) { if (components[i] === queryComponent) {
return components.splice(i, 1)[0] return components.splice(i, 1)[0]
} else if ( } else if (
components[i].type === CustomFieldQueryComponentType.Expression components[i].type === CustomFieldQueryElementType.Expression
) { ) {
let found = this.findComponent( let found = this.findComponent(
queryComponent, queryComponent,
@ -194,7 +212,7 @@ export class CustomFieldQueriesModel {
if (query === queryComponent) { if (query === queryComponent) {
foundComponent = this.queries.splice(i, 1)[0] foundComponent = this.queries.splice(i, 1)[0]
break break
} else if (query.type === CustomFieldQueryComponentType.Expression) { } else if (query.type === CustomFieldQueryElementType.Expression) {
let found = this.findComponent(queryComponent, query.value as any[]) let found = this.findComponent(queryComponent, query.value as any[])
if (found !== undefined) { if (found !== undefined) {
foundComponent = found foundComponent = found
@ -215,7 +233,7 @@ export class CustomFieldQueriesModel {
styleUrls: ['./custom-fields-query-dropdown.component.scss'], styleUrls: ['./custom-fields-query-dropdown.component.scss'],
}) })
export class CustomFieldsQueryDropdownComponent { export class CustomFieldsQueryDropdownComponent {
public CustomFieldQueryComponentType = CustomFieldQueryComponentType public CustomFieldQueryComponentType = CustomFieldQueryElementType
@Input() @Input()
title: string title: string

View File

@ -766,7 +766,7 @@ export class FilterEditorComponent
}) })
} }
let queries = this.customFieldQueriesModel.queries let queries = this.customFieldQueriesModel.queries
.filter((query) => query.value && query.operator) .filter((query) => query.isValid)
.map((query) => query.serialize()) .map((query) => query.serialize())
if (queries.length > 0) { if (queries.length > 0) {
filterRules.push({ filterRules.push({