1+ 'use strict' ;
12
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' ;
98
109const mapStateToProps = store => ( {
1110 focusComponent : store . workspace . focusComponent ,
1211} ) ;
1312
1413const mapDispatchToProps = dispatch => ( {
15- updateChildrenSort : ( { newSortValues } ) => dispatch ( updateChildrenSort ( { newSortValues } ) )
14+ updateChildrenSort : ( { newSortValues } ) => dispatch ( updateChildrenSort ( { newSortValues } ) ) ,
1615} ) ;
1716
1817class SortChildren extends Component {
1918 constructor ( props ) {
2019 super ( props ) ;
2120
22- this . state = {
23- draggedIndex : null ,
24- draggedOverIndex : null
21+ this . state = {
22+ draggedIndex : null ,
23+ draggedOverIndex : null ,
2524 } ;
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
3426
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+ } ;
3533
36- onDragStart = ( e , idx ) => {
37-
38- this . setState ( { draggedIndex :idx } )
34+ onDragStart = ( e , idx ) => {
35+ this . setState ( { draggedIndex : idx } ) ;
3936
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 ) ;
4239 e . dataTransfer . setDragImage ( e . target . parentNode , 20 , 20 ) ;
4340 } ;
44-
4541
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 } ) ;
5044 } ;
5145
5246 onDragEnd ( e ) {
5347 console . log ( `dragEnd this
5448 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+ ) {
6154 return ;
6255 }
6356
64- let currentSortValues = this . setLocalArray ( ) ;
65- // console.log(`currentSortValues`,JSON.stringify((currentSortValues)))
57+ let currentSortValues = this . setLocalArray ( ) ;
6658
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 ) ;
7462
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 ) ;
7865
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+ } ) ;
8069
81- this . props . updateChildrenSort ( { newSortValues : currentSortValues } ) ;
70+ console . log ( `currentSortValues after updating the sort ` , JSON . stringify ( currentSortValues ) ) ;
8271
83- this . setState ( { draggedIndex : 0 , draggedOverIndex : 0 } )
72+ this . props . updateChildrenSort ( { newSortValues : currentSortValues } ) ;
8473
74+ this . setState ( { draggedIndex : 0 , draggedOverIndex : 0 } ) ;
8575 }
8676
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+ } ;
9383
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' ,
12887 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+ ) ;
162146 }
163- }
147+ }
164148
165149export default connect (
166150 mapStateToProps ,
167- mapDispatchToProps
168- ) ( SortChildren ) ;
151+ mapDispatchToProps ,
152+ ) ( SortChildren ) ;
0 commit comments