1
1
import PropTypes from 'prop-types'
2
2
import React from 'react'
3
3
import * as dates from '../../utils/dates'
4
- import { findDOMNode } from 'react-dom '
4
+ import { DnDContext } from './DnDContext '
5
5
6
6
import Selection , {
7
7
getBoundsForNode ,
8
8
getEventNodeFromPoint ,
9
9
} from '../../Selection'
10
10
import TimeGridEvent from '../../TimeGridEvent'
11
- import { dragAccessors } from './common'
11
+ import { dragAccessors , eventTimes , pointInColumn } from './common'
12
12
import NoopWrapper from '../../NoopWrapper'
13
13
14
- const pointInColumn = ( bounds , { x, y } ) => {
15
- const { left, right, top } = bounds
16
- return x < right + 10 && x > left && y > top
17
- }
18
- const propTypes = { }
19
-
20
14
class EventContainerWrapper extends React . Component {
21
15
static propTypes = {
22
16
accessors : PropTypes . object . isRequired ,
@@ -27,20 +21,12 @@ class EventContainerWrapper extends React.Component {
27
21
resource : PropTypes . any ,
28
22
}
29
23
30
- static contextTypes = {
31
- draggable : PropTypes . shape ( {
32
- onStart : PropTypes . func ,
33
- onEnd : PropTypes . func ,
34
- onDropFromOutside : PropTypes . func ,
35
- onBeginAction : PropTypes . func ,
36
- dragAndDropAction : PropTypes . object ,
37
- dragFromOutsideItem : PropTypes . func ,
38
- } ) ,
39
- }
24
+ static contextType = DnDContext
40
25
41
26
constructor ( ...args ) {
42
27
super ( ...args )
43
28
this . state = { }
29
+ this . ref = React . createRef ( )
44
30
}
45
31
46
32
componentDidMount ( ) {
@@ -73,43 +59,31 @@ class EventContainerWrapper extends React.Component {
73
59
} )
74
60
}
75
61
76
- handleMove = ( point , boundaryBox ) => {
62
+ handleMove = ( point , bounds ) => {
63
+ if ( ! pointInColumn ( bounds , point ) ) return this . reset ( )
77
64
const { event } = this . context . draggable . dragAndDropAction
78
65
const { accessors, slotMetrics } = this . props
79
66
80
- if ( ! pointInColumn ( boundaryBox , point ) ) {
81
- this . reset ( )
82
- return
83
- }
84
-
85
- let currentSlot = slotMetrics . closestSlotFromPoint (
67
+ const newSlot = slotMetrics . closestSlotFromPoint (
86
68
{ y : point . y - this . eventOffsetTop , x : point . x } ,
87
- boundaryBox
69
+ bounds
88
70
)
89
71
90
- let eventStart = accessors . start ( event )
91
- let eventEnd = accessors . end ( event )
92
- let end = dates . add (
93
- currentSlot ,
94
- dates . diff ( eventStart , eventEnd , 'minutes' ) ,
95
- 'minutes'
96
- )
97
-
98
- this . update ( event , slotMetrics . getRange ( currentSlot , end , false , true ) )
72
+ const { duration } = eventTimes ( event , accessors )
73
+ let newEnd = dates . add ( newSlot , duration , 'milliseconds' )
74
+ this . update ( event , slotMetrics . getRange ( newSlot , newEnd , false , true ) )
99
75
}
100
76
101
- handleResize ( point , boundaryBox ) {
102
- let start , end
77
+ handleResize ( point , bounds ) {
103
78
const { accessors, slotMetrics } = this . props
104
79
const { event, direction } = this . context . draggable . dragAndDropAction
80
+ const newTime = slotMetrics . closestSlotFromPoint ( point , bounds )
105
81
106
- let currentSlot = slotMetrics . closestSlotFromPoint ( point , boundaryBox )
82
+ let { start , end } = eventTimes ( event , accessors )
107
83
if ( direction === 'UP' ) {
108
- end = accessors . end ( event )
109
- start = dates . min ( currentSlot , slotMetrics . closestSlotFromDate ( end , - 1 ) )
84
+ start = dates . min ( newTime , slotMetrics . closestSlotFromDate ( end , - 1 ) )
110
85
} else if ( direction === 'DOWN' ) {
111
- start = accessors . start ( event )
112
- end = dates . max ( currentSlot , slotMetrics . closestSlotFromDate ( start ) )
86
+ end = dates . max ( newTime , slotMetrics . closestSlotFromDate ( start ) )
113
87
}
114
88
115
89
this . update ( event , slotMetrics . getRange ( start , end ) )
@@ -132,10 +106,11 @@ class EventContainerWrapper extends React.Component {
132
106
}
133
107
134
108
_selectable = ( ) => {
135
- let node = findDOMNode ( this )
109
+ let wrapper = this . ref . current
110
+ let node = wrapper . children [ 0 ]
136
111
let isBeingDragged = false
137
112
let selector = ( this . _selector = new Selection ( ( ) =>
138
- node . closest ( '.rbc-time-view' )
113
+ wrapper . closest ( '.rbc-time-view' )
139
114
) )
140
115
141
116
selector . on ( 'beforeSelect' , point => {
@@ -149,6 +124,12 @@ class EventContainerWrapper extends React.Component {
149
124
const eventNode = getEventNodeFromPoint ( node , point )
150
125
if ( ! eventNode ) return false
151
126
127
+ // eventOffsetTop is distance from the top of the event to the initial
128
+ // mouseDown position. We need this later to compute the new top of the
129
+ // event during move operations, since the final location is really a
130
+ // delta from this point. note: if we want to DRY this with WeekWrapper,
131
+ // probably better just to capture the mouseDown point here and do the
132
+ // placement computation in handleMove()...
152
133
this . eventOffsetTop = point . y - getBoundsForNode ( eventNode ) . top
153
134
} )
154
135
@@ -162,19 +143,14 @@ class EventContainerWrapper extends React.Component {
162
143
163
144
selector . on ( 'dropFromOutside' , point => {
164
145
if ( ! this . context . draggable . onDropFromOutside ) return
165
-
166
146
const bounds = getBoundsForNode ( node )
167
-
168
147
if ( ! pointInColumn ( bounds , point ) ) return
169
-
170
148
this . handleDropFromOutside ( point , bounds )
171
149
} )
172
150
173
151
selector . on ( 'dragOver' , point => {
174
152
if ( ! this . context . draggable . dragFromOutsideItem ) return
175
-
176
153
const bounds = getBoundsForNode ( node )
177
-
178
154
this . handleDropFromOutside ( point , bounds )
179
155
} )
180
156
@@ -220,7 +196,7 @@ class EventContainerWrapper extends React.Component {
220
196
this . _selector = null
221
197
}
222
198
223
- render ( ) {
199
+ renderContent ( ) {
224
200
const {
225
201
children,
226
202
accessors,
@@ -231,7 +207,6 @@ class EventContainerWrapper extends React.Component {
231
207
} = this . props
232
208
233
209
let { event, top, height } = this . state
234
-
235
210
if ( ! event ) return children
236
211
237
212
const events = children . props . children
@@ -271,8 +246,10 @@ class EventContainerWrapper extends React.Component {
271
246
) ,
272
247
} )
273
248
}
274
- }
275
249
276
- EventContainerWrapper . propTypes = propTypes
250
+ render ( ) {
251
+ return < div ref = { this . ref } > { this . renderContent ( ) } </ div >
252
+ }
253
+ }
277
254
278
255
export default EventContainerWrapper
0 commit comments