[ADF-3802] Default ascending order fix (#4070)

This commit is contained in:
davidcanonieto
2018-12-12 10:58:30 +00:00
committed by Eugenio Romano
parent b113ad83f1
commit 95260a7ca7
6 changed files with 48 additions and 28 deletions

View File

@@ -2,5 +2,6 @@
[options]="options" [options]="options"
[selected]="value" [selected]="value"
[ascending]="ascending" [ascending]="ascending"
(change)="onChanged($event)"> (valueChange)="onValueChanged($event)"
(sortingChange)="onSortingChanged($event)">
</adf-sorting-picker> </adf-sorting-picker>

View File

@@ -71,11 +71,23 @@ describe('SearchSortingPickerComponent', () => {
spyOn(queryBuilder, 'update').and.stub(); spyOn(queryBuilder, 'update').and.stub();
component.ngOnInit(); component.ngOnInit();
component.onChanged({ key: 'description', ascending: false }); component.onValueChanged('description');
expect(queryBuilder.update).toHaveBeenCalled(); expect(queryBuilder.update).toHaveBeenCalled();
expect(queryBuilder.sorting.length).toBe(1); expect(queryBuilder.sorting.length).toBe(1);
expect(queryBuilder.sorting[0].key).toEqual('description'); expect(queryBuilder.sorting[0].key).toEqual('description');
expect(queryBuilder.sorting[0].ascending).toBeTruthy();
});
it('should update query builder each time sorting is changed', () => {
spyOn(queryBuilder, 'update').and.stub();
component.ngOnInit();
component.onSortingChanged(false);
expect(queryBuilder.update).toHaveBeenCalled();
expect(queryBuilder.sorting.length).toBe(1);
expect(queryBuilder.sorting[0].key).toEqual('name');
expect(queryBuilder.sorting[0].ascending).toBeFalsy(); expect(queryBuilder.sorting[0].ascending).toBeFalsy();
}); });
}); });

View File

@@ -39,13 +39,18 @@ export class SearchSortingPickerComponent implements OnInit {
const primary = this.queryBuilder.getPrimarySorting(); const primary = this.queryBuilder.getPrimarySorting();
if (primary) { if (primary) {
this.value = primary.key; this.value = primary.key;
this.ascending = primary.ascending; this.ascending = this.getSortingOrder();
} }
} }
onChanged(sorting: { key: string, ascending: boolean }) { onValueChanged(key: string) {
this.value = sorting.key; this.value = key;
this.ascending = sorting.ascending; this.ascending = this.getSortingOrder();
this.applySorting();
}
onSortingChanged(ascending: boolean) {
this.ascending = ascending;
this.applySorting(); this.applySorting();
} }
@@ -67,4 +72,13 @@ export class SearchSortingPickerComponent implements OnInit {
} }
} }
private getSortingOrder(): boolean {
const option = this.findOptionByKey(this.value);
if (option) {
return option.ascending;
}
return this.queryBuilder.getPrimarySorting().ascending;
}
} }

View File

@@ -1,5 +1,5 @@
<mat-form-field> <mat-form-field>
<mat-select [(value)]="selected" (selectionChange)="onChanged($event)"> <mat-select [(value)]="selected" (selectionChange)="onOptionChanged($event)">
<mat-option *ngFor="let option of options" [value]="option.key"> <mat-option *ngFor="let option of options" [value]="option.key">
{{ option.label | translate }} {{ option.label | translate }}
</mat-option> </mat-option>

View File

@@ -27,23 +27,19 @@ describe('SortingPickerComponent', () => {
it('should raise changed event on changing value', (done) => { it('should raise changed event on changing value', (done) => {
component.selected = 'key1'; component.selected = 'key1';
component.ascending = false;
component.change.subscribe((event: { key: string, ascending: boolean }) => { component.valueChange.subscribe((key: string) => {
expect(event.key).toBe('key2'); expect(key).toBe('key2');
expect(event.ascending).toBeFalsy();
done(); done();
}); });
component.onChanged(<any> { value: 'key2' }); component.onOptionChanged(<any> { value: 'key2' });
}); });
it('should raise changed event on changing direction', (done) => { it('should raise changed event on changing direction', (done) => {
component.selected = 'key1';
component.ascending = false; component.ascending = false;
component.change.subscribe((event: { key: string, ascending: boolean }) => { component.sortingChange.subscribe((ascending: boolean) => {
expect(event.key).toBe('key1'); expect(ascending).toBeTruthy();
expect(event.ascending).toBeTruthy();
done(); done();
}); });
component.toggleSortDirection(); component.toggleSortDirection();

View File

@@ -38,24 +38,21 @@ export class SortingPickerComponent {
@Input() @Input()
ascending = true; ascending = true;
/** Raised each time sorting key or direction gets changed. */ /** Raised each time sorting key gets changed. */
@Output() @Output()
change = new EventEmitter<{ key: string, ascending: boolean }>(); valueChange = new EventEmitter<string>();
onChanged(event: MatSelectChange) { /** Raised each time direction gets changed. */
@Output()
sortingChange = new EventEmitter<boolean>();
onOptionChanged(event: MatSelectChange) {
this.selected = event.value; this.selected = event.value;
this.raiseChangedEvent(); this.valueChange.emit(this.selected);
} }
toggleSortDirection() { toggleSortDirection() {
this.ascending = !this.ascending; this.ascending = !this.ascending;
this.raiseChangedEvent(); this.sortingChange.emit(this.ascending);
}
private raiseChangedEvent() {
this.change.emit({
key: this.selected,
ascending: this.ascending
});
} }
} }