forked from tristen/datepickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
381 lines (330 loc) · 11.5 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
;(function(global) {
Date.prototype.getPickrDate = function(opts) { return (opts && opts.utc) ? this.getUTCDate() : this.getDate(); };
Date.prototype.getPickrFullYear = function(opts) { return (opts && opts.utc) ? this.getUTCFullYear() : this.getFullYear(); };
Date.prototype.getPickrMonth = function(opts) { return (opts && opts.utc) ? this.getUTCMonth() : this.getMonth(); };
Date.prototype.getPickrDay = function(opts) { return (opts && opts.utc) ? this.getUTCDay() : this.getDay(); };
var Datepickr = (function() {
var currentDate = new Date();
var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var buildCache = [];
var date = {
current: {
year: function(opts) { return currentDate.getPickrFullYear(opts); },
month: function(opts) { return currentDate.getPickrMonth(opts); },
day: function(opts) { return currentDate.getPickrDate(opts); }
},
month: {
string: function(month, months) {
var date = month;
return monthToStr(date, months);
},
numDays: function(month, year) {
// Check to see if february is a leap year.
// Otherwise, return the respective # of days.
return (month === 1 && !(year & 3) &&
(year % 1e2 || !(year % 4e2))) ?
29 : daysInMonth[month];
}
}
};
function calendarClick(e) {
var time = new Date(this.year, this.month).getTime();
switch (e.target.getAttribute('data-target')) {
case 'month-prev':
if (this.config.minDate && time <= this.config.minDate) return;
this.month--;
if (this.month < 0) {
this.year--;
this.month = 11;
}
rebuildCalendar.call(this);
break;
case 'month-next':
if (this.config.maxDate && time >= this.config.maxDate) return;
this.month++;
if (this.month > 11) {
this.year++;
this.month = 0;
}
rebuildCalendar.call(this);
break;
case 'day':
var today = new Date().getTime();
var d = new Date(this.year, this.month, e.target.textContent).getTime();
var c = e.target.classList;
if (this.config.halfDay) {
if (c.contains('halfday')) {
c.remove('halfday');
this.config.activeDays = this.config.activeDays.map(function(date) {
if (date[0] === d) date[1] = 1;
return date;
});
} else if (c.contains('active')) {
c.remove('active', 'halfday');
this.config.activeDays = this.config.activeDays.filter(function(date) {
return date[0] !== d;
});
} else {
c.add('active', 'halfday');
this.config.activeDays.push([d, 0.5]);
}
} else {
if (c.contains('active')) {
c.remove('active', 'halfday');
this.config.activeDays = this.config.activeDays.filter(function(date) {
return date[0] !== d;
});
} else {
if ( this.config.singleSelection ) {
var daylinks = this.element.querySelectorAll('a');
for (i = 0; i < daylinks.length; i++) {
daylinks[i].classList.remove('active', 'halfday');
}
this.config.activeDays = [[d, 1]];
} else {
this.config.activeDays.push([d, 1]);
}
c.add('active');
}
}
this.config.activeDays.sort(function(a, b) {
return a[0] - b[0];
});
this.callback(this.config.activeDays);
break;
}
}
function buildNode(nodeName, attributes, content) {
if (!(nodeName in buildCache)) {
buildCache[nodeName] = document.createElement(nodeName);
}
var element = buildCache[nodeName].cloneNode(false);
if (attributes) {
for (var attribute in attributes) {
element.setAttribute(attribute, attributes[attribute]);
}
}
if (content) {
if (typeof(content) === 'object') {
element.appendChild(content);
} else {
element.textContent = content;
}
}
return element;
}
function monthToStr(date, months) {
return months[date];
}
function roundDate(d) {
return new Date(d.getPickrFullYear(this.config), d.getPickrMonth(this.config), d.getPickrDate(this.config));
}
function isToday(year, month, day) {
return day === date.current.day(this.config) &&
month === date.current.month(this.config) &&
year === date.current.year(this.config);
}
function isPast(year, month, day) {
return new Date(year, month, day).getTime() < new Date().getTime();
}
function isFuture(year, month, day) {
return new Date(year, month, day).getTime() > new Date().getTime();
}
function isWeekend(year, month, day) {
var d = new Date(year, month, day).getPickrDay(this.config);
return d === 0 || d === 6;
}
function isOmitted(year, month, day) {
var d = new Date(year, month, day).getTime(),
is;
if (this.config.omitDays.length) {
this.config.omitDays.forEach(function(omitted) {
if (omitted === d) is = true;
});
}
return is;
}
function buildWeekdays(weekdays) {
var weekdayHtml = document.createDocumentFragment();
weekdays.forEach(function(weekday) {
weekdayHtml.appendChild(buildNode('th', {}, weekday));
});
return weekdayHtml;
}
function rebuildCalendar() {
while (this.calendarBody.hasChildNodes()) {
this.calendarBody.removeChild(this.calendarBody.lastChild);
}
var firstOfMonth = new Date(this.year, this.month, 1).getPickrDay(this.config),
numDays = date.month.numDays(this.month, this.year);
this.currentMonth.textContent = date.month.string(this.month, this.config.months) + ' ' + this.year;
this.calendarBody.appendChild(buildDays.call(this, firstOfMonth, numDays, this.month, this.year));
}
function buildCurrentMonth(config, month, year, months) {
return buildNode('strong', {
class: 'small'
}, date.month.string(month, months) + ' ' + year);
}
function buildMonths(config, month, year) {
var months = buildNode('div', {
'class': 'months'
});
var prevMonth = buildNode('a', {
'class': 'icon next button short quiet',
'data-target': 'month-next',
'href': '#'
});
var nextMonth = buildNode('a', {
'class': 'icon prev button short quiet',
'data-target': 'month-prev',
'href': '#'
});
months.appendChild(prevMonth);
months.appendChild(nextMonth);
return months;
}
function buildDays(firstOfMonth, numDays, month, year) {
var calendarBody = document.createDocumentFragment(),
row = buildNode('tr'),
dayCount = 0,
klass,
omit,
i;
// Print out previous month's 'days'
for (i = 1; i <= firstOfMonth; i++) {
row.appendChild(buildNode('td'));
dayCount++;
}
for (i = 1; i <= numDays; i++) {
omit = false;
// If we have reached the end of a week,
// wrap to the next line.
if (dayCount === 7) {
calendarBody.appendChild(row);
row = buildNode('tr');
dayCount = 0;
}
if (isToday.call(this, year, month, i)) {
if (this.config.omitWeekends && isWeekend.call(this, year, month, i)) {
klass = 'today quiet';
omit = true;
} else {
klass = 'today';
}
} else if (this.config.omitPast && isPast(year, month, i) ||
this.config.omitFuture && isFuture(year, month, i) ||
this.config.omitWeekends && isWeekend.call(this, year, month, i) ||
this.config.omitDays && this.config.omitDays.length && isOmitted.call(this, year, month, i)) {
klass = 'fill-light quiet';
omit = true;
} else {
klass = 'fill-light';
}
var self = this;
// If any dates were passed set day as active.
if (this.config.activeDays.length) {
this.config.activeDays.forEach(function(d) {
if (roundDate.call(self, new Date(d[0])).getTime() === new Date(year, month, i).getTime()) {
klass += (d[1] === 1) ? ' active' : ' halfday active';
}
});
}
row.appendChild(buildNode('td', {}, buildNode('a', {
'class': klass,
'data-target': (!omit) ? 'day' : false,
'href': '#'
}, i)));
dayCount++;
}
// If we haven't finished at the end of the week,
// start writing out the 'days' for the next month.
for (i = 1; i <= (7 - dayCount); i++) {
row.appendChild(buildNode('td'));
}
calendarBody.appendChild(row);
return calendarBody;
}
function buildCalendar() {
var firstOfMonth = new Date(this.config.startYear, this.config.startMonth, 1).getPickrDay(this.config);
var numDays = date.month.numDays(this.month, this.year);
var calendarDiv = buildNode('div', {
class: 'date-pickr'
});
this.currentMonth = buildCurrentMonth(this.config, this.month, this.year, this.config.months);
var months = buildMonths(this.config, this.month, this.year);
months.appendChild(this.currentMonth);
var calendar = buildNode('table', {
class: 'small'
}, buildNode('thead', {}, buildNode('tr', {
class: 'weekdays'
}, buildWeekdays(this.config.weekdays))));
this.calendarBody = buildNode('tbody');
this.calendarBody.appendChild(buildDays.call(this, firstOfMonth, numDays, this.month, this.year));
calendar.appendChild(this.calendarBody);
calendarDiv.appendChild(months);
calendarDiv.appendChild(calendar);
this.element.appendChild(calendarDiv);
calendarDiv.addEventListener('click', function(e) {
e.preventDefault();
calendarClick.call(this, e);
}.bind(this));
return calendarDiv;
}
return function(el, cb, opts) {
var datepickr = {};
this.element = el;
this.callback = cb;
this.config = {
utc: false,
weekdays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
minDate: null,
maxDate: null,
halfDay: false,
omitPast: false,
omitFuture: false,
omitWeekends: false,
omitDays: [],
activeDays: [],
singleSelection: false
};
this.config.startYear = date.current.year(this.config);
this.config.startMonth = date.current.month(this.config);
if (opts) {
for (var key in opts) {
if (this.config.hasOwnProperty(key)) {
this.config[key] = opts[key];
}
}
}
var self = this;
// Normalize any active days by rounding them.
if (this.config.activeDays.length) {
this.config.activeDays = this.config.activeDays.map(function(d) {
return [roundDate.call(self, new Date(d[0])).getTime(), d[1]];
});
}
// Normalize any omitted days by rounding them.
if (this.config.omitDays.length) {
this.config.omitDays = this.config.omitDays.map(function(d) {
return roundDate.call(self, new Date(d)).getTime();
});
}
datepickr.options = function(opts) {
if (opts) {
for (var key in opts) {
if (this.config.hasOwnProperty(key)) {
this.config[key] = opts[key];
}
}
}
}.bind(this);
this.year = this.config.startYear;
this.month = this.config.startMonth;
buildCalendar.call(this);
return datepickr;
};
})();
global.Datepickr = Datepickr;
if (typeof module !== 'undefined' && module.exports) module.exports = Datepickr;
})(this);