import { Entity } from '../providers/entity' ; import { Provider } from '../providers/provider' ; import { FilterRequest } from '../../../../controller/session/dependencies/search/dependencies/filters/controller.session.tab.search.filters.storage' ; import { RangeRequest } from '../../../../controller/session/dependencies/search/dependencies/timeranges/controller.session.tab.search.ranges.request' ; import { DisabledRequest } from '../../../../controller/session/dependencies/search/dependencies/disabled/controller.session.tab.search.disabled' ; import { IRangesStorageUpdated } from '../../../../controller/session/dependencies/search/dependencies/timeranges/controller.session.tab.search.ranges.storage' ; import { IComponentDesc } from 'chipmunk-client-material' ; import { Session } from '../../../../controller/session/session' ; import { Subscription } from 'rxjs' ; import { SidebarAppSearchManagerTimeRangesComponent } from './list/component' ; import { SidebarAppSearchManagerTimerangeDetailsComponent } from './details/component' ; import { IMenuItem } from '../../../../services/standalone/service.contextmenu' ; import { CancelablePromise } from 'chipmunk.client.toolkit' ; import { CdkDragDrop } from '@angular/cdk/drag-drop' ; import { EntityData } from '../providers/entity.data' ; import * as Toolkit from 'chipmunk.client.toolkit' ; import SearchManagerService , { TRequest , EListID } from '../service/service' ; export class ProviderRanges extends Provider < RangeRequest > { private _subs : { [ key ] : Subscription ; } = { } ; private _entities : Map < string , Entity < RangeRequest > > = new Map ( ) ; private _listID : EListID = EListID . rangesList ; 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 ( ) . getRangesAPI ( ) . getStorage ( ) . getObservable ( ) . updated . subscribe ( ( event ? ) => { super . change ( ) ; if ( event === undefined ) { return ; } if ( event . added instanceof RangeRequest ) { this . select ( ) . set ( { guid : event . added . getGUID ( ) } ) ; } if ( event . removed instanceof RangeRequest || event . ranges . length === 0 ) { this . select ( ) . drop ( ) ; } } ) ; } public get ( ) { const guids = [ ] ; const session = super . getSession ( ) ; const entities = session === undefined ? [ ] : session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . get ( ) . map ( ( filter ) => { let entity = this . _entities . get ( filter . getGUID ( ) ) ; if ( entity === undefined ) { entity = new Entity < RangeRequest > ( filter , filter . getGUID ( ) ) ; } else { entity . setEntity ( filter ) ; } this . _entities . set ( filter . getGUID ( ) , entity ) ; guids . push ( filter . 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 ( ) . getRangesAPI ( ) . getStorage ( ) . reorder ( params ) ; super . change ( ) ; } public getContentIfEmpty ( ) { return undefined ; } public getPanelName ( ) { return `Time Ranges` ; } public getPanelDesc ( ) { const count = this . get ( ) . length ; return `${ count } range${ count > 1 ? 's' : '' }` ; } public getDetailsPanelName ( ) { return `Time Range Details` ; } public getDetailsPanelDesc ( ) { return '' ; } public getListComp ( ) { return { factory : SidebarAppSearchManagerTimeRangesComponent , inputs : { provider : this , } , } ; } public getDetailsComp ( ) { return { factory : SidebarAppSearchManagerTimerangeDetailsComponent , inputs : { provider : this , } , } ; } public getContextMenuItems ( target , selected ) { const items = [ ] ; const filters = selected . filter ( ( entity ) => entity . getEntity ( ) instanceof FilterRequest ) ; const ranges = selected . filter ( ( entity ) => entity . getEntity ( ) instanceof RangeRequest ) ; const session = super . getSession ( ) ; if ( session === undefined ) { return [ ] ; } if ( selected . length >= 2 && filters . length >= 2 && filters . length === selected . length ) { items . push ( { caption : `Create Time Range` , handler : ( ) => { session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . add ( new RangeRequest ( { points : selected . map ( ( _ ) => _ . getEntity ( ) ) , alias : `Time range #${ session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . get ( ) . length + 1 }` , } ) ) ; } , } ) ; } if ( ranges . length > 0 ) { items . push ( { caption : `Remove bars from chart` , handler : ( ) => { ranges . forEach ( ( entity ) => { session . getTimestamp ( ) . removeRange ( entity . getEntity ( ) . getGUID ( ) ) ; } ) ; } , } ) ; } return items ; } public actions ( target , selected ) { const self = this ; const entities = selected . filter ( ( entity ) => { return entity . getEntity ( ) instanceof RangeRequest ; } ) ; const session = super . getSession ( ) ; if ( session === undefined ) { return { } ; } return { remove : entities . length !== 0 ? ( ) => { if ( entities . length === self . get ( ) . length ) { session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . clear ( ) ; self . change ( ) ; } else { entities . forEach ( ( entity ) => { session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . remove ( entity . getEntity ( ) ) ; } ) ; } entities . forEach ( ( entity ) => { session . getTimestamp ( ) . removeRange ( entity . getEntity ( ) . getGUID ( ) ) ; } ) ; } : undefined , } ; } public search ( entity ) { return ; } public apply ( ) { return new Promise ( ( resolve , reject ) => { let entities = this . select ( ) . getEntities ( ) ; entities = entities . length === 0 ? Array . from ( this . _entities . values ( ) ) : entities ; if ( entities . length === 0 ) { return resolve ( ) ; } const session = super . getSession ( ) ; if ( session === undefined ) { return reject ( new Error ( `No session has been found` ) ) ; } const errors = [ ] ; const stack = new Map ( ) ; entities . forEach ( ( entity ) => { const task = session . getSessionSearch ( ) . getRangesAPI ( ) . search ( entity . getEntity ( ) ) ; const id = Toolkit . guid ( ) ; if ( task instanceof Error ) { errors . push ( task ) ; } else { stack . set ( id , task ) ; task . catch ( ( err ) => { errors . push ( err ) ; } ) . finally ( ( ) => { stack . delete ( id ) ; if ( stack . size === 0 ) { if ( errors . length > 0 ) { reject ( new Error ( errors . map ( ( e ) => e . message ) . join ( '\n' ) ) ) ; } else { resolve ( ) ; } } } ) ; } } ) ; if ( stack . size === 0 && errors . length > 0 ) { reject ( new Error ( errors . map ( ( e ) => e . message ) . join ( '\n' ) ) ) ; } } ) ; } public isViable ( ) { const dragging = SearchManagerService . dragging ; if ( dragging ) { const request = dragging . getEntity ( ) ; if ( request instanceof DisabledRequest ) { if ( ( request as DisabledRequest ) . getEntity ( ) instanceof RangeRequest ) { return true ; } return false ; } else if ( request instanceof RangeRequest ) { return true ; } } return false ; } public itemDragged ( event ) { if ( event . previousContainer === event . container ) { this . reorder ( { prev : event . previousIndex , curt : event . currentIndex } ) ; } else { const session = super . getSession ( ) ; if ( session === undefined ) { return ; } const index = event . previousIndex ; const data = event . previousContainer . data ; if ( data . disabled !== undefined ) { const outside = data . disabled [ event . previousIndex ] !== undefined ? data . disabled [ index ] : undefined ; if ( outside !== undefined && typeof outside . getEntity ( ) . getEntity === 'function' && outside . getEntity ( ) . getEntity ( ) instanceof RangeRequest ) { session . getSessionSearch ( ) . getDisabledAPI ( ) . getStorage ( ) . remove ( outside . getEntity ( ) ) ; session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . add ( outside . getEntity ( ) . getEntity ( ) as RangeRequest , event . currentIndex ) ; } } } } public get listID ( ) : EListID { return this . _listID ; } }