@@ -17,6 +17,8 @@ limitations under the License.
1717'use strict' ;
1818
1919var React = require ( 'react' ) ;
20+ var DragSource = require ( 'react-dnd' ) . DragSource ;
21+ var DropTarget = require ( 'react-dnd' ) . DropTarget ;
2022var classNames = require ( 'classnames' ) ;
2123
2224var RoomTileController = require ( 'matrix-react-sdk/lib/controllers/molecules/RoomTile' )
@@ -25,10 +27,89 @@ var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg');
2527
2628var sdk = require ( 'matrix-react-sdk' )
2729
28- module . exports = React . createClass ( {
30+ /**
31+ * Specifies the drag source contract.
32+ * Only `beginDrag` function is required.
33+ */
34+ var roomTileSource = {
35+ beginDrag : function ( props ) {
36+ // Return the data describing the dragged item
37+ var item = {
38+ room : props . room ,
39+ originalList : props . roomSubList ,
40+ originalIndex : props . roomSubList . findRoomTile ( props . room ) . index ,
41+ targetList : props . roomSubList , // at first target is same as original
42+ } ;
43+
44+ console . log ( "roomTile beginDrag for " + item . room . roomId ) ;
45+
46+ return item ;
47+ } ,
48+
49+ endDrag : function ( props , monitor , component ) {
50+ var item = monitor . getItem ( ) ;
51+ var dropResult = monitor . getDropResult ( ) ;
52+
53+ console . log ( "roomTile endDrag for " + item . room . roomId + " with didDrop=" + monitor . didDrop ( ) ) ;
54+
55+ if ( ! monitor . didDrop ( ) || ! item . targetList . props . editable ) {
56+ props . roomSubList . moveRoomTile ( item . room , item . originalIndex ) ;
57+ if ( item . targetList && item . targetList !== item . originalList ) {
58+ item . targetList . removeRoomTile ( item . room ) ;
59+ }
60+ return ;
61+ }
62+ else {
63+ // if it's not manual ordering, we'll need to position the tile correctly here according to the right ordering
64+
65+ // When dropped on a compatible target, actually set the right tags for the new ordering
66+ // persistNewOrder(item.room, dropResult.listId);
67+ }
68+ }
69+ } ;
70+
71+ var roomTileTarget = {
72+ canDrop : function ( ) {
73+ return false ;
74+ } ,
75+
76+ hover : function ( props , monitor ) {
77+ var item = monitor . getItem ( ) ;
78+ console . log ( "hovering on room " + props . room . roomId + ", isOver=" + monitor . isOver ( ) ) ;
79+
80+ //console.log("item.targetList=" + item.targetList + ", roomSubList=" + props.roomSubList);
81+
82+ if ( item . targetList !== props . roomSubList ) {
83+ // we've switched target, so remove the tile from the previous target.
84+ // n.b. the previous target might actually be the source list.
85+ item . targetList . removeRoomTile ( item . room ) ;
86+ item . targetList = props . roomSubList ;
87+ }
88+
89+ if ( item . targetList . props . order === 'manual' && item . room . roomId !== props . room . roomId ) {
90+ var roomTile = props . roomSubList . findRoomTile ( props . room ) ;
91+ props . roomSubList . moveRoomTile ( item . room , roomTile . index ) ;
92+ }
93+ } ,
94+ } ;
95+
96+ var RoomTile = React . createClass ( {
2997 displayName : 'RoomTile' ,
3098 mixins : [ RoomTileController ] ,
3199
100+ propTypes : {
101+ connectDragSource : React . PropTypes . func . isRequired ,
102+ connectDropTarget : React . PropTypes . func . isRequired ,
103+ isDragging : React . PropTypes . bool . isRequired ,
104+ room : React . PropTypes . object . isRequired ,
105+ collapsed : React . PropTypes . bool . isRequired ,
106+ selected : React . PropTypes . bool . isRequired ,
107+ unread : React . PropTypes . bool . isRequired ,
108+ highlight : React . PropTypes . bool . isRequired ,
109+ isInvite : React . PropTypes . bool . isRequired ,
110+ roomSubList : React . PropTypes . object . isRequired ,
111+ } ,
112+
32113 getInitialState : function ( ) {
33114 return ( { hover : false } ) ;
34115 } ,
@@ -92,14 +173,41 @@ module.exports = React.createClass({
92173 }
93174
94175 var RoomAvatar = sdk . getComponent ( 'atoms.RoomAvatar' ) ;
95- return (
176+
177+ // These props are injected by React DnD,
178+ // as defined by your `collect` function above:
179+ var isDragging = this . props . isDragging ;
180+ var connectDragSource = this . props . connectDragSource ;
181+ var connectDropTarget = this . props . connectDropTarget ;
182+
183+ return connectDragSource ( connectDropTarget (
96184 < div className = { classes } onClick = { this . onClick } onMouseEnter = { this . onMouseEnter } onMouseLeave = { this . onMouseLeave } >
97185 < div className = "mx_RoomTile_avatar" >
98186 < RoomAvatar room = { this . props . room } width = "24" height = "24" />
99187 { badge }
100188 </ div >
101189 { label }
102190 </ div >
103- ) ;
191+ ) ) ;
104192 }
105193} ) ;
194+
195+ // Export the wrapped version, inlining the 'collect' functions
196+ // to more closely resemble the ES7
197+ module . exports =
198+ DropTarget ( 'RoomTile' , roomTileTarget , function ( connect ) {
199+ return {
200+ // Call this function inside render()
201+ // to let React DnD handle the drag events:
202+ connectDropTarget : connect . dropTarget ( ) ,
203+ }
204+ } ) (
205+ DragSource ( 'RoomTile' , roomTileSource , function ( connect , monitor ) {
206+ return {
207+ // Call this function inside render()
208+ // to let React DnD handle the drag events:
209+ connectDragSource : connect . dragSource ( ) ,
210+ // You can ask the monitor about the current drag state:
211+ isDragging : monitor . isDragging ( )
212+ } ;
213+ } ) ( RoomTile ) ) ;
0 commit comments