1- import { cloneArray , IBaseEventArgs , parseDate , resolveNestedPath } from '../core/utils' ;
2- import { GridType } from '../grids/common/grid.interface' ;
3- import { IGroupByExpandState } from './groupby-expand-state.interface' ;
4- import { IGroupByRecord } from './groupby-record.interface' ;
5- import { IGroupingState } from './groupby-state.interface' ;
6- import { IGroupingExpression } from './grouping-expression.interface' ;
7- import { IGroupByResult } from './grouping-result.interface' ;
8- import { getHierarchy , isHierarchyMatch } from './operations' ;
9-
10- const DATE_TYPE = 'date' ;
11- const TIME_TYPE = 'time' ;
12- const DATE_TIME_TYPE = 'dateTime' ;
1+ import { IBaseEventArgs } from '../core/utils' ;
132
143export enum SortingDirection {
154 None = 0 ,
@@ -32,8 +21,7 @@ export interface ISortingStrategy {
3221 ignoreCase : boolean ,
3322 valueResolver : ( obj : any , key : string , isDate ?: boolean ) => any ,
3423 isDate ?: boolean ,
35- isTime ?: boolean ,
36- grid ?: GridType
24+ isTime ?: boolean
3725 ) => any [ ] ;
3826}
3927
@@ -85,8 +73,8 @@ export class DefaultSortingStrategy implements ISortingStrategy {
8573 isDate : boolean ,
8674 isTime : boolean
8775 ) {
88- let a = valueResolver . call ( this , obj1 , key , isDate , isTime ) ;
89- let b = valueResolver . call ( this , obj2 , key , isDate , isTime ) ;
76+ let a = valueResolver ( obj1 , key , isDate , isTime ) ;
77+ let b = valueResolver ( obj2 , key , isDate , isTime ) ;
9078 if ( ignoreCase ) {
9179 a = a && a . toLowerCase ? a . toLowerCase ( ) : a ;
9280 b = b && b . toLowerCase ? b . toLowerCase ( ) : b ;
@@ -97,164 +85,4 @@ export class DefaultSortingStrategy implements ISortingStrategy {
9785 protected arraySort ( data : any [ ] , compareFn ?: ( arg0 : any , arg1 : any ) => number ) : any [ ] {
9886 return data . sort ( compareFn ) ;
9987 }
100- }
101-
102- export interface IGridSortingStrategy {
103- sort ( data : any [ ] , expressions : ISortingExpression [ ] , grid ?: GridType ) : any [ ] ;
104- }
105-
106- export class NoopSortingStrategy implements IGridSortingStrategy {
107- private static _instance : NoopSortingStrategy = null ;
108-
109- private constructor ( ) { }
110-
111- public static instance ( ) {
112- return this . _instance || ( this . _instance = new NoopSortingStrategy ( ) ) ;
113- }
114-
115- public sort ( data : any [ ] ) : any [ ] {
116- return data ;
117- }
118- }
119-
120- export class IgxSorting implements IGridSortingStrategy {
121- public sort ( data : any [ ] , expressions : ISortingExpression [ ] , grid ?: GridType ) : any [ ] {
122- return this . sortDataRecursive ( data , expressions , 0 , grid ) ;
123- }
124-
125- protected groupDataRecursive < T > ( data : T [ ] , state : IGroupingState , level : number ,
126- parent : IGroupByRecord , metadata : IGroupByRecord [ ] , grid : GridType = null ,
127- groupsRecords : any [ ] = [ ] , fullResult : IGroupByResult = { data : [ ] , metadata : [ ] } ) : T [ ] {
128- const expressions = state . expressions ;
129- const expansion = state . expansion ;
130- let i = 0 ;
131- let result = [ ] ;
132- while ( i < data . length ) {
133- const column = grid ? grid . getColumnByName ( expressions [ level ] . fieldName ) : null ;
134- const isDate = column ?. dataType === DATE_TYPE || column ?. dataType === DATE_TIME_TYPE ;
135- const isTime = column ?. dataType === TIME_TYPE ;
136- const group = this . groupedRecordsByExpression ( data , i , expressions [ level ] , isDate ) ;
137- const groupRow : IGroupByRecord = {
138- expression : expressions [ level ] ,
139- level,
140- records : cloneArray ( group ) ,
141- value : this . getFieldValue ( group [ 0 ] , expressions [ level ] . fieldName , isDate , isTime ) ,
142- groupParent : parent ,
143- groups : [ ] ,
144- height : grid ? grid . renderedRowHeight : null ,
145- column
146- } ;
147- if ( parent ) {
148- parent . groups . push ( groupRow ) ;
149- } else {
150- groupsRecords . push ( groupRow ) ;
151- }
152- const hierarchy = getHierarchy ( groupRow ) ;
153- const expandState : IGroupByExpandState = expansion . find ( ( s ) =>
154- isHierarchyMatch ( s . hierarchy || [ { fieldName : groupRow . expression . fieldName , value : groupRow . value } ] , hierarchy ) ) ;
155- const expanded = expandState ? expandState . expanded : state . defaultExpanded ;
156- let recursiveResult ;
157- result . push ( groupRow ) ;
158- metadata . push ( null ) ;
159- fullResult . data . push ( groupRow ) ;
160- fullResult . metadata . push ( null ) ;
161- if ( level < expressions . length - 1 ) {
162- recursiveResult = this . groupDataRecursive ( group , state , level + 1 , groupRow ,
163- expanded ? metadata : [ ] , grid , groupsRecords , fullResult ) ;
164- if ( expanded ) {
165- result = result . concat ( recursiveResult ) ;
166- }
167- } else {
168- for ( const groupItem of group ) {
169- fullResult . metadata . push ( groupRow ) ;
170- fullResult . data . push ( groupItem ) ;
171- }
172- if ( expanded ) {
173- metadata . push ( ...fullResult . metadata . slice ( fullResult . metadata . length - group . length ) ) ;
174- result . push ( ...fullResult . data . slice ( fullResult . data . length - group . length ) ) ;
175- }
176- }
177- i += group . length ;
178- }
179- return result ;
180- }
181-
182- protected getFieldValue ( obj : any , key : string , isDate : boolean = false , isTime : boolean = false ) : any {
183- let resolvedValue = resolveNestedPath ( obj , key ) ;
184- if ( isDate || isTime ) {
185- const date = parseDate ( resolvedValue ) ;
186- resolvedValue = isTime && date ?
187- new Date ( ) . setHours ( date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) , date . getMilliseconds ( ) ) : date ;
188-
189- }
190- return resolvedValue ;
191- }
192-
193- protected sortDataRecursive < T > ( data : T [ ] ,
194- expressions : ISortingExpression [ ] ,
195- expressionIndex : number = 0 ,
196- grid : GridType ) : T [ ] {
197- let i ;
198- let j ;
199- let gbData ;
200- let gbDataLen ;
201- const exprsLen = expressions . length ;
202- const dataLen = data . length ;
203- expressionIndex = expressionIndex || 0 ;
204- if ( expressionIndex >= exprsLen || dataLen <= 1 ) {
205- return data ;
206- }
207- const expr : ISortingExpression = expressions [ expressionIndex ] ;
208- if ( ! expr . strategy ) {
209- expr . strategy = DefaultSortingStrategy . instance ( ) ;
210- }
211- const column = grid ?. getColumnByName ( expr . fieldName ) ;
212- const isDate = column ?. dataType === DATE_TYPE || column ?. dataType === DATE_TIME_TYPE ;
213- const isTime = column ?. dataType === TIME_TYPE ;
214- data = expr . strategy . sort ( data , expr . fieldName , expr . dir , expr . ignoreCase , this . getFieldValue , isDate , isTime , grid ) ;
215- if ( expressionIndex === exprsLen - 1 ) {
216- return data ;
217- }
218- // in case of multiple sorting
219- for ( i = 0 ; i < dataLen ; i ++ ) {
220- gbData = this . groupedRecordsByExpression ( data , i , expr , isDate ) ;
221- gbDataLen = gbData . length ;
222- if ( gbDataLen > 1 ) {
223- gbData = this . sortDataRecursive ( gbData , expressions , expressionIndex + 1 , grid ) ;
224- }
225- for ( j = 0 ; j < gbDataLen ; j ++ ) {
226- data [ i + j ] = gbData [ j ] ;
227- }
228- i += gbDataLen - 1 ;
229- }
230- return data ;
231- }
232-
233- private groupedRecordsByExpression ( data : any [ ] ,
234- index : number ,
235- expression : IGroupingExpression ,
236- isDate : boolean = false ) : any [ ] {
237- const res = [ ] ;
238- const key = expression . fieldName ;
239- const len = data . length ;
240- const groupval = this . getFieldValue ( data [ index ] , key , isDate ) ;
241- res . push ( data [ index ] ) ;
242- index ++ ;
243- const comparer = expression . groupingComparer || DefaultSortingStrategy . instance ( ) . compareValues ;
244- for ( let i = index ; i < len ; i ++ ) {
245- if ( comparer ( this . getFieldValue ( data [ i ] , key , isDate ) , groupval ) === 0 ) {
246- res . push ( data [ i ] ) ;
247- } else {
248- break ;
249- }
250- }
251- return res ;
252- }
253- }
254-
255- export class IgxDataRecordSorting extends IgxSorting {
256-
257- protected getFieldValue ( obj : any , key : string , isDate : boolean = false , isTime : boolean = false ) : any {
258- return super . getFieldValue ( obj . data , key , isDate , isTime ) ;
259- }
260- }
88+ }
0 commit comments