1
1
2
+ "use strict" ;
3
+
2
4
import React , { Component } from "react" ;
3
5
import { connect } from "react-redux" ;
4
6
import { updateChildrenSort } from "../actions/components" ;
7
+ import { width } from "window-size" ;
8
+ import cloneDeep from "../utils/cloneDeep" ;
5
9
6
10
const mapStateToProps = store => ( {
7
11
focusComponent : store . workspace . focusComponent ,
8
12
} ) ;
9
13
10
14
const mapDispatchToProps = dispatch => ( {
11
- updateChildrenSort : ( { newChildrenArray } ) => dispatch ( updateChildrenSort ( { newChildrenArray } ) )
15
+ updateChildrenSort : ( { newSortValues } ) => dispatch ( updateChildrenSort ( { newSortValues } ) )
12
16
} ) ;
13
17
14
18
class SortChildren extends Component {
15
19
constructor ( props ) {
16
20
super ( props ) ;
17
21
18
22
this . state = {
19
- localChildrenArray : this . setLocalArray ( )
20
- //localChildrenArray: this.props.focusComponent.childrenArray,
21
- //shortArray: this.setLocalArray()
22
- } ;
23
-
24
- }
25
-
23
+ draggedIndex : null ,
24
+ draggedOverIndex : null
25
+ } ;
26
+ } // end constrcutor
27
+
26
28
setLocalArray = ( ) => {
27
29
const localArray = this . props . focusComponent . childrenArray . map ( ( child , idx ) => {
28
- return { Childid : child . childId , childSort : child . childSort }
30
+ return { childId : child . childId , childSort : child . childSort }
29
31
} )
30
32
return localArray ;
31
33
}
32
34
35
+
33
36
onDragStart = ( e , idx ) => {
34
- console . log ( 'dragging index: ' , idx )
35
- this . draggedIndex = idx ;
37
+
38
+ this . setState ( { draggedIndex :idx } )
39
+
36
40
e . dataTransfer . effectAllowed = "move" ;
37
41
e . dataTransfer . setData ( "text/html" , e . target . parentNode ) ;
38
42
e . dataTransfer . setDragImage ( e . target . parentNode , 20 , 20 ) ;
39
43
} ;
40
44
41
45
42
- onDragOver = ( draggedOverIndex ) => {
43
- //const draggedOverIndex = idx;
46
+ onDragOver = ( idx ) => {
47
+
48
+ this . setState ( { draggedOverIndex :idx } )
49
+ // console.log(`onDragOver idx=${idx} this.state.draggedOverIndex:${this.state.draggedOverIndex}`)
50
+ } ;
44
51
45
- // if the item is dragged over itself, ignore
46
- if ( this . draggedIndex === draggedOverIndex ) {
52
+ onDragEnd ( e ) {
53
+ console . log ( `dragEnd this
54
+ state.draggedIndex: ${ this . state . draggedIndex }
55
+ this.state.draggedOverIndex: ${ this . state . draggedOverIndex } `
56
+ )
57
+ //const {draggedIndex, draggedOverIndex } = this.state;
58
+ if ( this . state . draggedIndex === this . state . draggedOverIndex
59
+ // || !this.state.draggedIndex || this.state.draggedOverIndex
60
+ ) {
47
61
return ;
48
62
}
49
63
50
- //console.log(`OnDrageOver: draggedOverIndex: ${draggedOverIndex} this.draggedIndex:${this.draggedIndex}`)
51
- // filter out the currently dragged item
52
- let draggedChild = this . state . localChildrenArray [ this . draggedIndex ] ;
53
- //console.log(`dragged item:`,draggedChild)
64
+ let currentSortValues = this . setLocalArray ( ) ;
65
+ // console.log(`currentSortValues`,JSON.stringify((currentSortValues)))
54
66
55
- let localChildrenArray = [ ...this . state . localChildrenArray ]
56
- //console.log('localChildrenArray BEFORE removing ',JSON.stringify(localChildrenArray))
67
+ // remove the dragged Item and save it, we will use add it back in a moment.
68
+ const draggedBaby = currentSortValues [ this . state . draggedIndex ] ;
69
+ currentSortValues . splice ( this . state . draggedIndex , 1 ) ;
70
+
71
+ // put back the dragge item after the dragged Over
72
+ currentSortValues . splice ( this . state . draggedOverIndex , 0 , draggedBaby )
73
+ //console.log(`currentSortValues after reAdding the dragged baby `,JSON.stringify(currentSortValues))
57
74
75
+ currentSortValues = currentSortValues . map ( ( child , idx ) => {
76
+ return { childId :child . childId , childSort : idx + 1 }
77
+ } )
58
78
59
- localChildrenArray . splice ( this . draggedIndex , 1 )
60
- //console.log('localChildrenArray after removing ',JSON.stringify(localChildrenArray))
79
+ console . log ( `currentSortValues after updating the sort ` , JSON . stringify ( currentSortValues ) )
61
80
62
- //add the dragged item after the dragged over item
63
- localChildrenArray . splice ( draggedOverIndex , 0 , draggedChild ) ;
64
- //console.log('localChildrenArray final: ',JSON.stringify(localChildrenArray))
65
- this . setState ( { localChildrenArray } ) ;
81
+ this . props . updateChildrenSort ( { newSortValues : currentSortValues } ) ;
66
82
67
- this . props . updateChildrenSort ( { newChildrenArray : localChildrenArray } ) ;
68
-
69
- } ;
83
+ this . setState ( { draggedIndex : 0 , draggedOverIndex : 0 } )
84
+
85
+ }
70
86
71
87
render ( ) {
72
88
const ulStyle = {
@@ -84,46 +100,66 @@ render() {
84
100
lineHeight : 1 ,
85
101
cursor : "move" ,
86
102
}
87
- const children = this . props . focusComponent . childrenArray ;
88
- const List = children
103
+ //const children = this.props.focusComponent.childrenArray;
104
+ // const List = children
105
+ const List = cloneDeep ( this . props . focusComponent . childrenArray )
89
106
. sort ( ( a , b ) => a . childSort - b . childSort )
90
107
. map ( ( child , idx ) => {
91
- //console.log(`mappping...... ${idx} ${child.componentName + child.childId} childSort ${child.childSort}`)
92
108
return (
93
109
< li
94
110
style = { liStyle }
95
- id = { child . Childid }
111
+ id = { child . childId }
96
112
key = { idx }
97
113
>
98
114
< div className = "drag" draggable
99
115
onDragStart = { e => this . onDragStart ( e , idx ) }
100
116
onDragOver = { e => this . onDragOver ( idx ) }
117
+ onDragEnd = { e => this . onDragEnd ( ) }
101
118
>
102
119
{ child . componentName + child . childId }
103
120
</ div >
104
121
105
122
</ li >
106
123
) } )
107
- console . log ( 'children' )
108
- console . log ( children )
109
- console . log ( 'List' )
110
- console . log ( List )
111
124
return (
112
- < div style = { {
125
+ < div
126
+ style = { {
127
+ minWidth : '200px' ,
113
128
position : 'relative' ,
114
- float : 'right ' ,
129
+ float : 'left ' ,
115
130
marginTop : '20px' ,
116
131
marginRIght : '20px' ,
117
- } } >
132
+ } }
133
+ >
118
134
< h3 > Childrens List</ h3 >
119
135
< ul style = { ulStyle } >
120
- { List }
136
+
137
+ { cloneDeep ( this . props . focusComponent . childrenArray )
138
+ . sort ( ( a , b ) => a . childSort - b . childSort )
139
+ . map ( ( child , idx ) => {
140
+ return (
141
+ < li
142
+ style = { liStyle }
143
+ id = { child . childId }
144
+ key = { idx }
145
+ >
146
+ < div className = "drag" draggable
147
+ onDragStart = { e => this . onDragStart ( e , idx ) }
148
+ onDragOver = { e => this . onDragOver ( idx ) }
149
+ onDragEnd = { e => this . onDragEnd ( ) }
150
+ >
151
+ { child . componentName + child . childId }
152
+ </ div >
153
+
154
+ </ li >
155
+ ) } ) }
156
+
157
+
158
+ { /* {List} */ }
121
159
</ ul >
122
160
</ div >
123
- )
124
-
125
- }
126
-
161
+ )
162
+ }
127
163
}
128
164
129
165
export default connect (
0 commit comments