flatpickr
Version:
A lightweight, powerful javascript datetime picker
1,192 lines (1,179 loc) • 89.9 kB
JavaScript
/* flatpickr v4.0.1, @license MIT */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.flatpickr = factory());
}(this, (function () { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var __assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
function compareDates(date1, date2, timeless) {
if (timeless !== false) {
return (new Date(date1.getTime()).setHours(0, 0, 0, 0) -
new Date(date2.getTime()).setHours(0, 0, 0, 0));
}
return date1.getTime() - date2.getTime();
}
var monthToStr = function (monthNumber, shorthand, locale) { return locale.months[shorthand ? "shorthand" : "longhand"][monthNumber]; };
var getWeek = function (givenDate) {
var onejan = new Date(givenDate.getFullYear(), 0, 1);
return Math.ceil(((givenDate.getTime() - onejan.getTime()) / 86400000 +
onejan.getDay() +
1) /
7);
};
var duration = {
DAY: 86400000,
};
var defaults = {
_disable: [],
_enable: [],
allowInput: false,
altFormat: "F j, Y",
altInput: false,
altInputClass: "form-control input",
animate: window && window.navigator.userAgent.indexOf("MSIE") === -1,
ariaDateFormat: "F j, Y",
clickOpens: true,
closeOnSelect: true,
conjunction: ", ",
dateFormat: "Y-m-d",
defaultHour: 12,
defaultMinute: 0,
defaultSeconds: 0,
disable: [],
disableMobile: false,
enable: [],
enableSeconds: false,
enableTime: false,
getWeek: getWeek,
hourIncrement: 1,
ignoredFocusElements: [],
inline: false,
locale: "default",
minuteIncrement: 5,
mode: "single",
nextArrow: "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 17 17'><g></g><path d='M13.207 8.472l-7.854 7.854-0.707-0.707 7.146-7.146-7.146-7.148 0.707-0.707 7.854 7.854z' /></svg>",
noCalendar: false,
onChange: [],
onClose: [],
onDayCreate: [],
onDestroy: [],
onKeyDown: [],
onMonthChange: [],
onOpen: [],
onParseConfig: [],
onReady: [],
onValueUpdate: [],
onYearChange: [],
plugins: [],
position: "auto",
positionElement: undefined,
prevArrow: "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 17 17'><g></g><path d='M5.207 8.471l7.146 7.147-0.707 0.707-7.853-7.854 7.854-7.853 0.707 0.707-7.147 7.146z' /></svg>",
shorthandCurrentMonth: false,
static: false,
time_24hr: false,
weekNumbers: false,
wrap: false,
};
var english = {
weekdays: {
shorthand: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
longhand: [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
],
},
months: {
shorthand: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
],
longhand: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
},
daysInMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
firstDayOfWeek: 0,
ordinal: function (nth) {
var s = nth % 100;
if (s > 3 && s < 21)
return "th";
switch (s % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
},
rangeSeparator: " to ",
weekAbbreviation: "Wk",
scrollTitle: "Scroll to increment",
toggleTitle: "Click to toggle",
amPM: ["AM", "PM"],
};
var pad = function (number) { return ("0" + number).slice(-2); };
var int = function (bool) { return (bool === true ? 1 : 0); };
function debounce(func, wait, immediate) {
if (immediate === void 0) { immediate = false; }
var timeout;
return function () {
var context = this, args = arguments;
timeout !== null && clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
if (!immediate)
func.apply(context, args);
}, wait);
if (immediate && !timeout)
func.apply(context, args);
};
}
var arrayify = function (obj) {
return obj instanceof Array ? obj : [obj];
};
function mouseDelta(e) {
var delta = e.wheelDelta || -e.deltaY;
return delta >= 0 ? 1 : -1;
}
function toggleClass(elem, className, bool) {
if (bool === true)
return elem.classList.add(className);
elem.classList.remove(className);
}
function createElement(tag, className, content) {
var e = window.document.createElement(tag);
className = className || "";
content = content || "";
e.className = className;
if (content !== undefined)
e.textContent = content;
return e;
}
function clearNode(node) {
while (node.firstChild)
node.removeChild(node.firstChild);
}
function findParent(node, condition) {
if (condition(node))
return node;
else if (node.parentNode)
return findParent(node.parentNode, condition);
return undefined;
}
function createNumberInput(inputClassName) {
var wrapper = createElement("div", "numInputWrapper"), numInput = createElement("input", "numInput " + inputClassName), arrowUp = createElement("span", "arrowUp"), arrowDown = createElement("span", "arrowDown");
numInput.type = "text";
numInput.pattern = "\\d*";
wrapper.appendChild(numInput);
wrapper.appendChild(arrowUp);
wrapper.appendChild(arrowDown);
return wrapper;
}
var do_nothing = function () { return undefined; };
var revFormat = {
D: do_nothing,
F: function (dateObj, monthName, locale) {
dateObj.setMonth(locale.months.longhand.indexOf(monthName));
},
G: function (dateObj, hour) {
dateObj.setHours(parseFloat(hour));
},
H: function (dateObj, hour) {
dateObj.setHours(parseFloat(hour));
},
J: function (dateObj, day) {
dateObj.setDate(parseFloat(day));
},
K: function (dateObj, amPM) {
dateObj.setHours(dateObj.getHours() % 12 + 12 * int(/pm/i.test(amPM)));
},
M: function (dateObj, shortMonth, locale) {
dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth));
},
S: function (dateObj, seconds) {
dateObj.setSeconds(parseFloat(seconds));
},
U: function (_, unixSeconds) { return new Date(parseFloat(unixSeconds) * 1000); },
W: function (dateObj, weekNum) {
var weekNumber = parseInt(weekNum);
return new Date(dateObj.getFullYear(), 0, 2 + (weekNumber - 1) * 7, 0, 0, 0, 0);
},
Y: function (dateObj, year) {
dateObj.setFullYear(parseFloat(year));
},
Z: function (_, ISODate) { return new Date(ISODate); },
d: function (dateObj, day) {
dateObj.setDate(parseFloat(day));
},
h: function (dateObj, hour) {
dateObj.setHours(parseFloat(hour));
},
i: function (dateObj, minutes) {
dateObj.setMinutes(parseFloat(minutes));
},
j: function (dateObj, day) {
dateObj.setDate(parseFloat(day));
},
l: do_nothing,
m: function (dateObj, month) {
dateObj.setMonth(parseFloat(month) - 1);
},
n: function (dateObj, month) {
dateObj.setMonth(parseFloat(month) - 1);
},
s: function (dateObj, seconds) {
dateObj.setSeconds(parseFloat(seconds));
},
w: do_nothing,
y: function (dateObj, year) {
dateObj.setFullYear(2000 + parseFloat(year));
},
};
var tokenRegex = {
D: "(\\w+)",
F: "(\\w+)",
G: "(\\d\\d|\\d)",
H: "(\\d\\d|\\d)",
J: "(\\d\\d|\\d)\\w+",
K: "(am|AM|Am|aM|pm|PM|Pm|pM)",
M: "(\\w+)",
S: "(\\d\\d|\\d)",
U: "(.+)",
W: "(\\d\\d|\\d)",
Y: "(\\d{4})",
Z: "(.+)",
d: "(\\d\\d|\\d)",
h: "(\\d\\d|\\d)",
i: "(\\d\\d|\\d)",
j: "(\\d\\d|\\d)",
l: "(\\w+)",
m: "(\\d\\d|\\d)",
n: "(\\d\\d|\\d)",
s: "(\\d\\d|\\d)",
w: "(\\d\\d|\\d)",
y: "(\\d{2})",
};
var formats = {
Z: function (date) { return date.toISOString(); },
D: function (date, locale, options) {
return locale.weekdays.shorthand[formats.w(date, locale, options)];
},
F: function (date, locale, options) {
return monthToStr(formats.n(date, locale, options) - 1, false, locale);
},
G: function (date, locale, options) {
return pad(formats.h(date, locale, options));
},
H: function (date) { return pad(date.getHours()); },
J: function (date, locale) {
return locale.ordinal !== undefined
? date.getDate() + locale.ordinal(date.getDate())
: date.getDate();
},
K: function (date) { return (date.getHours() > 11 ? "PM" : "AM"); },
M: function (date, locale) {
return monthToStr(date.getMonth(), true, locale);
},
S: function (date) { return pad(date.getSeconds()); },
U: function (date) { return date.getTime() / 1000; },
W: function (date, _, options) {
return options.getWeek(date);
},
Y: function (date) { return date.getFullYear(); },
d: function (date) { return pad(date.getDate()); },
h: function (date) { return (date.getHours() % 12 ? date.getHours() % 12 : 12); },
i: function (date) { return pad(date.getMinutes()); },
j: function (date) { return date.getDate(); },
l: function (date, locale) {
return locale.weekdays.longhand[date.getDay()];
},
m: function (date) { return pad(date.getMonth() + 1); },
n: function (date) { return date.getMonth() + 1; },
s: function (date) { return date.getSeconds(); },
w: function (date) { return date.getDay(); },
y: function (date) { return String(date.getFullYear()).substring(2); },
};
function FlatpickrInstance(element, instanceConfig) {
var self = {};
self.parseDate = parseDate;
self.formatDate = formatDate;
self._animationLoop = [];
self._handlers = [];
self._bind = bind;
self._setHoursFromDate = setHoursFromDate;
self.changeMonth = changeMonth;
self.changeYear = changeYear;
self.clear = clear;
self.close = close;
self._createElement = createElement;
self.destroy = destroy;
self.isEnabled = isEnabled;
self.jumpToDate = jumpToDate;
self.open = open;
self.redraw = redraw;
self.set = set;
self.setDate = setDate;
self.toggle = toggle;
function setupHelperFunctions() {
self.utils = {
getDaysInMonth: function (month, yr) {
if (month === void 0) { month = self.currentMonth; }
if (yr === void 0) { yr = self.currentYear; }
if (month === 1 && ((yr % 4 === 0 && yr % 100 !== 0) || yr % 400 === 0))
return 29;
return self.l10n.daysInMonth[month];
},
};
}
function init() {
self.element = self.input = element;
self.isOpen = false;
parseConfig();
setupLocale();
setupInputs();
setupDates();
setupHelperFunctions();
if (!self.isMobile)
build();
bindEvents();
if (self.selectedDates.length || self.config.noCalendar) {
if (self.config.enableTime) {
setHoursFromDate(self.config.noCalendar
? self.latestSelectedDateObj || self.config.minDate
: undefined);
}
updateValue(false);
}
self.showTimeInput =
self.selectedDates.length > 0 || self.config.noCalendar;
if (self.weekWrapper !== undefined && self.daysContainer !== undefined) {
self.calendarContainer.style.width =
self.daysContainer.offsetWidth + self.weekWrapper.offsetWidth + "px";
}
if (!self.isMobile)
positionCalendar();
triggerEvent("onReady");
}
function bindToInstance(fn) {
return fn.bind(self);
}
function updateTime(e) {
if (self.config.noCalendar && !self.selectedDates.length) {
self.setDate(new Date().setHours(self.config.defaultHour, self.config.defaultMinute, self.config.defaultSeconds), false);
setHoursFromInputs();
updateValue();
}
timeWrapper(e);
if (self.selectedDates.length === 0)
return;
if (!self.minDateHasTime ||
e.type !== "input" ||
e.target.value.length >= 2) {
setHoursFromInputs();
updateValue();
}
else {
setTimeout(function () {
setHoursFromInputs();
updateValue();
}, 1000);
}
}
function ampm2military(hour, amPM) {
return hour % 12 + 12 * int(amPM === "PM");
}
function military2ampm(hour) {
switch (hour % 24) {
case 0:
case 12:
return 12;
default:
return hour % 12;
}
}
function setHoursFromInputs() {
if (self.hourElement === undefined || self.minuteElement === undefined)
return;
var hours = (parseInt(self.hourElement.value.slice(-2), 10) || 0) % 24, minutes = (parseInt(self.minuteElement.value, 10) || 0) % 60, seconds = self.secondElement !== undefined
? (parseInt(self.secondElement.value, 10) || 0) % 60
: 0;
if (self.amPM !== undefined)
hours = ampm2military(hours, self.amPM.textContent);
if (self.config.minDate &&
self.minDateHasTime &&
self.latestSelectedDateObj &&
compareDates(self.latestSelectedDateObj, self.config.minDate) === 0) {
hours = Math.max(hours, self.config.minDate.getHours());
if (hours === self.config.minDate.getHours())
minutes = Math.max(minutes, self.config.minDate.getMinutes());
}
if (self.config.maxDate &&
self.maxDateHasTime &&
self.latestSelectedDateObj &&
compareDates(self.latestSelectedDateObj, self.config.maxDate) === 0) {
hours = Math.min(hours, self.config.maxDate.getHours());
if (hours === self.config.maxDate.getHours())
minutes = Math.min(minutes, self.config.maxDate.getMinutes());
}
setHours(hours, minutes, seconds);
}
function setHoursFromDate(dateObj) {
var date = dateObj || self.latestSelectedDateObj;
if (date)
setHours(date.getHours(), date.getMinutes(), date.getSeconds());
}
function setHours(hours, minutes, seconds) {
if (self.latestSelectedDateObj !== undefined) {
self.latestSelectedDateObj.setHours(hours % 24, minutes, seconds || 0, 0);
}
if (!self.hourElement || !self.minuteElement || self.isMobile)
return;
self.hourElement.value = pad(!self.config.time_24hr
? (12 + hours) % 12 + 12 * int(hours % 12 === 0)
: hours);
self.minuteElement.value = pad(minutes);
if (self.amPM !== undefined)
self.amPM.textContent = hours >= 12 ? "PM" : "AM";
if (self.secondElement !== undefined)
self.secondElement.value = pad(seconds);
}
function onYearInput(event) {
var year = parseInt(event.target.value) + (event.delta || 0);
if (year.toString().length === 4 || event.key === "Enter") {
self.currentYearElement.blur();
if (!/[^\d]/.test(year.toString()))
changeYear(year);
}
}
function bind(element, event, handler) {
if (event instanceof Array)
return event.forEach(function (ev) { return bind(element, ev, handler); });
if (element instanceof Array)
return element.forEach(function (el) { return bind(el, event, handler); });
element.addEventListener(event, handler);
self._handlers.push({ element: element, event: event, handler: handler });
}
function onClick(handler) {
return function (evt) { return evt.which === 1 && handler(evt); };
}
function triggerChange() {
triggerEvent("onChange");
}
function bindEvents() {
if (self.config.wrap) {
["open", "close", "toggle", "clear"].forEach(function (evt) {
Array.prototype.forEach.call(self.element.querySelectorAll("[data-" + evt + "]"), function (el) {
return bind(el, "click", self[evt]);
});
});
}
if (self.isMobile) {
setupMobile();
return;
}
var debouncedResize = debounce(onResize, 50);
self._debouncedChange = debounce(triggerChange, 300);
if (self.config.mode === "range" && self.daysContainer)
bind(self.daysContainer, "mouseover", function (e) {
return onMouseOver(e.target);
});
bind(window.document.body, "keydown", onKeyDown);
if (!self.config.static)
bind(self._input, "keydown", onKeyDown);
if (!self.config.inline && !self.config.static)
bind(window, "resize", debouncedResize);
if (window.ontouchstart !== undefined)
bind(window.document.body, "touchstart", documentClick);
bind(window.document.body, "mousedown", onClick(documentClick));
bind(self._input, "blur", documentClick);
if (self.config.clickOpens === true) {
bind(self._input, "focus", self.open);
bind(self._input, "mousedown", onClick(self.open));
}
if (self.daysContainer !== undefined) {
self.monthNav.addEventListener("wheel", function (e) { return e.preventDefault(); });
bind(self.monthNav, "wheel", debounce(onMonthNavScroll, 10));
bind(self.monthNav, "mousedown", onClick(onMonthNavClick));
bind(self.monthNav, ["keyup", "increment"], onYearInput);
bind(self.daysContainer, "mousedown", onClick(selectDate));
if (self.config.animate) {
bind(self.daysContainer, ["webkitAnimationEnd", "animationend"], animateDays);
bind(self.monthNav, ["webkitAnimationEnd", "animationend"], animateMonths);
}
}
if (self.timeContainer !== undefined &&
self.minuteElement !== undefined &&
self.hourElement !== undefined) {
var selText = function (e) {
return e.target.select();
};
bind(self.timeContainer, ["wheel", "input", "increment"], updateTime);
bind(self.timeContainer, "mousedown", onClick(timeIncrement));
bind(self.timeContainer, ["wheel", "increment"], self._debouncedChange);
bind(self.timeContainer, "input", triggerChange);
bind([self.hourElement, self.minuteElement], ["focus", "click"], selText);
if (self.secondElement !== undefined)
bind(self.secondElement, "focus", function () { return self.secondElement && self.secondElement.select(); });
if (self.amPM !== undefined) {
bind(self.amPM, "mousedown", onClick(function (e) {
updateTime(e);
triggerChange();
}));
}
}
}
function processPostDayAnimation() {
self._animationLoop.forEach(function (f) { return f(); });
self._animationLoop = [];
}
function animateDays(e) {
if (self.daysContainer && self.daysContainer.childNodes.length > 1) {
switch (e.animationName) {
case "fpSlideLeft":
self.daysContainer.lastChild &&
self.daysContainer.lastChild.classList.remove("slideLeftNew");
self.daysContainer.removeChild(self.daysContainer
.firstChild);
self.days = self.daysContainer.firstChild;
processPostDayAnimation();
break;
case "fpSlideRight":
self.daysContainer.firstChild &&
self.daysContainer.firstChild.classList.remove("slideRightNew");
self.daysContainer.removeChild(self.daysContainer
.lastChild);
self.days = self.daysContainer.firstChild;
processPostDayAnimation();
break;
default:
break;
}
}
}
function animateMonths(e) {
switch (e.animationName) {
case "fpSlideLeftNew":
case "fpSlideRightNew":
self.navigationCurrentMonth.classList.remove("slideLeftNew");
self.navigationCurrentMonth.classList.remove("slideRightNew");
var nav = self.navigationCurrentMonth;
while (nav.nextSibling &&
/curr/.test(nav.nextSibling.className))
self.monthNav.removeChild(nav.nextSibling);
while (nav.previousSibling &&
/curr/.test(nav.previousSibling.className))
self.monthNav.removeChild(nav.previousSibling);
self.oldCurMonth = undefined;
break;
}
}
function jumpToDate(jumpDate) {
var jumpTo = jumpDate !== undefined
? parseDate(jumpDate)
: self.latestSelectedDateObj ||
(self.config.minDate && self.config.minDate > self.now
? self.config.minDate
: self.config.maxDate && self.config.maxDate < self.now
? self.config.maxDate
: self.now);
try {
if (jumpTo !== undefined) {
self.currentYear = jumpTo.getFullYear();
self.currentMonth = jumpTo.getMonth();
}
}
catch (e) {
console.error(e.stack);
console.warn("Invalid date supplied: " + jumpTo);
}
self.redraw();
}
function timeIncrement(e) {
if (~e.target.className.indexOf("arrow"))
incrementNumInput(e, e.target.classList.contains("arrowUp") ? 1 : -1);
}
function incrementNumInput(e, delta, inputElem) {
var target = e && e.target;
var input = inputElem ||
(target && target.parentNode && target.parentNode.firstChild);
var event = createEvent("increment");
event.delta = delta;
input && input.dispatchEvent(event);
}
function build() {
var fragment = window.document.createDocumentFragment();
self.calendarContainer = createElement("div", "flatpickr-calendar");
self.calendarContainer.tabIndex = -1;
if (!self.config.noCalendar) {
fragment.appendChild(buildMonthNav());
self.innerContainer = createElement("div", "flatpickr-innerContainer");
if (self.config.weekNumbers) {
var _a = buildWeeks(), weekWrapper = _a.weekWrapper, weekNumbers = _a.weekNumbers;
self.innerContainer.appendChild(weekWrapper);
self.weekNumbers = weekNumbers;
self.weekWrapper = weekWrapper;
}
self.rContainer = createElement("div", "flatpickr-rContainer");
self.rContainer.appendChild(buildWeekdays());
if (!self.daysContainer) {
self.daysContainer = createElement("div", "flatpickr-days");
self.daysContainer.tabIndex = -1;
}
buildDays();
self.rContainer.appendChild(self.daysContainer);
self.innerContainer.appendChild(self.rContainer);
fragment.appendChild(self.innerContainer);
}
if (self.config.enableTime) {
fragment.appendChild(buildTime());
}
toggleClass(self.calendarContainer, "rangeMode", self.config.mode === "range");
toggleClass(self.calendarContainer, "animate", self.config.animate);
self.calendarContainer.appendChild(fragment);
var customAppend = self.config.appendTo !== undefined && self.config.appendTo.nodeType;
if (self.config.inline || self.config.static) {
self.calendarContainer.classList.add(self.config.inline ? "inline" : "static");
if (self.config.inline && !customAppend && self.element.parentNode) {
self.element.parentNode.insertBefore(self.calendarContainer, self._input.nextSibling);
}
if (self.config.static) {
var wrapper = createElement("div", "flatpickr-wrapper");
if (self.element.parentNode)
self.element.parentNode.insertBefore(wrapper, self.element);
wrapper.appendChild(self.element);
if (self.altInput)
wrapper.appendChild(self.altInput);
wrapper.appendChild(self.calendarContainer);
}
}
if (!self.config.static && !self.config.inline)
(self.config.appendTo !== undefined
? self.config.appendTo
: window.document.body).appendChild(self.calendarContainer);
}
function createDay(className, date, dayNumber, i) {
var dateIsEnabled = isEnabled(date, true), dayElement = createElement("span", "flatpickr-day " + className, date.getDate().toString());
dayElement.dateObj = date;
dayElement.$i = i;
dayElement.setAttribute("aria-label", self.formatDate(date, self.config.ariaDateFormat));
if (compareDates(date, self.now) === 0) {
self.todayDateElem = dayElement;
dayElement.classList.add("today");
}
if (dateIsEnabled) {
dayElement.tabIndex = -1;
if (isDateSelected(date)) {
dayElement.classList.add("selected");
self.selectedDateElem = dayElement;
if (self.config.mode === "range") {
toggleClass(dayElement, "startRange", self.selectedDates[0] &&
compareDates(date, self.selectedDates[0]) === 0);
toggleClass(dayElement, "endRange", self.selectedDates[1] &&
compareDates(date, self.selectedDates[1]) === 0);
}
}
}
else {
dayElement.classList.add("disabled");
if (self.selectedDates[0] &&
self.minRangeDate &&
date > self.minRangeDate &&
date < self.selectedDates[0])
self.minRangeDate = date;
else if (self.selectedDates[0] &&
self.maxRangeDate &&
date < self.maxRangeDate &&
date > self.selectedDates[0])
self.maxRangeDate = date;
}
if (self.config.mode === "range") {
if (isDateInRange(date) && !isDateSelected(date))
dayElement.classList.add("inRange");
if (self.selectedDates.length === 1 &&
self.minRangeDate !== undefined &&
self.maxRangeDate !== undefined &&
(date < self.minRangeDate || date > self.maxRangeDate))
dayElement.classList.add("notAllowed");
}
if (self.weekNumbers &&
className !== "prevMonthDay" &&
dayNumber % 7 === 1) {
self.weekNumbers.insertAdjacentHTML("beforeend", "<span class='disabled flatpickr-day'>" +
self.config.getWeek(date) +
"</span>");
}
triggerEvent("onDayCreate", dayElement);
return dayElement;
}
function focusOnDay(currentIndex, offset) {
var newIndex = currentIndex + offset || 0, targetNode = (currentIndex !== undefined
? self.days.childNodes[newIndex]
: self.selectedDateElem ||
self.todayDateElem ||
self.days.childNodes[0]);
var focus = function () {
targetNode = targetNode || self.days.childNodes[newIndex];
targetNode.focus();
if (self.config.mode === "range")
onMouseOver(targetNode);
};
if (targetNode === undefined && offset !== 0) {
if (offset > 0) {
self.changeMonth(1, true, undefined, true);
newIndex = newIndex % 42;
}
else if (offset < 0) {
self.changeMonth(-1, true, undefined, true);
newIndex += 42;
}
return afterDayAnim(focus);
}
focus();
}
function afterDayAnim(fn) {
self.config.animate === true ? self._animationLoop.push(fn) : fn();
}
function buildDays(delta) {
if (self.daysContainer === undefined) {
return;
}
var firstOfMonth = (new Date(self.currentYear, self.currentMonth, 1).getDay() -
self.l10n.firstDayOfWeek +
7) %
7, isRangeMode = self.config.mode === "range";
var prevMonthDays = self.utils.getDaysInMonth((self.currentMonth - 1 + 12) % 12);
var daysInMonth = self.utils.getDaysInMonth(), days = window.document.createDocumentFragment();
var dayNumber = prevMonthDays + 1 - firstOfMonth, dayIndex = 0;
if (self.weekNumbers && self.weekNumbers.firstChild)
self.weekNumbers.textContent = "";
if (isRangeMode) {
self.minRangeDate = new Date(self.currentYear, self.currentMonth - 1, dayNumber);
self.maxRangeDate = new Date(self.currentYear, self.currentMonth + 1, (42 - firstOfMonth) % daysInMonth);
}
for (; dayNumber <= prevMonthDays; dayNumber++, dayIndex++) {
days.appendChild(createDay("prevMonthDay", new Date(self.currentYear, self.currentMonth - 1, dayNumber), dayNumber, dayIndex));
}
for (dayNumber = 1; dayNumber <= daysInMonth; dayNumber++, dayIndex++) {
days.appendChild(createDay("", new Date(self.currentYear, self.currentMonth, dayNumber), dayNumber, dayIndex));
}
for (var dayNum = daysInMonth + 1; dayNum <= 42 - firstOfMonth; dayNum++, dayIndex++) {
days.appendChild(createDay("nextMonthDay", new Date(self.currentYear, self.currentMonth + 1, dayNum % daysInMonth), dayNum, dayIndex));
}
if (isRangeMode && self.selectedDates.length === 1 && days.childNodes[0]) {
self._hidePrevMonthArrow =
self._hidePrevMonthArrow ||
(!!self.minRangeDate &&
self.minRangeDate > days.childNodes[0].dateObj);
self._hideNextMonthArrow =
self._hideNextMonthArrow ||
(!!self.maxRangeDate &&
self.maxRangeDate <
new Date(self.currentYear, self.currentMonth + 1, 1));
}
else
updateNavigationCurrentMonth();
var dayContainer = createElement("div", "dayContainer");
dayContainer.appendChild(days);
if (!self.config.animate || delta === undefined)
clearNode(self.daysContainer);
else {
while (self.daysContainer.childNodes.length > 1)
self.daysContainer.removeChild(self.daysContainer.firstChild);
}
if (delta && delta >= 0)
self.daysContainer.appendChild(dayContainer);
else
self.daysContainer.insertBefore(dayContainer, self.daysContainer.firstChild);
self.days = self.daysContainer.childNodes[0];
}
function buildMonthNav() {
var monthNavFragment = window.document.createDocumentFragment();
self.monthNav = createElement("div", "flatpickr-month");
self.prevMonthNav = createElement("span", "flatpickr-prev-month");
self.prevMonthNav.innerHTML = self.config.prevArrow;
self.currentMonthElement = createElement("span", "cur-month");
self.currentMonthElement.title = self.l10n.scrollTitle;
var yearInput = createNumberInput("cur-year");
self.currentYearElement = yearInput.childNodes[0];
self.currentYearElement.title = self.l10n.scrollTitle;
if (self.config.minDate)
self.currentYearElement.min = self.config.minDate
.getFullYear()
.toString();
if (self.config.maxDate) {
self.currentYearElement.max = self.config.maxDate
.getFullYear()
.toString();
self.currentYearElement.disabled =
!!self.config.minDate &&
self.config.minDate.getFullYear() === self.config.maxDate.getFullYear();
}
self.nextMonthNav = createElement("span", "flatpickr-next-month");
self.nextMonthNav.innerHTML = self.config.nextArrow;
self.navigationCurrentMonth = createElement("span", "flatpickr-current-month");
self.navigationCurrentMonth.appendChild(self.currentMonthElement);
self.navigationCurrentMonth.appendChild(yearInput);
monthNavFragment.appendChild(self.prevMonthNav);
monthNavFragment.appendChild(self.navigationCurrentMonth);
monthNavFragment.appendChild(self.nextMonthNav);
self.monthNav.appendChild(monthNavFragment);
Object.defineProperty(self, "_hidePrevMonthArrow", {
get: function () { return self.__hidePrevMonthArrow; },
set: function (bool) {
if (self.__hidePrevMonthArrow !== bool)
self.prevMonthNav.style.display = bool ? "none" : "block";
self.__hidePrevMonthArrow = bool;
},
});
Object.defineProperty(self, "_hideNextMonthArrow", {
get: function () { return self.__hideNextMonthArrow; },
set: function (bool) {
if (self.__hideNextMonthArrow !== bool)
self.nextMonthNav.style.display = bool ? "none" : "block";
self.__hideNextMonthArrow = bool;
},
});
updateNavigationCurrentMonth();
return self.monthNav;
}
function buildTime() {
self.calendarContainer.classList.add("hasTime");
if (self.config.noCalendar)
self.calendarContainer.classList.add("noCalendar");
self.timeContainer = createElement("div", "flatpickr-time");
self.timeContainer.tabIndex = -1;
var separator = createElement("span", "flatpickr-time-separator", ":");
var hourInput = createNumberInput("flatpickr-hour");
self.hourElement = hourInput.childNodes[0];
var minuteInput = createNumberInput("flatpickr-minute");
self.minuteElement = minuteInput.childNodes[0];
self.hourElement.tabIndex = self.minuteElement.tabIndex = -1;
self.hourElement.value = pad(self.latestSelectedDateObj
? self.latestSelectedDateObj.getHours()
: self.config.time_24hr
? self.config.defaultHour
: military2ampm(self.config.defaultHour));
self.minuteElement.value = pad(self.latestSelectedDateObj
? self.latestSelectedDateObj.getMinutes()
: self.config.defaultMinute);
self.hourElement.step = self.config.hourIncrement.toString();
self.minuteElement.step = self.config.minuteIncrement.toString();
self.hourElement.min = self.config.time_24hr ? "0" : "1";
self.hourElement.max = self.config.time_24hr ? "23" : "12";
self.minuteElement.min = "0";
self.minuteElement.max = "59";
self.hourElement.title = self.minuteElement.title = self.l10n.scrollTitle;
self.timeContainer.appendChild(hourInput);
self.timeContainer.appendChild(separator);
self.timeContainer.appendChild(minuteInput);
if (self.config.time_24hr)
self.timeContainer.classList.add("time24hr");
if (self.config.enableSeconds) {
self.timeContainer.classList.add("hasSeconds");
var secondInput = createNumberInput("flatpickr-second");
self.secondElement = secondInput.childNodes[0];
self.secondElement.value = pad(self.latestSelectedDateObj
? self.latestSelectedDateObj.getSeconds()
: self.config.defaultSeconds);
self.secondElement.step = self.minuteElement.step;
self.secondElement.min = self.minuteElement.min;
self.secondElement.max = self.minuteElement.max;
self.timeContainer.appendChild(createElement("span", "flatpickr-time-separator", ":"));
self.timeContainer.appendChild(secondInput);
}
if (!self.config.time_24hr) {
self.amPM = createElement("span", "flatpickr-am-pm", self.l10n.amPM[int((self.latestSelectedDateObj
? self.hourElement.value
: self.config.defaultHour) > 11)]);
self.amPM.title = self.l10n.toggleTitle;
self.amPM.tabIndex = -1;
self.timeContainer.appendChild(self.amPM);
}
return self.timeContainer;
}
function buildWeekdays() {
if (!self.weekdayContainer)
self.weekdayContainer = createElement("div", "flatpickr-weekdays");
var firstDayOfWeek = self.l10n.firstDayOfWeek;
var weekdays = self.l10n.weekdays.shorthand.slice();
if (firstDayOfWeek > 0 && firstDayOfWeek < weekdays.length) {
weekdays = weekdays.splice(firstDayOfWeek, weekdays.length).concat(weekdays.splice(0, firstDayOfWeek));
}
self.weekdayContainer.innerHTML = "\n <span class=flatpickr-weekday>\n " + weekdays.join("</span><span class=flatpickr-weekday>") + "\n </span>\n ";
return self.weekdayContainer;
}
function buildWeeks() {
self.calendarContainer.classList.add("hasWeeks");
var weekWrapper = createElement("div", "flatpickr-weekwrapper");
weekWrapper.appendChild(createElement("span", "flatpickr-weekday", self.l10n.weekAbbreviation));
var weekNumbers = createElement("div", "flatpickr-weeks");
weekWrapper.appendChild(weekNumbers);
return {
weekWrapper: weekWrapper,
weekNumbers: weekNumbers,
};
}
function changeMonth(value, is_offset, animate, from_keyboard) {
if (is_offset === void 0) { is_offset = true; }
if (animate === void 0) { animate = self.config.animate; }
if (from_keyboard === void 0) { from_keyboard = false; }
var delta = is_offset ? value : value - self.currentMonth;
if ((delta < 0 && self._hidePrevMonthArrow) ||
(delta > 0 && self._hideNextMonthArrow))
return;
self.currentMonth += delta;
if (self.currentMonth < 0 || self.currentMonth > 11) {
self.currentYear += self.currentMonth > 11 ? 1 : -1;
self.currentMonth = (self.currentMonth + 12) % 12;
triggerEvent("onYearChange");
}
buildDays(animate ? delta : undefined);
if (!animate) {
triggerEvent("onMonthChange");
return updateNavigationCurrentMonth();
}
var nav = self.navigationCurrentMonth;
if (delta < 0) {
while (nav.nextSibling &&
/curr/.test(nav.nextSibling.className))
self.monthNav.removeChild(nav.nextSibling);
}
else if (delta > 0) {
while (nav.previousSibling &&
/curr/.test(nav.previousSibling.className))
self.monthNav.removeChild(nav.previousSibling);
}
self.oldCurMonth = self.navigationCurrentMonth;
self.navigationCurrentMonth = self.monthNav.insertBefore(self.oldCurMonth.cloneNode(true), delta > 0 ? self.oldCurMonth.nextSibling : self.oldCurMonth);
var daysContainer = self.daysContainer;
if (daysContainer.firstChild && daysContainer.lastChild) {
if (delta > 0) {
daysContainer.firstChild.classList.add("slideLeft");
daysContainer.lastChild.classList.add("slideLeftNew");
self.oldCurMonth.classList.add("slideLeft");
self.navigationCurrentMonth.classList.add("slideLeftNew");
}
else if (delta < 0) {
daysContainer.firstChild.classList.add("slideRightNew");
daysContainer.lastChild.classList.add("slideRight");
self.oldCurMonth.classList.add("slideRight");
self.navigationCurrentMonth.classList.add("slideRightNew");
}
}
self.currentMonthElement = self.navigationCurrentMonth
.firstChild;
self.currentYearElement = self.navigationCurrentMonth.lastChild
.childNodes[0];
updateNavigationCurrentMonth();
if (self.oldCurMonth.firstChild)
self.oldCurMonth.firstChild.textContent = monthToStr(self.currentMonth - delta, self.config.shorthandCurrentMonth, self.l10n);
triggerEvent("onMonthChange");
if (from_keyboard &&
document.activeElement &&
document.activeElement.$i) {
var index_1 = document.activeElement.$i;
afterDayAnim(function () {
focusOnDay(index_1, 0);
});
}
}
function clear(triggerChangeEvent) {
if (triggerChangeEvent === void 0) { triggerChangeEvent = true; }
self.input.value = "";
if (self.altInput)
self.altInput.value = "";
if (self.mobileInput)
self.mobileInput.value = "";
self.selectedDates = [];
self.latestSelectedDateObj = undefined;
self.showTimeInput = false;
self.redraw();
if (triggerChangeEvent === true)
triggerEvent("onChange");
}
function close() {
self.isOpen = false;
if (!self.isMobile) {
self.calendarContainer.classList.remove("open");
self._input.classList.remove("active");
}
triggerEvent("onClose");
}
function destroy() {
if (self.config !== undefined)
triggerEvent("onDestroy");
for (var i = self._handlers.length; i--;) {
var h = self._handlers[i];
h.element.removeEventListener(h.event, h.handler);
}
self._handlers = [];
if (self.mobileInput) {
if (self.mobileInput.parentNode)
self.mobileInput.parentNode.removeChild(self.mobileInput);
self.mobileInput = undefined;
}
else if (self.calendarContainer && self.calendarContainer.parentNode)
self.calendarContainer.parentNode.removeChild(self.calendarContainer);
if (self.altInput) {
self.input.type = "text";
if (self.altInput.parentNode)
self.altInput.parentNode.removeChild(self.altInput);
delete self.altInput;
}
if (self.input) {
self.input.type = self.input._type;
self.input.classList.remove("flatpickr-input");
self.input.removeAttribute("readonly");
self.input.value = "";
}
[
"_showTimeInput",
"latestSelectedDateObj",
"_hideNextMonthArrow",
"_hidePrevMonthArrow",
"__hideNextMonthArrow",
"__hidePrevMonthArrow",
"isMobile",
"isOpen",
"selectedDateElem",
"minDateHasTime",
"maxDateHasTime",
"days",
"daysContainer",
"_input",
"_positionElement",
"innerContainer",
"rContainer",
"monthNav",
"todayDateElem",
"calendarContainer",
"weekdayContainer",
"prevMonthNav",
"nextMonthNav",
"currentMonthElement",
"currentYearElement",
"navigationCurrentMonth",
"selectedDateElem",
"config",
].forEach(function (k) {
try {
delete self[k];
}
catch (_) { }
});
}
function isCalendarElem(elem) {
if (self.config.appendTo && self.config.appendTo.contains(elem))
return true;
return self.calendarContainer.contains(elem);
}
function documentClick(e) {
if (self.isOpen && !self.config.inline) {
var isCalendarElement = isCalendarElem(e.target);
var isInput = e.target === self.input ||
e.target === self.altInput ||
self.element.contains(e.target) ||
(e.path &&
e.path.indexOf &&
(~e.path.indexOf(self.input) ||
~e.path.indexOf(self.altInput)));
var lostFocus = e.type === "blur"
? isInput &&
e.relatedTarget &&
!isCalendarElem(e.relatedTarget)
: !isInput && !isCalendarElement;
if (lostFocus &&
self.config.ignoredFocusElements.indexOf(e.target) === -1) {
self.close();
if (self.config.mode === "range" && self.selectedDates.length === 1) {
self.clear(false);
self.redraw();
}
}
}
}
function changeYear(newYear) {
if (!newYear ||
(self.currentYearElement.min &&
newYear < parseInt(self.currentYearElement.min)) ||
(self.currentYearElement.max &&
newYear > parseInt(self.currentYearElement.max)))
return;
var newYearNum = newYear, isNewYear = self.currentYear !== newYearNum;
self.currentYear = newYearNum || self.currentYear;
if (self.config.maxDate &&
self.currentYear === self.config.maxDate.getFullYear()) {
self.currentMonth = Math.min(self.config.maxDate.getMonth(), self.currentMonth);
}
else if (self.config.minDate &&
self.currentYear === self.config.minDate.getFullYear()) {
self.currentMonth = Math.max(self.config.minDate.getMonth(), self.currentMonth);
}
if (isNewYear) {
self.redraw();
triggerEvent("onYearChange");
}
}
function isEnabled(date, timeless) {
if (timeless === void 0) { timeless = true; }
var dateToCheck = self.parseDate(date, undefined, timeless);
if ((self.config.minDate &&
dateToCheck &&
compareDates(dateToCheck, self.config.minDate, timeless !== undefined ? timeless : !self.minDateHasTime) < 0) ||
(self.config.maxDate &&
dateToCheck &&
compareDates(dateToCheck, self.config.maxDate, timeless !== undefined ? timeless : !self.maxDateHasTime) > 0))
return false;
if (!self.config.enable.length && !self.config.disable.length)
return true;
if (dateToCheck === undefined)
return false;
var bool = self.config.enable.length > 0, array = bool ? self.config.enable : self.config.disabl