import { TreeService } from './tree.service' ; import { Tree } from './tree' ; import { TreeModel } from './tree.types' ; import { NodeMenuItemAction } from './menu/menu.events' ; import { TreeInternalComponent } from './tree-internal.component' ; import { MouseButtons } from './utils/event.utils' ; import { get } from './utils/fn.utils' ; export class TreeController { private tree : Tree ; private treeService : TreeService ; constructor ( private component ) { this . tree = this . component . tree ; this . treeService = this . component . treeService ; } public select ( ) { if ( ! this . isSelected ( ) ) { this . component . onNodeSelected ( { button : MouseButtons . Left } ) ; } } public unselect ( ) { if ( this . isSelected ( ) ) { this . component . onNodeUnselected ( { button : MouseButtons . Left } ) ; } } public isSelected ( ) { return this . component . isSelected ; } public expand ( ) { if ( this . isCollapsed ( ) ) { this . component . onSwitchFoldingType ( ) ; } } public isExpanded ( ) { return this . tree . isNodeExpanded ( ) ; } public collapse ( ) { if ( this . isExpanded ( ) ) { this . component . onSwitchFoldingType ( ) ; } } public isCollapsed ( ) { return this . tree . isNodeCollapsed ( ) ; } public toTreeModel ( ) { return this . tree . toTreeModel ( ) ; } public rename ( newValue ) { this . tree . markAsBeingRenamed ( ) ; this . component . applyNewValue ( { type : 'keyup' , value : newValue } ) ; } public remove ( ) { this . component . onMenuItemSelected ( { nodeMenuItemAction : NodeMenuItemAction . Remove } ) ; } public addChild ( newNode ) { if ( this . tree . hasDeferredChildren ( ) && ! this . tree . childrenWereLoaded ( ) ) { return ; } const newTree = this . tree . createNode ( Array . isArray ( newNode . children ) , newNode ) ; this . treeService . fireNodeCreated ( newTree ) ; } public addChildAsync ( newNode ) { if ( this . tree . hasDeferredChildren ( ) && ! this . tree . childrenWereLoaded ( ) ) { return Promise . reject ( new Error ( 'This node loads its children asynchronously, hence child cannot be added this way' ) ) ; } const newTree = this . tree . createNode ( Array . isArray ( newNode . children ) , newNode ) ; this . treeService . fireNodeCreated ( newTree ) ; return new Promise ( resolve => { setTimeout ( ( ) => { resolve ( newTree ) ; } ) ; } ) ; } public changeNodeId ( id ) { if ( ! id ) { throw Error ( 'You should supply an id!' ) ; } if ( this . treeService . hasController ( id ) ) { throw Error ( `Controller already exists for the given id: ${ id }` ) ; } this . treeService . deleteController ( this . tree . id ) ; this . tree . id = id ; this . treeService . setController ( this . tree . id , this ) ; } public reloadChildren ( ) { this . tree . reloadChildren ( ) ; } public setChildren ( children ) { if ( ! this . tree . isLeaf ( ) ) { this . tree . setChildren ( children ) ; } } public startRenaming ( ) { this . tree . markAsBeingRenamed ( ) ; } public check ( ) { this . component . onNodeChecked ( ) ; } public uncheck ( ) { this . component . onNodeUnchecked ( ) ; } public isChecked ( ) { return this . tree . checked ; } public isIndetermined ( ) { return get ( this . component , 'checkboxElementRef.nativeElement.indeterminate' ) ; } public allowSelection ( ) { this . tree . selectionAllowed = true ; } public forbidSelection ( ) { this . tree . selectionAllowed = false ; } public isSelectionAllowed ( ) { return this . tree . selectionAllowed ; } }