11import {
2- AfterContentChecked ,
2+ AfterViewInit ,
33 Component ,
44 ElementRef ,
55 Input ,
@@ -12,18 +12,18 @@ import {
1212} from '@angular/core' ;
1313
1414import * as TreeTypes from './tree.types' ;
15- import { Ng2TreeSettings } from './tree.types' ;
16- import { Tree } from './tree' ;
17- import { TreeController } from './tree-controller' ;
18- import { NodeMenuService } from './menu/node-menu.service' ;
19- import { NodeMenuItemAction , NodeMenuItemSelectedEvent } from './menu/menu.events' ;
20- import { NodeEditableEvent , NodeEditableEventAction } from './editable/editable.events' ;
21- import { NodeCheckedEvent , NodeEvent } from './tree.events' ;
22- import { TreeService } from './tree.service' ;
15+ import { Ng2TreeSettings } from './tree.types' ;
16+ import { Tree } from './tree' ;
17+ import { TreeController } from './tree-controller' ;
18+ import { NodeMenuService } from './menu/node-menu.service' ;
19+ import { NodeMenuItemAction , NodeMenuItemSelectedEvent } from './menu/menu.events' ;
20+ import { NodeEditableEvent , NodeEditableEventAction } from './editable/editable.events' ;
21+ import { NodeCheckedEvent , NodeEvent } from './tree.events' ;
22+ import { TreeService } from './tree.service' ;
2323import * as EventUtils from './utils/event.utils' ;
24- import { NodeDraggableEvent } from './draggable/draggable.events' ;
25- import { Subscription } from 'rxjs/Subscription' ;
26- import { get , isNil } from './utils/fn.utils' ;
24+ import { NodeDraggableEvent } from './draggable/draggable.events' ;
25+ import { Subscription } from 'rxjs/Subscription' ;
26+ import { get , isNil } from './utils/fn.utils' ;
2727
2828@Component ( {
2929 selector : 'tree-internal' ,
@@ -80,7 +80,7 @@ import {get, isNil} from './utils/fn.utils';
8080 </ul>
8181 `
8282} )
83- export class TreeInternalComponent implements OnInit , OnChanges , OnDestroy , AfterContentChecked {
83+ export class TreeInternalComponent implements OnInit , OnChanges , OnDestroy , AfterViewInit {
8484 @Input ( )
8585 public tree : Tree ;
8686
@@ -106,11 +106,9 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
106106 public nodeElementRef : ElementRef ) {
107107 }
108108
109- public ngAfterContentChecked ( ) : void {
110- // if a node was checked in settings
111- // we should notify parent nodes about this
112- // (they need to switch to appropriate state as well)
113- if ( this . tree . checked ) {
109+ public ngAfterViewInit ( ) : void {
110+ if ( this . tree . checked && ! ( this . tree as any ) . firstCheckedFired ) {
111+ ( this . tree as any ) . firstCheckedFired = true ;
114112 this . treeService . fireNodeChecked ( this . tree ) ;
115113 }
116114 }
@@ -151,9 +149,7 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
151149
152150 this . subscriptions . push ( this . treeService . nodeChecked$ . merge ( this . treeService . nodeUnchecked$ )
153151 . filter ( ( e : NodeCheckedEvent ) => this . eventContainsId ( e ) && this . tree . hasChild ( e . node ) )
154- . subscribe ( ( e : NodeCheckedEvent ) => {
155- this . updateCheckboxState ( ) ;
156- } ) ) ;
152+ . subscribe ( ( e : NodeCheckedEvent ) => this . updateCheckboxState ( ) ) ) ;
157153 }
158154
159155 public ngOnChanges ( changes : SimpleChanges ) : void {
@@ -335,10 +331,6 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
335331 }
336332
337333 updateCheckboxState ( ) : void {
338- if ( ! this . checkboxElementRef ) {
339- return ;
340- }
341-
342334 // Calling setTimeout so the value of isChecked will be updated and after that I'll check the children status.
343335 setTimeout ( ( ) => {
344336 const checkedChildrenAmount = this . tree . checkedChildrenAmount ( ) ;
@@ -351,6 +343,7 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
351343 this . tree . checked = true ;
352344 this . treeService . fireNodeChecked ( this . tree ) ;
353345 } else {
346+ this . tree . checked = false ;
354347 this . checkboxElementRef . nativeElement . indeterminate = true ;
355348 this . treeService . fireNodeIndetermined ( this . tree ) ;
356349 }
0 commit comments