1
1
import {
2
- AfterContentChecked ,
2
+ AfterViewInit ,
3
3
Component ,
4
4
ElementRef ,
5
5
Input ,
@@ -12,18 +12,18 @@ import {
12
12
} from '@angular/core' ;
13
13
14
14
import * 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' ;
23
23
import * 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' ;
27
27
28
28
@Component ( {
29
29
selector : 'tree-internal' ,
@@ -80,7 +80,7 @@ import {get, isNil} from './utils/fn.utils';
80
80
</ul>
81
81
`
82
82
} )
83
- export class TreeInternalComponent implements OnInit , OnChanges , OnDestroy , AfterContentChecked {
83
+ export class TreeInternalComponent implements OnInit , OnChanges , OnDestroy , AfterViewInit {
84
84
@Input ( )
85
85
public tree : Tree ;
86
86
@@ -106,11 +106,9 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
106
106
public nodeElementRef : ElementRef ) {
107
107
}
108
108
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 ;
114
112
this . treeService . fireNodeChecked ( this . tree ) ;
115
113
}
116
114
}
@@ -151,9 +149,7 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
151
149
152
150
this . subscriptions . push ( this . treeService . nodeChecked$ . merge ( this . treeService . nodeUnchecked$ )
153
151
. 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 ( ) ) ) ;
157
153
}
158
154
159
155
public ngOnChanges ( changes : SimpleChanges ) : void {
@@ -335,10 +331,6 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
335
331
}
336
332
337
333
updateCheckboxState ( ) : void {
338
- if ( ! this . checkboxElementRef ) {
339
- return ;
340
- }
341
-
342
334
// Calling setTimeout so the value of isChecked will be updated and after that I'll check the children status.
343
335
setTimeout ( ( ) => {
344
336
const checkedChildrenAmount = this . tree . checkedChildrenAmount ( ) ;
@@ -351,6 +343,7 @@ export class TreeInternalComponent implements OnInit, OnChanges, OnDestroy, Afte
351
343
this . tree . checked = true ;
352
344
this . treeService . fireNodeChecked ( this . tree ) ;
353
345
} else {
346
+ this . tree . checked = false ;
354
347
this . checkboxElementRef . nativeElement . indeterminate = true ;
355
348
this . treeService . fireNodeIndetermined ( this . tree ) ;
356
349
}
0 commit comments