import { Component , ContentChild , EventEmitter , Inject , Input , OnChanges , OnDestroy , OnInit , Output , SimpleChanges , TemplateRef , ViewChild } from '@angular/core' ; import { TreeService } from './tree.service' ; import * as TreeTypes from './tree.types' ; import { MenuItemSelectedEvent , NodeCheckedEvent , NodeEvent , NodeUncheckedEvent } from './tree.events' ; import { Tree } from './tree' ; import { TreeController } from './tree-controller' ; import { Subscription } from 'rxjs/Subscription' ; @ Component ( { selector : 'tree' , template : `<tree-internal #rootComponent [tree]="tree" [settings]="settings" [template]="template"></tree-internal>` , providers : [ TreeService ] } ) export class TreeComponent implements OnInit , OnChanges , OnDestroy { private static EMPTY_TREE : Tree = new Tree ( { value : '' } ) ; @ Input ( 'tree' ) public treeModel : TreeTypes . TreeModel ; @ Input ( ) public nodeTemplae : TemplateRef < any > ; @ Input ( ) public settings : TreeTypes . Ng2TreeSettings ; @ Output ( ) public nodeCreated : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeRemoved : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeRenamed : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeSelected : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeUnselected : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeMoved : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeExpanded : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeCollapsed : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeHovered : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public loadNextLevel : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) public nodeChecked : EventEmitter < NodeCheckedEvent > = new EventEmitter ( ) ; @ Output ( ) public nodeUnchecked : EventEmitter < NodeUncheckedEvent > = new EventEmitter ( ) ; @ Output ( ) public menuItemSelected : EventEmitter < any > = new EventEmitter ( ) ; public tree : Tree ; @ ViewChild ( 'rootComponent' ) public rootComponent ; @ ContentChild ( TemplateRef ) public template ; private subscriptions : Subscription [ ] = [ ] ; public constructor ( @ Inject ( TreeService ) private treeService ) { } public ngOnChanges ( changes ) { if ( ! this . treeModel ) { this . tree = TreeComponent . EMPTY_TREE ; } else { this . tree = new Tree ( this . treeModel ) ; } } public ngOnInit ( ) { this . subscriptions . push ( this . treeService . nodeRemoved$ . subscribe ( ( e ) => { this . nodeRemoved . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeRenamed$ . subscribe ( ( e ) => { this . nodeRenamed . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeCreated$ . subscribe ( ( e ) => { this . nodeCreated . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeSelected$ . subscribe ( ( e ) => { this . nodeSelected . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeHovered$ . subscribe ( ( e ) => { this . nodeHovered . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeUnselected$ . subscribe ( ( e ) => { this . nodeUnselected . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeMoved$ . subscribe ( ( e ) => { this . nodeMoved . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeExpanded$ . subscribe ( ( e ) => { this . nodeExpanded . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeCollapsed$ . subscribe ( ( e ) => { this . nodeCollapsed . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . menuItemSelected$ . subscribe ( ( e ) => { this . menuItemSelected . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . loadNextLevel$ . subscribe ( ( e ) => { this . loadNextLevel . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeChecked$ . subscribe ( ( e ) => { this . nodeChecked . emit ( e ) ; } ) ) ; this . subscriptions . push ( this . treeService . nodeUnchecked$ . subscribe ( ( e ) => { this . nodeUnchecked . emit ( e ) ; } ) ) ; } public getController ( ) { return this . rootComponent . controller ; } public getControllerByNodeId ( id ) { return this . treeService . getController ( id ) ; } ngOnDestroy ( ) { this . subscriptions . forEach ( sub => sub && sub . unsubscribe ( ) ) ; } }