Move validation
This commit is contained in:
parent
2ad2c56b4c
commit
def9f4c348
@ -26,6 +26,30 @@ export class CustomFieldQueriesModel {
|
||||
}
|
||||
}
|
||||
|
||||
public isValid(): boolean {
|
||||
return (
|
||||
this.queries.length > 0 &&
|
||||
this.validateExpression(this.queries[0] as CustomFieldQueryExpression)
|
||||
)
|
||||
}
|
||||
|
||||
private validateAtom(atom: CustomFieldQueryAtom) {
|
||||
let valid: boolean = !!(atom.field && atom.operator && atom.value)
|
||||
return valid
|
||||
}
|
||||
|
||||
private validateExpression(expression: CustomFieldQueryExpression) {
|
||||
return (
|
||||
expression.operator &&
|
||||
expression.value.length > 0 &&
|
||||
(expression.value as CustomFieldQueryElement[]).every((e) =>
|
||||
e.type === CustomFieldQueryElementType.Atom
|
||||
? this.validateAtom(e as CustomFieldQueryAtom)
|
||||
: this.validateExpression(e as CustomFieldQueryExpression)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
public addQuery(query: CustomFieldQueryAtom) {
|
||||
if (this.queries.length === 0) {
|
||||
this.addExpression()
|
||||
@ -133,7 +157,7 @@ export class CustomFieldsQueryDropdownComponent {
|
||||
this._selectionModel.changed.complete()
|
||||
}
|
||||
model.changed.subscribe((updatedModel) => {
|
||||
this.selectionModelChange.next(updatedModel)
|
||||
this.onModelChange()
|
||||
})
|
||||
this._selectionModel = model
|
||||
}
|
||||
@ -142,6 +166,12 @@ export class CustomFieldsQueryDropdownComponent {
|
||||
return this._selectionModel
|
||||
}
|
||||
|
||||
private onModelChange() {
|
||||
if (this.selectionModel.isValid()) {
|
||||
this.selectionModelChange.next(this.selectionModel)
|
||||
}
|
||||
}
|
||||
|
||||
@Output()
|
||||
selectionModelChange = new EventEmitter<CustomFieldQueriesModel>()
|
||||
|
||||
|
@ -764,9 +764,9 @@ export class FilterEditorComponent
|
||||
})
|
||||
})
|
||||
}
|
||||
let queries = this.customFieldQueriesModel.queries
|
||||
.filter((query) => query.isValid)
|
||||
.map((query) => query.serialize())
|
||||
let queries = this.customFieldQueriesModel.queries.map((query) =>
|
||||
query.serialize()
|
||||
)
|
||||
if (queries.length > 0) {
|
||||
filterRules.push({
|
||||
rule_type: FILTER_CUSTOM_FIELDS_LOOKUP,
|
||||
|
@ -64,7 +64,6 @@ export const CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP = {
|
||||
CustomFieldQueryOperator.GreaterThanOrEqual,
|
||||
CustomFieldQueryOperator.LessThan,
|
||||
CustomFieldQueryOperator.LessThanOrEqual,
|
||||
// CustomFieldQueryOperator.Range,
|
||||
],
|
||||
[CustomFieldQueryOperatorGroups.Containment]: [
|
||||
CustomFieldQueryOperator.Contains,
|
||||
@ -155,10 +154,6 @@ export class CustomFieldQueryElement {
|
||||
throw new Error('Implemented in subclass')
|
||||
}
|
||||
|
||||
public get isValid(): boolean {
|
||||
throw new Error('Implemented in subclass')
|
||||
}
|
||||
|
||||
protected _operator: string = null
|
||||
public set operator(value: string) {
|
||||
this._operator = value
|
||||
@ -179,12 +174,12 @@ export class CustomFieldQueryElement {
|
||||
}
|
||||
|
||||
export class CustomFieldQueryAtom extends CustomFieldQueryElement {
|
||||
protected _field: string
|
||||
set field(value: string) {
|
||||
this._field = value
|
||||
if (this.isValid) this.changed.next(this)
|
||||
protected _field: number
|
||||
set field(field: any) {
|
||||
this._field = parseInt(field, 10)
|
||||
this.changed.next(this)
|
||||
}
|
||||
get field(): string {
|
||||
get field(): number {
|
||||
return this._field
|
||||
}
|
||||
|
||||
@ -217,7 +212,7 @@ export class CustomFieldQueryAtom extends CustomFieldQueryElement {
|
||||
return super.operator
|
||||
}
|
||||
|
||||
constructor(queryArray: [string, string, string] = [null, null, null]) {
|
||||
constructor(queryArray: [number, string, string] = [null, null, null]) {
|
||||
super(CustomFieldQueryElementType.Atom)
|
||||
;[this._field, this._operator, this._value] = queryArray
|
||||
}
|
||||
@ -233,10 +228,6 @@ export class CustomFieldQueryAtom extends CustomFieldQueryElement {
|
||||
public override serialize() {
|
||||
return [this._field, this._operator, this._value.toString()]
|
||||
}
|
||||
|
||||
public override get isValid(): boolean {
|
||||
return !!(this._field && this._operator && this._value !== null)
|
||||
}
|
||||
}
|
||||
|
||||
export class CustomFieldQueryExpression extends CustomFieldQueryElement {
|
||||
@ -282,14 +273,6 @@ export class CustomFieldQueryExpression extends CustomFieldQueryElement {
|
||||
return [this._operator, value]
|
||||
}
|
||||
|
||||
public override get isValid(): boolean {
|
||||
return (
|
||||
this._operator &&
|
||||
this._value.length > 0 &&
|
||||
(this._value as any[]).every((v) => v.isValid)
|
||||
)
|
||||
}
|
||||
|
||||
public addAtom(
|
||||
atom: CustomFieldQueryAtom = new CustomFieldQueryAtom([
|
||||
null,
|
||||
|
Loading…
x
Reference in New Issue
Block a user