mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
[ADF-2131] Search sorting (#3334)
* sorting configuration * detect primary sorting and use with document list * search results sorting * docs update * unit tests and code updates * update code * update code * generic sorting picker, test updates * ability to switch off client side sorting * update docs for document list
This commit is contained in:
committed by
Eugenio Romano
parent
73bc62ae8f
commit
07440731fa
@@ -138,6 +138,10 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
|
||||
@Input()
|
||||
sorting = ['name', 'asc'];
|
||||
|
||||
/** Defines sorting mode. Can be either `client` or `server`. */
|
||||
@Input()
|
||||
sortingMode = 'client';
|
||||
|
||||
/** The inline style to apply to every row. See
|
||||
* the Angular NgStyle
|
||||
* docs for more details and usage examples.
|
||||
@@ -329,7 +333,7 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
|
||||
|
||||
ngOnInit() {
|
||||
this.loadLayoutPresets();
|
||||
this.data = new ShareDataTableAdapter(this.documentListService, null, this.getDefaultSorting());
|
||||
this.data = new ShareDataTableAdapter(this.documentListService, null, this.getDefaultSorting(), this.sortingMode);
|
||||
this.data.thumbnails = this.thumbnails;
|
||||
this.data.permissionsStyle = this.permissionsStyle;
|
||||
|
||||
@@ -367,7 +371,7 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
|
||||
}
|
||||
|
||||
if (!this.data) {
|
||||
this.data = new ShareDataTableAdapter(this.documentListService, schema, this.getDefaultSorting());
|
||||
this.data = new ShareDataTableAdapter(this.documentListService, schema, this.getDefaultSorting(), this.sortingMode);
|
||||
} else if (schema && schema.length > 0) {
|
||||
this.data.setColumns(schema);
|
||||
}
|
||||
@@ -381,6 +385,18 @@ export class DocumentListComponent implements OnInit, OnChanges, OnDestroy, Afte
|
||||
ngOnChanges(changes: SimpleChanges) {
|
||||
this.resetSelection();
|
||||
|
||||
if (changes.sortingMode && !changes.sortingMode.firstChange && this.data) {
|
||||
this.data.sortingMode = changes.sortingMode.currentValue;
|
||||
}
|
||||
|
||||
if (changes.sorting && !changes.sorting.firstChange && this.data) {
|
||||
const newValue = changes.sorting.currentValue;
|
||||
if (newValue && newValue.length > 0) {
|
||||
const [key, direction] = newValue;
|
||||
this.data.setSorting(new DataSorting(key, direction));
|
||||
}
|
||||
}
|
||||
|
||||
if (changes.folderNode && changes.folderNode.currentValue) {
|
||||
this.currentFolderId = changes.folderNode.currentValue.id;
|
||||
this.resetNewFolderPagination();
|
||||
|
@@ -32,6 +32,31 @@ describe('ShareDataTableAdapter', () => {
|
||||
spyOn(documentListService, 'getDocumentThumbnailUrl').and.returnValue(imageUrl);
|
||||
});
|
||||
|
||||
it('should use client sorting by default', () => {
|
||||
const adapter = new ShareDataTableAdapter(documentListService, []);
|
||||
expect(adapter.sortingMode).toBe('client');
|
||||
});
|
||||
|
||||
it('should not be case sensitive for sorting mode value', () => {
|
||||
const adapter = new ShareDataTableAdapter(documentListService, []);
|
||||
|
||||
adapter.sortingMode = 'CLIENT';
|
||||
expect(adapter.sortingMode).toBe('client');
|
||||
|
||||
adapter.sortingMode = 'SeRvEr';
|
||||
expect(adapter.sortingMode).toBe('server');
|
||||
});
|
||||
|
||||
it('should fallback to client sorting for unknown values', () => {
|
||||
const adapter = new ShareDataTableAdapter(documentListService, []);
|
||||
|
||||
adapter.sortingMode = 'SeRvEr';
|
||||
expect(adapter.sortingMode).toBe('server');
|
||||
|
||||
adapter.sortingMode = 'quantum';
|
||||
expect(adapter.sortingMode).toBe('client');
|
||||
});
|
||||
|
||||
it('should setup rows and columns with constructor', () => {
|
||||
let schema = [<DataColumn> {}];
|
||||
let adapter = new ShareDataTableAdapter(documentListService, schema);
|
||||
|
@@ -26,6 +26,7 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
ERR_ROW_NOT_FOUND: string = 'Row not found';
|
||||
ERR_COL_NOT_FOUND: string = 'Column not found';
|
||||
|
||||
private _sortingMode: string;
|
||||
private sorting: DataSorting;
|
||||
private rows: DataRow[];
|
||||
private columns: DataColumn[];
|
||||
@@ -37,12 +38,26 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
permissionsStyle: PermissionStyleModel[];
|
||||
selectedRow: DataRow;
|
||||
|
||||
set sortingMode(value: string) {
|
||||
let newValue = (value || 'client').toLowerCase();
|
||||
if (newValue !== 'client' && newValue !== 'server') {
|
||||
newValue = 'client';
|
||||
}
|
||||
this._sortingMode = newValue;
|
||||
}
|
||||
|
||||
get sortingMode(): string {
|
||||
return this._sortingMode;
|
||||
}
|
||||
|
||||
constructor(private documentListService: DocumentListService,
|
||||
schema: DataColumn[] = [],
|
||||
sorting?: DataSorting) {
|
||||
sorting?: DataSorting,
|
||||
sortingMode: string = 'client') {
|
||||
this.rows = [];
|
||||
this.columns = schema || [];
|
||||
this.sorting = sorting;
|
||||
this.sortingMode = sortingMode;
|
||||
}
|
||||
|
||||
getRows(): Array<DataRow> {
|
||||
@@ -148,6 +163,10 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
}
|
||||
|
||||
private sortRows(rows: DataRow[], sorting: DataSorting) {
|
||||
if (this.sortingMode === 'server') {
|
||||
return;
|
||||
}
|
||||
|
||||
const options: Intl.CollatorOptions = {};
|
||||
|
||||
if (sorting && sorting.key && rows && rows.length > 0) {
|
||||
@@ -194,17 +213,19 @@ export class ShareDataTableAdapter implements DataTableAdapter {
|
||||
rows = rows.filter(this.filter);
|
||||
}
|
||||
|
||||
// Sort by first sortable or just first column
|
||||
if (this.columns && this.columns.length > 0) {
|
||||
let sorting = this.getSorting();
|
||||
if (sorting) {
|
||||
this.sortRows(rows, sorting);
|
||||
} else {
|
||||
let sortable = this.columns.filter(c => c.sortable);
|
||||
if (sortable.length > 0) {
|
||||
this.sort(sortable[0].key, 'asc');
|
||||
if (this.sortingMode !== 'server') {
|
||||
// Sort by first sortable or just first column
|
||||
if (this.columns && this.columns.length > 0) {
|
||||
let sorting = this.getSorting();
|
||||
if (sorting) {
|
||||
this.sortRows(rows, sorting);
|
||||
} else {
|
||||
this.sort(this.columns[0].key, 'asc');
|
||||
let sortable = this.columns.filter(c => c.sortable);
|
||||
if (sortable.length > 0) {
|
||||
this.sort(sortable[0].key, 'asc');
|
||||
} else {
|
||||
this.sort(this.columns[0].key, 'asc');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user