Skip to content

Commit 335bb57

Browse files
feat: allow to replace any field in widget filters (#2820)
AB#120295
1 parent a904310 commit 335bb57

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

libs/shared/src/lib/services/context/context.service.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,31 +355,34 @@ export class ContextService {
355355
): T {
356356
const filter = cloneDeep(f);
357357
const filterValue = this.filterValue(this.filter.getValue());
358-
// Regex to detect {{filter.}} in object
359358
const filterRegex = this.filterValueRegex;
360-
// Regex to detect {{context.}} in object
361359
const contextRegex = /(?<={{context\.)(.*?)(?=}})/gim;
362360

363-
if ('field' in filter && filter.field) {
364-
// If it's a filter descriptor, replace value ( if string )
365-
if (filter.value && typeof filter.value === 'string') {
366-
const filterName = filter.value?.match(filterRegex)?.[0];
361+
// Helper to replace a property value if it matches filter/context pattern
362+
const replaceProp = (obj: any, prop: string) => {
363+
if (obj[prop] && typeof obj[prop] === 'string') {
364+
const filterName = obj[prop]?.match(filterRegex)?.[0];
367365
if (filterName) {
368-
filter.value = get(filterValue, filterName);
366+
obj[prop] = get(filterValue, filterName);
369367
} else {
370-
const contextName = filter.value?.match(contextRegex)?.[0];
368+
const contextName = obj[prop]?.match(contextRegex)?.[0];
371369
if (contextName) {
372-
filter.value = get(this.context, contextName);
370+
obj[prop] = get(this.context, contextName);
373371
}
374372
}
375373
}
374+
};
375+
376+
if ('field' in filter && filter.field) {
377+
// If it's a filter descriptor, replace value/operator/field
378+
replaceProp(filter, 'value');
379+
replaceProp(filter, 'operator');
380+
replaceProp(filter, 'field');
376381
} else if ('filters' in filter && filter.filters) {
377-
// If it's a composite filter, replace values in filters
382+
// If it's a composite filter, replace values in filters and replace logic
378383
filter.filters = filter.filters
379384
.map((f) => this.injectContext(f))
380385
.filter((f) => {
381-
// Filter out fields that are not in the available filter field
382-
// Meaning, their values are still using the {{filter.}} syntax
383386
if ('value' in f) {
384387
return isObject(f.value)
385388
? !isNil(f.value) && !isEmpty(f.value)
@@ -388,6 +391,7 @@ export class ContextService {
388391
return true;
389392
}
390393
});
394+
replaceProp(filter, 'logic');
391395
}
392396
return filter;
393397
}

0 commit comments

Comments
 (0)