import { Directive , OnDestroy , OnInit , HostListener } from '@angular/core' ; import { DocumentListComponent } from '@alfresco/adf-content-services' ; import { ActivatedRoute , Router } from '@angular/router' ; import { UserPreferencesService } from '@alfresco/adf-core' ; import { Subject } from 'rxjs' ; import { Store } from '@ngrx/store' ; import { AppHookService } from '@alfresco/aca-shared' ; import { SetSelectedNodesAction } from '@alfresco/aca-shared/store' ; import { takeUntil , filter } from 'rxjs/operators' ; import { MinimalNodeEntity } from '@alfresco/js-api' ; @ Directive ( { selector : '[acaDocumentList]' } ) export class DocumentListDirective implements OnInit , OnDestroy { private isLibrary = false ; selectedNode : MinimalNodeEntity ; onDestroy$ = new Subject < boolean > ( ) ; get sortingPreferenceKey ( ) : string { return this . route . snapshot . data . sortingPreferenceKey ; } constructor ( private store , private documentList , private preferences , private route , private router , private appHookService ) { } ngOnInit ( ) { this . documentList . stickyHeader = true ; this . documentList . includeFields = [ 'isFavorite' , 'aspectNames' , 'definition' ] ; this . isLibrary = this . documentList . currentFolderId === '-mysites-' || this . router . url . endsWith ( '/libraries' ) || this . router . url . startsWith ( '/search-libraries' ) ; if ( this . sortingPreferenceKey ) { const current = this . documentList . sorting ; const key = this . preferences . get ( `${ this . sortingPreferenceKey }.sorting.key` , current [ 0 ] ) ; const direction = this . preferences . get ( `${ this . sortingPreferenceKey }.sorting.direction` , current [ 1 ] ) ; this . documentList . sorting = [ key , direction ] ; this . documentList . data . setSorting ( { key , direction } ) ; } this . documentList . ready . pipe ( filter ( ( ) => ! this . router . url . includes ( 'viewer:view' ) ) , takeUntil ( this . onDestroy$ ) ) . subscribe ( ( ) => this . onReady ( ) ) ; this . appHookService . reload . pipe ( takeUntil ( this . onDestroy$ ) ) . subscribe ( ( ) => { this . reload ( this . selectedNode ) ; } ) ; this . appHookService . reset . pipe ( takeUntil ( this . onDestroy$ ) ) . subscribe ( ( ) => { this . reset ( ) ; } ) ; } ngOnDestroy ( ) { this . onDestroy$ . next ( true ) ; this . onDestroy$ . complete ( ) ; } @ HostListener ( 'sorting-changed' , [ '$event' ] ) onSortingChanged ( event ) { if ( this . sortingPreferenceKey ) { this . preferences . set ( `${ this . sortingPreferenceKey }.sorting.key` , event . detail . key ) ; this . preferences . set ( `${ this . sortingPreferenceKey }.sorting.direction` , event . detail . direction ) ; } } @ HostListener ( 'node-select' , [ '$event' ] ) onNodeSelect ( event ) { if ( ! ! event . detail && ! ! event . detail . node ) { this . updateSelection ( ) ; this . selectedNode = event . detail . node ; } } @ HostListener ( 'node-unselect' ) onNodeUnselect ( ) { this . updateSelection ( ) ; } onReady ( ) { this . updateSelection ( ) ; } private updateSelection ( ) { const selection = this . documentList . selection . map ( ( node ) => { node [ 'isLibrary' ] = this . isLibrary ; return node ; } ) ; this . store . dispatch ( new SetSelectedNodesAction ( selection ) ) ; } private reload ( selectedNode ? ) { this . documentList . resetSelection ( ) ; if ( selectedNode ) { this . store . dispatch ( new SetSelectedNodesAction ( [ selectedNode ] ) ) ; } else { this . store . dispatch ( new SetSelectedNodesAction ( [ ] ) ) ; } this . documentList . reload ( ) ; } private reset ( ) { this . selectedNode = null ; this . documentList . resetSelection ( ) ; this . store . dispatch ( new SetSelectedNodesAction ( [ ] ) ) ; } }