Simplify the operations a bit, fix number comparison stuff

This commit is contained in:
shamoon 2024-09-08 10:26:13 -07:00
parent 301e875308
commit c4842954d8
2 changed files with 61 additions and 35 deletions

View File

@ -24,7 +24,7 @@
</div> </div>
</div> </div>
<ng-template #dateValue let-atom="atom"> <ng-template #comparisonValueTemplate let-atom="atom">
@if (getCustomFieldByID(atom.field)?.data_type === CustomFieldDataType.Date) { @if (getCustomFieldByID(atom.field)?.data_type === CustomFieldDataType.Date) {
<input class="form-control" placeholder="yyyy-mm-dd" <input class="form-control" placeholder="yyyy-mm-dd"
[(ngModel)]="atom.value" [(ngModel)]="atom.value"
@ -33,6 +33,8 @@
<button class="btn btn-sm btn-outline-secondary rounded-end" (click)="d.toggle()" type="button"> <button class="btn btn-sm btn-outline-secondary rounded-end" (click)="d.toggle()" type="button">
<i-bs name="calendar-event"></i-bs> <i-bs name="calendar-event"></i-bs>
</button> </button>
} @else if (getCustomFieldByID(atom.field)?.data_type === CustomFieldDataType.Float || getCustomFieldByID(atom.field)?.data_type === CustomFieldDataType.Integer) {
<input class="w-25 form-control rounded-end" type="number" [(ngModel)]="atom.value" [disabled]="disabled">
} @else { } @else {
<input class="w-25 form-control rounded-end" type="text" [(ngModel)]="atom.value" [disabled]="disabled"> <input class="w-25 form-control rounded-end" type="text" [(ngModel)]="atom.value" [disabled]="disabled">
} }
@ -65,10 +67,16 @@
</select> </select>
} }
@case (CustomFieldQueryOperator.GreaterThanOrEqual) { @case (CustomFieldQueryOperator.GreaterThanOrEqual) {
<ng-container *ngTemplateOutlet="dateValue; context: { atom: atom }"></ng-container> <ng-container *ngTemplateOutlet="comparisonValueTemplate; context: { atom: atom }"></ng-container>
} }
@case (CustomFieldQueryOperator.LessThanOrEqual) { @case (CustomFieldQueryOperator.LessThanOrEqual) {
<ng-container *ngTemplateOutlet="dateValue; context: { atom: atom }"></ng-container> <ng-container *ngTemplateOutlet="comparisonValueTemplate; context: { atom: atom }"></ng-container>
}
@case (CustomFieldQueryOperator.GreaterThan) {
<ng-container *ngTemplateOutlet="comparisonValueTemplate; context: { atom: atom }"></ng-container>
}
@case (CustomFieldQueryOperator.LessThan) {
<ng-container *ngTemplateOutlet="comparisonValueTemplate; context: { atom: atom }"></ng-container>
} }
@default { @default {
<input class="w-25 form-control rounded-end" type="text" [(ngModel)]="atom.value" [disabled]="disabled"> <input class="w-25 form-control rounded-end" type="text" [(ngModel)]="atom.value" [disabled]="disabled">

View File

@ -15,8 +15,6 @@ export enum CustomFieldQueryOperator {
Exists = 'exists', Exists = 'exists',
Contains = 'contains', Contains = 'contains',
IContains = 'icontains', IContains = 'icontains',
IStartsWith = 'istartswith',
IEndsWith = 'iendswith',
GreaterThan = 'gt', GreaterThan = 'gt',
GreaterThanOrEqual = 'gte', GreaterThanOrEqual = 'gte',
LessThan = 'lt', LessThan = 'lt',
@ -31,8 +29,6 @@ export const CUSTOM_FIELD_QUERY_OPERATOR_LABELS = {
[CustomFieldQueryOperator.Exists]: $localize`Exists`, [CustomFieldQueryOperator.Exists]: $localize`Exists`,
[CustomFieldQueryOperator.Contains]: $localize`Contains`, [CustomFieldQueryOperator.Contains]: $localize`Contains`,
[CustomFieldQueryOperator.IContains]: $localize`Contains (case-insensitive)`, [CustomFieldQueryOperator.IContains]: $localize`Contains (case-insensitive)`,
[CustomFieldQueryOperator.IStartsWith]: $localize`Starts with (case-insensitive)`,
[CustomFieldQueryOperator.IEndsWith]: $localize`Ends with (case-insensitive)`,
[CustomFieldQueryOperator.GreaterThan]: $localize`Greater than`, [CustomFieldQueryOperator.GreaterThan]: $localize`Greater than`,
[CustomFieldQueryOperator.GreaterThanOrEqual]: $localize`Greater than or equal to`, [CustomFieldQueryOperator.GreaterThanOrEqual]: $localize`Greater than or equal to`,
[CustomFieldQueryOperator.LessThan]: $localize`Less than`, [CustomFieldQueryOperator.LessThan]: $localize`Less than`,
@ -45,21 +41,18 @@ export enum CustomFieldQueryOperatorGroups {
String = 'string', String = 'string',
Arithmetic = 'arithmetic', Arithmetic = 'arithmetic',
Containment = 'containment', Containment = 'containment',
Subset = 'subset',
Date = 'date', Date = 'date',
} }
// Modified from filters.py > SUPPORTED_EXPR_OPERATORS
export const CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP = { export const CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP = {
[CustomFieldQueryOperatorGroups.Basic]: [ [CustomFieldQueryOperatorGroups.Basic]: [
CustomFieldQueryOperator.Exact,
CustomFieldQueryOperator.In,
CustomFieldQueryOperator.IsNull,
CustomFieldQueryOperator.Exists, CustomFieldQueryOperator.Exists,
CustomFieldQueryOperator.IsNull,
CustomFieldQueryOperator.Exact,
], ],
[CustomFieldQueryOperatorGroups.String]: [ [CustomFieldQueryOperatorGroups.String]: [CustomFieldQueryOperator.IContains],
CustomFieldQueryOperator.IContains,
CustomFieldQueryOperator.IStartsWith,
CustomFieldQueryOperator.IEndsWith,
],
[CustomFieldQueryOperatorGroups.Arithmetic]: [ [CustomFieldQueryOperatorGroups.Arithmetic]: [
CustomFieldQueryOperator.GreaterThan, CustomFieldQueryOperator.GreaterThan,
CustomFieldQueryOperator.GreaterThanOrEqual, CustomFieldQueryOperator.GreaterThanOrEqual,
@ -69,6 +62,7 @@ export const CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP = {
[CustomFieldQueryOperatorGroups.Containment]: [ [CustomFieldQueryOperatorGroups.Containment]: [
CustomFieldQueryOperator.Contains, CustomFieldQueryOperator.Contains,
], ],
[CustomFieldQueryOperatorGroups.Subset]: [CustomFieldQueryOperator.In],
[CustomFieldQueryOperatorGroups.Date]: [ [CustomFieldQueryOperatorGroups.Date]: [
CustomFieldQueryOperator.GreaterThanOrEqual, CustomFieldQueryOperator.GreaterThanOrEqual,
CustomFieldQueryOperator.LessThanOrEqual, CustomFieldQueryOperator.LessThanOrEqual,
@ -106,7 +100,10 @@ export const CUSTOM_FIELD_QUERY_OPERATOR_GROUPS_BY_TYPE = {
CustomFieldQueryOperatorGroups.Basic, CustomFieldQueryOperatorGroups.Basic,
CustomFieldQueryOperatorGroups.Containment, CustomFieldQueryOperatorGroups.Containment,
], ],
[CustomFieldDataType.Select]: [CustomFieldQueryOperatorGroups.Basic], [CustomFieldDataType.Select]: [
CustomFieldQueryOperatorGroups.Basic,
CustomFieldQueryOperatorGroups.Subset,
],
} }
export const CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR = { export const CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR = {
@ -114,15 +111,13 @@ export const CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR = {
[CustomFieldQueryOperator.IsNull]: 'boolean', [CustomFieldQueryOperator.IsNull]: 'boolean',
[CustomFieldQueryOperator.Exists]: 'boolean', [CustomFieldQueryOperator.Exists]: 'boolean',
[CustomFieldQueryOperator.IContains]: 'string', [CustomFieldQueryOperator.IContains]: 'string',
[CustomFieldQueryOperator.GreaterThanOrEqual]: 'string', [CustomFieldQueryOperator.GreaterThanOrEqual]: 'string|number',
[CustomFieldQueryOperator.LessThanOrEqual]: 'string', [CustomFieldQueryOperator.LessThanOrEqual]: 'string|number',
[CustomFieldQueryOperator.GreaterThan]: 'number',
[CustomFieldQueryOperator.LessThan]: 'number',
// TODO: Implement these // TODO: Implement these
// [CustomFieldQueryOperator.In]: 'array', // [CustomFieldQueryOperator.In]: 'array',
// [CustomFieldQueryOperator.Contains]: 'string', // [CustomFieldQueryOperator.Contains]: 'string',
// [CustomFieldQueryOperator.IStartsWith]: 'string',
// [CustomFieldQueryOperator.IEndsWith]: 'string',
// [CustomFieldQueryOperator.GreaterThan]: 'number',
// [CustomFieldQueryOperator.LessThan]: 'number',
// [CustomFieldQueryOperator.Range]: 'array', // [CustomFieldQueryOperator.Range]: 'array',
} }
@ -189,23 +184,46 @@ export class CustomFieldQueryAtom extends CustomFieldQueryElement {
} }
override set operator(operator: string) { override set operator(operator: string) {
const newType: string = CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR[operator] const newTypes: string[] =
if (typeof this.value !== newType) { CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR[operator]?.split('|')
switch (newType) { if (!newTypes) {
case 'string': this.value = null
this.value = '' }
break if (!newTypes.includes(typeof this.value)) {
case 'boolean': if (newTypes.length === 1) {
this.value = 'true' switch (newTypes[0]) {
break case 'string':
// TODO: Implement these this.value = ''
default: break
case 'boolean':
this.value = 'true'
break
case 'number':
try {
this.value = parseFloat(this.value as string).toString()
} catch (e) {
this.value = null
}
break
// TODO: Implement all
default:
this.value = null
break
}
} else {
if (newTypes.includes('number')) {
try {
this.value = parseFloat(this.value as string).toString()
} catch (e) {
this.value = null
}
} else {
this.value = null this.value = null
break }
} }
} else if ( } else if (
['true', 'false'].includes(this.value as string) && ['true', 'false'].includes(this.value as string) &&
newType === 'string' newTypes.includes('string')
) { ) {
this.value = '' this.value = ''
} }