flatpickr-ce 4.8.0 → 4.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/flatpickr.js CHANGED
@@ -1,4 +1,4 @@
1
- /* flatpickr v4.8.0, @license MIT */
1
+ /* flatpickr v4.9.0, @license MIT */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
4
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -281,32 +281,59 @@
281
281
 
282
282
  var doNothing = function () { return undefined; };
283
283
  var monthToStr = function (monthNumber, shorthand, locale) { return locale.months[shorthand ? "shorthand" : "longhand"][monthNumber]; };
284
+ // The parser matches names with the i flag, which compares characters by their
285
+ // upper case, so a matched month name is looked up the same way
286
+ var monthIndex = function (monthNames, monthName) {
287
+ return monthNames.map(function (name) { return name.toUpperCase(); }).indexOf(monthName.toUpperCase());
288
+ };
289
+ // Date carries a value past a field's range over into the next field (month 13
290
+ // into the next year, hour 24 into the next day), so a value the field doesn't
291
+ // have gives an invalid date, which the parser reports as such
292
+ var invalidDate = function () { return new Date(NaN); };
293
+ var setInRange = function (min, max, set) { return function (dateObj, data) {
294
+ var value = parseFloat(data);
295
+ if (value < min || value > max)
296
+ return invalidDate();
297
+ set(dateObj, value);
298
+ }; };
299
+ // The day is written after the year and the month (see createDateParser), so a
300
+ // day that month doesn't have moves the date into the next one
301
+ var setDay = function (dateObj, day) {
302
+ var value = parseFloat(day);
303
+ dateObj.setDate(value);
304
+ if (dateObj.getDate() !== value)
305
+ return invalidDate();
306
+ };
307
+ var setMonth = setInRange(1, 12, function (dateObj, month) {
308
+ dateObj.setMonth(month - 1, 1);
309
+ });
310
+ var setHours = setInRange(0, 23, function (dateObj, hour) {
311
+ dateObj.setHours(hour);
312
+ });
313
+ var setHours12 = setInRange(0, 23, function (dateObj, hour) {
314
+ dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + hour);
315
+ });
316
+ var setSeconds = setInRange(0, 59, function (dateObj, seconds) {
317
+ dateObj.setSeconds(seconds);
318
+ });
284
319
  // Month setters start the month on the 1st: the day, if any, is written after
285
320
  // the month (see createDateParser), and the base date's day may not exist in it
286
321
  var revFormat = {
287
322
  D: doNothing,
288
323
  F: function (dateObj, monthName, locale) {
289
- dateObj.setMonth(locale.months.longhand.indexOf(monthName), 1);
290
- },
291
- G: function (dateObj, hour) {
292
- dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
293
- },
294
- H: function (dateObj, hour) {
295
- dateObj.setHours(parseFloat(hour));
296
- },
297
- J: function (dateObj, day) {
298
- dateObj.setDate(parseFloat(day));
324
+ dateObj.setMonth(monthIndex(locale.months.longhand, monthName), 1);
299
325
  },
326
+ G: setHours12,
327
+ H: setHours,
328
+ J: setDay,
300
329
  K: function (dateObj, amPM, locale) {
301
330
  dateObj.setHours((dateObj.getHours() % 12) +
302
331
  12 * int(new RegExp(locale.amPM[1], "i").test(amPM)));
303
332
  },
304
333
  M: function (dateObj, shortMonth, locale) {
305
- dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth), 1);
306
- },
307
- S: function (dateObj, seconds) {
308
- dateObj.setSeconds(parseFloat(seconds));
334
+ dateObj.setMonth(monthIndex(locale.months.shorthand, shortMonth), 1);
309
335
  },
336
+ S: setSeconds,
310
337
  U: function (_, unixSeconds) { return new Date(parseFloat(unixSeconds) * 1000); },
311
338
  W: function (dateObj, weekNum, locale) {
312
339
  var weekNumber = parseInt(weekNum);
@@ -318,28 +345,16 @@
318
345
  dateObj.setFullYear(parseFloat(year));
319
346
  },
320
347
  Z: function (_, ISODate) { return new Date(ISODate); },
321
- d: function (dateObj, day) {
322
- dateObj.setDate(parseFloat(day));
323
- },
324
- h: function (dateObj, hour) {
325
- dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
326
- },
327
- i: function (dateObj, minutes) {
328
- dateObj.setMinutes(parseFloat(minutes));
329
- },
330
- j: function (dateObj, day) {
331
- dateObj.setDate(parseFloat(day));
332
- },
348
+ d: setDay,
349
+ h: setHours12,
350
+ i: setInRange(0, 59, function (dateObj, minutes) {
351
+ dateObj.setMinutes(minutes);
352
+ }),
353
+ j: setDay,
333
354
  l: doNothing,
334
- m: function (dateObj, month) {
335
- dateObj.setMonth(parseFloat(month) - 1, 1);
336
- },
337
- n: function (dateObj, month) {
338
- dateObj.setMonth(parseFloat(month) - 1, 1);
339
- },
340
- s: function (dateObj, seconds) {
341
- dateObj.setSeconds(parseFloat(seconds));
342
- },
355
+ m: setMonth,
356
+ n: setMonth,
357
+ s: setSeconds,
343
358
  u: function (_, unixMillSeconds) {
344
359
  return new Date(parseFloat(unixMillSeconds));
345
360
  },
@@ -507,7 +522,7 @@
507
522
  var escaped = format[i - 1] === "\\" || isBackSlash;
508
523
  if (tokenRegex[token_1] && !escaped) {
509
524
  regexStr += tokenRegex[token_1];
510
- var match = new RegExp(regexStr).exec(date);
525
+ var match = new RegExp(regexStr, "i").exec(date);
511
526
  if (match && (matched = true)) {
512
527
  ops[(_a = writeOrder[token_1]) !== null && _a !== void 0 ? _a : 2].push({
513
528
  fn: revFormat[token_1],
@@ -589,7 +604,7 @@
589
604
  if (hours === maxHr)
590
605
  minutes = Math.min(maxMinutes, minutes);
591
606
  if (hours === maxHr && minutes === maxMinutes)
592
- seconds = config.maxDate.getSeconds();
607
+ seconds = Math.min(seconds, config.maxDate.getSeconds());
593
608
  }
594
609
  return { hours: hours, minutes: minutes, seconds: seconds };
595
610
  }
@@ -647,6 +662,8 @@
647
662
  self._bind = bind;
648
663
  self._setHoursFromDate = setHoursFromDate;
649
664
  self._positionCalendar = positionCalendar;
665
+ self._showValue = showValue;
666
+ self._typedValue = typedValue;
650
667
  self.changeMonth = changeMonth;
651
668
  self.changeYear = changeYear;
652
669
  self.clear = clear;
@@ -819,20 +836,22 @@
819
836
  ? self.config.maxTime
820
837
  : self.config.maxDate;
821
838
  hours = Math.min(hours, maxTime.getHours());
822
- if (hours === maxTime.getHours())
839
+ if (hours === maxTime.getHours()) {
823
840
  minutes = Math.min(minutes, maxTime.getMinutes());
824
- if (minutes === maxTime.getMinutes())
825
- seconds = Math.min(seconds, maxTime.getSeconds());
841
+ if (minutes === maxTime.getMinutes())
842
+ seconds = Math.min(seconds, maxTime.getSeconds());
843
+ }
826
844
  }
827
845
  if (limitMinHours) {
828
846
  var minTime = self.config.minTime !== undefined
829
847
  ? self.config.minTime
830
848
  : self.config.minDate;
831
849
  hours = Math.max(hours, minTime.getHours());
832
- if (hours === minTime.getHours() && minutes < minTime.getMinutes())
833
- minutes = minTime.getMinutes();
834
- if (minutes === minTime.getMinutes())
835
- seconds = Math.max(seconds, minTime.getSeconds());
850
+ if (hours === minTime.getHours()) {
851
+ minutes = Math.max(minutes, minTime.getMinutes());
852
+ if (minutes === minTime.getMinutes())
853
+ seconds = Math.max(seconds, minTime.getSeconds());
854
+ }
836
855
  }
837
856
  }
838
857
  setHours(hours, minutes, seconds);
@@ -842,7 +861,7 @@
842
861
  */
843
862
  function setHoursFromDate(dateObj) {
844
863
  var date = dateObj || self.latestSelectedDateObj;
845
- if (date && date instanceof Date) {
864
+ if (date) {
846
865
  setHours(date.getHours(), date.getMinutes(), date.getSeconds());
847
866
  }
848
867
  }
@@ -876,6 +895,9 @@
876
895
  */
877
896
  function onYearInput(event) {
878
897
  var eventTarget = getEventTarget(event);
898
+ // bound on monthNav, which also holds the month dropdown
899
+ if (self.yearElements.indexOf(eventTarget) === -1)
900
+ return;
879
901
  var year = parseInt(eventTarget.value) + (event.delta || 0);
880
902
  if (year / 1000 > 1 ||
881
903
  (event.key === "Enter" && !/[^\d]/.test(year.toString()))) {
@@ -980,6 +1002,18 @@
980
1002
  });
981
1003
  }
982
1004
  }
1005
+ /**
1006
+ * The date, or the minDate or maxDate it falls outside of; the view falls
1007
+ * back to it when there is no selected date to show
1008
+ */
1009
+ function withinLimits(date) {
1010
+ var _a = self.config, minDate = _a.minDate, maxDate = _a.maxDate;
1011
+ if (minDate && minDate.getTime() > date.getTime())
1012
+ return minDate;
1013
+ if (maxDate && maxDate.getTime() < date.getTime())
1014
+ return maxDate;
1015
+ return date;
1016
+ }
983
1017
  /**
984
1018
  * Set the calendar view to a particular date.
985
1019
  * @param {Date} jumpDate the date to set the view to
@@ -988,12 +1022,7 @@
988
1022
  function jumpToDate(jumpDate, triggerChange) {
989
1023
  var jumpTo = jumpDate !== undefined
990
1024
  ? self.parseDate(jumpDate)
991
- : self.latestSelectedDateObj ||
992
- (self.config.minDate && self.config.minDate > self.now
993
- ? self.config.minDate
994
- : self.config.maxDate && self.config.maxDate < self.now
995
- ? self.config.maxDate
996
- : self.now);
1025
+ : self.latestSelectedDateObj || withinLimits(self.now);
997
1026
  var oldYear = self.currentYear;
998
1027
  var oldMonth = self.currentMonth;
999
1028
  try {
@@ -1091,8 +1120,9 @@
1091
1120
  if (self.element.parentNode)
1092
1121
  self.element.parentNode.insertBefore(wrapper, self.element);
1093
1122
  wrapper.appendChild(self.element);
1094
- if (self.altInput)
1095
- wrapper.appendChild(self.altInput);
1123
+ // next to the input, which with wrap is inside self.element
1124
+ if (self.altInput && self.input.parentNode)
1125
+ self.input.parentNode.insertBefore(self.altInput, self.input.nextSibling);
1096
1126
  wrapper.appendChild(self.calendarContainer);
1097
1127
  }
1098
1128
  }
@@ -1552,16 +1582,16 @@
1552
1582
  function clear(triggerChangeEvent, toInitial) {
1553
1583
  if (triggerChangeEvent === void 0) { triggerChangeEvent = true; }
1554
1584
  if (toInitial === void 0) { toInitial = true; }
1555
- self.input.value = "";
1556
- if (self.altInput !== undefined)
1557
- self.altInput.value = "";
1558
1585
  if (self.mobileInput !== undefined)
1559
1586
  self.mobileInput.value = "";
1560
1587
  self.selectedDates = [];
1561
1588
  self.latestSelectedDateObj = undefined;
1589
+ self._showValue();
1562
1590
  if (toInitial === true) {
1563
- self.currentYear = self._initialDate.getFullYear();
1564
- self.currentMonth = self._initialDate.getMonth();
1591
+ // minDate and maxDate may have changed since init
1592
+ var initialDate = withinLimits(self._initialDate);
1593
+ self.currentYear = initialDate.getFullYear();
1594
+ self.currentMonth = initialDate.getMonth();
1565
1595
  }
1566
1596
  if (self.config.enableTime === true) {
1567
1597
  var _a = getDefaultHours(self.config), hours = _a.hours, minutes = _a.minutes, seconds = _a.seconds;
@@ -1774,20 +1804,30 @@
1774
1804
  !(e.relatedTarget && isCalendarElem(e.relatedTarget)))
1775
1805
  announceCommit(commitTypedValue());
1776
1806
  }
1807
+ /**
1808
+ * What was typed into the input since showValue() wrote it, if anything.
1809
+ * A plugin that lays the value out across inputs of its own (rangePlugin)
1810
+ * reads it from those
1811
+ */
1812
+ function typedValue() {
1813
+ return self._input.value.trimEnd() === getDateStr()
1814
+ ? undefined
1815
+ : self._input.value;
1816
+ }
1777
1817
  /**
1778
1818
  * Applies what was typed into the input, if anything, without announcing it.
1779
1819
  * Returns the state to compare against when announcing.
1780
1820
  */
1781
1821
  function commitTypedValue() {
1782
- // nothing typed since flatpickr last wrote the input
1783
- if (self._input.value.trimEnd() === getDateStr())
1822
+ var typed = self._typedValue();
1823
+ if (typed === undefined)
1784
1824
  return undefined;
1785
1825
  var committed = {
1786
1826
  times: getSelectedTimes(),
1787
1827
  year: self.currentYear,
1788
1828
  month: self.currentMonth,
1789
1829
  };
1790
- self.setDate(self._input.value, false, self.config.altInput ? self.config.altFormat : self.config.dateFormat);
1830
+ self.setDate(typed, false, self.config.altInput ? self.config.altFormat : self.config.dateFormat);
1791
1831
  // hold a typed time within minTime/maxTime, as the time inputs do
1792
1832
  if (self.hourElement !== undefined) {
1793
1833
  setHoursFromInputs();
@@ -1827,6 +1867,10 @@
1827
1867
  var allowInput = self.config.allowInput;
1828
1868
  var allowKeydown = self.isOpen && (!allowInput || !isInput);
1829
1869
  var allowInlineKeydown = self.config.inline && isInput && !allowInput;
1870
+ // the month dropdown moves through its options with the arrow keys itself
1871
+ var isMonthDropdownArrowKey = eventTarget === self.monthsDropdownContainer &&
1872
+ e.keyCode >= 37 &&
1873
+ e.keyCode <= 40;
1830
1874
  if (e.keyCode === 13 && isInput) {
1831
1875
  if (allowInput) {
1832
1876
  announceCommit(commitTypedValue());
@@ -1837,9 +1881,10 @@
1837
1881
  self.open();
1838
1882
  }
1839
1883
  }
1840
- else if (isCalendarElem(eventTarget) ||
1841
- allowKeydown ||
1842
- allowInlineKeydown) {
1884
+ else if (!isMonthDropdownArrowKey &&
1885
+ (isCalendarElem(eventTarget) ||
1886
+ allowKeydown ||
1887
+ allowInlineKeydown)) {
1843
1888
  var isTimeObj = !!self.timeContainer &&
1844
1889
  self.timeContainer.contains(eventTarget);
1845
1890
  switch (e.keyCode) {
@@ -2067,6 +2112,12 @@
2067
2112
  if (self.selectedDates) {
2068
2113
  if (!self.config.allowInvalidPreload)
2069
2114
  self.selectedDates = filterEnabledDates(self.selectedDates, true);
2115
+ // the view and the time inputs follow the latest pick; move it only
2116
+ // when the new limit dropped it (#3002)
2117
+ if (self.latestSelectedDateObj &&
2118
+ self.selectedDates.indexOf(self.latestSelectedDateObj) === -1)
2119
+ self.latestSelectedDateObj =
2120
+ self.selectedDates[self.selectedDates.length - 1];
2070
2121
  if (!self.selectedDates.length && type === "min")
2071
2122
  setHoursFromDate(dateObj);
2072
2123
  updateValue();
@@ -2227,7 +2278,7 @@
2227
2278
  .slice()
2228
2279
  .sort(function (a, b) { return b.length - a.length; })
2229
2280
  .join("|") + ")";
2230
- tokenRegex.K = "(" + self.l10n.amPM[0] + "|" + self.l10n.amPM[1] + "|" + self.l10n.amPM[0].toLowerCase() + "|" + self.l10n.amPM[1].toLowerCase() + ")";
2281
+ tokenRegex.K = "(" + self.l10n.amPM[0] + "|" + self.l10n.amPM[1] + ")";
2231
2282
  if (userConfig.time_24hr === undefined &&
2232
2283
  flatpickr.defaultConfig.time_24hr === undefined) {
2233
2284
  self.config.time_24hr = self.l10n.time_24hr;
@@ -2364,9 +2415,10 @@
2364
2415
  return;
2365
2416
  var target = t;
2366
2417
  var selectedDate = (self.latestSelectedDateObj = new Date(target.dateObj.getTime()));
2367
- var shouldChangeMonth = (selectedDate.getMonth() < self.currentMonth ||
2368
- selectedDate.getMonth() >
2369
- self.currentMonth + self.config.showMonths - 1) &&
2418
+ var firstMonth = self.currentYear * 12 + self.currentMonth;
2419
+ var selectedMonth = selectedDate.getFullYear() * 12 + selectedDate.getMonth();
2420
+ var shouldChangeMonth = (selectedMonth < firstMonth ||
2421
+ selectedMonth > firstMonth + self.config.showMonths - 1) &&
2370
2422
  self.config.mode !== "range";
2371
2423
  self.selectedDateElem = target;
2372
2424
  if (self.config.mode === "single")
@@ -2498,14 +2550,21 @@
2498
2550
  dates = inputDate
2499
2551
  .split(self.l10n.rangeSeparator)
2500
2552
  .map(function (date) { return self.parseDate(date, format); });
2553
+ // getDateStr() writes a same-day range as its one date without
2554
+ // enableTime (#1478): it reads back as both ends (#3010)
2555
+ if (dates.length === 1 && !self.config.enableTime)
2556
+ dates.push(self.parseDate(inputDate, format));
2501
2557
  break;
2502
2558
  }
2503
2559
  }
2504
2560
  else
2505
2561
  self.config.errorHandler(new Error("Invalid date supplied: " + JSON.stringify(inputDate)));
2562
+ // allowInvalidPreload keeps the dates the rules rule out, not the ones
2563
+ // that couldn't be parsed (#3000)
2564
+ var parsed = dates.filter(function (d) { return d instanceof Date; });
2506
2565
  self.selectedDates = self.config.allowInvalidPreload
2507
- ? dates
2508
- : filterEnabledDates(dates.filter(function (d) { return d instanceof Date; }), false);
2566
+ ? parsed
2567
+ : filterEnabledDates(parsed, false);
2509
2568
  if (self.config.mode === "range")
2510
2569
  self.selectedDates.sort(function (a, b) { return a.getTime() - b.getTime(); });
2511
2570
  }
@@ -2564,13 +2623,7 @@
2564
2623
  self._initialDate =
2565
2624
  self.selectedDates.length > 0
2566
2625
  ? self.selectedDates[0]
2567
- : self.config.minDate &&
2568
- self.config.minDate.getTime() > self.now.getTime()
2569
- ? self.config.minDate
2570
- : self.config.maxDate &&
2571
- self.config.maxDate.getTime() < self.now.getTime()
2572
- ? self.config.maxDate
2573
- : self.now;
2626
+ : withinLimits(self.now);
2574
2627
  self.currentYear = self._initialDate.getFullYear();
2575
2628
  self.currentMonth = self._initialDate.getMonth();
2576
2629
  if (self.selectedDates.length > 0)
@@ -2693,9 +2746,7 @@
2693
2746
  }
2694
2747
  function isDateSelected(date) {
2695
2748
  for (var i = 0; i < self.selectedDates.length; i++) {
2696
- var selectedDate = self.selectedDates[i];
2697
- if (selectedDate instanceof Date &&
2698
- compareDates(selectedDate, date) === 0)
2749
+ if (compareDates(self.selectedDates[i], date) === 0)
2699
2750
  return "" + i;
2700
2751
  }
2701
2752
  return false;
@@ -2763,12 +2814,19 @@
2763
2814
  self.formatDate(currentDate, self.mobileFormatStr) !== mobileValue)
2764
2815
  self.mobileInput.value = mobileValue;
2765
2816
  }
2817
+ self._showValue();
2818
+ if (triggerChange !== false)
2819
+ triggerEvent("onValueUpdate");
2820
+ }
2821
+ /**
2822
+ * Writes the selected dates into the input and altInput. A plugin that lays
2823
+ * the value out across inputs of its own (rangePlugin) writes those too
2824
+ */
2825
+ function showValue() {
2766
2826
  self.input.value = getDateStr(self.config.dateFormat);
2767
2827
  if (self.altInput !== undefined) {
2768
2828
  self.altInput.value = getDateStr(self.config.altFormat);
2769
2829
  }
2770
- if (triggerChange !== false)
2771
- triggerEvent("onValueUpdate");
2772
2830
  }
2773
2831
  function onMonthNavClick(e) {
2774
2832
  var eventTarget = getEventTarget(e);