mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
[ADF-2914] support for number range patterns (#3282)
* support for number range patterns * fix memory leak for tag actions
This commit is contained in:
committed by
Eugenio Romano
parent
f63614e964
commit
3a3acd23ff
@@ -85,4 +85,43 @@ describe('SearchNumberRangeComponent', () => {
|
||||
expect(context.update).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fetch format from the settings', () => {
|
||||
component.settings = {
|
||||
field: 'cm:content.size',
|
||||
format: '<{FROM} TO {TO}>'
|
||||
};
|
||||
component.ngOnInit();
|
||||
|
||||
expect(component.field).toEqual('cm:content.size');
|
||||
expect(component.format).toEqual('<{FROM} TO {TO}>');
|
||||
});
|
||||
|
||||
it('should use default format if not provided', () => {
|
||||
component.settings = {
|
||||
field: 'cm:content.size'
|
||||
};
|
||||
component.ngOnInit();
|
||||
|
||||
expect(component.field).toEqual('cm:content.size');
|
||||
expect(component.format).toEqual('[{FROM} TO {TO}]');
|
||||
});
|
||||
|
||||
it('should format value based on the current pattern', () => {
|
||||
const context: any = {
|
||||
queryFragments: {},
|
||||
update() {}
|
||||
};
|
||||
|
||||
component.id = 'range1';
|
||||
component.settings = {
|
||||
field: 'cm:content.size',
|
||||
format: '<{FROM} TO {TO}>'
|
||||
};
|
||||
component.context = context;
|
||||
component.ngOnInit();
|
||||
|
||||
component.apply({ from: '0', to: '100' }, true);
|
||||
expect(context.queryFragments['range1']).toEqual('cm:content.size:<0 TO 100>');
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -41,7 +41,16 @@ export class SearchNumberRangeComponent implements SearchWidget, OnInit {
|
||||
settings?: SearchWidgetSettings;
|
||||
context?: SearchQueryBuilderService;
|
||||
|
||||
field: string;
|
||||
format = '[{FROM} TO {TO}]';
|
||||
|
||||
ngOnInit(): void {
|
||||
|
||||
if (this.settings) {
|
||||
this.field = this.settings.field;
|
||||
this.format = this.settings.format || '[{FROM} TO {TO}]';
|
||||
}
|
||||
|
||||
const validators = Validators.compose([
|
||||
Validators.required,
|
||||
Validators.pattern(/^-?(0|[1-9]\d*)?$/)
|
||||
@@ -57,12 +66,30 @@ export class SearchNumberRangeComponent implements SearchWidget, OnInit {
|
||||
}
|
||||
|
||||
apply(model: { from: string, to: string }, isValid: boolean) {
|
||||
if (isValid && this.id && this.context && this.settings && this.settings.field) {
|
||||
this.context.queryFragments[this.id] = `${this.settings.field}:[${model.from} TO ${model.to}]`;
|
||||
if (isValid && this.id && this.context && this.field) {
|
||||
const map = new Map<string, string>();
|
||||
map.set('FROM', model.from);
|
||||
map.set('TO', model.to);
|
||||
|
||||
const value = this.formatString(this.format, map);
|
||||
const query = `${this.field}:${value}`;
|
||||
|
||||
this.context.queryFragments[this.id] = query;
|
||||
this.context.update();
|
||||
}
|
||||
}
|
||||
|
||||
private formatString(str: string, map: Map<string, string>): string {
|
||||
let result = str;
|
||||
|
||||
map.forEach((value, key) => {
|
||||
const expr = new RegExp('{' + key + '}', 'gm');
|
||||
result = result.replace(expr, value);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.form.reset({
|
||||
from: '',
|
||||
|
Reference in New Issue
Block a user