module Sfx { export class TreeViewModel { public childGroupViewModel : TreeNodeGroupViewModel ; public selectedNode : TreeNodeViewModel ; public showOkItems : boolean = true ; public showWarningItems : boolean = true ; public showErrorItems : boolean = true ; public searchTerm : string = "" ; public firstTreeSelect : boolean = 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 : ( ) => angular . IPromise < ITreeNode [ ] > ; private _selectTreeNodeOpId : number = 1 ; constructor ( public $q , childrenQuery ) { this . _childrenQuery = childrenQuery ; this . refreshChildren ( ) ; } public refreshChildren ( ) { this . childGroupViewModel = new TreeNodeGroupViewModel ( this , null , this . _childrenQuery ) ; if ( this . childGroupViewModel . isCollapsed ) { this . childGroupViewModel . 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 ) { let selectedNode = this . selectedNode ; switch ( event . which ) { case 40 : this . selectedNode . selectNext ( ) ; break ; case 38 : this . selectedNode . selectPrevious ( ) ; break ; case 39 : this . selectedNode . expandOrMoveToChild ( ) ; break ; case 37 : this . selectedNode . collapseOrMoveToParent ( ) ; break ; } if ( selectedNode !== this . selectedNode ) { event . preventDefault ( ) ; } } public refresh ( ) { return this . childGroupViewModel . refreshExpandedChildrenRecursively ( ) ; } public selectTreeNode ( path , skipSelectAction ? ) { this . _selectTreeNodeOpId ++ ; let 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 ? ) { return group . expand ( ) . then ( ( ) => { if ( opId !== this . _selectTreeNodeOpId ) { return ; } let node = null ; let nodes = group . children ; for ( let i = 0 ; i < nodes . length ; i ++ ) { if ( nodes [ i ] . nodeId === path [ currIndex ] ) { node = nodes [ i ] ; break ; } } if ( node ) { if ( node . parent && node . parent . listSettings ) { let 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 . childGroupViewModel , opId , skipSelectAction ) ; } } } ) ; } } }