Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #109 from msanchezal/US115894
Browse files Browse the repository at this point in the history
US115894: part 3 call sort in the start
  • Loading branch information
msanchezal authored Apr 13, 2020
2 parents 1e50cc9 + cc55b21 commit 2c17c89
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
17 changes: 12 additions & 5 deletions src/cardstack/al-cardstack-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -237,22 +241,25 @@ 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 );
}
} 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;
Expand Down
4 changes: 2 additions & 2 deletions test/al-cardstack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ 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];
expect( card.properties.date_created ).to.be.gte( last );
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 );
Expand Down

0 comments on commit 2c17c89

Please sign in to comment.