@@ -2,6 +2,7 @@ import React, {Component} from 'react';
2
2
import _ from 'lodash' ;
3
3
import { Affix , Button } from 'antd' ;
4
4
import NodeView from './NodeView' ;
5
+ import { relativeTimeRounding } from 'moment' ;
5
6
class AppView extends Component {
6
7
constructor ( props ) {
7
8
super ( props ) ;
@@ -474,7 +475,7 @@ class AppView extends Component {
474
475
475
476
}
476
477
onFocusChanged = ( id , isFocus ) => {
477
- // console.log('onFocusChanged',id, isFocus)
478
+ console . log ( 'onFocusChanged' , id , isFocus )
478
479
let { focusId} = this . state
479
480
if ( isFocus ) focusId = id
480
481
else focusId = null
@@ -517,8 +518,12 @@ class AppView extends Component {
517
518
518
519
519
520
520
- let rootNode = { id : 'root' }
521
+ let rootNode = { id : 'root' }
522
+ console . log ( 'begin generateStateData' , rootNode , nodes , _ . cloneDeep ( releations ) )
523
+
521
524
this . generateStateData ( rootNode , nodes , releations )
525
+ console . log ( 'after generateStateData' , rootNode , nodes , _ . cloneDeep ( releations ) )
526
+
522
527
this . setState ( {
523
528
data : rootNode ,
524
529
nodes,
@@ -529,10 +534,137 @@ class AppView extends Component {
529
534
this . printReleations ( _ . cloneDeep ( releations ) )
530
535
531
536
}
537
+ onDelete = ( id ) => {
538
+ console . log ( 'onDelete' , id ) ;
539
+ let { data, nodes, releations} = this . state
540
+ console . log ( 'begin----------------------onDelete------------------------------------begin' )
541
+ this . printReleations ( _ . cloneDeep ( releations ) )
542
+ console . log ( 'begin onDelete' , _ . cloneDeep ( releations ) )
543
+ // console.log('clone releations', _.cloneDeep(releations))
544
+
545
+ console . log ( 'onDelete' , id )
546
+ let rootNode = { id : 'root' }
547
+ // _.find(releations, d => d.id == id).text = text
548
+ let node = _ . find ( releations , d => d . id == id )
549
+ let parentNode = null
550
+ if ( node ) parentNode = _ . find ( releations , d => d . id == node . parent_id )
551
+ let leftNode = null
552
+ if ( node ) leftNode = _ . find ( releations , d => d . id == node . left_id )
553
+ let rightNode = null
554
+ if ( node ) rightNode = _ . find ( releations , d => d . id == node . right_id )
555
+ let parentParentNode = null
556
+ if ( parentNode ) parentParentNode = _ . find ( releations , d => d . id == parentNode . parent_id )
557
+
558
+
559
+ console . log ( 'curentNode' , node )
560
+ console . log ( 'parentNode' , parentNode )
561
+ console . log ( 'leftNode' , leftNode )
562
+ console . log ( 'rightNode' , rightNode )
563
+ console . log ( 'parentParentNode' , parentParentNode )
564
+ // if (n)
565
+ if ( parentNode . id == 'root' && ! leftNode ) {
566
+ console . log ( 'only one node , skip delete ' )
567
+ return
568
+
569
+ }
570
+ let children = _ . find ( releations , d => d . parent_id == id )
571
+ if ( _ . isEmpty ( children ) ) {
572
+ // DO Delete
573
+ console . log ( ' DO Delete ' )
574
+ // node.parent_id = parentNode.id
575
+
576
+ if ( leftNode && rightNode ) {
577
+ leftNode . right_id = rightNode . id
578
+ rightNode . left_id = leftNode . id
579
+ } else {
580
+ if ( leftNode && ! rightNode ) {
581
+ leftNode . right_id = null
582
+ }
583
+ if ( rightNode && ! leftNode ) {
584
+ rightNode . left_id = null
585
+ }
586
+
587
+ }
588
+ let new_releation = null ;
589
+ if ( leftNode ) {
590
+ new_releation = leftNode
591
+ } else {
592
+ if ( parentNode ) new_releation = parentNode
593
+ }
594
+ let focusId = new_releation ? new_releation . id : null
595
+ this . check ( _ . clone ( releations ) )
596
+
597
+ this . setState ( { focusId} , ( ) => {
598
+ releations = _ . filter ( releations , d => d . id != id )
599
+ nodes = _ . filter ( nodes , d => d . id != id )
600
+ // this.setState({releations, nodes})
601
+ // let rootNode = {id: 'root'}
602
+ this . generateStateData ( rootNode , nodes , releations )
603
+ this . setState ( {
604
+ data : rootNode ,
605
+ nodes,
606
+ releations,
607
+ focusId
608
+ } , ( ) => this . save ( ) )
609
+ this . domFocus ( focusId ) ;
610
+ console . log ( 'end onDelete setState' , rootNode , nodes , _ . cloneDeep ( releations ) , focusId )
611
+ this . printReleations ( _ . cloneDeep ( releations ) )
612
+ } )
613
+
614
+
615
+
616
+ }
617
+
618
+ }
619
+ onDirectionChange = ( id , direction ) => {
620
+ console . log ( 'onDirectionChange' , id , direction ) ;
621
+ let { data, nodes, releations} = this . state
622
+ // console.log('begin----------------------onDirectionChange------------------------------------begin')
623
+ // this.printReleations(_.cloneDeep(releations))
624
+ // console.log('begin onDirectionChange' , _.cloneDeep(releations))
625
+ // console.log('clone releations', _.cloneDeep(releations))
626
+
627
+ // console.log('onDirectionChange', id)
628
+ let rootNode = { id : 'root' }
629
+ // _.find(releations, d => d.id == id).text = text
630
+ let node = _ . find ( releations , d => d . id == id )
631
+ let parentNode = null
632
+ if ( node ) parentNode = _ . find ( releations , d => d . id == node . parent_id )
633
+ let leftNode = null
634
+ if ( node ) leftNode = _ . find ( releations , d => d . id == node . left_id )
635
+ let rightNode = null
636
+ if ( node ) rightNode = _ . find ( releations , d => d . id == node . right_id )
637
+ let parentParentNode = null
638
+ if ( parentNode ) parentParentNode = _ . find ( releations , d => d . id == parentNode . parent_id )
639
+
640
+ let focusId = null
641
+ if ( direction == 'up' ) {
642
+ if ( leftNode ) focusId = leftNode . id
643
+ if ( ! leftNode && parentNode ) focusId = parentNode . id
644
+ if ( focusId ) this . setState ( { focusId} , ( ) => { this . domFocus ( focusId ) } )
645
+ }
646
+ if ( direction == 'down' ) {
647
+ focusId = null
648
+ if ( rightNode ) focusId = rightNode . id
649
+ if ( focusId ) this . setState ( { focusId} , ( ) => { this . domFocus ( focusId ) } )
650
+ }
651
+
652
+
653
+ }
654
+ domFocus = ( id ) => {
655
+ let input_id = `input_of_${ id } ` ;
656
+ let element = document . getElementById ( input_id ) ;
657
+ console . log ( 'domFocus' , id , input_id , element )
658
+ if ( ! id ) return ;
659
+ // if(!ele)
660
+ setTimeout ( ( ) => {
661
+ if ( element ) element . focus ( ) ;
662
+ } , 0 )
663
+ }
532
664
render ( ) {
533
665
return (
534
666
< div >
535
- < NodeView root = { true } children = { this . state . data . children } onTabChange = { this . onTabChange } onTextChange = { this . onTextChange } focusId = { this . state . focusId } onFocusChanged = { this . onFocusChanged } onPressEnter = { this . onPressEnter } />
667
+ < NodeView root = { true } children = { this . state . data . children } onTabChange = { this . onTabChange } onTextChange = { this . onTextChange } focusId = { this . state . focusId } onFocusChanged = { this . onFocusChanged } onPressEnter = { this . onPressEnter } onDelete = { this . onDelete } onDirectionChange = { this . onDirectionChange } />
536
668
</ div >
537
669
)
538
670
}
0 commit comments