-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (237 loc) · 6.22 KB
/
index.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
var moment = require('moment');
var $ = require('jquery');
var bind = require('bind');
var Emitter = require('emitter');
function Calendar(options) {
Emitter.call(this);
this.el = $(options.template || this.template);
this.el.on('click', '.js-next', bind(this, 'next') );
this.el.on('click', '.js-previous', bind(this, 'previous') );
this.el.on('click', '.js-today', bind(this, 'today') );
this.el.on('click', '.js-select', bind(this, 'onSelect') );
this.title = this.el.find(this.titleSelector);
this.body = this.el.find(this.bodySelector);
this.current = this.selected = moment();
this.render();
};
/**
* Allows the calendar to emit events
*/
Emitter(Calendar.prototype);
/**
* Class given to the selected day element
* @type {String}
*/
Calendar.prototype.selectedClass = 'is-selected';
/**
* Class given to the element for today
* @type {String}
*/
Calendar.prototype.todayClass = 'is-today';
/**
* Class given to disabled days. These are days
* that are rendered on calendar but aren't in
* the current month.
* @type {String}
*/
Calendar.prototype.disabledClass = 'is-disabled';
/**
* Format for the title of the calendar
* @type {String}
*/
Calendar.prototype.titleFormat = 'MMMM YYYY';
/**
* Format of the day returned when calling this.date()
* @type {String}
*/
Calendar.prototype.format = null;
/**
* Selector that matches the element for the calendar title
* @type {String}
*/
Calendar.prototype.titleSelector = '.js-title';
/**
* Selector that matches the body of the calendar
* @type {String}
*/
Calendar.prototype.bodySelector = '.js-body';
/**
* The current selected date
* @type {Moment}
*/
Calendar.prototype.selected = null;
/**
* The current rendered date
* @type {Moment}
*/
Calendar.prototype.current = null;
/**
* Select today and render todays month
* @return {Calendar}
*/
Calendar.prototype.today = function() {
this.view();
this.select();
return this;
};
/**
* Render the previous month
* @return {Calendar}
*/
Calendar.prototype.previous = function() {
this.view(moment(this.current).subtract('months', 1));
return this;
};
/**
* Render the next month
* @return {Calendar}
*/
Calendar.prototype.next = function() {
this.view(moment(this.current).add('months', 1));
return this;
};
/**
* Select a date on the calendar
* @param {Moment} date Moment instance
* @return {Calendar}
*/
Calendar.prototype.select = function(date) {
this.selected = moment(date);
this.emit('change', this.date());
return this;
};
/**
* View a date on the calendar but don't select it
* @param {String} date Any date format understood by Moment
* @return {Calendar}
*/
Calendar.prototype.view = function(date) {
this.current = moment(date);
this.render();
return this;
};
/**
* Get the current selected date. Uses this.format
* to determine the format to return the date by
* default. If this is null, it will uses Moment's
* default date format
* @return {Moment}
*/
Calendar.prototype.date = function() {
return this.selected.format(this.format);
};
/**
* Check if 2 dates are actually the same day
* @param {Moment} a
* @param {Moment} b
* @return {Boolean}
*/
Calendar.prototype.isSameDay = function(a, b) {
return a.diff(b,'days') === 0;
};
/**
* See if two dates are in the same month
* @param {Moment} a
* @param {Moment} b
* @return {Boolean}
*/
Calendar.prototype.isSameMonth = function(a, b) {
return this.isSameDay(moment(a).date(1), moment(b).date(1));
};
/**
* Get the date that the calendar needs to start rendering
* from. This will include the previous month if this month
* starts on any day other than Sunday
* @param {Moment} date
* @return {Moment}
*/
Calendar.prototype.getStartDate = function(date) {
var lastMonth = moment(date).subtract('months', 1);
var daysInLastMonth = lastMonth.daysInMonth();
var inactiveBeforeDays = moment(date).date(1).day() - 1;
if( inactiveBeforeDays > 0 ) {
return moment(lastMonth).date( daysInLastMonth - inactiveBeforeDays );
}
else {
return moment(date).date(1);
}
};
/**
* Gets the element for a single day given the data
* for a the day
* @param {Object} data Data describing that day
* @return {Element}
*/
Calendar.prototype.renderDay = function(data) {
var day = $('<span />');
day.toggleClass(this.selectedClass, data.isSelected);
day.toggleClass(this.disabledClass, data.isDisabled);
day.toggleClass(this.todayClass, data.isToday);
day.attr('data-date', data.date);
day.addClass('js-select');
day.text(data.day);
return day[0];
};
/**
* Get the calendar title
* @return {[type]} [description]
*/
Calendar.prototype.renderTitle = function() {
var title = this.current.format(this.titleFormat);
this.title.text(title);
return this;
};
/**
* Get the calendar body by looping over each
* day within that month and creating an element
* for it. This method should return an element
* that can be placed inside the body of the calendar
* @return {Calendar}
*/
Calendar.prototype.renderBody = function() {
var fragment = document.createDocumentFragment();
var current = this.getStartDate(this.current);
var today = moment();
for (var i = 0; i <= 41; i++) {
fragment.appendChild(this.renderDay({
day: current.date(),
date: current.format(),
isSelected: this.isSameDay(current, this.selected),
isDisabled: !this.isSameMonth(current, this.current),
isToday: this.isSameDay(current, today)
}));
current.add('days', 1);
};
this.body.empty().append(fragment);
return this;
};
/**
* Update the calendar
* @return {Calendar}
*/
Calendar.prototype.render = function() {
this.renderBody();
this.renderTitle();
return this;
};
/**
* Remove the calendar from the DOM and remove all events
* @return {Calendar}
*/
Calendar.prototype.remove = function() {
this.el.off();
this.el.remove();
this._callbacks = {};
return this;
};
/**
* When a day element is clicked, select that day using data attributes
* This could have been done with views and whatnot, but since we only
* need this one bit of data, this is the easiest way
* @param {Event} event
* @return {void}
*/
Calendar.prototype.onSelect = function(event) {
this.select(event.currentTarget.getAttribute('data-date'));
};
module.exports = Calendar;