-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathTimeSlots.js
150 lines (128 loc) · 4.41 KB
/
TimeSlots.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
import * as dates from './dates'
const getDstOffset = (start, end) =>
start.getTimezoneOffset() - end.getTimezoneOffset()
const getKey = (min, max, step, slots) =>
`${+dates.startOf(min, 'minutes')}` +
`${+dates.startOf(max, 'minutes')}` +
`${step}-${slots}`
export function getSlotMetrics({ min: start, max: end, step, timeslots }) {
const key = getKey(start, end, step, timeslots)
// if the start is on a DST-changing day but *after* the moment of DST
// transition we need to add those extra minutes to our minutesFromMidnight
const daystart = dates.startOf(start, 'day')
const daystartdstoffset = getDstOffset(daystart, start)
const totalMin =
1 + dates.diff(start, end, 'minutes') + getDstOffset(start, end)
const minutesFromMidnight =
dates.diff(daystart, start, 'minutes') + daystartdstoffset
const numGroups = Math.ceil(totalMin / (step * timeslots))
const numSlots = numGroups * timeslots
const groups = new Array(numGroups)
const slots = new Array(numSlots)
// Each slot date is created from "zero", instead of adding `step` to
// the previous one, in order to avoid DST oddities
for (let grp = 0; grp < numGroups; grp++) {
groups[grp] = new Array(timeslots)
for (let slot = 0; slot < timeslots; slot++) {
const slotIdx = grp * timeslots + slot
const minFromStart = slotIdx * step
// A date with total minutes calculated from the start of the day
slots[slotIdx] = groups[grp][slot] = new Date(
start.getFullYear(),
start.getMonth(),
start.getDate(),
0,
minutesFromMidnight + minFromStart,
0,
0
)
}
}
// Necessary to be able to select up until the last timeslot in a day
const lastSlotMinFromStart = slots.length * step
slots.push(
new Date(
start.getFullYear(),
start.getMonth(),
start.getDate(),
0,
minutesFromMidnight + lastSlotMinFromStart,
0,
0
)
)
function positionFromDate(date) {
const diff = dates.diff(start, date, 'minutes') + getDstOffset(start, date)
return Math.min(diff, totalMin)
}
return {
groups,
update(args) {
if (getKey(args) !== key) return getSlotMetrics(args)
return this
},
dateIsInGroup(date, groupIndex) {
const nextGroup = groups[groupIndex + 1]
return dates.inRange(
date,
groups[groupIndex][0],
nextGroup ? nextGroup[0] : end,
'minutes'
)
},
nextSlot(slot) {
let next = slots[Math.min(slots.indexOf(slot) + 1, slots.length - 1)]
// in the case of the last slot we won't a long enough range so manually get it
if (next === slot) next = dates.add(slot, step, 'minutes')
return next
},
closestSlotToPosition(percent) {
const slot = Math.min(
slots.length - 1,
Math.max(0, Math.floor(percent * numSlots))
)
return slots[slot]
},
closestSlotFromPoint(point, boundaryRect) {
let range = Math.abs(boundaryRect.top - boundaryRect.bottom)
return this.closestSlotToPosition((point.y - boundaryRect.top) / range)
},
closestSlotFromDate(date, offset = 0) {
if (dates.lt(date, start, 'minutes')) return slots[0]
const diffMins = dates.diff(start, date, 'minutes')
return slots[(diffMins - (diffMins % step)) / step + offset]
},
startsBeforeDay(date) {
return dates.lt(date, start, 'day')
},
startsAfterDay(date) {
return dates.gt(date, end, 'day')
},
startsBefore(date) {
return dates.lt(dates.merge(start, date), start, 'minutes')
},
startsAfter(date) {
return dates.gt(dates.merge(end, date), end, 'minutes')
},
getRange(rangeStart, rangeEnd, ignoreMin, ignoreMax) {
if (!ignoreMin)
rangeStart = dates.min(end, dates.max(start, rangeStart))
if (!ignoreMax)
rangeEnd = dates.min(end, dates.max(start, rangeEnd))
const rangeStartMin = positionFromDate(rangeStart)
const rangeEndMin = positionFromDate(rangeEnd)
const top =
rangeEndMin - rangeStartMin < step
? ((rangeStartMin - step) / (step * numSlots)) * 100
: (rangeStartMin / (step * numSlots)) * 100
return {
top,
height: (rangeEndMin / (step * numSlots)) * 100 - top,
start: positionFromDate(rangeStart),
startDate: rangeStart,
end: positionFromDate(rangeEnd),
endDate: rangeEnd,
}
},
}
}