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