From cc55b21fe92d91dab223fa6d238bb1b72f5685d0 Mon Sep 17 00:00:00 2001 From: Maryit Date: Mon, 13 Apr 2020 16:20:49 -0500 Subject: [PATCH] US115894: part 3 call sort in the start --- package.json | 2 +- src/cardstack/al-cardstack-view.ts | 17 ++++++++++++----- test/al-cardstack.spec.ts | 4 ++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2464e3f..19d8a2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@al/common", - "version": "1.2.16", + "version": "1.2.17", "description": "A collection of lightweight utilities and common types shared across NEPAL libraries and applications based on them.", "main": "./dist/umd/index.js", "scripts": { diff --git a/src/cardstack/al-cardstack-view.ts b/src/cardstack/al-cardstack-view.ts index 95406b2..5184e44 100644 --- a/src/cardstack/al-cardstack-view.ts +++ b/src/cardstack/al-cardstack-view.ts @@ -38,7 +38,7 @@ export abstract class AlCardstackView< EntityType=any, public textFilter: string|RegExp|null = null; // Regular expression to filter results with (deprecated?) public groupingBy: AlCardstackPropertyDescriptor|null = null; // Grouping property public sortingBy: AlCardstackPropertyDescriptor|null = null; // Sortation property - public sortOrder: string = "ASC"; // Sortation direction, either "ASC" or "DESC". Yes, "sortation" is a real word ;-) + public sortOrder: 'asc'|'desc' = "asc"; // Sortation direction, either "asc" or "desc". Yes, "sortation" is a real word ;-) public dateRange: Date[] = []; public checked: boolean = false; // Defines which filters are currently "active" @@ -93,7 +93,11 @@ export abstract class AlCardstackView< EntityType=any, // if we have pagination enable just load a section of data if( this.localPagination ) { - this.applyFiltersAndSearch(); + if(this.sortingBy && this.sortOrder) { + this.applySortBy(this.sortingBy, this.sortOrder ); + } else { + this.applyFiltersAndSearch(); + } } else { // if we dont have pagination enable load all data this.addNextSection(ingestedCards); @@ -237,14 +241,17 @@ export abstract class AlCardstackView< EntityType=any, * This is the default implementation, which can be called if the deriving class doesn't implement OR wants to call into the super class. * Returning `true` indicates that the current list of items needs to be flushed and data retrieval should start from scratch. */ - public applySortBy( descriptor:AlCardstackPropertyDescriptor, order:string = "DESC" ):boolean { + public applySortBy( descriptor:AlCardstackPropertyDescriptor, order:string = "desc" ):boolean { + this.sortingBy = descriptor; + this.sortOrder = order.toLowerCase() === 'asc' ? 'asc' : 'desc'; + this.rawCards = this.rawCards.sort( ( a, b ) => { let pa = a.properties[descriptor.property]; let pb = b.properties[descriptor.property]; if ( typeof( pa ) === 'string' || typeof( pb ) === 'string' ) { pa = pa ? pa: ''; pb = pb ? pb : ''; - if ( order === 'ASC' ) { + if ( order === 'asc' ) { return pa.localeCompare( pb ); } else { return pb.localeCompare( pa ); @@ -252,7 +259,7 @@ export abstract class AlCardstackView< EntityType=any, } else if ( typeof( pa ) === 'number' || typeof( pb ) === 'number' ) { a = pa ? pa: 0; pb = pb ? pb : 0; - if ( order === 'ASC' ) { + if ( order === 'asc' ) { return pa - pb; } else { return pb - pa; diff --git a/test/al-cardstack.spec.ts b/test/al-cardstack.spec.ts index 55fc7ef..cd644bf 100644 --- a/test/al-cardstack.spec.ts +++ b/test/al-cardstack.spec.ts @@ -250,7 +250,7 @@ describe( 'AlCardstackView', () => { } ); it( 'should sort numeric properties in the expected way', () => { let date = stack.getProperty( "date_created" ); - stack.applySortBy( date, 'ASC' ); + stack.applySortBy( date, 'asc' ); let last = 0; for ( let i = 0; i < stack.cards.length; i++ ) { let card = stack.cards[i]; @@ -258,7 +258,7 @@ describe( 'AlCardstackView', () => { last = card.properties.date_created; } - stack.applySortBy( date, 'DESC' ); + stack.applySortBy( date, 'desc' ); for ( let i = 0; i < stack.cards.length; i++ ) { let card = stack.cards[i]; expect( card.properties.date_created ).to.be.lte( last );