Skip to content

Commit ec298f9

Browse files
jhonyeduardojhosefmarks
authored andcommitted
feat(page-dynamic-search): adiciona a propriedade p-literals
Adiciona a propriedade p-literals, possibilitando customizar a tradução padrão. Também utiliza a linguagem definida no PoI18n para fazer as traduções default. Fixes DTHFUI-2079
1 parent 08f2382 commit ec298f9

16 files changed

+497
-109
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './po-page-dynamic-search.component';
22
export * from './po-page-dynamic-search.interface';
3+
export * from './po-page-dynamic-search-literals.interface';
34

45
export * from './po-page-dynamic-search.module';

projects/templates/src/lib/components/po-page-dynamic-search/po-advanced-filter/po-advanced-filter-base.component.spec.ts

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import { PoAdvancedFilterBaseComponent } from './po-advanced-filter-base.component';
2-
1+
import * as utilsFunctions from './../../../utils/util';
32
import { expectPropertiesValues } from '../../../util-test/util-expect.spec';
3+
import { PoLanguageService } from '../../../../../../ui/src/lib/services/po-language/po-language.service';
4+
5+
import { PoAdvancedFilterBaseComponent, poAdvancedFiltersLiteralsDefault } from './po-advanced-filter-base.component';
46

57
describe('PoAdvancedFilterBaseComponent', () => {
6-
const component = new PoAdvancedFilterBaseComponent();
8+
let component;
9+
10+
const languageService = new PoLanguageService();
11+
12+
beforeEach(() => {
13+
component = new PoAdvancedFilterBaseComponent(languageService);
14+
});
715

816
it('should be created', () => {
917
expect(component).toBeTruthy();
@@ -23,6 +31,114 @@ describe('PoAdvancedFilterBaseComponent', () => {
2331
expectPropertiesValues(component, 'filters', validValues, validValues);
2432
});
2533

34+
describe('literals:', () => {
35+
const poLocaleDefault = utilsFunctions.poLocaleDefault;
36+
37+
it('should be in portuguese if browser is setted with an unsupported language', () => {
38+
component['language'] = 'zw';
39+
40+
component.literals = {};
41+
42+
expect(component.literals).toEqual(poAdvancedFiltersLiteralsDefault[poLocaleDefault]);
43+
});
44+
45+
it('should be in portuguese if browser is setted with `pt`', () => {
46+
component['language'] = 'pt';
47+
48+
component.literals = {};
49+
50+
expect(component.literals).toEqual(poAdvancedFiltersLiteralsDefault.pt);
51+
});
52+
53+
it('should be in english if browser is setted with `en`', () => {
54+
component['language'] = 'en';
55+
56+
component.literals = {};
57+
58+
expect(component.literals).toEqual(poAdvancedFiltersLiteralsDefault.en);
59+
});
60+
61+
it('should be in spanish if browser is setted with `es`', () => {
62+
component['language'] = 'es';
63+
64+
component.literals = {};
65+
66+
expect(component.literals).toEqual(poAdvancedFiltersLiteralsDefault.es);
67+
});
68+
69+
it('should be in russian if browser is setted with `ru`', () => {
70+
component['language'] = 'ru';
71+
72+
component.literals = {};
73+
74+
expect(component.literals).toEqual(poAdvancedFiltersLiteralsDefault.ru);
75+
});
76+
77+
it('should accept custom literals', () => {
78+
component['language'] = poLocaleDefault;
79+
80+
const customLiterals = Object.assign({}, poAdvancedFiltersLiteralsDefault[poLocaleDefault]);
81+
82+
customLiterals.title = 'Filtro avançado';
83+
customLiterals.cancelLabel = 'Fechar';
84+
customLiterals.confirmLabel = 'Aplicar';
85+
86+
component.literals = customLiterals;
87+
88+
expect(component.literals).toEqual(customLiterals);
89+
});
90+
91+
it('should update property with default literals if is setted with invalid values', () => {
92+
const invalidValues = [null, undefined, false, true, '', 'literals', 0, 10, [], [1, 2], () => {}];
93+
94+
component['language'] = 'pt';
95+
96+
expectPropertiesValues(component, 'literals', invalidValues, poAdvancedFiltersLiteralsDefault[poLocaleDefault]);
97+
});
98+
99+
it('should get literals directly from poAdvancedFiltersLiteralsDefault if it not initialized', () => {
100+
component['language'] = 'pt';
101+
102+
expect(component.literals).toEqual(poAdvancedFiltersLiteralsDefault['pt']);
103+
});
104+
});
105+
106+
});
107+
108+
describe('Methods:', () => {
109+
110+
it('getValuesFromForm: should return all items that property isn´t undefined or ``.', () => {
111+
component.filter = { name: 'name', birthdate: 'Birthdate', age: '', Adress: undefined };
112+
113+
const filteredItems = { name: 'name', birthdate: 'Birthdate' };
114+
115+
expect(component['getValuesFromForm']()).toEqual(filteredItems);
116+
});
117+
118+
it('primaryAction: should emit `searchEvent` and call `getValuesFromForm` and `poModal.close`', () => {
119+
component.poModal = <any> { close: () => {} };
120+
121+
spyOn(component.searchEvent, 'emit');
122+
spyOn(component.poModal, 'close');
123+
spyOn(component, <any> 'getValuesFromForm');
124+
125+
component.primaryAction.action();
126+
127+
expect(component.searchEvent.emit).toHaveBeenCalled();
128+
expect(component.poModal.close).toHaveBeenCalled();
129+
expect(component['getValuesFromForm']).toHaveBeenCalled();
130+
});
131+
132+
it('secondaryAction: should call `poModal.close`', () => {
133+
component.poModal = <any> { close: () => {} };
134+
135+
spyOn(component.poModal, 'close');
136+
137+
component.secondaryAction.action();
138+
139+
expect(component.poModal.close).toHaveBeenCalled();
140+
});
141+
26142
});
27143

28144
});
Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
import { EventEmitter, Input, Output } from '@angular/core';
1+
import { EventEmitter, Input, Output, ViewChild } from '@angular/core';
22

3-
import { PoDynamicFormField } from '@portinari/portinari-ui';
3+
import { PoDynamicFormField, PoLanguageService, PoModalAction, PoModalComponent } from '@portinari/portinari-ui';
44

5-
import { browserLanguage, poLocaleDefault } from '../../../utils/util';
5+
import { poLocaleDefault } from '../../../utils/util';
6+
7+
import { PoAdvancedFilterLiterals } from './po-advanced-filter-literals.interface';
68

79
export const poAdvancedFiltersLiteralsDefault = {
8-
en: {
10+
en: <PoAdvancedFilterLiterals> {
911
title: 'Advanced search',
10-
primaryActionLabel: 'Apply filters',
11-
secondaryActionLabel: 'Cancel'
12+
cancelLabel: 'Cancel',
13+
confirmLabel: 'Apply filters'
1214
},
13-
es: {
15+
es: <PoAdvancedFilterLiterals> {
1416
title: 'Búsqueda avanzada',
15-
primaryActionLabel: 'Aplicar filtros',
16-
secondaryActionLabel: 'Cancelar'
17+
cancelLabel: 'Cancelar',
18+
confirmLabel: 'Aplicar filtros'
1719
},
18-
pt: {
20+
pt: <PoAdvancedFilterLiterals> {
1921
title: 'Busca avançada',
20-
primaryActionLabel: 'Aplicar filtros',
21-
secondaryActionLabel: 'Cancelar'
22+
cancelLabel: 'Cancelar',
23+
confirmLabel: 'Aplicar filtros'
24+
},
25+
ru: <PoAdvancedFilterLiterals> {
26+
title: 'Расширенный поиск',
27+
cancelLabel: 'отменить',
28+
confirmLabel: 'Применить фильтры'
2229
}
2330
};
2431

@@ -32,11 +39,29 @@ export const poAdvancedFiltersLiteralsDefault = {
3239
*/
3340
export class PoAdvancedFilterBaseComponent {
3441

42+
@ViewChild(PoModalComponent, { static: true }) poModal: PoModalComponent;
43+
3544
private _filters: Array<PoDynamicFormField> = [];
45+
private _literals: PoAdvancedFilterLiterals;
46+
47+
filter = {};
48+
language: string = poLocaleDefault;
49+
50+
primaryAction: PoModalAction = {
51+
action: () => {
52+
const models = this.getValuesFromForm();
3653

37-
literals = {
38-
...poAdvancedFiltersLiteralsDefault[poLocaleDefault],
39-
...poAdvancedFiltersLiteralsDefault[browserLanguage()]
54+
this.searchEvent.emit(models);
55+
this.poModal.close();
56+
},
57+
label: this.literals.confirmLabel
58+
};
59+
60+
secondaryAction: PoModalAction = {
61+
action: () => {
62+
this.poModal.close();
63+
},
64+
label: this.literals.cancelLabel
4065
};
4166

4267
/**
@@ -51,7 +76,42 @@ export class PoAdvancedFilterBaseComponent {
5176
return this._filters;
5277
}
5378

79+
/** Objeto com as literais usadas no `po-advanced-filter`. */
80+
@Input('p-literals') set literals(value: PoAdvancedFilterLiterals) {
81+
if (value instanceof Object && !(value instanceof Array)) {
82+
this._literals = {
83+
...poAdvancedFiltersLiteralsDefault[poLocaleDefault],
84+
...poAdvancedFiltersLiteralsDefault[this.language],
85+
...value
86+
};
87+
} else {
88+
this._literals = poAdvancedFiltersLiteralsDefault[this.language];
89+
}
90+
91+
this.primaryAction.label = this.literals.confirmLabel;
92+
this.secondaryAction.label = this.literals.cancelLabel;
93+
}
94+
95+
get literals() {
96+
return this._literals || poAdvancedFiltersLiteralsDefault[this.language];
97+
}
98+
5499
/** Função que será disparada e receberá os valores do formulário ao ser clicado no botão buscar. */
55100
@Output('p-search-event') searchEvent = new EventEmitter<any>();
56101

102+
constructor(languageService: PoLanguageService) {
103+
this.language = languageService.getShortLanguage();
104+
}
105+
106+
// Retorna os models dos campos preenchidos
107+
private getValuesFromForm() {
108+
Object.keys(this.filter).forEach(property => {
109+
if (this.filter[property] === undefined || this.filter[property] === '') {
110+
delete this.filter[property];
111+
}
112+
});
113+
114+
return this.filter;
115+
}
116+
57117
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @docsPrivate
3+
*
4+
* @description
5+
*
6+
* Interface para definição das literais usadas no `po-advanced-filter`.
7+
*/
8+
export interface PoAdvancedFilterLiterals {
9+
10+
/** Título do filtro avançado. */
11+
title?: string;
12+
13+
/** Texto exibido no botão de cancelamento do filtro avaçando. */
14+
cancelLabel?: string;
15+
16+
/** Texto exibido no botão de confirmação do filtro avaçando. */
17+
confirmLabel?: string;
18+
19+
}

projects/templates/src/lib/components/po-page-dynamic-search/po-advanced-filter/po-advanced-filter.component.spec.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { FormsModule } from '@angular/forms';
33

4-
import { configureTestSuite } from './../../../util-test/util-expect.spec';
5-
64
import { PoDynamicModule, PoFieldModule, PoModalModule } from '@portinari/portinari-ui';
75

6+
import { configureTestSuite } from './../../../util-test/util-expect.spec';
7+
88
import { PoAdvancedFilterBaseComponent } from './po-advanced-filter-base.component';
99
import { PoAdvancedFilterComponent } from './po-advanced-filter.component';
1010

@@ -41,34 +41,6 @@ describe('PoAdvancedFilterComponent', () => {
4141

4242
describe('Methods:', () => {
4343

44-
it('primaryAction: should emit `searchEvent` and call `getValuesFromForm` and `poModal.close`', () => {
45-
spyOn(component.searchEvent, 'emit');
46-
spyOn(component.poModal, 'close');
47-
spyOn(component, 'getValuesFromForm');
48-
49-
component.primaryAction.action();
50-
51-
expect(component.searchEvent.emit).toHaveBeenCalled();
52-
expect(component.poModal.close).toHaveBeenCalled();
53-
expect(component.getValuesFromForm).toHaveBeenCalled();
54-
});
55-
56-
it('secondaryAction: should call `poModal.close`', () => {
57-
spyOn(component.poModal, 'close');
58-
59-
component.secondaryAction.action();
60-
61-
expect(component.poModal.close).toHaveBeenCalled();
62-
});
63-
64-
it('getValuesFromForm: should return all items if no property is undefined or ``', () => {
65-
component.filter = { name: 'name', birthdate: 'Birthdate', age: '', Adress: undefined };
66-
67-
const filteredItems = { name: 'name', birthdate: 'Birthdate' };
68-
69-
expect(component.getValuesFromForm()).toEqual(filteredItems);
70-
});
71-
7244
it('open: should call `poModal.open` and set `filter` with {}', () => {
7345
component.filter = filters;
7446

projects/templates/src/lib/components/po-page-dynamic-search/po-advanced-filter/po-advanced-filter.component.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, ViewChild } from '@angular/core';
22

3-
import { PoDynamicFormComponent, PoModalAction, PoModalComponent } from '@portinari/portinari-ui';
3+
import { PoDynamicFormComponent, PoLanguageService } from '@portinari/portinari-ui';
44

55
import { PoAdvancedFilterBaseComponent } from './po-advanced-filter-base.component';
66

@@ -22,38 +22,10 @@ import { PoAdvancedFilterBaseComponent } from './po-advanced-filter-base.compone
2222
})
2323
export class PoAdvancedFilterComponent extends PoAdvancedFilterBaseComponent {
2424

25-
filter = {};
26-
27-
@ViewChild(PoModalComponent, { static: true }) poModal: PoModalComponent;
28-
2925
@ViewChild(PoDynamicFormComponent, { static: true }) poDynamicForm: PoDynamicFormComponent;
3026

31-
primaryAction: PoModalAction = {
32-
action: () => {
33-
const models = this.getValuesFromForm();
34-
35-
this.searchEvent.emit(models);
36-
this.poModal.close();
37-
},
38-
label: this.literals.primaryActionLabel
39-
};
40-
41-
secondaryAction: PoModalAction = {
42-
action: () => {
43-
this.poModal.close();
44-
},
45-
label: this.literals.secondaryActionLabel
46-
};
47-
48-
// Retorna os models dos campos preenchidos
49-
getValuesFromForm() {
50-
Object.keys(this.filter).forEach(property => {
51-
if (this.filter[property] === undefined || this.filter[property] === '') {
52-
delete this.filter[property];
53-
}
54-
});
55-
56-
return this.filter;
27+
constructor(languageService: PoLanguageService) {
28+
super(languageService);
5729
}
5830

5931
open() {

0 commit comments

Comments
 (0)