import { Entity } from '../providers/entity' ; import { Provider } from '../providers/provider' ; import { DisabledRequest , IUpdateEvent , } from '../../../../controller/session/dependencies/search/dependencies/disabled/controller.session.tab.search.disabled.storage' ; import { IComponentDesc } from 'chipmunk-client-material' ; import { Session } from '../../../../controller/session/session' ; import { Subscription } from 'rxjs' ; import { SidebarAppSearchManagerDisabledsComponent } from './list/component' ; import { IMenuItem } from '../../../../services/standalone/service.contextmenu' ; import { IDisabledEntitySupport } from 'src/app/environment/controller/session/dependencies/search/dependencies/disabled/controller.session.tab.search.disabled.support' ; import { Logger } from 'chipmunk.client.toolkit' ; import { FilterRequest } from '../../../../controller/session/dependencies/search/dependencies/filters/controller.session.tab.search.filters.request' ; import { ChartRequest } from '../../../../controller/session/dependencies/search/dependencies/charts/controller.session.tab.search.charts.request' ; import { CdkDragDrop } from '@angular/cdk/drag-drop' ; import { TRequest , EListID } from '../service/service' ; import { EntityData } from '../providers/entity.data' ; export class ProviderDisabled extends Provider < DisabledRequest > { private _subs : { [ key ] : Subscription ; } = { } ; private _entities : Map < string , Entity < DisabledRequest > > = new Map ( ) ; private _logger : Logger = new Logger ( 'ProviderDisabled' ) ; private _listID : EListID = EListID . disabledList ; constructor ( ) { super ( ) ; this . setSessionController ( super . getSession ( ) ) ; } public unsubscribe ( ) { this . _subs !== undefined && Object . keys ( this . _subs ) . forEach ( ( key ) => { this . _subs [ key ] . unsubscribe ( ) ; } ) ; } public setSessionController ( session ) { this . unsubscribe ( ) ; if ( session === undefined ) { return ; } this . _subs . updated = session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . getObservable ( ) . updated . subscribe ( ( event ? ) => { super . change ( ) ; if ( event === undefined ) { return ; } if ( event . added instanceof DisabledRequest ) { this . select ( ) . set ( { guid : event . added . getGUID ( ) , sender : undefined , ignore : true , } ) ; } if ( event . removed instanceof DisabledRequest || event . requests . length === 0 ) { this . select ( ) . drop ( ) ; } } ) ; } public get ( ) { const guids = [ ] ; const session = super . getSession ( ) ; const entities = session === undefined ? [ ] : session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . get ( ) . map ( ( item ) => { let entity = this . _entities . get ( item . getGUID ( ) ) ; if ( entity === undefined ) { entity = new Entity < DisabledRequest > ( item , item . getGUID ( ) ) ; } else { entity . setEntity ( item ) ; } this . _entities . set ( item . getGUID ( ) , entity ) ; guids . push ( item . getGUID ( ) ) ; return entity ; } ) ; this . _entities . forEach ( ( _ , guid ) => { if ( guids . indexOf ( guid ) === - 1 ) { this . _entities . delete ( guid ) ; } } ) ; return entities ; } public reorder ( params ) { const session = super . getSession ( ) ; if ( session === undefined ) { return ; } session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . reorder ( params ) ; super . change ( ) ; } public getContentIfEmpty ( ) { return undefined ; } public getPanelName ( ) { return `Disabled` ; } public getPanelDesc ( ) { const count = this . get ( ) . length ; return `${ count } ${ count > 1 ? 'entities' : 'entity' }` ; } public getDetailsPanelName ( ) { return undefined ; } public getDetailsPanelDesc ( ) { return undefined ; } public getListComp ( ) { return { factory : SidebarAppSearchManagerDisabledsComponent , inputs : { provider : this , } , } ; } public getDetailsComp ( ) { return undefined ; } public getContextMenuItems ( target , selected ) { const session = this . getSession ( ) ; if ( session === undefined ) { return [ ] ; } const entities = selected . filter ( ( entity ) => { return ! ( entity . getEntity ( ) instanceof DisabledRequest ) ; } ) . map ( ( entity ) => { return entity . getEntity ( ) ; } ) ; const disableds = selected . filter ( ( entity ) => { return entity . getEntity ( ) instanceof DisabledRequest ; } ) . map ( ( entity ) => { return entity . getEntity ( ) ; } ) ; const match = disableds . find ( ( entity ) => { return entity . getEntity ( ) . matches !== undefined ; } ) ; const items = [ ] ; if ( entities . length > 0 && disableds . length === 0 ) { items . push ( { caption : `Disable` , handler : ( ) => { session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . add ( entities . map ( ( entity ) => { entity . remove ( session ) ; return new DisabledRequest ( entity ) ; } ) ) ; } , } ) ; } if ( entities . length === 0 && disableds . length > 0 ) { items . push ( { caption : `Enable` , handler : ( ) => { disableds . forEach ( ( disabled ) => { disabled . getEntity ( ) . restore ( session ) ; session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . remove ( disabled ) ; } ) ; } , } ) ; } if ( match !== undefined ) { const entry = match . getEntity ( ) ; items . push ( { caption : `Show Matches` , handler : ( ) => { super . openSearchToolbarApp ( ) . catch ( ( error ) => { this . _logger . error ( error . message ) ; } ) ; entry . matches !== undefined && entry . matches ( session ) ; } , } ) ; } return items ; } public search ( entity ) { const cEntity = entity . getEntity ( ) . getEntity ( ) ; const session = this . getSession ( ) ; if ( session === undefined ) { return ; } super . openSearchToolbarApp ( ) . then ( ( ) => { if ( cEntity instanceof ChartRequest ) { session . getSessionSearch ( ) . search ( new FilterRequest ( { request : ( cEntity as ChartRequest ) . asDesc ( ) . request , flags : { casesensitive : false , wholeword : false , regexp : true , } , } ) ) ; } else if ( cEntity instanceof FilterRequest ) { session . getSessionSearch ( ) . search ( cEntity ) ; } } ) . catch ( ( error ) => { this . _logger . error ( `Failed to show matches due to error: ${ error . message }` ) ; } ) ; } public actions ( target , selected ) { const self = this ; const disableds = selected . filter ( ( entity ) => { return entity . getEntity ( ) instanceof DisabledRequest ; } ) . map ( ( entity ) => { return entity . getEntity ( ) ; } ) ; const session = this . getSession ( ) ; if ( session === undefined ) { return { } ; } return { remove : disableds . length !== 0 ? ( ) => { disableds . forEach ( ( disabled ) => { session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . remove ( disabled ) ; } ) ; } : undefined , } ; } public isViable ( ) { return true ; } public itemDragged ( event ) { if ( event . previousContainer === event . container ) { this . reorder ( { prev : event . previousIndex , curt : event . currentIndex } ) ; } else { const index = event . previousIndex ; const data = event . previousContainer . data ; if ( data . entries !== undefined ) { const outside = data . entries [ event . previousIndex ] !== undefined ? data . entries [ index ] : undefined ; if ( outside !== undefined ) { const session = this . getSession ( ) ; if ( session === undefined ) { return ; } session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . add ( new DisabledRequest ( outside . getEntity ( ) as unknown as IDisabledEntitySupport ) , event . currentIndex ) ; outside . getEntity ( ) . remove ( session ) ; } } } } public get listID ( ) : EListID { return this . _listID ; } }