-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
WeekWrapper.js
261 lines (220 loc) · 7.58 KB
/
WeekWrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import PropTypes from 'prop-types'
import React from 'react'
import EventRow from '../../EventRow'
import Selection, { getBoundsForNode } from '../../Selection'
import { eventSegments } from '../../utils/eventLevels'
import { getSlotAtX, pointInBox } from '../../utils/selection'
import { dragAccessors, eventTimes } from './common'
import { DnDContext } from './DnDContext'
class WeekWrapper extends React.Component {
static propTypes = {
isAllDay: PropTypes.bool,
slotMetrics: PropTypes.object.isRequired,
accessors: PropTypes.object.isRequired,
getters: PropTypes.object.isRequired,
components: PropTypes.object.isRequired,
resourceId: PropTypes.any,
rtl: PropTypes.bool,
localizer: PropTypes.any,
}
static contextType = DnDContext
constructor(...args) {
super(...args)
this.state = {}
this.ref = React.createRef()
}
componentDidMount() {
this._selectable()
}
componentWillUnmount() {
this._teardownSelectable()
}
reset() {
if (this.state.segment) this.setState({ segment: null })
}
update(event, start, end) {
const segment = eventSegments(
{ ...event, end, start, __isPreview: true },
this.props.slotMetrics.range,
dragAccessors,
this.props.localizer
)
const { segment: lastSegment } = this.state
if (
lastSegment &&
segment.span === lastSegment.span &&
segment.left === lastSegment.left &&
segment.right === lastSegment.right
) {
return
}
this.setState({ segment })
}
handleMove = (point, bounds, draggedEvent) => {
if (!pointInBox(bounds, point)) return this.reset()
const event = this.context.draggable.dragAndDropAction.event || draggedEvent
const { accessors, slotMetrics, rtl, localizer } = this.props
const slot = getSlotAtX(bounds, point.x, rtl, slotMetrics.slots)
const date = slotMetrics.getDateForSlot(slot)
// Adjust the dates, but maintain the times when moving
let { start, duration } = eventTimes(event, accessors, localizer)
start = localizer.merge(date, start)
const end = localizer.add(start, duration, 'milliseconds')
// LATER: when dragging a multi-row event, only the first row is animating
this.update(event, start, end)
}
handleDropFromOutside = (point, bounds) => {
if (!this.context.draggable.onDropFromOutside) return
const { slotMetrics, rtl, localizer } = this.props
const slot = getSlotAtX(bounds, point.x, rtl, slotMetrics.slots)
const start = slotMetrics.getDateForSlot(slot)
this.context.draggable.onDropFromOutside({
start,
end: localizer.add(start, 1, 'day'),
allDay: false,
})
}
handleDragOverFromOutside = (point, node) => {
const item = this.context.draggable.dragFromOutsideItem ? this.context.draggable.dragFromOutsideItem() : null
if (!item) return
this.handleMove(point, node, item)
}
handleResize(point, bounds) {
const { event, direction } = this.context.draggable.dragAndDropAction
const { accessors, slotMetrics, rtl, localizer } = this.props
let { start, end } = eventTimes(event, accessors, localizer)
const slot = getSlotAtX(bounds, point.x, rtl, slotMetrics.slots)
const date = slotMetrics.getDateForSlot(slot)
const cursorInRow = pointInBox(bounds, point)
if (direction === 'RIGHT') {
if (cursorInRow) {
if (slotMetrics.last < start) return this.reset()
if (localizer.eq(localizer.startOf(end, 'day'), end))
end = localizer.add(date, 1, 'day')
else end = date
} else if (
localizer.inRange(start, slotMetrics.first, slotMetrics.last) ||
(bounds.bottom < point.y && +slotMetrics.first > +start)
) {
end = localizer.add(slotMetrics.last, 1, 'milliseconds')
} else {
this.setState({ segment: null })
return
}
const originalEnd = accessors.end(event)
end = localizer.merge(end, originalEnd)
if (localizer.lt(end, start)) {
end = originalEnd
}
} else if (direction === 'LEFT') {
if (cursorInRow) {
if (slotMetrics.first > end) return this.reset()
start = date
} else if (
localizer.inRange(end, slotMetrics.first, slotMetrics.last) ||
(bounds.top > point.y && localizer.lt(slotMetrics.last, end))
) {
start = localizer.add(slotMetrics.first, -1, 'milliseconds')
} else {
this.reset()
return
}
const originalStart = accessors.start(event)
start = localizer.merge(start, originalStart)
if (localizer.gt(start, end)) {
start = originalStart
}
}
this.update(event, start, end)
}
_selectable = () => {
let node = this.ref.current.closest('.rbc-month-row, .rbc-allday-cell')
let container = node.closest('.rbc-month-view, .rbc-time-view')
let isMonthRow = node.classList.contains('rbc-month-row')
// Valid container check only necessary in TimeGrid views
let selector = (this._selector = new Selection(() => container, {
validContainers: [
...(!isMonthRow ? ['.rbc-day-slot', '.rbc-allday-cell'] : []),
],
}))
selector.on('beforeSelect', (point) => {
const { isAllDay } = this.props
const { action } = this.context.draggable.dragAndDropAction
const bounds = getBoundsForNode(node)
const isInBox = pointInBox(bounds, point)
return (
action === 'move' || (action === 'resize' && (!isAllDay || isInBox))
)
})
selector.on('selecting', (box) => {
const bounds = getBoundsForNode(node)
const { dragAndDropAction } = this.context.draggable
if (dragAndDropAction.action === 'move') this.handleMove(box, bounds)
if (dragAndDropAction.action === 'resize') this.handleResize(box, bounds)
})
selector.on('selectStart', () => this.context.draggable.onStart())
selector.on('select', (point) => {
const bounds = getBoundsForNode(node)
if (!this.state.segment) return
if (!pointInBox(bounds, point)) {
this.reset()
} else {
this.handleInteractionEnd()
}
})
selector.on('dropFromOutside', (point) => {
if (!this.context.draggable.onDropFromOutside) return
const bounds = getBoundsForNode(node)
if (!pointInBox(bounds, point)) return
this.handleDropFromOutside(point, bounds)
})
selector.on('dragOverFromOutside', (point) => {
if (!this.context.draggable.dragFromOutsideItem) return
const bounds = getBoundsForNode(node)
this.handleDragOverFromOutside(point, bounds)
})
selector.on('click', () => this.context.draggable.onEnd(null))
selector.on('reset', () => {
this.reset()
this.context.draggable.onEnd(null)
})
}
handleInteractionEnd = () => {
const { resourceId, isAllDay } = this.props
const { event } = this.state.segment
this.reset()
this.context.draggable.onEnd({
start: event.start,
end: event.end,
resourceId,
isAllDay,
})
}
_teardownSelectable = () => {
if (!this._selector) return
this._selector.teardown()
this._selector = null
}
render() {
const { children, accessors } = this.props
let { segment } = this.state
return (
<div ref={this.ref} className="rbc-addons-dnd-row-body">
{children}
{segment && (
<EventRow
{...this.props}
selected={null}
className="rbc-addons-dnd-drag-row"
segments={[segment]}
accessors={{
...accessors,
...dragAccessors,
}}
/>
)}
</div>
)
}
}
export default WeekWrapper