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