Repository navigation
Expand file tree
/
Copy pathdatepicker.js
More file actions
535 lines (435 loc) · 16.4 KB
/
Copy pathdatepicker.js
File metadata and controls
535 lines (435 loc) · 16.4 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
(function(root, returnDatepicker) {
if (typeof exports === 'object') return module.exports = returnDatepicker();
if (typeof define === 'function' && define.amd) return define(function() {return returnDatepicker()});
return root.datepicker = returnDatepicker();
})(this, function() {
'use strict';
const months = {
0: 'January',
1: 'February',
2: 'March',
3: 'April',
4: 'May',
5: 'June',
6: 'July',
7: 'August',
8: 'September',
9: 'October',
10: 'November',
11: 'December'
};
const days = {
1: 'Sun',
2: 'Mon',
3: 'Tue',
4: 'Wed',
5: 'Thu',
6: 'Fri',
0: 'Sat'
};
const sides = {
t: 'top',
r: 'right',
b: 'bottom',
l: 'left'
};
const datepickers = [];
/*
*
*/
function Datepicker(selector, options) {
const el = document.querySelector(selector);
const parent = el.parentElement;
options = sanitizeOptions(options || defaults(), el, selector);
const calendar = document.createElement('div');
const startDate = options.startDate || new Date(new Date().toLocaleDateString());
const dateSelected = options.dateSelected;
const noPosition = selector === 'body' || selector === 'html';
const instance = {
// The calendar will be positioned relative to this element (except when 'body' or 'html').
el: el,
parent: parent,
// Indicates whether to use a <input> element or not as the calendars anchor .
nonInput: el.nodeName !== 'INPUT',
// Flag indicating if `el` is 'body' or 'html' for `calculatePosition`.
noPosition: noPosition,
// Calendar position relative to `el`.
position: noPosition ? false : options.position,
// Date obj used to indicate what month to start the calendar on.
startDate: startDate,
// Starts the calendar with a date selected.
dateSelected: dateSelected,
// Low end of selectable dates.
minDate: options.minDate,
// High end of selectable dates.
maxDate: options.maxDate,
// Disabled the ability to select days on the weekend.
noWeekends: !!options.noWeekends,
// The element our calendar is constructed in.
calendar: calendar,
// Month of `startDate` or `dateSelected`.
currentMonth: (startDate || dateSelected).getMonth(),
// Month name in plain english.
currentMonthName: months[(startDate || dateSelected).getMonth()],
// Year of `startDate` or `dateSelected`.
currentYear: (startDate || dateSelected).getFullYear(),
// Method to programatically set the calendar's date.
setDate: setDate,
// Method that removes the calendar from the DOM along with associated events.
remove: remove,
// Callback fired when a date is selected.
onSelect: options.onSelect,
// Callback fired when the calendar is shown.
onShow: options.onShow,
// Callback fired when the calendar is hidden.
onHide: options.onHide,
// Callback fired when the month is changed.
onMonthChange: options.onMonthChange
};
// Populate the <input> field or set attributes on the `el`.
if (dateSelected) setElValues(el, instance);
calendar.classList.add('datepicker');
calendar.classList.add('hidden');
datepickers.push(el);
calendarHtml(startDate || dateSelected, instance);
classChangeObserver(calendar, instance);
window.addEventListener('click', oneHandler.bind(instance));
window.addEventListener('focusin', oneHandler.bind(instance));
window.addEventListener('keyup', oneHandler.bind(instance));
if (getComputedStyle(parent).position === 'static') {
instance.parent = parent;
parent.style.position = 'relative';
}
parent.appendChild(calendar);
return instance;
}
/*
* Will run checks on the provided options object to ensure correct types.
* Returns an options object if everything checks out.
*/
function sanitizeOptions(options, el, selector) {
// An invalid selected has been provided.
if (!el) throw new Error(`"${selector}" doesn't reference any element on the page.`);
// Check if the provided element already has a datepicker attached.
if (datepickers.includes(el)) throw new Error('A datepicker already exists on that element.');
let {position, maxDate, minDate, dateSelected} = options;
// Ensure the accuracy of `options.position` & call `establishPosition`.
if (position) {
const found = ['tr', 'tl', 'br', 'bl'].some(dir => position === dir);
const msg = '"options.position" needs to be one of the following: tl, tr, bl, or br.';
if (!found) throw new Error(msg);
options.position = establishPosition(position);
} else {
options.position = establishPosition('bl');
}
// Check that various options have been provided a JavaScript Date object.
// If so, strip the time from those dates (for accurate future comparisons).
['startDate', 'dateSelected', 'minDate', 'maxDate'].forEach(date => {
if (options[date]) {
if (!dateCheck(options[date]) || isNaN(+options[date])) {
throw new TypeError(`"options.${date}" needs to be a valid JavaScript Date object.`);
}
// Strip the time from the date.
options[date] = new Date(options[date].toLocaleDateString());
}
});
options.startDate = options.startDate || options.dateSelected;
if (maxDate < minDate) {
throw new Error('"maxDate" in options is less than "minDate".');
}
if (dateSelected) {
if (minDate > dateSelected) {
throw new Error('"dateSelected" in options is less than "minDate".');
}
if (maxDate < dateSelected) {
throw new Error('"dateSelected" in options is greater than "maxDate".');
}
}
// Callbacks.
['onSelect', 'onShow', 'onHide', 'onMonthChange'].forEach(fxn => {
options[fxn] = typeof options[fxn] === 'function' && options[fxn];
});
return options;
}
/*
* Returns an object containing all the default settings.
*/
function defaults() {
return {
startDate: new Date(new Date().toLocaleDateString()),
position: 'bl'
};
}
/*
* Returns an object representing the position of the calendar
* relative to the calendars <input> element.
*/
function establishPosition(position) {
const obj = {};
obj[sides[position[0]]] = 1;
obj[sides[position[1]]] = 1;
return obj;
}
/*
* Populates `calendar.innerHTML` with the contents
* of the calendar controls and month.
*/
function calendarHtml(date, instance) {
const controls = createControls(date, instance);
const month = createMonth(date, instance);
instance.calendar.innerHTML = controls + month;
}
/*
* Creates the calendar controls.
* Returns a string representation of DOM elements.
*/
function createControls(date) {
return [
'<div class="controls">',
'<div class="arrow left"></div>',
'<div class="month-year">',
`<span class="month">${months[date.getMonth()]}</span>`,
`<span class="year">${date.getFullYear()}</span>`,
'</div>',
'<div class="arrow right"></div>',
'</div>'
].join('');
}
/*
* Creates the calendar month structure.
* Returns a string representation of DOM elements.
*/
function createMonth(date, instance) {
const {
minDate,
maxDate,
dateSelected,
currentYear,
currentMonth,
noWeekends
} = instance;
// Same year, same month?
const today = new Date();
const isThisMonth = today.toJSON().slice(0, 7) === date.toJSON().slice(0, 7);
// Calculations for the squares on the calendar.
const copy = new Date(new Date(date).setDate(1));
const offset = copy.getDay(); // Preceding empty squares.
copy.setMonth(copy.getMonth() + 1);
copy.setDate(0); // Last day in the current month.
const daysInMonth = copy.getDate(); // Squares with a number.
// Will contain string representations of HTML for the squares.
const calendarSquares = [];
// Fancy calculations for the total # of squares.
let totalSquares = ((offset + daysInMonth) / 7 | 0) * 7;
totalSquares += (offset + daysInMonth) % 7 ? 7 : 0;
for (let i = 1; i <= totalSquares; i++) {
let weekday = days[i % 7];
let num = i - offset;
let otherClass = '';
let span = `<span class="num">${num}</span>`;
let thisDay = new Date(currentYear, currentMonth, num);
let isEmpty = num < 1 || num > daysInMonth;
// Empty squares.
if (isEmpty) {
otherClass = 'empty';
span = '';
// Disabled & current squares.
} else {
let disabled = (minDate && thisDay < minDate) || (maxDate && thisDay > maxDate);
const weekend = weekday === 'Sat' || weekday === 'Sun';
const currentValidDay = isThisMonth && !disabled && num === today.getDate();
disabled = disabled || (noWeekends && weekend);
otherClass = disabled ? 'disabled' : currentValidDay ? 'current' : '';
}
// Currently selected day.
if (+thisDay === +dateSelected && !isEmpty) otherClass += ' active';
calendarSquares.push(`<div class="square num ${weekday} ${otherClass}">${span}</div>`);
}
// Add the header row of days of the week.
const daysAndSquares = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'].map(day => {
return `<div class="square day">${day}</div>`;
}).concat(calendarSquares);
// Throw error...
// The # of squares on the calendar should ALWAYS be a multiple of 7.
if (daysAndSquares.length % 7 !== 0 ) {
const msg = 'Calendar not constructed properly. The # of squares should be a multiple of 7.';
throw new Error(msg);
}
// Wrap it all in a tidy div.
daysAndSquares.unshift('<div class="squares">');
daysAndSquares.push('</div>');
return daysAndSquares.join('');
}
/*
* Highlights the selected date.
* Calls `setElValues`.
*/
function selectDay(target, instance) {
const { currentMonth, currentYear, calendar, el } = instance;
const active = calendar.querySelector('.active');
const num = target.textContent;
// Keep track of the currently selected date.
instance.dateSelected = new Date(currentYear, currentMonth, num);
// Re-establish the active (highlighted) date.
if (active) active.classList.remove('active');
target.classList.add('active');
// Populate the <input> field (or not) with a readble value
// and store the individual date values as attributes.
setElValues(el, instance);
// Hide the calendar after a day has been selected.
instance.calendar.classList.add('hidden');
if (instance.onSelect) instance.onSelect(instance);
}
/*
* Populates the <input> fields with a readble value
* and stores the individual date values as attributes.
*/
function setElValues(el, instance) {
if (instance.nonInput) return;
el.value = instance.dateSelected.toDateString();
}
/*
* Updates `this.currentMonth` & `this.currentYear` based on right or left arrows.
* Creates a `newDate` based on the updated month & year.
* Calls `calendarHtml` with the updated date.
*/
function changeMonth(classList, instance) {
instance.currentMonth += classList.contains('right') ? 1 : -1;
if (instance.currentMonth === 12) {
instance.currentMonth = 0;
instance.currentYear++
} else if (instance.currentMonth === -1) {
instance.currentMonth = 11;
instance.currentYear--;
}
const newDate = new Date(instance.currentYear, instance.currentMonth, 1);
calendarHtml(newDate, instance);
instance.currentMonthName = months[instance.currentMonth];
instance.onMonthChange && instance.onMonthChange(instance);
}
/*
* Sets the `style` attribute on the calendar after doing calculations.
*/
function calculatePosition(instance) {
// Don't position the calendar in reference to the <body> or <html> elements.
if (instance.noPosition) return;
const {el, calendar, position, parent} = instance;
const {top, right} = position;
const parentRect = parent.getBoundingClientRect();
const elRect = el.getBoundingClientRect();
const calRect = calendar.getBoundingClientRect();
const offset = elRect.top - parentRect.top + parent.scrollTop;
const style = `
top:${offset - (top ? calRect.height : (elRect.height * -1))}px;
left:${elRect.left - parentRect.left + (right ? elRect.width - calRect.width : 0)}px;
`;
calendar.setAttribute('style', style);
}
/*
* Method that programatically sets the date.
*/
function setDate(date) {
if (!dateCheck(date)) throw new TypeError('`setDate` needs a JavaScript Date object.');
date = new Date(date.toLocaleDateString()); // Remove the time.
this.currentYear = date.getFullYear();
this.currentMonth = date.getMonth();
this.currentMonthName = months[date.getMonth()];
this.dateSelected = date;
setElValues(this.el, this);
calendarHtml(date, this);
}
function dateCheck(date) {
return ({}).toString.call(date) === '[object Date]';
}
/*
* Removes all event listeners added by the constructor.
* Removes the current instance from the array of instances.
*/
function remove() {
window.removeEventListener('click', oneHandler);
window.removeEventListener('focusin', oneHandler);
window.removeEventListener('keyup', oneHandler);
this.calendar.remove();
this.observer.disconnect(); // Stop the mutationObserver. https://goo.gl/PgFCEr
// Remove styling done to the parent element.
if (this.calendar.hasOwnProperty('parentStyle')) this.parent.style.position = '';
// Remove this datepicker's `el` from the list.
const index = datepickers.indexOf(this.el);
if (index > -1) datepickers.splice(index, 1);
}
/////////////////////
// EVENT FUNCTIONS //
/////////////////////
/*
* Mutation observer
* 1. Will trigger the user-provided `onShow` callback when the calendar is shown.
* 2. Will call `calculatePosition` when calendar is shown.
*/
function classChangeObserver(calendar, instance) {
instance.observer = new MutationObserver((mutations, thing) => {
// Calendar has been shown.
if (mutations[0].oldValue.includes('hidden')) {
calculatePosition(instance);
instance.onShow && instance.onShow(instance);
// Calendar has been hidden.
} else {
instance.onHide && instance.onHide(instance);
}
});
instance.observer.observe(calendar, {
attributes: 1,
attributeFilter: ['class'],
attributeOldValue: 1
});
}
/*
* Handles `click` events when the calendar's `el` is an <input>.
* Handles `focusin` events for all other types of `el`'s.
*/
function oneHandler(e) {
// Add `e.path` if it doesn't exist.
if (!e.path) {
let node = e.target;
e.path = [];
while (node !== document) {
e.path.push(node);
node = node.parentNode;
}
}
const calClasses = this.calendar.classList;
const hidden = calClasses.contains('hidden');
// Only pay attention to `keyup` events if the character is the TAB key.
if (e.type === 'keyup' && e.keyCode !== 9) return;
// Only pay attention to `focusin` events if the calendar's el is an <input>.
// `focusin` bubbles, `focus` does not.
if (e.type === 'focusin') return e.target === this.el && calClasses.remove('hidden');
// Calendar's el is 'html' or 'body'.
// Anything but the calendar was clicked.
if (this.noPosition) {
e.path.includes(this.calendar) ? calendarClicked(e, this) : calClasses.toggle('hidden');
// When the calendar is hidden...
} else if (hidden) {
e.target === this.el && calClasses.remove('hidden');
// Clicked on the calendar.
} else if (e.path.includes(this.calendar)) {
calendarClicked(e, this);
} else {
e.target !== this.el && calClasses.add('hidden');
}
function calendarClicked(e, instance) {
const classList = e.target.classList;
// A number was clicked.
if (classList.contains('num')) {
const target = e.target.nodeName === 'SPAN' ? e.target.parentNode : e.target;
const doNothing = ['disabled', 'active', 'empty'].some(name => {
return target.classList.contains(name);
});
!doNothing && selectDay(target, instance);
// Month arrows were clicked.
} else if (classList.contains('arrow')) {
changeMonth(classList, instance);
}
}
}
return Datepicker;
});