import { IClusterHealthChunkQueryDescription , IClusterHealthChunk } from '../Models/HealthChunkRawDataTypes' ; import { TreeNodeGroupViewModel } from './TreeNodeGroupViewModel' ; import { ITreeNode } from './TreeTypes' ; import { Observable } from 'rxjs' ; export class TreeViewModel { public childGroupViewModel : TreeNodeGroupViewModel ; public selectedNode : TreeNodeGroupViewModel ; public showOkItems = true ; public showWarningItems = true ; public showErrorItems = true ; public searchTerm = '' ; public orderbyHealthState = false ; public firstTreeSelect = true ; public get isLoading ( ) : boolean { return ! this . childGroupViewModel || ( this . childGroupViewModel . children . length === 0 && this . childGroupViewModel . loadingChildren ) ; } public get isEmpty ( ) : boolean { return this . childGroupViewModel && ! this . childGroupViewModel . children . length ; } private childrenQuery : ( ) => Observable < ITreeNode [ ] > ; private selectTreeNodeOpId = 1 ; constructor ( childrenQuery ) { this . childrenQuery = childrenQuery ; this . refreshChildren ( ) ; } public refreshChildren ( ) { const baseNode = { childrenQuery : this . childrenQuery , displayName : ( ) => '' , nodeId : 'base' } ; this . childGroupViewModel = new TreeNodeGroupViewModel ( this , baseNode , null ) ; if ( this . childGroupViewModel . isCollapsed ) { this . childGroupViewModel . toggle ( ) . subscribe ( ( ) => { if ( this . childGroupViewModel . children . length > 0 ) { this . childGroupViewModel . children [ 0 ] . toggle ( ) ; } } ) ; } } public selectNode ( node ) { if ( this . selectedNode === node ) { return false ; } if ( this . selectedNode ) { this . selectedNode . selected = false ; } if ( node ) { node . selected = true ; } this . selectedNode = node ; return true ; } public onKeyDown ( event ) { const selectedNode = this . selectedNode ; switch ( event . key ) { case 'ArrowDown' : this . selectedNode . selectNext ( ) ; break ; case 'ArrowUp' : this . selectedNode . selectPrevious ( ) ; break ; case 'ArrowRight' : this . selectedNode . expandOrMoveToChild ( ) ; break ; case 'ArrowLeft' : this . selectedNode . collapseOrMoveToParent ( ) ; break ; } if ( selectedNode !== this . selectedNode ) { event . preventDefault ( ) ; } } public refresh ( ) { return this . childGroupViewModel . refreshExpandedChildrenRecursively ( ) ; } public selectTreeNode ( path , skipSelectAction ? ) { this . selectTreeNodeOpId ++ ; const opId = this . selectTreeNodeOpId ; return this . selectTreeNodeInternal ( path , 0 , this . childGroupViewModel , opId , skipSelectAction ) ; } public addHealthChunkFiltersRecursively ( clusterHealthQueryDescription ) { this . childGroupViewModel . updateHealthChunkQueryRecursively ( clusterHealthQueryDescription ) ; return clusterHealthQueryDescription ; } public mergeClusterHealthStateChunk ( clusterHealthChunk ) { return this . childGroupViewModel . updateDataModelFromHealthChunkRecursively ( clusterHealthChunk ) ; } private selectTreeNodeInternal ( path , currIndex , group , opId , skipSelectAction ? ) { const observ = group . expand ( ) ; observ . subscribe ( ( ) => { if ( opId !== this . selectTreeNodeOpId ) { return ; } let node = null ; const nodes = group . children ; for ( const n of nodes ) { if ( n . nodeId === path [ currIndex ] ) { node = n ; break ; } } if ( node ) { if ( node . parent && node . parent . listSettings ) { const index = nodes . indexOf ( node ) ; if ( index >= 0 ) { node . parent . listSettings . setPageWithIndex ( index ) ; } } if ( currIndex === path . length - 1 ) { if ( ! node . selected ) { node . select ( 0 , skipSelectAction ) ; } } else { this . selectTreeNodeInternal ( path , currIndex + 1 , node , opId , skipSelectAction ) . subscribe ( ) ; } } } ) ; return observ ; } }