import _ from 'lodash' ; import { Subject , BehaviorSubject } from 'rxjs' ; import moment from 'moment' ; import { areRefreshIntervalsDifferent , areTimeRangesDifferent } from './lib/diff_time_picker_vals' ; import { getForceNow } from './lib/get_force_now' ; import { TimefilterConfig , InputTimeRange , TimeRangeBounds } from './types' ; import { calculateBounds , getTime , RefreshInterval , TimeRange } from '../../../common' ; import { TimeHistoryContract } from './time_history' ; import { IndexPattern } from '../../index_patterns' ; export class Timefilter { private enabledUpdated$ = new BehaviorSubject ( false ) ; private timeUpdate$ = new Subject ( ) ; private refreshIntervalUpdate$ = new Subject ( ) ; private autoRefreshFetch$ = new Subject ( ) ; private fetch$ = new Subject ( ) ; private _time : TimeRange ; private _refreshInterval ! : RefreshInterval ; private _history : TimeHistoryContract ; private _isTimeRangeSelectorEnabled : boolean = false ; private _isAutoRefreshSelectorEnabled : boolean = false ; private _autoRefreshIntervalId : number = 0 ; private readonly timeDefaults : TimeRange ; private readonly refreshIntervalDefaults : RefreshInterval ; constructor ( config , timeHistory ) { this . _history = timeHistory ; this . timeDefaults = config . timeDefaults ; this . refreshIntervalDefaults = config . refreshIntervalDefaults ; this . _time = config . timeDefaults ; this . setRefreshInterval ( config . refreshIntervalDefaults ) ; } public isTimeRangeSelectorEnabled ( ) { return this . _isTimeRangeSelectorEnabled ; } public isAutoRefreshSelectorEnabled ( ) { return this . _isAutoRefreshSelectorEnabled ; } public getEnabledUpdated$ = ( ) => { return this . enabledUpdated$ . asObservable ( ) ; } ; public getTimeUpdate$ = ( ) => { return this . timeUpdate$ . asObservable ( ) ; } ; public getRefreshIntervalUpdate$ = ( ) => { return this . refreshIntervalUpdate$ . asObservable ( ) ; } ; public getAutoRefreshFetch$ = ( ) => { return this . autoRefreshFetch$ . asObservable ( ) ; } ; public getFetch$ = ( ) => { return this . fetch$ . asObservable ( ) ; } ; public getTime = ( ) : TimeRange => { const { from , to } = this . _time ; return { ... this . _time , from : moment . isMoment ( from ) ? from . toISOString ( ) : from , to : moment . isMoment ( to ) ? to . toISOString ( ) : to , } ; } ; param { Object } time property property public setTime = ( time ) => { const newTime = Object . assign ( this . getTime ( ) , time ) ; if ( areTimeRangesDifferent ( this . getTime ( ) , newTime ) ) { this . _time = { from : newTime . from , to : newTime . to , } ; this . _history . add ( this . _time ) ; this . timeUpdate$ . next ( ) ; this . fetch$ . next ( ) ; } } ; public getRefreshInterval = ( ) => { return _ . clone ( this . _refreshInterval ) ; } ; param { Object } refreshInterval property property public setRefreshInterval = ( refreshInterval ) => { const prevRefreshInterval = this . getRefreshInterval ( ) ; const newRefreshInterval = { ... prevRefreshInterval , ... refreshInterval } ; if ( newRefreshInterval . value <= 0 ) { newRefreshInterval . value = 0 ; newRefreshInterval . pause = true ; } this . _refreshInterval = { value : newRefreshInterval . value , pause : newRefreshInterval . pause , } ; if ( prevRefreshInterval && areRefreshIntervalsDifferent ( prevRefreshInterval , newRefreshInterval ) ) { this . refreshIntervalUpdate$ . next ( ) ; if ( ! newRefreshInterval . pause && newRefreshInterval . value !== 0 ) { this . fetch$ . next ( ) ; } } clearInterval ( this . _autoRefreshIntervalId ) ; if ( ! newRefreshInterval . pause ) { this . _autoRefreshIntervalId = window . setInterval ( ( ) => this . autoRefreshFetch$ . next ( ) , newRefreshInterval . value ) ; } } ; public createFilter = ( indexPattern , timeRange ? ) => { return getTime ( indexPattern , timeRange ? timeRange : this . _time , { forceNow : this . getForceNow ( ) , } ) ; } ; public getBounds ( ) { return this . calculateBounds ( this . _time ) ; } public calculateBounds ( timeRange ) { return calculateBounds ( timeRange , { forceNow : this . getForceNow ( ) } ) ; } public getActiveBounds ( ) { if ( this . isTimeRangeSelectorEnabled ( ) ) { return this . getBounds ( ) ; } } /**
     * Show the time bounds selector part of the time filter
     */ public enableTimeRangeSelector = ( ) => { this . _isTimeRangeSelectorEnabled = true ; this . enabledUpdated$ . next ( true ) ; } ; /**
     * Hide the time bounds selector part of the time filter
     */ public disableTimeRangeSelector = ( ) => { this . _isTimeRangeSelectorEnabled = false ; this . enabledUpdated$ . next ( false ) ; } ; /**
     * Show the auto refresh part of the time filter
     */ public enableAutoRefreshSelector = ( ) => { this . _isAutoRefreshSelectorEnabled = true ; this . enabledUpdated$ . next ( true ) ; } ; /**
     * Hide the auto refresh part of the time filter
     */ public disableAutoRefreshSelector = ( ) => { this . _isAutoRefreshSelectorEnabled = false ; this . enabledUpdated$ . next ( false ) ; } ; public getTimeDefaults ( ) { return _ . cloneDeep ( this . timeDefaults ) ; } public getRefreshIntervalDefaults ( ) { return _ . cloneDeep ( this . refreshIntervalDefaults ) ; } private getForceNow = ( ) => { return getForceNow ( ) ; } ; } export type TimefilterContract = PublicMethodsOf < Timefilter > ;