/
Calendar.js
208 lines (173 loc) · 8.49 KB
/
Calendar.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactAddonsCssTransitionGroup = require('react-addons-css-transition-group');
var _reactAddonsCssTransitionGroup2 = _interopRequireDefault(_reactAddonsCssTransitionGroup);
var _animations = require('../animations');
var _time = require('../utils/time.js');
var _time2 = _interopRequireDefault(_time);
var _utils = require('../utils/utils.js');
var _utils2 = _interopRequireDefault(_utils);
var _CalendarMonth = require('./CalendarMonth.js');
var _CalendarMonth2 = _interopRequireDefault(_CalendarMonth);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var DIRECTION_STEPS = { left: -1, right: 1 };
var factory = function factory(IconButton) {
var Calendar = function (_Component) {
_inherits(Calendar, _Component);
function Calendar() {
var _Object$getPrototypeO;
var _temp, _this, _ret;
_classCallCheck(this, Calendar);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Calendar)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = {
viewDate: _this.props.selectedDate
}, _this.handleDayClick = function (day) {
_this.props.onChange(_time2.default.setDay(_this.state.viewDate, day), true);
}, _this.handleYearClick = function (event) {
var year = parseInt(event.target.id);
var viewDate = _time2.default.setYear(_this.props.selectedDate, year);
_this.setState({ viewDate: viewDate });
_this.props.onChange(viewDate, false);
}, _this.handleKeys = function (e) {
var selectedDate = _this.props.selectedDate;
if (e.which === 37 || e.which === 38 || e.which === 39 || e.which === 40 || e.which === 13) e.preventDefault();
switch (e.which) {
case 13:
_this.props.handleSelect();break; // enter
case 37:
_this.handleDayArrowKey(_time2.default.addDays(selectedDate, -1));break; // left
case 38:
_this.handleDayArrowKey(_time2.default.addDays(selectedDate, -7));break; // up
case 39:
_this.handleDayArrowKey(_time2.default.addDays(selectedDate, 1));break; // right
case 40:
_this.handleDayArrowKey(_time2.default.addDays(selectedDate, 7));break; // down
default:
break;
}
}, _this.handleDayArrowKey = function (date) {
_this.setState({ viewDate: date });
_this.props.onChange(date, false);
}, _this.changeViewMonth = function (event) {
var direction = event.target.id;
_this.setState({
direction: direction,
viewDate: _time2.default.addMonths(_this.state.viewDate, DIRECTION_STEPS[direction])
});
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Calendar, [{
key: 'componentWillMount',
value: function componentWillMount() {
document.body.addEventListener('keydown', this.handleKeys);
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
if (this.refs.activeYear) {
this.scrollToActive();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
document.body.removeEventListener('keydown', this.handleKeys);
}
}, {
key: 'scrollToActive',
value: function scrollToActive() {
this.refs.years.scrollTop = this.refs.activeYear.offsetTop - this.refs.years.offsetHeight / 2 + this.refs.activeYear.offsetHeight / 2;
}
}, {
key: 'renderYears',
value: function renderYears() {
var _this2 = this;
return _react2.default.createElement(
'ul',
{ 'data-react-toolbox': 'years', ref: 'years', className: this.props.theme.years },
_utils2.default.range(1900, 2100).map(function (year) {
return _react2.default.createElement('li', {
children: year,
className: year === _this2.state.viewDate.getFullYear() ? _this2.props.theme.active : '',
id: year,
key: year,
onClick: _this2.handleYearClick,
ref: year === _this2.state.viewDate.getFullYear() ? 'activeYear' : undefined
});
})
);
}
}, {
key: 'renderMonths',
value: function renderMonths() {
var theme = this.props.theme;
var animation = this.state.direction === 'left' ? _animations.SlideLeft : _animations.SlideRight;
return _react2.default.createElement(
'div',
{ 'data-react-toolbox': 'calendar' },
_react2.default.createElement(IconButton, { id: 'left', className: theme.prev, icon: 'chevron_left', onClick: this.changeViewMonth }),
_react2.default.createElement(IconButton, { id: 'right', className: theme.next, icon: 'chevron_right', onClick: this.changeViewMonth }),
_react2.default.createElement(
_reactAddonsCssTransitionGroup2.default,
{ transitionName: animation, transitionEnterTimeout: 350, transitionLeaveTimeout: 350 },
_react2.default.createElement(_CalendarMonth2.default, {
key: this.state.viewDate.getMonth(),
locale: this.props.locale,
maxDate: this.props.maxDate,
minDate: this.props.minDate,
onDayClick: this.handleDayClick,
selectedDate: this.props.selectedDate,
sundayFirstDayOfWeek: this.props.sundayFirstDayOfWeek,
theme: this.props.theme,
viewDate: this.state.viewDate
})
)
);
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
{ className: this.props.theme.calendar },
this.props.display === 'months' ? this.renderMonths() : this.renderYears()
);
}
}]);
return Calendar;
}(_react.Component);
Calendar.propTypes = {
display: _react.PropTypes.oneOf(['months', 'years']),
handleSelect: _react.PropTypes.func,
locale: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
maxDate: _react.PropTypes.object,
minDate: _react.PropTypes.object,
onChange: _react.PropTypes.func,
selectedDate: _react.PropTypes.object,
sundayFirstDayOfWeek: _react2.default.PropTypes.bool,
theme: _react.PropTypes.shape({
active: _react.PropTypes.string,
calendar: _react.PropTypes.string,
next: _react.PropTypes.string,
prev: _react.PropTypes.string,
years: _react.PropTypes.string
}),
viewDate: _react.PropTypes.object
};
Calendar.defaultProps = {
display: 'months',
selectedDate: new Date()
};
return Calendar;
};
exports.default = factory;