packageDocumentation module import { EMPTY } from "rxjs/internal/observable/empty" ; import { TreeNodeItem } from "../../TreeDataProvider" ; import { Observable } from "../Observable" ; import { CheckboxStateChange } from "../TreeEvents" ; import { TreeModelNode , TreeModelNodeEditingInfo } from "../TreeModel" ; import { TreeModelSource } from "../TreeModelSource" ; import { ITreeNodeLoader , TreeNodeLoadResult } from "../TreeNodeLoader" ; internal export class TreeModelMutator { private _modelSource : TreeModelSource ; private _nodeLoader : ITreeNodeLoader ; private _collapsedChildrenDisposalEnabled : boolean ; constructor ( modelSource , nodeLoader , collapsedChildrenDisposalEnabled ) { this . _modelSource = modelSource ; this . _nodeLoader = nodeLoader ; this . _collapsedChildrenDisposalEnabled = collapsedChildrenDisposalEnabled ; } public get modelSource ( ) { return this . _modelSource ; } public expandNode ( nodeId ) { let needToLoadChildren = false ; this . _modelSource . modifyModel ( ( model ) => { const node = model . getNode ( nodeId ) ; if ( node === undefined || node . isExpanded ) { return ; } needToLoadChildren = node . numChildren === undefined ; node . isExpanded = true ; if ( needToLoadChildren ) { node . isLoading = true ; } } ) ; const expandedNode = this . _modelSource . getModel ( ) . getNode ( nodeId ) ; return needToLoadChildren && expandedNode ? this . _nodeLoader . loadNode ( expandedNode , 0 ) : EMPTY ; } public collapseNode ( nodeId ) { this . _modelSource . modifyModel ( ( model ) => { const node = model . getNode ( nodeId ) ; if ( node === undefined || ! node . isExpanded ) { return ; } node . isExpanded = false ; if ( this . _collapsedChildrenDisposalEnabled ) { model . clearChildren ( node . id ) ; } } ) ; } public modifySelection ( nodesToSelect , nodesToDeselect ) { this . _modelSource . modifyModel ( ( model ) => { for ( const nodeItem of nodesToSelect ) { const node = model . getNode ( nodeItem . id ) ; if ( node !== undefined ) { node . isSelected = true ; } } for ( const nodeItem of nodesToDeselect ) { const node = model . getNode ( nodeItem . id ) ; if ( node !== undefined ) { node . isSelected = false ; } } } ) ; } public replaceSelection ( nodesToSelect ) { this . _modelSource . modifyModel ( ( model ) => { for ( const node of model . iterateTreeModelNodes ( ) ) { node . isSelected = false ; } for ( const nodeItem of nodesToSelect ) { const node = model . getNode ( nodeItem . id ) ; if ( node !== undefined ) { node . isSelected = true ; } } } ) ; } public clearNodeSelection ( ) { this . _modelSource . modifyModel ( ( model ) => { for ( const node of model . iterateTreeModelNodes ( ) ) { node . isSelected = false ; } } ) ; } public setCheckboxStates ( stateChanges ) { this . _modelSource . modifyModel ( ( model ) => { for ( const { nodeItem , newState } of stateChanges ) { const node = model . getNode ( nodeItem . id ) ; if ( node !== undefined ) { node . checkbox . state = newState ; } } } ) ; } public activateEditing ( nodeId , onCommit ) { this . _modelSource . modifyModel ( ( model ) => { const node = model . getNode ( nodeId ) ; if ( ! node ) return ; if ( node . item . isEditable ) { node . editingInfo = this . createNodeEditingInfo ( nodeId , onCommit ) ; } } ) ; } private createNodeEditingInfo ( nodeId , onCommit ) { const closeEditing = ( ) => { this . _modelSource . modifyModel ( ( model ) => { const node = model . getNode ( nodeId ) ; if ( ! node ) return ; node . editingInfo = undefined ; } ) ; } ; const onEditCommitted = ( node , newValue ) => { onCommit ( node , newValue ) ; closeEditing ( ) ; } ; return { onCommit : onEditCommitted , onCancel : closeEditing } ; } }