-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·222 lines (166 loc) · 5.72 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
'use strict';
var Emitter = require('emitter')
, inGroupsOf = require('in-groups-of')
, templateEl = document.createElement('table')
;
// Setup template
templateEl.className = 'calendar-table';
templateEl.innerHTML = '<thead></thead><tbody></tbody>';
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
, days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
, daysInMonthArr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
;
/**
* Get days in `month` for `year`.
*
* @param {Number} month
* @param {Number} year
* @return {Number}
* @api private
*/
function daysInMonth(month, year) {
if (month === 1 && isLeapYear(year)) return 29;
return daysInMonthArr[month];
}
/**
* Check if `year` is a leap year.
*
* @param {Number} year
* @return {Boolean}
* @api private
*/
function isLeapYear(year) {
return ((0 === year % 4) && (0 !== year % 100))
|| (0 === year % 400)
|| (0 === year)
;
}
function dateAttr(date) {
var arr = arguments.length === 1 ? [ date.getFullYear(), date.getMonth() + 1, date.getDate() ] : [ arguments[0], arguments[1] + 1, arguments[2] ];
return arr.join('/');
}
function dayHTML(year, month, day, className) {
return '<td data-date="' + dateAttr(year, month, day) + '"' + (className !== undefined ? ' class="' + className + '"' : '') + '><div class="day">' + day + '</div></td>';
}
function Calendar(options) {
Emitter.call(this);
this.options = options || {};
// construct el
this.el = templateEl.cloneNode(true);
this._head = this.el.querySelector('thead');
this._body = this.el.querySelector('tbody');
this.renderHead();
this.setDate(this.options.date || new Date());
}
/**
* Expose helper methods
*/
Calendar.isLeapYear = isLeapYear;
Calendar.daysInMonth = daysInMonth;
Calendar.weekDays = days;
Calendar.months = months;
Calendar.dateAttr = dateAttr;
Emitter(Calendar.prototype);
Calendar.prototype.renderHead = function (len) {
var self = this;
this._head.innerHTML = '<tr class="subheading">' + days.map(function(day){
return '<th>' + day.slice(0, self.options.headLength || 2) + '</th>';
}).join('') + '</tr>';
return self.emit('render head');
};
Calendar.prototype.render = function () {
var start = new Date(this._date)
, month = start.getMonth()
, year = start.getFullYear()
, inMonth = daysInMonth(month, year)
;
start.setDate(1); // Should already be the first day if using setter
var beforeDate = new Date(start)
, daysBefore = start.getDay()
, total = 7 * Math.ceil((inMonth + daysBefore) / 7)
, afterDate = new Date(start)
, daysAfter = total - (inMonth + daysBefore)
, afterDay = 0
;
beforeDate.setMonth(month - 1);
afterDate.setMonth(month + 1);
var beforeDays = daysInMonth(beforeDate.getMonth(), beforeDate.getFullYear())
, afterDays = daysInMonth(afterDate.getMonth(), afterDate.getFullYear())
, cells = []
, rows
, day = 0
;
while (daysBefore--) {
cells.unshift(dayHTML(beforeDate.getFullYear(), beforeDate.getMonth(), beforeDays--, 'before-day'));
}
while (day++ < inMonth) {
cells.push(dayHTML(year, month, day));
}
while (daysAfter--) {
cells.push(dayHTML(afterDate.getFullYear(), afterDate.getMonth(), ++afterDay, 'after-day'))
}
rows = inGroupsOf(cells, 7);
this._body.innerHTML = rows.map(function (row) {
return '<tr>' + row.join('') + '</tr>';
}).join('');
return this.emit('render', this._date);
};
Calendar.prototype.setDate = function (date) {
// Private instance of the supplied date. Will parse strings if passed
date = new Date(date);
date.setDate(1); // Prevent overflowing month
this._date = date;
this
.emit('change year', date.getFullYear())
.emit('change month', date.getMonth())
.emit('change', date)
;
return this.render();
};
Calendar.prototype.setMonth = function (month) {
var prevYear = this._date.getFullYear()
, year
;
this._date.setMonth(month);
year = this._date.getFullYear();
if (prevYear !== year) {
this.emit('change year', year);
}
this
.emit('change month', this._date.getMonth())
.emit('change', this._date)
;
return this.render();
};
Calendar.prototype.setYear = function (year) {
this._date.setFullYear(year);
this
.emit('change year', year)
.emit('change', this._date)
;
return this.render();
};
Calendar.prototype.nextMonth = function () {
return this.setMonth(this._date.getMonth() + 1);
};
Calendar.prototype.prevMonth = function () {
return this.setMonth(this._date.getMonth() - 1);
};
Calendar.prototype.nextYear = function () {
return this.setYear(this._date.getFullYear() + 1);
};
Calendar.prototype.prevYear = function () {
return this.setYear(this._date.getFullYear() - 1);
};
Calendar.prototype.getElementByDate = function (date) {
date = date instanceof Date ? dateAttr(date) : date;
return this._body.querySelector('td[data-date="' + date + '"]');
};
Calendar.prototype.initEmit = function () {
this
.emit('change year', this._date.getFullYear())
.emit('change month', this._date.getMonth())
.emit('change', this._date)
;
};
module.exports = Calendar;