vendor/assets/javascripts/flatpickr.js in flatpickr-2.2.8.0 vs vendor/assets/javascripts/flatpickr.js in flatpickr-2.2.9.0

- old
+ new

@@ -1,10 +1,10 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; -/*! flatpickr v2.2.8, @license MIT */ +/*! flatpickr v2.2.9, @license MIT */ function Flatpickr(element, config) { var self = this; function init() { if (element._flatpickr) destroy(element._flatpickr); @@ -40,14 +40,10 @@ if (!self.isMobile) build(); bind(); - self.minDateHasTime = self.config.minDate && (self.config.minDate.getHours() || self.config.minDate.getMinutes() || self.config.minDate.getSeconds()); - - self.maxDateHasTime = self.config.maxDate && (self.config.maxDate.getHours() || self.config.maxDate.getMinutes() || self.config.maxDate.getSeconds()); - if (!self.isMobile) { Object.defineProperty(self, "dateIsPicked", { set: function set(bool) { if (bool) return self.calendarContainer.classList.add("dateIsPicked"); self.calendarContainer.classList.remove("dateIsPicked"); @@ -76,13 +72,18 @@ timeWrapper(e); if (!self.selectedDates.length) return; - if (e.type !== "input" || e.target.value.length >= 2) { + if (!self.minDateHasTime || e.type !== "input" || e.target.value.length >= 2) { setHoursFromInputs(); updateValue(); + } else { + setTimeout(function () { + setHoursFromInputs(); + updateValue(); + }, 1000); } } function setHoursFromInputs() { if (!self.config.enableTime) return; @@ -92,10 +93,11 @@ seconds = self.config.enableSeconds ? parseInt(self.secondElement.value, 10) || 0 : 0; if (self.amPM) hours = hours % 12 + 12 * (self.amPM.textContent === "PM"); if (self.minDateHasTime && 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()); } else if (self.maxDateHasTime && 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()); @@ -124,10 +126,18 @@ if (!self.config.time_24hr && self.selectedDates.length) self.amPM.textContent = self.latestSelectedDateObj.getHours() >= 12 ? "PM" : "AM"; if (self.config.enableSeconds) self.secondElement.value = self.pad(seconds); } + function onYearInput(event) { + if (event.target.value.length === 4) { + self.currentYearElement.blur(); + handleYearChange(event.target.value); + event.target.value = self.currentYear; + } + } + function bind() { if (self.config.wrap) { ["open", "close", "toggle", "clear"].forEach(function (el) { try { self.element.querySelector("[data-" + el + "]").addEventListener("click", self[el]); @@ -176,27 +186,23 @@ }); self.currentYearElement.addEventListener("focus", function () { self.currentYearElement.select(); }); - self.currentYearElement.addEventListener("input", function (event) { - if (event.target.value.length === 4) { - self.currentYearElement.blur(); - handleYearChange(event.target.value); - event.target.value = self.currentYear; - } - }); + self.currentYearElement.addEventListener("input", onYearInput); + self.currentYearElement.addEventListener("increment", onYearInput); self.days.addEventListener("click", selectDate); } if (self.config.enableTime) { self.timeContainer.addEventListener("transitionend", positionCalendar); self.timeContainer.addEventListener("wheel", function (e) { return debounce(updateTime(e), 5); }); self.timeContainer.addEventListener("input", updateTime); + self.timeContainer.addEventListener("increment", updateTime); self.timeContainer.addEventListener("wheel", self.debouncedChange); self.timeContainer.addEventListener("input", self.triggerChange); self.hourElement.addEventListener("focus", function () { @@ -238,14 +244,14 @@ function incrementNumInput(e, delta) { var input = e.target.parentNode.childNodes[0]; input.value = parseInt(input.value, 10) + delta * (input.step || 1); try { - input.dispatchEvent(new Event("input", { "bubbles": true })); + input.dispatchEvent(new Event("increment", { "bubbles": true })); } catch (e) { var ev = document.createEvent("CustomEvent"); - ev.initCustomEvent("input", true, true, {}); + ev.initCustomEvent("increment", true, true, {}); input.dispatchEvent(ev); } } function createNumberInput(inputClassName) { @@ -301,11 +307,11 @@ } } else document.body.appendChild(self.calendarContainer); } function createDay(className, date, dayNumber) { - var dateIsEnabled = isEnabled(date), + var dateIsEnabled = isEnabled(date, true), dayElement = createElement("span", "flatpickr-day " + className, date.getDate()); dayElement.dateObj = date; if (compareDates(date, self.now) === 0) dayElement.classList.add("today"); @@ -523,13 +529,11 @@ if (!self.config.noCalendar) self.days.focus(); triggerEvent("MonthChange"); } - function clear() { - var triggerChangeEvent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; - + function clear(triggerChangeEvent) { self.input.value = ""; if (self.altInput) self.altInput.value = ""; if (self.mobileInput) self.mobileInput.value = ""; @@ -634,13 +638,16 @@ self.redraw(); triggerEvent("YearChange"); } } - function isEnabled(date) { - if (self.config.minDate && compareDates(date, self.config.minDate) < 0 || self.config.maxDate && compareDates(date, self.config.maxDate) > 0) return false; + function isEnabled(date, timeless) { + var ltmin = compareDates(date, self.config.minDate, typeof timeless !== "undefined" ? timeless : !self.minDateHasTime) < 0; + var gtmax = compareDates(date, self.config.maxDate, typeof timeless !== "undefined" ? timeless : !self.maxDateHasTime) > 0; + if (ltmin || gtmax) return false; + if (!self.config.enable.length && !self.config.disable.length) return true; var dateToCheck = self.parseDate(date, true); // timeless var bool = self.config.enable.length > 0, @@ -802,11 +809,10 @@ } function parseConfig() { var boolOpts = ["utc", "wrap", "weekNumbers", "allowInput", "clickOpens", "time_24hr", "enableTime", "noCalendar", "altInput", "shorthandCurrentMonth", "inline", "static", "enableSeconds", "disableMobile"]; self.config = Object.create(Flatpickr.defaultConfig); - var userConfig = _extends({}, self.instanceConfig, self.element.dataset || {}); Object.defineProperty(self.config, "minDate", { get: function get() { return this._minDate; }, @@ -818,10 +824,12 @@ return this._maxDate; }, set: minMaxDateSetter("max") }); + var userConfig = _extends({}, self.instanceConfig, JSON.parse(JSON.stringify(self.element.dataset || {}))); + _extends(self.config, userConfig); for (var i = 0; i < boolOpts.length; i++) { self.config[boolOpts[i]] = self.config[boolOpts[i]] === true || self.config[boolOpts[i]] === "true"; }if (!userConfig.dateFormat && userConfig.enableTime) { @@ -1000,37 +1008,56 @@ if (self.config.disable.length) self.config.disable = parseDateRules(self.config.disable); if (self.config.enable.length) self.config.enable = parseDateRules(self.config.enable); self.selectedDates = self.selectedDates.filter(function (d) { - return d instanceof Date && d.getTime() && isEnabled(d); + return d instanceof Date && d.getTime() && isEnabled(d, false); }); + self.selectedDates.sort(function (a, b) { + return a.getTime() - b.getTime(); + }); + var initialDate = self.selectedDates.length ? self.selectedDates[0] : self.config.minDate > self.now ? self.config.minDate : self.now; self.currentYear = initialDate.getFullYear(); self.currentMonth = initialDate.getMonth(); if (self.selectedDates.length) self.latestSelectedDateObj = self.selectedDates[0]; + + self.minDateHasTime = self.config.minDate && (self.config.minDate.getHours() || self.config.minDate.getMinutes() || self.config.minDate.getSeconds()); + + self.maxDateHasTime = self.config.maxDate && (self.config.maxDate.getHours() || self.config.maxDate.getMinutes() || self.config.maxDate.getSeconds()); + + Object.defineProperty(self, "latestSelectedDateObj", { + get: function get() { + return self._selectedDateObj || self.selectedDates[self.selectedDates.length - 1] || null; + }, + set: function set(date) { + self._selectedDateObj = date; + } + }); } function setupHelperFunctions() { self.utils = { duration: { DAY: 86400000 }, - getDaysinMonth: function getDaysinMonth() { - var month = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : self.currentMonth; - var yr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : self.currentYear; + getDaysinMonth: function getDaysinMonth(month, yr) { + month = typeof month === "undefined" ? self.currentMonth : month; + yr = typeof yr === "undefined" ? self.currentYear : yr; + if (month === 1 && (yr % 4 === 0 && yr % 100 !== 0 || yr % 400 === 0)) return 29; + return self.l10n.daysInMonth[month]; }, + monthToStr: function monthToStr(monthNumber, shorthand) { + shorthand = typeof shorthand === "undefined" ? self.config.shorthandCurrentMonth : shorthand; - monthToStr: function monthToStr(monthNumber) { - var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : self.config.shorthandCurrentMonth; - return self.l10n.months[(short ? "short" : "long") + "hand"][monthNumber]; + return self.l10n.months[(shorthand ? "short" : "long") + "hand"][monthNumber]; } }; } /* istanbul ignore next */ @@ -1311,14 +1338,18 @@ timeout = setTimeout(later, wait); if (immediate && !timeout) func.apply(context, args); }; } - function compareDates(date1, date2) { + function compareDates(date1, date2, timeless) { if (!(date1 instanceof Date) || !(date2 instanceof Date)) return false; - return new Date(date1.getTime()).setHours(0, 0, 0, 0) - new Date(date2.getTime()).setHours(0, 0, 0, 0); + 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(); } function timeWrapper(e) { e.preventDefault(); if (e && ((e.target.value || e.target.textContent).length >= 2 || // typed two digits @@ -1528,21 +1559,21 @@ toggleTitle: "Click to toggle" } }; Flatpickr.l10ns.default = Object.create(Flatpickr.l10ns.en); - Flatpickr.localize = function (l10n) { return _extends(Flatpickr.l10ns.default, l10n || {}); }; +Flatpickr.setDefaults = function (config) { + return _extends(Flatpickr.defaultConfig, config || {}); +}; Flatpickr.prototype = { pad: function pad(number) { return ("0" + number).slice(-2); }, - parseDate: function parseDate(date) { - var timeless = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; - + parseDate: function parseDate(date, timeless) { if (!date) return null; var dateTimeRegex = /(\d+)/g, timeRegex = /^(\d{1,2})[:\s](\d\d)?[:\s]?(\d\d)?\s?(a|p)?/i, timestamp = /^(\d+)$/g,