-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Calendar.jsx
337 lines (277 loc) · 7.56 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
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
import React, { PropTypes } from 'react';
import uncontrollable from 'uncontrollable';
import {
accessor
, elementType
, dateFormat
, views as componentViews } from './utils/propTypes';
import localizer from './localizer'
import { notify } from './utils/helpers';
import { navigate, views } from './utils/constants';
import dates from './utils/dates';
import defaultFormats from './formats';
import Month from './Month';
import Day from './Day';
import Week from './Week';
import Agenda from './Agenda';
import Toolbar from './Toolbar';
import omit from 'lodash/object/omit';
import defaults from 'lodash/object/defaults';
const VIEWS = {
[views.MONTH]: Month,
[views.WEEK]: Week,
[views.DAY]: Day,
[views.AGENDA]: Agenda
};
const Formats = {
[views.MONTH]: 'monthHeaderFormat',
[views.WEEK]: 'dayRangeHeaderFormat',
[views.DAY]: 'dayHeaderFormat',
[views.AGENDA]: 'agendaHeaderFormat'
}
function viewNames(_views){
return !Array.isArray(_views) ? Object.keys(_views) : _views
}
function isValidView(view, { views: _views }) {
let names = viewNames(_views)
return names.indexOf(view) !== -1
}
let now = new Date();
let Calendar = React.createClass({
propTypes: {
/**
* The current date value of the calendar. Determines the visible view range
*/
date: PropTypes.instanceOf(Date),
/**
* An array of event objects to display on the calendar
*/
events: PropTypes.arrayOf(PropTypes.object),
/**
* Callback fired when the `date` value changes.
*/
onNavigate: PropTypes.func,
/**
* A callback fired when a date selection is made.
*
* ```js
* function(
* slotInfo: object {
* start: date,
* end: date,
* slots: array<date>
* }
* )
* ```
*/
onSelectSlot: PropTypes.func,
/**
* Callback fired when an event node is selected.
*
* ```js
* function(event: object)
* ```
*/
onSelectEvent: PropTypes.func,
/**
* An array of built in view names
*/
views: componentViews,
/**
* Determines whether the toolbar is displayed
*/
toolbar: PropTypes.bool,
/**
* Allows mouse selection of ranges of dates/times.
*/
selectable: PropTypes.bool,
/**
* Accessor for the event title, used to display event information. Should
* resolve to a `renderable` value.
*
* @type {(func|string)}
*/
titleAccessor: accessor,
/**
* Determines whether the event should be considered an "all day" event and ignore time.
* Must resolve to a `boolean` value.
*
* @type {(func|string)}
*/
allDayAccessor: accessor,
/**
* The start date/time of the event. Must resolve to a JavaScript `Date` object.
*
* @type {(func|string)}
*/
startAccessor: accessor,
/**
* The end date/time of the event. Must resolve to a JavaScript `Date` object.
*
* @type {(func|string)}
*/
endAccessor: accessor,
formats: PropTypes.shape({
/**
* Format for the day of the month heading in the Month view.
*/
dateFormat,
/**
* A day of the week format for Week and Day headings
*/
dayFormat: dateFormat,
/**
* Week day name format for the Month week day headings.
*/
weekdayFormat: dateFormat,
/**
* Toolbar header format for the Month view.
*/
monthHeaderFormat: dateFormat,
/**
* Toolbar header format for the Week views.
*/
weekHeaderFormat: dateFormat,
/**
* Toolbar header format for the Day view.
*/
dayHeaderFormat: dateFormat,
/**
* Toolbar header format for the Agenda view.
*/
agendaHeaderFormat: dateFormat,
/**
* A time range format for selecting time slots.
*/
selectRangeFormat: dateFormat,
agendaDateFormat: dateFormat,
agendaTimeFormat: dateFormat,
agendaTimeRangeFormat: dateFormat
}),
components: PropTypes.shape({
event: elementType,
agenda: PropTypes.shape({
date: elementType,
time: elementType,
event: elementType
}),
day: PropTypes.shape({ event: elementType }),
week: PropTypes.shape({ event: elementType }),
month: PropTypes.shape({ event: elementType })
}),
messages: PropTypes.shape({
allDay: PropTypes.string,
previous: PropTypes.string,
next: PropTypes.string,
today: PropTypes.string,
month: PropTypes.string,
week: PropTypes.string,
day: PropTypes.string,
agenda: PropTypes.string
})
},
getDefaultProps() {
return {
popup: false,
toolbar: true,
view: views.MONTH,
views: [views.MONTH, views.WEEK, views.DAY, views.AGENDA],
date: now,
titleAccessor: 'title',
allDayAccessor: 'allDay',
startAccessor: 'start',
endAccessor: 'end'
};
},
render() {
let {
view, toolbar, events
, culture
, components = {}
, formats = {}
, date: current } = this.props;
formats = defaultFormats(formats)
let View = VIEWS[view];
let headerSingle = view === views.MONTH || view === views.DAY
let names = viewNames(this.props.views)
let { start, end } = View.range(current, this.props);
let headerFormat = formats[Formats[view]];
let label = headerSingle
? localizer.format(current, headerFormat, culture)
: localizer.format({ start, end }, headerFormat, culture)
let elementProps = omit(this.props, Object.keys(Calendar.propTypes))
let viewComponents = defaults(
components[view] || {},
omit(components, names)
)
return (
<div {...elementProps}
className='rbc-calendar'
>
{ toolbar &&
<Toolbar
date={current}
view={view}
views={names}
label={label}
onViewChange={this._view}
onNavigate={this._navigate}
/>
}
<View
ref='view'
{...this.props}
{...formats}
formats={undefined}
events={events}
date={current}
components={viewComponents}
onNavigate={this._navigate}
onHeaderClick={this._headerClick}
onSelectEvent={this._select}
onSelectSlot={this._selectSlot}
onShowMore={this._showMore}
/>
</div>
);
},
_navigate(action, newDate) {
let { view, date, onNavigate } = this.props;
switch (action){
case navigate.TODAY:
date = new Date()
break;
case navigate.DATE:
date = newDate
break;
default:
date = VIEWS[view].navigate(newDate || date, action)
}
onNavigate(date, view)
if (action === navigate.DATE)
this._viewNavigate(date)
},
_viewNavigate(nextDate){
let { view, date, culture } = this.props;
if (dates.eq(date, nextDate, view, localizer.startOfWeek(culture))) {
this._view(views.DAY)
}
},
_view(view){
if (view !== this.props.view && isValidView(view, this.props))
this.props.onView(view)
},
_select(event){
notify(this.props.onSelectEvent, event)
},
_selectSlot(slotInfo){
notify(this.props.onSelectSlot, slotInfo)
},
_headerClick(date){
let { view } = this.props;
if ( view === views.MONTH || view === views.WEEK)
this._view(views.day)
this._navigate(navigate.DATE, date)
}
});
export default uncontrollable(Calendar, { view: 'onView', date: 'onNavigate', selected: 'onSelectEvent' })