11
2+ "use strict" ;
3+
24import React , { Component } from "react" ;
35import { connect } from "react-redux" ;
46import { updateChildrenSort } from "../actions/components" ;
7+ import { width } from "window-size" ;
8+ import cloneDeep from "../utils/cloneDeep" ;
59
610const mapStateToProps = store => ( {
711 focusComponent : store . workspace . focusComponent ,
812} ) ;
913
1014const mapDispatchToProps = dispatch => ( {
11- updateChildrenSort : ( { newChildrenArray } ) => dispatch ( updateChildrenSort ( { newChildrenArray } ) )
15+ updateChildrenSort : ( { newSortValues } ) => dispatch ( updateChildrenSort ( { newSortValues } ) )
1216} ) ;
1317
1418class SortChildren extends Component {
1519 constructor ( props ) {
1620 super ( props ) ;
1721
1822 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+
2628 setLocalArray = ( ) => {
2729 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 }
2931 } )
3032 return localArray ;
3133 }
3234
35+
3336 onDragStart = ( e , idx ) => {
34- console . log ( 'dragging index: ' , idx )
35- this . draggedIndex = idx ;
37+
38+ this . setState ( { draggedIndex :idx } )
39+
3640 e . dataTransfer . effectAllowed = "move" ;
3741 e . dataTransfer . setData ( "text/html" , e . target . parentNode ) ;
3842 e . dataTransfer . setDragImage ( e . target . parentNode , 20 , 20 ) ;
3943 } ;
4044
4145
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+ } ;
4451
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+ ) {
4761 return ;
4862 }
4963
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)))
5466
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))
5774
75+ currentSortValues = currentSortValues . map ( ( child , idx ) => {
76+ return { childId :child . childId , childSort : idx + 1 }
77+ } )
5878
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 ) )
6180
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 } ) ;
6682
67- this . props . updateChildrenSort ( { newChildrenArray : localChildrenArray } ) ;
68-
69- } ;
83+ this . setState ( { draggedIndex : 0 , draggedOverIndex : 0 } )
84+
85+ }
7086
7187render ( ) {
7288 const ulStyle = {
@@ -84,46 +100,66 @@ render() {
84100 lineHeight : 1 ,
85101 cursor : "move" ,
86102 }
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 )
89106 . sort ( ( a , b ) => a . childSort - b . childSort )
90107 . map ( ( child , idx ) => {
91- //console.log(`mappping...... ${idx} ${child.componentName + child.childId} childSort ${child.childSort}`)
92108 return (
93109 < li
94110 style = { liStyle }
95- id = { child . Childid }
111+ id = { child . childId }
96112 key = { idx }
97113 >
98114 < div className = "drag" draggable
99115 onDragStart = { e => this . onDragStart ( e , idx ) }
100116 onDragOver = { e => this . onDragOver ( idx ) }
117+ onDragEnd = { e => this . onDragEnd ( ) }
101118 >
102119 { child . componentName + child . childId }
103120 </ div >
104121
105122 </ li >
106123 ) } )
107- console . log ( 'children' )
108- console . log ( children )
109- console . log ( 'List' )
110- console . log ( List )
111124 return (
112- < div style = { {
125+ < div
126+ style = { {
127+ minWidth : '200px' ,
113128 position : 'relative' ,
114- float : 'right ' ,
129+ float : 'left ' ,
115130 marginTop : '20px' ,
116131 marginRIght : '20px' ,
117- } } >
132+ } }
133+ >
118134 < h3 > Childrens List</ h3 >
119135 < 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} */ }
121159 </ ul >
122160 </ div >
123- )
124-
125- }
126-
161+ )
162+ }
127163}
128164
129165export default connect (
0 commit comments