@@ -291,18 +291,15 @@ export default class TreeStore extends TreeEventTarget {
291
291
} else {
292
292
// 设置的节点不是当前已选中节点,要么当前没有选中节点,要么当前有选中节点
293
293
if ( value ) {
294
- if ( this . currentSelectedKey === null ) {
295
- // 当前没有选中节点
296
- node . selected = value
297
- this . currentSelectedKey = node [ this . options . keyField ]
298
- } else {
294
+ if ( this . currentSelectedKey !== null ) {
299
295
// 取消当前已选中,设置新的选中节点
300
296
if ( this . mapData [ this . currentSelectedKey ] ) {
301
297
this . mapData [ this . currentSelectedKey ] . selected = false
302
298
}
303
- node . selected = value
304
- this . currentSelectedKey = node [ this . options . keyField ]
305
299
}
300
+ node . selected = value
301
+ this . currentSelectedKey = node [ this . options . keyField ]
302
+ this . unloadSelectedKey = null
306
303
}
307
304
}
308
305
@@ -327,9 +324,7 @@ export default class TreeStore extends TreeEventTarget {
327
324
triggerDataChange : boolean = true
328
325
) : void {
329
326
if ( value ) {
330
- if ( this . currentSelectedKey ) {
331
- this . setSelected ( this . currentSelectedKey , false , false , false )
332
- }
327
+ this . currentSelectedKey = null
333
328
this . unloadSelectedKey = key
334
329
} else {
335
330
if ( this . unloadSelectedKey === key ) {
@@ -492,8 +487,13 @@ export default class TreeStore extends TreeEventTarget {
492
487
}
493
488
}
494
489
490
+ private isChildrenChanged ( node : TreeNode , newNode : ITreeNodeOptions ) : boolean {
491
+ return ( 'children' in newNode ) && ( ! ! node . children . length || ! ! newNode . children ?. length )
492
+ }
493
+
495
494
updateNode ( key : TreeNodeKeyType , newNode : ITreeNodeOptions , triggerEvent = true , triggerDataChange = true ) {
496
- if ( ! this . mapData [ key ] ) return
495
+ const node = this . mapData [ key ]
496
+ if ( ! node ) return
497
497
498
498
const newNodeCopy : ITreeNodeOptions = { }
499
499
const notAllowedFields = [
@@ -512,14 +512,15 @@ export default class TreeStore extends TreeEventTarget {
512
512
513
513
const previousCheckedKeys = this . getCheckedKeys ( )
514
514
const previousSelectedKey = this . getSelectedKey ( )
515
+ let triggerSetDataFlag = this . isChildrenChanged ( node , newNodeCopy )
515
516
516
- if ( 'children' in newNodeCopy ) {
517
+ if ( ( 'children' in newNodeCopy ) && ( ! ! node . children . length || ! ! newNodeCopy . children ?. length ) ) {
517
518
// remove all children
518
519
this . removeChildren ( key , false , false )
519
520
520
521
// add new children
521
522
if ( Array . isArray ( newNodeCopy . children ) ) {
522
- this . loadChildren ( this . mapData [ key ] , newNodeCopy . children , this . mapData [ key ] . expand )
523
+ this . loadChildren ( node , newNodeCopy . children , node . expand )
523
524
}
524
525
525
526
delete newNodeCopy . children
@@ -537,7 +538,7 @@ export default class TreeStore extends TreeEventTarget {
537
538
delete newNodeCopy . expand
538
539
}
539
540
Object . keys ( newNodeCopy ) . forEach ( ( field ) => {
540
- this . mapData [ key ] [ field ] = newNodeCopy [ field ]
541
+ node [ field ] = newNodeCopy [ field ]
541
542
} )
542
543
543
544
const currentCheckedKeys = this . getCheckedKeys ( )
@@ -554,6 +555,9 @@ export default class TreeStore extends TreeEventTarget {
554
555
}
555
556
556
557
if ( triggerDataChange ) {
558
+ if ( triggerSetDataFlag ) {
559
+ this . emit ( 'set-data' )
560
+ }
557
561
this . emit ( 'visible-data-change' )
558
562
}
559
563
}
@@ -564,9 +568,15 @@ export default class TreeStore extends TreeEventTarget {
564
568
565
569
const previousCheckedKeys = this . getCheckedKeys ( )
566
570
const previousSelectedKey = this . getSelectedKey ( )
571
+ let triggerSetDataFlag = false
567
572
568
- validNodes . forEach ( ( node ) => {
569
- this . updateNode ( node [ this . options . keyField ] , node , false , false )
573
+ validNodes . forEach ( ( newNode ) => {
574
+ const key = newNode [ this . options . keyField ]
575
+ const node = this . mapData [ key ]
576
+ if ( node ) {
577
+ triggerSetDataFlag = triggerSetDataFlag || this . isChildrenChanged ( node , newNode )
578
+ this . updateNode ( key , newNode , false , false )
579
+ }
570
580
} )
571
581
572
582
const currentCheckedKeys = this . getCheckedKeys ( )
@@ -580,6 +590,10 @@ export default class TreeStore extends TreeEventTarget {
580
590
this . triggerSelectedChange ( true , false )
581
591
}
582
592
593
+ if ( triggerSetDataFlag ) {
594
+ this . emit ( 'set-data' )
595
+ }
596
+
583
597
this . emit ( 'visible-data-change' )
584
598
}
585
599
@@ -894,6 +908,7 @@ export default class TreeStore extends TreeEventTarget {
894
908
if ( ! node || ! node . children . length ) return null
895
909
896
910
const firstChild = node . children [ 0 ]
911
+ let movingNode = firstChild
897
912
898
913
// 从 flatData 中移除
899
914
const index = this . findIndex ( node )
@@ -905,6 +920,11 @@ export default class TreeStore extends TreeEventTarget {
905
920
// 从 mapData 中移除
906
921
delete this . mapData [ this . flatData [ i ] [ this . options . keyField ] ]
907
922
deleteCount ++
923
+
924
+ // 如果是 Selected 的节点,则记录
925
+ if ( this . flatData [ i ] . selected ) {
926
+ movingNode = this . flatData [ i ]
927
+ }
908
928
} else break
909
929
}
910
930
this . flatData . splice ( index + 1 , deleteCount )
@@ -915,7 +935,7 @@ export default class TreeStore extends TreeEventTarget {
915
935
node . indeterminate = false
916
936
917
937
// 更新被移除处父节点状态
918
- this . updateMovingNodeStatus ( firstChild , triggerEvent , triggerDataChange )
938
+ this . updateMovingNodeStatus ( movingNode , triggerEvent , triggerDataChange )
919
939
920
940
if ( triggerDataChange ) {
921
941
this . emit ( 'visible-data-change' )
@@ -935,14 +955,16 @@ export default class TreeStore extends TreeEventTarget {
935
955
const currentCheckedKeys = this . getCheckedKeys ( )
936
956
const flattenChildren = this . flattenData (
937
957
node . children ,
938
- this . getSelectedKey === null
958
+ this . getSelectedKey ( ) === null
939
959
)
940
960
this . insertIntoFlatData ( parentIndex + 1 , flattenChildren )
941
961
// 如果有未加载的选中节点,判断其是否已加载
942
962
this . setUnloadCheckedKeys ( currentCheckedKeys )
943
963
if ( this . unloadSelectedKey !== null ) {
944
964
this . setUnloadSelectedKey ( this . unloadSelectedKey )
945
965
}
966
+
967
+ this . checkNodeUpward ( node , true )
946
968
}
947
969
948
970
private getInsertedNode (
@@ -1168,8 +1190,6 @@ export default class TreeStore extends TreeEventTarget {
1168
1190
if ( node . checked && this . options . cascade ) {
1169
1191
// 向下勾选,包括自身
1170
1192
this . checkNodeDownward ( node , true )
1171
- // 向上勾选父节点直到根节点
1172
- this . checkNodeUpward ( node )
1173
1193
}
1174
1194
1175
1195
if ( node . selected && overrideSelected ) {
@@ -1191,6 +1211,12 @@ export default class TreeStore extends TreeEventTarget {
1191
1211
this . flattenData ( node . children , overrideSelected , result )
1192
1212
}
1193
1213
}
1214
+
1215
+ if ( this . options . cascade && ! ! length ) {
1216
+ // 向上勾选父节点直到根节点
1217
+ this . checkNodeUpward ( nodes [ 0 ] )
1218
+ }
1219
+
1194
1220
return result
1195
1221
}
1196
1222
@@ -1230,9 +1256,10 @@ export default class TreeStore extends TreeEventTarget {
1230
1256
/**
1231
1257
* 向上勾选/取消勾选父节点,不包括自身
1232
1258
* @param node 需要勾选的节点
1259
+ * @param fromCurrentNode 是否从当前节点开始处理
1233
1260
*/
1234
- private checkNodeUpward ( node : TreeNode ) {
1235
- let parent = node . _parent
1261
+ private checkNodeUpward ( node : TreeNode , fromCurrentNode = false ) {
1262
+ let parent = fromCurrentNode ? node : node . _parent
1236
1263
while ( parent ) {
1237
1264
this . checkParentNode ( parent )
1238
1265
parent = parent . _parent
0 commit comments