import { Pipe , PipeTransform } from '@angular/core' ; import { GtConfigField } from '../interfaces/gt-config-field' ; import { GtRow } from '../interfaces/gt-row' ; @ Pipe ( { name : 'gtOrderBy' } ) export class GtOrderByPipe < R extends GtRow > implements PipeTransform { static _orderByComparator ( a , b ) { if ( typeof a === 'boolean' ) { a = a . toString ( ) ; } if ( typeof b === 'boolean' ) { b = b . toString ( ) ; } if ( isNaN ( parseFloat ( a ) ) || ! isFinite ( a ) || ( isNaN ( parseFloat ( b ) ) || ! isFinite ( b ) ) ) { if ( b === null || ( typeof b === 'undefined' && ( a !== null && typeof a !== 'undefined' ) ) ) { return 1 ; } if ( a === null || ( typeof a === 'undefined' && ( b !== null && typeof b !== 'undefined' ) ) ) { return - 1 ; } try { if ( a . toLowerCase ( ) < b . toLowerCase ( ) ) { return - 1 ; } if ( a . toLowerCase ( ) > b . toLowerCase ( ) ) { return 1 ; } } catch ( error ) { return 0 ; } } else { if ( parseFloat ( a ) < parseFloat ( b ) ) { return - 1 ; } if ( parseFloat ( a ) > parseFloat ( b ) ) { return 1 ; } } return 0 ; } /** Return property */ private getProperty = function ( array , key ) { for ( let i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] . objectKey === key ) { return array [ i ] ; } } } ; /** Return sort function */ private getSortFunction ( field ) { if ( ! field ) { return false ; } if ( typeof field . sort === 'function' ) { return field . sort ; } else if ( typeof field . value === 'function' ) { return field . value ; } else { return false ; } } transform ( input , sortByProperties , fields , refreshSorting , refreshData ) { if ( ! Array . isArray ( input ) || input === null ) { return input ; } if ( ! Array . isArray ( sortByProperties ) || ( Array . isArray ( sortByProperties ) && sortByProperties . length === 1 ) ) { const propertyToCheck = sortByProperties [ 0 ] ; const desc = propertyToCheck . substr ( 0 , 1 ) === '-' ; if ( ! propertyToCheck || propertyToCheck === '-' || propertyToCheck === '+' ) { return ! desc ? input . sort ( ) : input . sort ( ) . reverse ( ) ; } else { const property = propertyToCheck . substr ( 0 , 1 ) === '+' || propertyToCheck . substr ( 0 , 1 ) === '-' ? propertyToCheck . substr ( 1 ) : propertyToCheck ; const sortFunction = this . getSortFunction ( this . getProperty ( fields , property ) ) ; return input . sort ( function ( a , b ) { const propertyA = sortFunction === false ? property === '$$gtRowId' ? + a . $$gtRowId . split ( '_' ) [ 0 ] : a [ property ] : sortFunction ( a ) ; const propertyB = sortFunction === false ? property === '$$gtRowId' ? + b . $$gtRowId . split ( '_' ) [ 0 ] : b [ property ] : sortFunction ( b ) ; if ( typeof propertyA === 'undefined' && typeof propertyB === 'undefined' ) { return ; } return ! desc ? GtOrderByPipe . _orderByComparator ( propertyA , propertyB ) : - GtOrderByPipe . _orderByComparator ( propertyA , propertyB ) ; } ) ; } } else { return input . sort ( ( a , b ) => { for ( let i = 0 ; i < sortByProperties . length ; i ++ ) { const desc = sortByProperties [ i ] . substr ( 0 , 1 ) === '-' ; const property = sortByProperties [ i ] . substr ( 0 , 1 ) === '+' || sortByProperties [ i ] . substr ( 0 , 1 ) === '-' ? sortByProperties [ i ] . substr ( 1 ) : sortByProperties [ i ] ; const sortFunction = this . getSortFunction ( this . getProperty ( fields , property ) ) ; const propertyA = sortFunction === false ? property === '$$gtRowId' ? + a . $$gtRowId . split ( '_' ) [ 0 ] : a [ property ] : sortFunction ( a ) ; const propertyB = sortFunction === false ? property === '$$gtRowId' ? + b . $$gtRowId . split ( '_' ) [ 0 ] : b [ property ] : sortFunction ( b ) ; const comparison = ! desc ? GtOrderByPipe . _orderByComparator ( propertyA , propertyB ) : - GtOrderByPipe . _orderByComparator ( propertyA , propertyB ) ; if ( comparison !== 0 ) { return comparison ; } } return 0 ; } ) ; } } }