-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalendar.jsx
306 lines (248 loc) · 8.57 KB
/
calendar.jsx
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
import React, { Component } from "react"
import { exportCalendar } from './export_calendar.jsx'
function timeToMinutes(time) {
const s = time.split(':');
return parseInt(s[0]) * 60 + parseInt(s[1]);
}
function minutesToTime(total_minutes) {
const hours = Math.floor(total_minutes / 60)
let minutes = total_minutes % 60
minutes = minutes + ''
while(minutes.length < 2) {
minutes = '0' + minutes
}
return `${hours}:${minutes}`
}
// from colorbrewer2.org
// const COLORS = ['#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462',
// '#b3de69','#fccde5','#d9d9d9','#bc80bd','#ccebc5','#ffed6f'];
const COLORS_BORDER = ['#66c2a5','#fc8d62','#8da0cb','#e78ac3','#a6d854','#ffd92f','#e5c494','#b3b3b3']
const COLORS = ['#b3e2cd','#fdcdac','#cbd5e8','#f4cae4','#e6f5c9','#fff2ae','#f1e2cc','#cccccc']
class Section extends Component {
render() {
const {section} = this.props
const courseTitle = `${section['Subject']} ${section['Catalog Number']}`
const sectionTitle = `${section["Course Component"]} ${section["Section"]}`
const facilityTitle = section['Facility']
return (
<div className="Section"
style={{
backgroundColor: section['Color'],
borderColor: section['BorderColor'],
top: this.props.top + "%",
height: this.props.height + "%",
position: "absolute"
}}>
<span className="CourseTitle"> {courseTitle} {sectionTitle}</span>
<div className="FacilityTitle">
{facilityTitle}
</div>
</div>
)
}
}
const DAY_TITLE_MAP = {
'M': 'Mon',
'T': 'Tue',
'W': 'Wed',
'R': 'Thu',
'F': 'Fri',
'S': 'Sat'
}
class CalendarDayColumn extends Component {
getPercentPosition(time, props) {
let minute = timeToMinutes(time)
return 100 * (minute - props.minMinute) / (props.maxMinute - props.minMinute)
}
render() {
if(!this.props.hasSaturday && this.props.day == 'S') {
return <div></div>
}
let saturdayClass = 'CalendarDayColumnWithoutSaturday'
if(this.props.hasSaturday) {
saturdayClass = 'CalendarDayColumnWithSaturday'
}
const courseElements = this.props.sections.map( (section) => {
const top = this.getPercentPosition(section["Start Time"], this.props)
const bottom = this.getPercentPosition(section["End Time"], this.props)
const height = bottom - top
const title = section['Class Number']
return <Section key={title} section={section} top={top} height={height} />
})
const dayTitle = DAY_TITLE_MAP[this.props.day]
return (
<div className={"CalendarDayColumn " + saturdayClass} >
<div className="CalendarDayTitle"> {dayTitle} </div>
<div className="CalendarDaySections">
{courseElements}
</div>
</div>
);
}
}
class CalendarTimeColumn extends Component {
getPercentPosition(minute, props) {
return 100 * (minute - props.minMinute) / (props.maxMinute - props.minMinute)
}
render() {
let { minMinute, maxMinute } = this.props
let elements = []
for(let minute = minMinute; minute <= maxMinute; minute += 60) {
const time = minutesToTime(minute)
const top = this.getPercentPosition(minute, this.props)
elements.push(
<div className="CalendarTimeLabel" key={time} style={{top: top + '%'}}>
{time}
</div>
)
}
return (
<div className="CalendarTimeColumn">
<div className="CalendarDayTitle"> </div>
<div className="CalendarTimeColumnContent">
{elements}
</div>
</div>
)
}
}
class Calendar extends Component {
getSections(courses) {
let out = []
let index = 0;
for(let course of courses) {
const color = COLORS[index % COLORS.length]
const borderColor = COLORS_BORDER[index % COLORS.length]
for(let section of course) {
section['Index'] = index
section['Color'] = color
section['BorderColor'] = borderColor
out.push(section)
}
index += 1
}
return out
}
render() {
const sections = this.getSections(this.props.courses)
let dayElements = {}
const days = 'MTWRFS'.split('')
for(const day of days) {
dayElements[day] = []
}
let minMinute = null;
let maxMinute = null;
for(let section of sections) {
const meetingDays = section['Meeting Days'].split('')
for(let day of meetingDays) {
let startMinute = timeToMinutes(section['Start Time'])
let endMinute = timeToMinutes(section['End Time'])
if(minMinute === null || startMinute < minMinute) {
minMinute = startMinute
}
if(maxMinute === null || endMinute > maxMinute) {
maxMinute = endMinute
}
dayElements[day].push(section)
}
}
maxMinute = maxMinute + 32
minMinute = minMinute - (minMinute % 60)
maxMinute = maxMinute - (maxMinute % 60)
let hasSaturday = false
if(dayElements['S'].length > 0) {
hasSaturday = true
}
let dayColumns = days.map( (day) => {
return (
<CalendarDayColumn key={day} day={day} hasSaturday={hasSaturday}
sections={dayElements[day]}
minMinute={minMinute} maxMinute={maxMinute}
/>
)
})
return (
<div className="Calendar">
<CalendarTimeColumn minMinute={minMinute} maxMinute={maxMinute} />
{dayColumns}
</div>
)
}
}
class CalendarPicker extends Component {
render() {
const { numCalendars, selected, calendar } = this.props
return (
<div className="CalendarPicker">
<div className="CalendarNumber">
Calendar {selected+1}/{numCalendars}
</div>
<div className="CalendarButtons">
<span className="btn" onClick={() => {
window.store.dispatch({
type: 'PREV_CALENDAR_INDEX'
})
}}> Previous </span>
<span className="btn" onClick={() => {
window.store.dispatch({
type: 'NEXT_CALENDAR_INDEX'
})
}}> Next </span>
</div>
<div className="ExportButton">
<span className="btn" id="exportCalendar" onClick={() => {
exportCalendar(calendar)
}}>Export</span>
</div>
</div>
)
}
}
export class Calendars extends Component {
componentDidMount() {
this.unsubscribe = window.store.subscribe(() => this.forceUpdate())
}
componentWillUnmount() {
this.unsubscribe()
}
render() {
const state = window.store.getState()
const { calendars, index } = state.calendars
if(calendars.length == 0) {
return (
<div className="Calendars">
No possible calendars
</div>
)
}
const calendar = calendars[index]
return (
<div className="Calendars">
<CalendarPicker numCalendars={calendars.length}
selected={index}
calendar={calendar} />
<Calendar courses={calendar} />
</div>
)
}
}
$(document).keydown(function(e) {
switch(e.which) {
case 37: // left
window.store.dispatch({
type: 'PREV_CALENDAR_INDEX'
})
break;
case 39: // right
window.store.dispatch({
type: 'NEXT_CALENDAR_INDEX'
})
break;
case 38: // up
break;
case 40: // down
break;
default: return; // exit this handler for other keys
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});