1
1
import * as React from 'react' ;
2
2
import { IFile , FilesQueryResult } from '../../../../services/FileBrowserService.types' ;
3
- import { GeneralHelper } from '../../../../common/utilities/GeneralHelper' ;
3
+ import { GeneralHelper , sortDate , sortString } from '../../../../common/utilities/GeneralHelper' ;
4
4
import { LoadingState } from './IFileBrowserState' ;
5
5
import { TilesList } from '../TilesList/TilesList' ;
6
6
import { IFilePickerResult } from '../../FilePicker.types' ;
@@ -59,7 +59,7 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
59
59
{
60
60
key : 'column2' ,
61
61
name : strings . NameField ,
62
- fieldName : 'fileLeafRef ' ,
62
+ fieldName : 'name ' ,
63
63
minWidth : 210 ,
64
64
isRowHeader : true ,
65
65
isResizable : true ,
@@ -81,11 +81,11 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
81
81
{
82
82
key : 'column3' ,
83
83
name : strings . ModifiedField ,
84
- fieldName : 'dateModifiedValue ' ,
84
+ fieldName : 'modified ' ,
85
85
minWidth : 120 ,
86
86
isResizable : true ,
87
87
onColumnClick : this . _onColumnClick ,
88
- data : 'number ' ,
88
+ data : 'date ' ,
89
89
onRender : ( item : IFile ) => {
90
90
//const dateModified = moment(item.modified).format(strings.DateFormat);
91
91
return < span > { item . modified } </ span > ;
@@ -108,7 +108,7 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
108
108
{
109
109
key : 'column5' ,
110
110
name : strings . FileSizeField ,
111
- fieldName : 'fileSizeRaw ' ,
111
+ fieldName : 'fileSize ' ,
112
112
minWidth : 70 ,
113
113
maxWidth : 90 ,
114
114
isResizable : true ,
@@ -169,9 +169,9 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
169
169
< div className = { styles . scrollablePaneWrapper } >
170
170
< ScrollablePane >
171
171
172
- {
173
- this . state . selectedView !== 'tiles' ?
174
- (
172
+ {
173
+ this . state . selectedView !== 'tiles' ?
174
+ (
175
175
< DetailsList
176
176
items = { this . state . items }
177
177
compact = { this . state . selectedView === 'compact' }
@@ -187,17 +187,17 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
187
187
onRenderRow = { this . _onRenderRow }
188
188
onRenderMissingItem = { ( ) => { this . _loadNextDataRequest ( ) ; return null ; } }
189
189
/> ) :
190
- ( < TilesList
191
- fileBrowserService = { this . props . fileBrowserService }
192
- filePickerResult = { this . state . filePickerResult }
193
- selection = { this . _selection }
194
- items = { this . state . items }
195
-
196
- onFolderOpen = { this . _handleOpenFolder }
197
- onFileSelected = { this . _itemSelectionChanged }
198
- onNextPageDataRequest = { this . _loadNextDataRequest }
199
- /> )
200
- }
190
+ ( < TilesList
191
+ fileBrowserService = { this . props . fileBrowserService }
192
+ filePickerResult = { this . state . filePickerResult }
193
+ selection = { this . _selection }
194
+ items = { this . state . items }
195
+
196
+ onFolderOpen = { this . _handleOpenFolder }
197
+ onFileSelected = { this . _itemSelectionChanged }
198
+ onNextPageDataRequest = { this . _loadNextDataRequest }
199
+ /> )
200
+ }
201
201
</ ScrollablePane >
202
202
</ div >
203
203
</ div >
@@ -378,31 +378,56 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
378
378
isSortedDescending = ! isSortedDescending ;
379
379
}
380
380
381
- // Sort the items.
382
- items = items ! . concat ( [ ] ) . sort ( ( a , b ) => {
383
- const firstValue = a [ column . fieldName || '' ] ;
384
- const secondValue = b [ column . fieldName || '' ] ;
381
+ const updatedColumns : IColumn [ ] = columns ! . map ( col => {
382
+ col . isSorted = col . key === column . key ;
385
383
386
- if ( isSortedDescending ) {
387
- return firstValue > secondValue ? - 1 : 1 ;
388
- } else {
389
- return firstValue > secondValue ? 1 : - 1 ;
384
+ if ( col . isSorted ) {
385
+ col . isSortedDescending = isSortedDescending ;
390
386
}
387
+
388
+ return col ;
391
389
} ) ;
392
390
393
- // Reset the items and columns to match the state.
394
- this . setState ( {
395
- items : items ,
396
- columns : columns ! . map ( col => {
397
- col . isSorted = col . key === column . key ;
391
+ if ( ! this . state . nextPageQueryString ) { // all items have been loaded to the client
392
+ // Sort the items.
393
+ items = items ! . concat ( [ ] ) . sort ( ( a , b ) => {
394
+ if ( a . isFolder && ! b . isFolder ) {
395
+ return 1 ;
396
+ }
397
+ else if ( ! a . isFolder && b . isFolder ) {
398
+ return - 1 ;
399
+ }
400
+ let firstValue = a [ column . fieldName ] || '' ;
401
+ let secondValue = b [ column . fieldName ] || '' ;
398
402
399
- if ( col . isSorted ) {
400
- col . isSortedDescending = isSortedDescending ;
403
+ if ( column . data === 'string' ) {
404
+ return sortString ( firstValue , secondValue , isSortedDescending ) ;
405
+ }
406
+ else if ( column . data === 'date' ) {
407
+ return sortDate ( firstValue , secondValue , isSortedDescending ) ;
408
+ }
409
+ else if ( column . data === 'number' ) {
410
+ firstValue = parseFloat ( firstValue ) ;
411
+ secondValue = parseFloat ( secondValue ) ;
401
412
}
402
413
403
- return col ;
404
- } )
405
- } ) ;
414
+ return isSortedDescending ? secondValue - firstValue : firstValue - secondValue ;
415
+ } ) ;
416
+
417
+ // Reset the items and columns to match the state.
418
+ this . setState ( {
419
+ items : items ,
420
+ columns : updatedColumns
421
+ } ) ;
422
+ }
423
+ else {
424
+ this . setState ( {
425
+ items : [ ] ,
426
+ columns : updatedColumns
427
+ } , ( ) => {
428
+ this . _getListItems ( false ) ;
429
+ } ) ;
430
+ }
406
431
}
407
432
408
433
/**
@@ -454,20 +479,20 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
454
479
* Handles item click.
455
480
*/
456
481
private _handleItemInvoked = ( item : IFile ) => {
457
- // If a file is selected, open the library
458
- if ( item . isFolder ) {
459
- this . _handleOpenFolder ( item ) ;
460
- } else {
461
- // Otherwise, remember it was selected
462
- this . _itemSelectionChanged ( item ) ;
463
- }
464
- }
482
+ // If a file is selected, open the library
483
+ if ( item . isFolder ) {
484
+ this . _handleOpenFolder ( item ) ;
485
+ } else {
486
+ // Otherwise, remember it was selected
487
+ this . _itemSelectionChanged ( item ) ;
488
+ }
489
+ }
465
490
466
491
/**
467
492
* Gets all files in a library with a matchihg path
468
493
*/
469
494
private async _getListItems ( concatenateResults : boolean = false ) {
470
- const { libraryUrl, folderPath, accepts } = this . props ;
495
+ const { libraryUrl, folderPath, accepts, fileBrowserService } = this . props ;
471
496
let { items, nextPageQueryString } = this . state ;
472
497
473
498
let filesQueryResult : FilesQueryResult = { items : [ ] , nextHref : null } ;
@@ -480,8 +505,18 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
480
505
loadingState,
481
506
nextPageQueryString
482
507
} ) ;
508
+
509
+ let sortField : string | undefined = undefined ;
510
+ let isDesc : boolean | undefined = undefined ;
511
+
512
+ const sortByCol = this . state . columns ! . filter ( c => c . isSorted ) [ 0 ] ;
513
+ if ( sortByCol ) {
514
+ sortField = fileBrowserService . getSPFieldNameForFileProperty ( sortByCol . fieldName ) ;
515
+ isDesc = ! ! sortByCol . isSortedDescending ;
516
+ }
517
+
483
518
// Load files in the folder
484
- filesQueryResult = await this . props . fileBrowserService . getListItems ( libraryUrl , folderPath , accepts , nextPageQueryString ) ;
519
+ filesQueryResult = await this . props . fileBrowserService . getListItems ( libraryUrl , folderPath , accepts , nextPageQueryString , sortField , isDesc ) ;
485
520
} catch ( error ) {
486
521
filesQueryResult . items = null ;
487
522
console . error ( error . message ) ;
@@ -515,4 +550,8 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
515
550
} ) ;
516
551
}
517
552
}
553
+
554
+ private _onClientSort ( column : IColumn ) : void {
555
+
556
+ }
518
557
}
0 commit comments