-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
DayColumn.js
451 lines (380 loc) · 12.3 KB
/
DayColumn.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import React, { createRef } from 'react'
import PropTypes from 'prop-types'
import clsx from 'clsx'
import Selection, { getBoundsForNode, isEvent } from './Selection'
import * as TimeSlotUtils from './utils/TimeSlots'
import { isSelected } from './utils/selection'
import { notify } from './utils/helpers'
import * as DayEventLayout from './utils/DayEventLayout'
import TimeSlotGroup from './TimeSlotGroup'
import TimeGridEvent from './TimeGridEvent'
import { DayLayoutAlgorithmPropType } from './utils/propTypes'
import DayColumnWrapper from './DayColumnWrapper'
class DayColumn extends React.Component {
state = { selecting: false, timeIndicatorPosition: null }
intervalTriggered = false
constructor(...args) {
super(...args)
this.slotMetrics = TimeSlotUtils.getSlotMetrics(this.props)
this.containerRef = createRef()
}
componentDidMount() {
this.props.selectable && this._selectable()
if (this.props.isNow) {
this.setTimeIndicatorPositionUpdateInterval()
}
}
componentWillUnmount() {
this._teardownSelectable()
this.clearTimeIndicatorInterval()
}
componentDidUpdate(prevProps, prevState) {
if (this.props.selectable && !prevProps.selectable) this._selectable()
if (!this.props.selectable && prevProps.selectable)
this._teardownSelectable()
const { getNow, isNow, localizer, date, min, max } = this.props
const getNowChanged = localizer.neq(prevProps.getNow(), getNow(), 'minutes')
if (prevProps.isNow !== isNow || getNowChanged) {
this.clearTimeIndicatorInterval()
if (isNow) {
const tail =
!getNowChanged &&
localizer.eq(prevProps.date, date, 'minutes') &&
prevState.timeIndicatorPosition === this.state.timeIndicatorPosition
this.setTimeIndicatorPositionUpdateInterval(tail)
}
} else if (
isNow &&
(localizer.neq(prevProps.min, min, 'minutes') ||
localizer.neq(prevProps.max, max, 'minutes'))
) {
this.positionTimeIndicator()
}
}
/**
* @param tail {Boolean} - whether `positionTimeIndicator` call should be
* deferred or called upon setting interval (`true` - if deferred);
*/
setTimeIndicatorPositionUpdateInterval(tail = false) {
if (!this.intervalTriggered && !tail) {
this.positionTimeIndicator()
}
this._timeIndicatorTimeout = window.setTimeout(() => {
this.intervalTriggered = true
this.positionTimeIndicator()
this.setTimeIndicatorPositionUpdateInterval()
}, 60000)
}
clearTimeIndicatorInterval() {
this.intervalTriggered = false
window.clearTimeout(this._timeIndicatorTimeout)
}
positionTimeIndicator() {
const { min, max, getNow } = this.props
const current = getNow()
if (current >= min && current <= max) {
const top = this.slotMetrics.getCurrentTimePosition(current)
this.intervalTriggered = true
this.setState({ timeIndicatorPosition: top })
} else {
this.clearTimeIndicatorInterval()
}
}
render() {
const {
date,
max,
rtl,
isNow,
resource,
accessors,
localizer,
getters: { dayProp, ...getters },
components: { eventContainerWrapper: EventContainer, ...components },
} = this.props
this.slotMetrics = this.slotMetrics.update(this.props)
let { slotMetrics } = this
let { selecting, top, height, startDate, endDate } = this.state
let selectDates = { start: startDate, end: endDate }
const { className, style } = dayProp(max, resource)
const DayColumnWrapperComponent =
components.dayColumnWrapper || DayColumnWrapper
return (
<DayColumnWrapperComponent
ref={this.containerRef}
date={date}
style={style}
className={clsx(
className,
'rbc-day-slot',
'rbc-time-column',
isNow && 'rbc-now',
isNow && 'rbc-today', // WHY
selecting && 'rbc-slot-selecting'
)}
slotMetrics={slotMetrics}
resource={resource}
>
{slotMetrics.groups.map((grp, idx) => (
<TimeSlotGroup
key={idx}
group={grp}
resource={resource}
getters={getters}
components={components}
/>
))}
<EventContainer
localizer={localizer}
resource={resource}
accessors={accessors}
getters={getters}
components={components}
slotMetrics={slotMetrics}
>
<div className={clsx('rbc-events-container', rtl && 'rtl')}>
{this.renderEvents({
events: this.props.backgroundEvents,
isBackgroundEvent: true,
})}
{this.renderEvents({ events: this.props.events })}
</div>
</EventContainer>
{selecting && (
<div className="rbc-slot-selection" style={{ top, height }}>
<span>{localizer.format(selectDates, 'selectRangeFormat')}</span>
</div>
)}
{isNow && this.intervalTriggered && (
<div
className="rbc-current-time-indicator"
style={{ top: `${this.state.timeIndicatorPosition}%` }}
/>
)}
</DayColumnWrapperComponent>
)
}
renderEvents = ({ events, isBackgroundEvent }) => {
let {
rtl,
selected,
accessors,
localizer,
getters,
components,
step,
timeslots,
dayLayoutAlgorithm,
resizable,
} = this.props
const { slotMetrics } = this
const { messages } = localizer
let styledEvents = DayEventLayout.getStyledEvents({
events,
accessors,
slotMetrics,
minimumStartDifference: Math.ceil((step * timeslots) / 2),
dayLayoutAlgorithm,
})
return styledEvents.map(({ event, style }, idx) => {
let end = accessors.end(event)
let start = accessors.start(event)
let format = 'eventTimeRangeFormat'
let label
const startsBeforeDay = slotMetrics.startsBeforeDay(start)
const startsAfterDay = slotMetrics.startsAfterDay(end)
if (startsBeforeDay) format = 'eventTimeRangeEndFormat'
else if (startsAfterDay) format = 'eventTimeRangeStartFormat'
if (startsBeforeDay && startsAfterDay) label = messages.allDay
else label = localizer.format({ start, end }, format)
let continuesPrior = startsBeforeDay || slotMetrics.startsBefore(start)
let continuesAfter = startsAfterDay || slotMetrics.startsAfter(end)
return (
<TimeGridEvent
style={style}
event={event}
label={label}
key={'evt_' + idx}
getters={getters}
rtl={rtl}
components={components}
continuesPrior={continuesPrior}
continuesAfter={continuesAfter}
accessors={accessors}
resource={this.props.resource}
selected={isSelected(event, selected)}
onClick={(e) =>
this._select(
{
...event,
...(this.props.resource && {
sourceResource: this.props.resource,
}),
...(isBackgroundEvent && { isBackgroundEvent: true }),
},
e
)
}
onDoubleClick={(e) => this._doubleClick(event, e)}
isBackgroundEvent={isBackgroundEvent}
onKeyPress={(e) => this._keyPress(event, e)}
resizable={resizable}
/>
)
})
}
_selectable = () => {
let node = this.containerRef.current
const { longPressThreshold, localizer } = this.props
let selector = (this._selector = new Selection(() => node, {
longPressThreshold: longPressThreshold,
}))
let maybeSelect = (box) => {
let onSelecting = this.props.onSelecting
let current = this.state || {}
let state = selectionState(box)
let { startDate: start, endDate: end } = state
if (onSelecting) {
if (
(localizer.eq(current.startDate, start, 'minutes') &&
localizer.eq(current.endDate, end, 'minutes')) ||
onSelecting({ start, end, resourceId: this.props.resource }) === false
)
return
}
if (
this.state.start !== state.start ||
this.state.end !== state.end ||
this.state.selecting !== state.selecting
) {
this.setState(state)
}
}
let selectionState = (point) => {
let currentSlot = this.slotMetrics.closestSlotFromPoint(
point,
getBoundsForNode(node)
)
if (!this.state.selecting) {
this._initialSlot = currentSlot
}
let initialSlot = this._initialSlot
if (localizer.lte(initialSlot, currentSlot)) {
currentSlot = this.slotMetrics.nextSlot(currentSlot)
} else if (localizer.gt(initialSlot, currentSlot)) {
initialSlot = this.slotMetrics.nextSlot(initialSlot)
}
const selectRange = this.slotMetrics.getRange(
localizer.min(initialSlot, currentSlot),
localizer.max(initialSlot, currentSlot)
)
return {
...selectRange,
selecting: true,
top: `${selectRange.top}%`,
height: `${selectRange.height}%`,
}
}
let selectorClicksHandler = (box, actionType) => {
if (!isEvent(this.containerRef.current, box)) {
const { startDate, endDate } = selectionState(box)
this._selectSlot({
startDate,
endDate,
action: actionType,
box,
})
}
this.setState({ selecting: false })
}
selector.on('selecting', maybeSelect)
selector.on('selectStart', maybeSelect)
selector.on('beforeSelect', (box) => {
if (this.props.selectable !== 'ignoreEvents') return
return !isEvent(this.containerRef.current, box)
})
selector.on('click', (box) => selectorClicksHandler(box, 'click'))
selector.on('doubleClick', (box) =>
selectorClicksHandler(box, 'doubleClick')
)
selector.on('select', (bounds) => {
if (this.state.selecting) {
this._selectSlot({ ...this.state, action: 'select', bounds })
this.setState({ selecting: false })
}
})
selector.on('reset', () => {
if (this.state.selecting) {
this.setState({ selecting: false })
}
})
}
_teardownSelectable = () => {
if (!this._selector) return
this._selector.teardown()
this._selector = null
}
_selectSlot = ({ startDate, endDate, action, bounds, box }) => {
let current = startDate,
slots = []
while (this.props.localizer.lte(current, endDate)) {
slots.push(current)
current = new Date(+current + this.props.step * 60 * 1000) // using Date ensures not to create an endless loop the day DST begins
}
notify(this.props.onSelectSlot, {
slots,
start: startDate,
end: endDate,
resourceId: this.props.resource,
action,
bounds,
box,
})
}
_select = (...args) => {
notify(this.props.onSelectEvent, args)
}
_doubleClick = (...args) => {
notify(this.props.onDoubleClickEvent, args)
}
_keyPress = (...args) => {
notify(this.props.onKeyPressEvent, args)
}
}
DayColumn.propTypes = {
events: PropTypes.array.isRequired,
backgroundEvents: PropTypes.array.isRequired,
step: PropTypes.number.isRequired,
date: PropTypes.instanceOf(Date).isRequired,
min: PropTypes.instanceOf(Date).isRequired,
max: PropTypes.instanceOf(Date).isRequired,
getNow: PropTypes.func.isRequired,
isNow: PropTypes.bool,
rtl: PropTypes.bool,
resizable: PropTypes.bool,
accessors: PropTypes.object.isRequired,
components: PropTypes.object.isRequired,
getters: PropTypes.object.isRequired,
localizer: PropTypes.object.isRequired,
showMultiDayTimes: PropTypes.bool,
culture: PropTypes.string,
timeslots: PropTypes.number,
selected: PropTypes.object,
selectable: PropTypes.oneOf([true, false, 'ignoreEvents']),
eventOffset: PropTypes.number,
longPressThreshold: PropTypes.number,
onSelecting: PropTypes.func,
onSelectSlot: PropTypes.func.isRequired,
onSelectEvent: PropTypes.func.isRequired,
onDoubleClickEvent: PropTypes.func.isRequired,
onKeyPressEvent: PropTypes.func,
className: PropTypes.string,
dragThroughEvents: PropTypes.bool,
resource: PropTypes.any,
dayLayoutAlgorithm: DayLayoutAlgorithmPropType,
}
DayColumn.defaultProps = {
dragThroughEvents: true,
timeslots: 2,
}
export default DayColumn