import { Directive , EventEmitter , HostListener , Output } from '@angular/core' ; import { InfiniteScrollService } from '../../../services/infinite-scroll.service' ; enum SortType { TYPE = 'metadata.mime_type' , SIZE = 'length' , AUTHOR = 'metadata.author_id' , DATE = 'uploadDate' , FILENAME = 'filename' } @ Directive ( { selector : '[tableSortEvent]' } ) export class TableSortEventDirective { constructor ( private scrollService ) { } @ Output ( ) valueChanged = new EventEmitter < any > ( ) ; public htmlElement : HTMLElement ; private readonly tag : string = 'viewScroll' ; @ HostListener ( 'click' , [ '$event' ] ) sortDirectionHandler ( event ) { document . getElementById ( 'file-view-list' ) . scrollTop = 0 ; this . htmlElement = ( event . target as HTMLElement ) ; if ( this . htmlElement . getAttribute ( 'scope' ) ) { this . removeActiveClass ( ) ; this . setSortDirection ( ) ; } } private removeActiveClass ( ) { const children = this . htmlElement . parentNode . children ; let count = children . length ; while ( count -- ) { children [ count ] . classList . remove ( 'active' ) ; } } private setSortDirection ( ) { if ( this . htmlElement . classList . contains ( '_desc' ) ) { this . htmlElement . classList . remove ( '_desc' ) ; this . htmlElement . classList . add ( '_asc' ) ; } else { this . htmlElement . classList . remove ( '_asc' ) ; this . htmlElement . classList . add ( '_desc' ) ; } this . htmlElement . classList . add ( 'active' ) ; this . reloadApiParameters ( ) ; } private reloadApiParameters ( ) { this . scrollService . setCollectionParameters ( 1 , 100 , ( SortType ) [ this . htmlElement . innerText . toUpperCase ( ) ] , this . htmlElement . classList . contains ( '_desc' ) ? - 1 : 1 , this . tag ) ; this . valueChanged . emit ( this . scrollService . getCollectionParameters ( this . tag ) ) ; } }