import { Subject , Subscription } from 'rxjs' ; import { getContrastColor , scheme_color_accent } from '../../../../../../theme/colors' ; import { FilterRequest , IDesc as IFilterDesc , } from '../filters/controller.session.tab.search.filters.request' ; import { IDisabledEntitySupport , EEntityTypeRef , } from '../disabled/controller.session.tab.search.disabled.support' ; import { Session } from '../../../../session' ; import * as Toolkit from 'chipmunk.client.toolkit' ; export interface IDesc { guid : string ; alias : string ; points : Array < FilterRequest | IFilterDesc > ; color : string ; strict : boolean ; } export interface IDescOptional { guid ? : string ; alias ? : string ; points : Array < FilterRequest | IFilterDesc > ; color ? : string ; strict ? : boolean ; } export interface IDescUpdating { alias ? : string ; points ? : Array < FilterRequest | IFilterDesc > ; color ? : string ; strict ? : boolean ; } export interface IRangeUpdateEvent { range : RangeRequest ; updated : { points : boolean ; color : boolean ; alias : boolean ; strict : boolean ; } ; } export class RangeRequest implements IDisabledEntitySupport { private _points : FilterRequest [ ] ; private _color : string ; private _alias : string ; private _strict : boolean ; private _guid : string ; private _subscriptions : { [ key ] : Subscription [ ] ; } = { updated : [ ] , } ; private _subjects : { updated : Subject < IRangeUpdateEvent > ; } = { updated : new Subject < IRangeUpdateEvent > ( ) , } ; public static isValidAlias ( alias ) { if ( typeof alias !== 'string' || alias . trim ( ) === '' ) { return false ; } return true ; } constructor ( desc ) { if ( ! ( desc . points instanceof Array ) || desc . points . length < 2 ) { throw new Error ( `To create range should be defined at least 2 FilterRequest as start and end of range` ) ; } this . _points = desc . points . map ( ( filter ) => { return filter instanceof FilterRequest ? filter : new FilterRequest ( filter ) ; } ) ; if ( typeof desc . guid === 'string' ) { this . _guid = desc . guid ; } else { this . _guid = Toolkit . hash ( this . _points . map ( ( _ ) => _ . getHash ( ) ) . join ( '' ) ) ; } if ( typeof desc . color === 'string' ) { this . _color = desc . color ; } else { this . _color = getContrastColor ( scheme_color_accent , true ) ; } if ( typeof desc . strict === 'boolean' ) { this . _strict = desc . strict ; } else { this . _strict = true ; } if ( typeof desc . alias !== 'string' || desc . alias . trim ( ) === '' ) { this . _alias = `Time range` ; } else { this . _alias = desc . alias ; } } public destroy ( ) { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { ( this . _subscriptions [ key ] as Subscription [ ] ) . forEach ( ( subscription ) => { subscription . unsubscribe ( ) ; } ) ; } ) ; } public onUpdated ( handler ) { this . _subscriptions . updated . push ( this . _subjects . updated . asObservable ( ) . subscribe ( handler ) ) ; } public getColor ( ) { return this . _color ; } public getAlias ( ) { return this . _alias ; } public getStrictState ( ) { return this . _strict ; } public asDesc ( ) { return { alias : this . getAlias ( ) , guid : this . getGUID ( ) , points : this . getPoints ( ) . map ( ( filter ) => { return filter . asDesc ( ) ; } ) , color : this . getColor ( ) , strict : this . getStrictState ( ) , } ; } public update ( desc ) { const event = { updated : { points : false , color : false , alias : false , strict : false , } , range : this , } ; if ( desc . points instanceof Array && desc . points . length >= 2 ) { event . updated . points = true ; } if ( typeof desc . strict === 'boolean' && this . setStrictState ( desc . strict , true ) ) { event . updated . strict = true ; } if ( typeof desc . color === 'string' && this . setColor ( desc . color ) ) { event . updated . color = true ; } if ( typeof desc . alias === 'string' && this . setAlias ( desc . alias ) ) { event . updated . alias = true ; } const hasToBeEmitted = event . updated . points || event . updated . color || event . updated . strict ; if ( hasToBeEmitted ) { this . _subjects . updated . next ( event ) ; } return hasToBeEmitted ; } public setColor ( color , silence = false ) { if ( this . _color === color ) { return false ; } this . _color = color ; if ( ! silence ) { this . _subjects . updated . next ( { updated : { points : false , color : true , alias : false , strict : false , } , range : this , } ) ; } return true ; } public setStrictState ( strict , silence = false ) { if ( this . _strict === strict ) { return false ; } this . _strict = strict ; if ( ! silence ) { this . _subjects . updated . next ( { updated : { points : false , color : false , alias : false , strict : true , } , range : this , } ) ; } return true ; } public setAlias ( alias , silence = false ) { if ( this . _alias === alias ) { return false ; } this . _alias = alias ; if ( ! silence ) { this . _subjects . updated . next ( { updated : { points : false , color : false , alias : true , strict : false , } , range : this , } ) ; } return true ; } public getPoints ( ) { return this . _points ; } public setPoint ( filter , index , silence = false ) { if ( this . _points [ index ] === undefined ) { return false ; } this . _points [ index ] = filter ; if ( ! silence ) { this . _subjects . updated . next ( { updated : { points : true , color : false , alias : false , strict : false , } , range : this , } ) ; } return true ; } public getGUID ( ) { return this . _guid ; } public getDisplayName ( ) { return this . _alias ; } public getIcon ( ) { return 'alarm' ; } public getTypeRef ( ) { return EEntityTypeRef . range ; } public remove ( session ) { session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . remove ( this ) ; } public restore ( session ) { session . getSessionSearch ( ) . getRangesAPI ( ) . getStorage ( ) . add ( this ) ; } }