flatpickr-ce 4.7.2 → 4.8.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.
Files changed (119) hide show
  1. package/README.md +26 -0
  2. package/dist/esm/index.js +309 -179
  3. package/dist/esm/l10n/az.js +4 -0
  4. package/dist/esm/l10n/bg.js +1 -0
  5. package/dist/esm/l10n/br.js +67 -0
  6. package/dist/esm/l10n/da.js +4 -0
  7. package/dist/esm/l10n/de.js +4 -0
  8. package/dist/esm/l10n/es.js +4 -0
  9. package/dist/esm/l10n/fi.js +4 -0
  10. package/dist/esm/l10n/fr.js +4 -0
  11. package/dist/esm/l10n/gl.js +62 -0
  12. package/dist/esm/l10n/index.js +6 -0
  13. package/dist/esm/l10n/ko.js +4 -0
  14. package/dist/esm/l10n/pl.js +4 -0
  15. package/dist/esm/l10n/pt.js +4 -0
  16. package/dist/esm/l10n/sv.js +4 -0
  17. package/dist/esm/l10n/tg.js +62 -0
  18. package/dist/esm/l10n/zh.js +4 -0
  19. package/dist/esm/l10n/zh_tw.js +4 -0
  20. package/dist/esm/plugins/confirmDate/confirmDate.js +5 -2
  21. package/dist/esm/plugins/monthSelect/index.js +16 -49
  22. package/dist/esm/plugins/rangePlugin.js +32 -30
  23. package/dist/esm/types/options.js +2 -0
  24. package/dist/esm/utils/dates.js +16 -5
  25. package/dist/esm/utils/formatting.js +4 -4
  26. package/dist/flatpickr.js +390 -191
  27. package/dist/flatpickr.min.js +2 -2
  28. package/dist/l10n/ar-dz.d.ts +3 -0
  29. package/dist/l10n/ar.d.ts +3 -0
  30. package/dist/l10n/at.d.ts +3 -0
  31. package/dist/l10n/az.d.ts +3 -0
  32. package/dist/l10n/az.js +4 -0
  33. package/dist/l10n/be.d.ts +3 -0
  34. package/dist/l10n/bg.d.ts +3 -0
  35. package/dist/l10n/bg.js +1 -0
  36. package/dist/l10n/bn.d.ts +3 -0
  37. package/dist/l10n/br.d.ts +75 -0
  38. package/dist/l10n/br.js +80 -0
  39. package/dist/l10n/bs.d.ts +3 -0
  40. package/dist/l10n/cat.d.ts +3 -0
  41. package/dist/l10n/ckb.d.ts +3 -0
  42. package/dist/l10n/cs.d.ts +3 -0
  43. package/dist/l10n/cy.d.ts +3 -0
  44. package/dist/l10n/da.d.ts +3 -0
  45. package/dist/l10n/da.js +4 -0
  46. package/dist/l10n/de.d.ts +3 -0
  47. package/dist/l10n/de.js +4 -0
  48. package/dist/l10n/eo.d.ts +3 -0
  49. package/dist/l10n/es.d.ts +3 -0
  50. package/dist/l10n/es.js +4 -0
  51. package/dist/l10n/et.d.ts +3 -0
  52. package/dist/l10n/fa.d.ts +3 -0
  53. package/dist/l10n/fi.d.ts +3 -0
  54. package/dist/l10n/fi.js +4 -0
  55. package/dist/l10n/fo.d.ts +3 -0
  56. package/dist/l10n/fr.d.ts +3 -0
  57. package/dist/l10n/fr.js +4 -0
  58. package/dist/l10n/ga.d.ts +3 -0
  59. package/dist/l10n/gl.d.ts +75 -0
  60. package/dist/l10n/gl.js +75 -0
  61. package/dist/l10n/gr.d.ts +3 -0
  62. package/dist/l10n/he.d.ts +3 -0
  63. package/dist/l10n/hi.d.ts +3 -0
  64. package/dist/l10n/hr.d.ts +3 -0
  65. package/dist/l10n/hu.d.ts +3 -0
  66. package/dist/l10n/hy.d.ts +3 -0
  67. package/dist/l10n/id.d.ts +3 -0
  68. package/dist/l10n/index.js +417 -170
  69. package/dist/l10n/is.d.ts +3 -0
  70. package/dist/l10n/it.d.ts +3 -0
  71. package/dist/l10n/ja.d.ts +3 -0
  72. package/dist/l10n/ka.d.ts +3 -0
  73. package/dist/l10n/km.d.ts +3 -0
  74. package/dist/l10n/ko.d.ts +3 -0
  75. package/dist/l10n/ko.js +4 -0
  76. package/dist/l10n/kz.d.ts +3 -0
  77. package/dist/l10n/lt.d.ts +3 -0
  78. package/dist/l10n/lv.d.ts +3 -0
  79. package/dist/l10n/mk.d.ts +3 -0
  80. package/dist/l10n/mn.d.ts +3 -0
  81. package/dist/l10n/ms.d.ts +3 -0
  82. package/dist/l10n/my.d.ts +3 -0
  83. package/dist/l10n/nl.d.ts +3 -0
  84. package/dist/l10n/nn.d.ts +3 -0
  85. package/dist/l10n/no.d.ts +3 -0
  86. package/dist/l10n/pa.d.ts +3 -0
  87. package/dist/l10n/pl.d.ts +3 -0
  88. package/dist/l10n/pl.js +4 -0
  89. package/dist/l10n/pt.d.ts +3 -0
  90. package/dist/l10n/pt.js +4 -0
  91. package/dist/l10n/ro.d.ts +3 -0
  92. package/dist/l10n/ru.d.ts +3 -0
  93. package/dist/l10n/si.d.ts +3 -0
  94. package/dist/l10n/sk.d.ts +3 -0
  95. package/dist/l10n/sl.d.ts +3 -0
  96. package/dist/l10n/sq.d.ts +3 -0
  97. package/dist/l10n/sr-cyr.d.ts +3 -0
  98. package/dist/l10n/sr.d.ts +3 -0
  99. package/dist/l10n/sv.d.ts +3 -0
  100. package/dist/l10n/sv.js +4 -0
  101. package/dist/l10n/tg.d.ts +75 -0
  102. package/dist/l10n/tg.js +76 -0
  103. package/dist/l10n/th.d.ts +3 -0
  104. package/dist/l10n/tr.d.ts +3 -0
  105. package/dist/l10n/uk.d.ts +3 -0
  106. package/dist/l10n/uz.d.ts +3 -0
  107. package/dist/l10n/uz_latn.d.ts +3 -0
  108. package/dist/l10n/vn.d.ts +3 -0
  109. package/dist/l10n/zh.d.ts +3 -0
  110. package/dist/l10n/zh.js +4 -0
  111. package/dist/l10n/zh_tw.d.ts +3 -0
  112. package/dist/l10n/zh_tw.js +4 -0
  113. package/dist/plugins/confirmDate/confirmDate.js +6 -2
  114. package/dist/plugins/minMaxTimePlugin.js +1 -0
  115. package/dist/plugins/monthSelect/index.js +20 -49
  116. package/dist/plugins/rangePlugin.js +45 -30
  117. package/dist/types/locale.d.ts +1 -1
  118. package/dist/types/options.d.ts +3 -1
  119. package/package.json +1 -1
package/dist/flatpickr.js CHANGED
@@ -1,4 +1,4 @@
1
- /* flatpickr v4.7.2, @license MIT */
1
+ /* flatpickr v4.8.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) :
@@ -43,6 +43,7 @@
43
43
  "onChange",
44
44
  "onClose",
45
45
  "onDayCreate",
46
+ "onDaysBuilt",
46
47
  "onDestroy",
47
48
  "onKeyDown",
48
49
  "onMonthChange",
@@ -105,6 +106,7 @@
105
106
  onChange: [],
106
107
  onClose: [],
107
108
  onDayCreate: [],
109
+ onDaysBuilt: [],
108
110
  onDestroy: [],
109
111
  onKeyDown: [],
110
112
  onMonthChange: [],
@@ -279,10 +281,12 @@
279
281
 
280
282
  var doNothing = function () { return undefined; };
281
283
  var monthToStr = function (monthNumber, shorthand, locale) { return locale.months[shorthand ? "shorthand" : "longhand"][monthNumber]; };
284
+ // Month setters start the month on the 1st: the day, if any, is written after
285
+ // the month (see createDateParser), and the base date's day may not exist in it
282
286
  var revFormat = {
283
287
  D: doNothing,
284
288
  F: function (dateObj, monthName, locale) {
285
- dateObj.setMonth(locale.months.longhand.indexOf(monthName));
289
+ dateObj.setMonth(locale.months.longhand.indexOf(monthName), 1);
286
290
  },
287
291
  G: function (dateObj, hour) {
288
292
  dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
@@ -298,7 +302,7 @@
298
302
  12 * int(new RegExp(locale.amPM[1], "i").test(amPM)));
299
303
  },
300
304
  M: function (dateObj, shortMonth, locale) {
301
- dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth));
305
+ dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth), 1);
302
306
  },
303
307
  S: function (dateObj, seconds) {
304
308
  dateObj.setSeconds(parseFloat(seconds));
@@ -328,10 +332,10 @@
328
332
  },
329
333
  l: doNothing,
330
334
  m: function (dateObj, month) {
331
- dateObj.setMonth(parseFloat(month) - 1);
335
+ dateObj.setMonth(parseFloat(month) - 1, 1);
332
336
  },
333
337
  n: function (dateObj, month) {
334
- dateObj.setMonth(parseFloat(month) - 1);
338
+ dateObj.setMonth(parseFloat(month) - 1, 1);
335
339
  },
336
340
  s: function (dateObj, seconds) {
337
341
  dateObj.setSeconds(parseFloat(seconds));
@@ -433,6 +437,17 @@
433
437
  y: function (date) { return String(date.getFullYear()).substring(2); },
434
438
  };
435
439
 
440
+ // The parsed fields are written one by one into a Date, which rolls a day its
441
+ // month doesn't have (February 30th) over into the next month, so the year is
442
+ // written first, then the month, then the rest in format order
443
+ var writeOrder = {
444
+ Y: 0,
445
+ y: 0,
446
+ F: 1,
447
+ M: 1,
448
+ m: 1,
449
+ n: 1,
450
+ };
436
451
  var createDateFormatter = function (_a) {
437
452
  var _b = _a.config, config = _b === void 0 ? defaults : _b, _c = _a.l10n, l10n = _c === void 0 ? english : _c, _d = _a.isMobile, isMobile = _d === void 0 ? false : _d;
438
453
  return function (dateObj, frmt, overrideLocale) {
@@ -455,6 +470,7 @@
455
470
  var createDateParser = function (_a) {
456
471
  var _b = _a.config, config = _b === void 0 ? defaults : _b, _c = _a.l10n, l10n = _c === void 0 ? english : _c;
457
472
  return function (date, givenFormat, timeless, customLocale) {
473
+ var _a;
458
474
  if (date !== 0 && !date)
459
475
  return undefined;
460
476
  var locale = customLocale || l10n;
@@ -484,7 +500,7 @@
484
500
  parsedDate = new Date(date);
485
501
  }
486
502
  else {
487
- var matched = void 0, ops = [];
503
+ var matched = void 0, ops = [[], [], []];
488
504
  for (var i = 0, matchIndex = 0, regexStr = ""; i < format.length; i++) {
489
505
  var token_1 = format[i];
490
506
  var isBackSlash = token_1 === "\\";
@@ -493,7 +509,7 @@
493
509
  regexStr += tokenRegex[token_1];
494
510
  var match = new RegExp(regexStr).exec(date);
495
511
  if (match && (matched = true)) {
496
- ops[token_1 !== "Y" ? "push" : "unshift"]({
512
+ ops[(_a = writeOrder[token_1]) !== null && _a !== void 0 ? _a : 2].push({
497
513
  fn: revFormat[token_1],
498
514
  val: match[++matchIndex],
499
515
  });
@@ -506,9 +522,11 @@
506
522
  !config || !config.noCalendar
507
523
  ? new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0)
508
524
  : new Date(new Date().setHours(0, 0, 0, 0));
509
- ops.forEach(function (_a) {
510
- var fn = _a.fn, val = _a.val;
511
- return (parsedDate = fn(parsedDate, val, locale) || parsedDate);
525
+ ops.forEach(function (group) {
526
+ return group.forEach(function (_a) {
527
+ var fn = _a.fn, val = _a.val;
528
+ return (parsedDate = fn(parsedDate, val, locale) || parsedDate);
529
+ });
512
530
  });
513
531
  parsedDate = matched ? parsedDate : undefined;
514
532
  }
@@ -599,17 +617,29 @@
599
617
  }
600
618
 
601
619
  var DEBOUNCED_CHANGE_MS = 300;
620
+ // Instances created so far, which numbers their fields' ids
621
+ var instanceCount = 0;
602
622
  function FlatpickrInstance(element, instanceConfig) {
603
623
  var self = {
604
624
  config: __assign(__assign({}, defaults), flatpickr.defaultConfig),
605
625
  l10n: english,
606
626
  };
627
+ // The options the user gave, at creation and through set(), as opposed to
628
+ // defaults
629
+ var userConfig = {};
630
+ // The calendar's own fields get ids, unique on the page and kept across
631
+ // rebuilds: Chrome flags a field with neither an id nor a name, and a name
632
+ // would get them submitted with a form holding a static or inline calendar
633
+ var idPrefix = "flatpickr-" + ++instanceCount;
607
634
  // The element may live in another document than the one flatpickr was
608
635
  // loaded in (an iframe, or a window.open() popup), each with its own globals.
609
636
  // A document without a window (e.g. a <template> clone not yet inserted)
610
637
  // can't render anything, so fall back to ours.
611
638
  var win = element.ownerDocument.defaultView || window;
612
639
  var doc = win.document;
640
+ // Listeners on the document and the window, bound only while the calendar
641
+ // is open; kept apart from _handlers, which last until destroy()
642
+ var openHandlers = [];
613
643
  self.parseDate = createDateParser({ config: self.config, l10n: self.l10n });
614
644
  self._handlers = [];
615
645
  self.pluginElements = [];
@@ -871,6 +901,26 @@
871
901
  function triggerChange() {
872
902
  triggerEvent("onChange");
873
903
  }
904
+ /**
905
+ * Listens for what an open calendar answers to outside itself: a press or a
906
+ * focus elsewhere closes it, a resize moves it
907
+ */
908
+ function bindOpenEvents() {
909
+ var listen = function (target, event, handler, options) {
910
+ target.addEventListener(event, handler, options);
911
+ openHandlers.push({
912
+ remove: function () { return target.removeEventListener(event, handler, options); },
913
+ });
914
+ };
915
+ if (!self.config.static)
916
+ listen(win, "resize", debounce(onResize, 50));
917
+ listen(doc, win.ontouchstart !== undefined ? "touchstart" : "mousedown", documentClick);
918
+ listen(doc, "focus", documentClick, { capture: true });
919
+ }
920
+ function unbindOpenEvents() {
921
+ openHandlers.forEach(function (h) { return h.remove(); });
922
+ openHandlers = [];
923
+ }
874
924
  /**
875
925
  * Adds all the necessary event listeners
876
926
  */
@@ -886,7 +936,6 @@
886
936
  setupMobile();
887
937
  return;
888
938
  }
889
- var debouncedResize = debounce(onResize, 50);
890
939
  self._debouncedChange = debounce(triggerChange, DEBOUNCED_CHANGE_MS);
891
940
  if (self.daysContainer && !/iPhone|iPad|iPod/i.test(navigator.userAgent))
892
941
  bind(self.daysContainer, "mouseover", function (e) {
@@ -897,13 +946,6 @@
897
946
  if (self.calendarContainer !== undefined) {
898
947
  bind(self.calendarContainer, "keydown", onKeyDown);
899
948
  }
900
- if (!self.config.inline && !self.config.static)
901
- bind(win, "resize", debouncedResize);
902
- if (win.ontouchstart !== undefined)
903
- bind(doc, "touchstart", documentClick);
904
- else
905
- bind(doc, "mousedown", documentClick);
906
- bind(doc, "focus", documentClick, { capture: true });
907
949
  if (self.config.clickOpens === true) {
908
950
  bind(self._input, "focus", self.open);
909
951
  bind(self._input, "click", self.open);
@@ -913,28 +955,31 @@
913
955
  bind(self.monthNav, ["keyup", "increment"], onYearInput);
914
956
  bind(self.daysContainer, "click", selectDate);
915
957
  }
916
- if (self.timeContainer !== undefined &&
917
- self.minuteElement !== undefined &&
918
- self.hourElement !== undefined) {
919
- var selText = function (e) {
920
- return getEventTarget(e).select();
921
- };
922
- bind(self.timeContainer, ["increment"], updateTime);
923
- bind(self.timeContainer, "blur", updateTime, { capture: true });
924
- bind(self.timeContainer, "click", timeIncrement);
925
- bind([self.hourElement, self.minuteElement], ["focus", "click"], selText);
926
- if (self.secondElement !== undefined)
927
- bind(self.secondElement, "focus", function () { return self.secondElement && self.secondElement.select(); });
928
- if (self.amPM !== undefined) {
929
- bind(self.amPM, "click", function (e) {
930
- updateTime(e);
931
- });
932
- }
933
- }
958
+ bindTimeEvents();
934
959
  if (self.config.allowInput) {
935
960
  bind(self._input, "blur", onBlur);
936
961
  }
937
962
  }
963
+ function bindTimeEvents() {
964
+ if (self.timeContainer === undefined ||
965
+ self.minuteElement === undefined ||
966
+ self.hourElement === undefined)
967
+ return;
968
+ var selText = function (e) {
969
+ return getEventTarget(e).select();
970
+ };
971
+ bind(self.timeContainer, ["increment"], updateTime);
972
+ bind(self.timeContainer, "blur", updateTime, { capture: true });
973
+ bind(self.timeContainer, "click", timeIncrement);
974
+ bind([self.hourElement, self.minuteElement], ["focus", "click"], selText);
975
+ if (self.secondElement !== undefined)
976
+ bind(self.secondElement, "focus", function () { return self.secondElement && self.secondElement.select(); });
977
+ if (self.amPM !== undefined) {
978
+ bind(self.amPM, "click", function (e) {
979
+ updateTime(e);
980
+ });
981
+ }
982
+ }
938
983
  /**
939
984
  * Set the calendar view to a particular date.
940
985
  * @param {Date} jumpDate the date to set the view to
@@ -962,15 +1007,17 @@
962
1007
  e.message = "Invalid date supplied: " + jumpTo;
963
1008
  self.config.errorHandler(e);
964
1009
  }
965
- if (triggerChange && self.currentYear !== oldYear) {
1010
+ if (triggerChange)
1011
+ triggerViewChange(oldYear, oldMonth);
1012
+ self.redraw();
1013
+ }
1014
+ function triggerViewChange(oldYear, oldMonth) {
1015
+ if (self.currentYear !== oldYear) {
966
1016
  triggerEvent("onYearChange");
967
- buildMonthSwitch();
968
1017
  }
969
- if (triggerChange &&
970
- (self.currentYear !== oldYear || self.currentMonth !== oldMonth)) {
1018
+ if (self.currentYear !== oldYear || self.currentMonth !== oldMonth) {
971
1019
  triggerEvent("onMonthChange");
972
1020
  }
973
- self.redraw();
974
1021
  }
975
1022
  /**
976
1023
  * The up/down arrow handler for time inputs
@@ -1065,24 +1112,25 @@
1065
1112
  dayElement.classList.add("today");
1066
1113
  dayElement.setAttribute("aria-current", "date");
1067
1114
  }
1068
- if (dateIsEnabled) {
1115
+ if (dateIsEnabled)
1069
1116
  dayElement.tabIndex = -1;
1070
- if (isDateSelected(date)) {
1071
- dayElement.classList.add("selected");
1117
+ else
1118
+ dayElement.classList.add("flatpickr-disabled");
1119
+ // a selected date can be disabled, e.g. one kept by allowInvalidPreload
1120
+ if (isDateSelected(date)) {
1121
+ dayElement.classList.add("selected");
1122
+ // keyboard navigation starts from here, and disabled days can't be focused
1123
+ if (dateIsEnabled)
1072
1124
  self.selectedDateElem = dayElement;
1073
- if (self.config.mode === "range") {
1074
- toggleClass(dayElement, "startRange", self.selectedDates[0] &&
1075
- compareDates(date, self.selectedDates[0], true) === 0);
1076
- toggleClass(dayElement, "endRange", self.selectedDates[1] &&
1077
- compareDates(date, self.selectedDates[1], true) === 0);
1078
- if (className === "nextMonthDay")
1079
- dayElement.classList.add("inRange");
1080
- }
1125
+ if (self.config.mode === "range") {
1126
+ toggleClass(dayElement, "startRange", self.selectedDates[0] &&
1127
+ compareDates(date, self.selectedDates[0], true) === 0);
1128
+ toggleClass(dayElement, "endRange", self.selectedDates[1] &&
1129
+ compareDates(date, self.selectedDates[1], true) === 0);
1130
+ if (className === "nextMonthDay")
1131
+ dayElement.classList.add("inRange");
1081
1132
  }
1082
1133
  }
1083
- else {
1084
- dayElement.classList.add("flatpickr-disabled");
1085
- }
1086
1134
  if (self.config.mode === "range") {
1087
1135
  if (isDateInRange(date) && !isDateSelected(date))
1088
1136
  dayElement.classList.add("inRange");
@@ -1116,35 +1164,49 @@
1116
1164
  }
1117
1165
  return undefined;
1118
1166
  }
1119
- function getNextAvailableDay(current, delta) {
1120
- var givenMonth = current.className.indexOf("Month") === -1
1121
- ? current.dateObj.getMonth()
1122
- : self.currentMonth;
1123
- var endMonth = delta > 0 ? self.config.showMonths : -1;
1124
- var loopDelta = delta > 0 ? 1 : -1;
1125
- for (var m = givenMonth - self.currentMonth; m != endMonth; m += loopDelta) {
1126
- var month = self.daysContainer.children[m];
1127
- var startIndex = givenMonth - self.currentMonth === m
1128
- ? current.$i + delta
1129
- : delta < 0
1130
- ? month.children.length - 1
1131
- : 0;
1132
- var numMonthDays = month.children.length;
1133
- for (var i = startIndex; i >= 0 && i < numMonthDays && i != (delta > 0 ? numMonthDays : -1); i += loopDelta) {
1134
- var c = month.children[i];
1135
- if (c.className.indexOf("hidden") === -1 &&
1136
- isEnabled(c.dateObj) &&
1137
- Math.abs(current.$i - i) >= Math.abs(delta))
1138
- return focusOnDayElem(c);
1139
- }
1167
+ function findEnabledDate(from, step) {
1168
+ var limit = step > 0 ? self.config.maxDate : self.config.minDate;
1169
+ // stop at minDate/maxDate, and after ten years either way
1170
+ for (var i = 0; i <= 3653; i++) {
1171
+ var date = new Date(from.getFullYear(), from.getMonth(), from.getDate() + i * step);
1172
+ if (limit !== undefined && compareDates(date, limit) * step > 0)
1173
+ return undefined;
1174
+ if (isEnabled(date))
1175
+ return date;
1176
+ }
1177
+ return undefined;
1178
+ }
1179
+ function getVisibleDay(date) {
1180
+ var days = self.daysContainer.querySelectorAll(".flatpickr-day:not(.hidden)");
1181
+ for (var i = 0; i < days.length; i++) {
1182
+ if (compareDates(days[i].dateObj, date) === 0)
1183
+ return days[i];
1140
1184
  }
1141
- self.changeMonth(loopDelta);
1142
- focusOnDay(getFirstAvailableDay(loopDelta), 0);
1143
1185
  return undefined;
1144
1186
  }
1187
+ function focusOnDate(date) {
1188
+ if (getVisibleDay(date) === undefined) {
1189
+ var firstMonth = self.currentYear * 12 + self.currentMonth;
1190
+ var targetMonth = date.getFullYear() * 12 + date.getMonth();
1191
+ // bring the target month in at the edge of the view it moves towards
1192
+ changeMonth(targetMonth < firstMonth
1193
+ ? targetMonth - firstMonth
1194
+ : targetMonth - (firstMonth + self.config.showMonths - 1));
1195
+ }
1196
+ var dayElem = getVisibleDay(date);
1197
+ if (dayElem !== undefined)
1198
+ focusOnDayElem(dayElem);
1199
+ }
1145
1200
  function focusOnDay(current, offset) {
1201
+ // no day grid, or a plugin such as monthSelect has replaced it
1202
+ if (self.daysContainer === undefined ||
1203
+ !self.rContainer.contains(self.daysContainer))
1204
+ return;
1146
1205
  var activeElement = getClosestActiveElement();
1147
- var dayFocused = isInView(activeElement || doc.body);
1206
+ var step = offset > 0 ? 1 : -1;
1207
+ var dayFocused = activeElement !== null &&
1208
+ activeElement.classList.contains("flatpickr-day") &&
1209
+ isInView(activeElement);
1148
1210
  var startElem = current !== undefined
1149
1211
  ? current
1150
1212
  : dayFocused
@@ -1153,15 +1215,28 @@
1153
1215
  ? self.selectedDateElem
1154
1216
  : self.todayDateElem !== undefined && isInView(self.todayDateElem)
1155
1217
  ? self.todayDateElem
1156
- : getFirstAvailableDay(offset > 0 ? 1 : -1);
1218
+ : getFirstAvailableDay(step);
1157
1219
  if (startElem === undefined) {
1158
- self._input.focus();
1220
+ // nothing enabled in view: look past its edge, and keep the focus in
1221
+ // the calendar if there is nothing there either (#3064)
1222
+ var target = offset !== 0
1223
+ ? findEnabledDate(step > 0
1224
+ ? new Date(self.currentYear, self.currentMonth + self.config.showMonths, 1)
1225
+ : new Date(self.currentYear, self.currentMonth, 0), step)
1226
+ : undefined;
1227
+ if (target !== undefined)
1228
+ focusOnDate(target);
1229
+ else
1230
+ self.daysContainer.focus();
1159
1231
  }
1160
1232
  else if (!dayFocused) {
1161
1233
  focusOnDayElem(startElem);
1162
1234
  }
1163
1235
  else {
1164
- getNextAvailableDay(startElem, offset);
1236
+ var dateObj = startElem.dateObj;
1237
+ var target = findEnabledDate(new Date(dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate() + offset), step);
1238
+ if (target !== undefined)
1239
+ focusOnDate(target);
1165
1240
  }
1166
1241
  }
1167
1242
  function buildMonthDays(year, month) {
@@ -1206,9 +1281,11 @@
1206
1281
  if (self.config.mode === "range" && self.selectedDates.length === 1) {
1207
1282
  onMouseOver();
1208
1283
  }
1284
+ triggerEvent("onDaysBuilt");
1209
1285
  }
1210
1286
  function buildMonthSwitch() {
1211
- if (self.config.showMonths > 1 ||
1287
+ if (self.monthsDropdownContainer === undefined ||
1288
+ self.config.showMonths > 1 ||
1212
1289
  self.config.monthSelectorType !== "dropdown")
1213
1290
  return;
1214
1291
  var shouldBuildMonth = function (month) {
@@ -1236,7 +1313,7 @@
1236
1313
  self.monthsDropdownContainer.appendChild(month);
1237
1314
  }
1238
1315
  }
1239
- function buildMonth() {
1316
+ function buildMonth(index) {
1240
1317
  var container = createElement("div", "flatpickr-month");
1241
1318
  var monthNavFragment = doc.createDocumentFragment();
1242
1319
  var monthElement;
@@ -1246,6 +1323,7 @@
1246
1323
  }
1247
1324
  else {
1248
1325
  self.monthsDropdownContainer = createElement("select", "flatpickr-monthDropdown-months");
1326
+ self.monthsDropdownContainer.id = idPrefix + "-month";
1249
1327
  self.monthsDropdownContainer.setAttribute("aria-label", self.l10n.monthAriaLabel);
1250
1328
  bind(self.monthsDropdownContainer, "change", function (e) {
1251
1329
  var target = getEventTarget(e);
@@ -1256,7 +1334,10 @@
1256
1334
  buildMonthSwitch();
1257
1335
  monthElement = self.monthsDropdownContainer;
1258
1336
  }
1259
- var yearInput = createNumberInput("cur-year", { tabindex: "-1" });
1337
+ var yearInput = createNumberInput("cur-year", {
1338
+ id: idPrefix + "-year-" + index,
1339
+ tabindex: "-1",
1340
+ });
1260
1341
  var yearElement = yearInput.getElementsByTagName("input")[0];
1261
1342
  yearElement.setAttribute("aria-label", self.l10n.yearAriaLabel);
1262
1343
  if (self.config.minDate) {
@@ -1286,8 +1367,8 @@
1286
1367
  self.yearElements = [];
1287
1368
  self.monthElements = [];
1288
1369
  }
1289
- for (var m = self.config.showMonths; m--;) {
1290
- var month = buildMonth();
1370
+ for (var i = 0; i < self.config.showMonths; i++) {
1371
+ var month = buildMonth(i);
1291
1372
  self.yearElements.push(month.yearElement);
1292
1373
  self.monthElements.push(month.monthElement);
1293
1374
  self.monthNav.appendChild(month.container);
@@ -1334,10 +1415,12 @@
1334
1415
  self.timeContainer.tabIndex = -1;
1335
1416
  var separator = createElement("span", "flatpickr-time-separator", ":");
1336
1417
  var hourInput = createNumberInput("flatpickr-hour", {
1418
+ id: idPrefix + "-hour",
1337
1419
  "aria-label": self.l10n.hourAriaLabel,
1338
1420
  });
1339
1421
  self.hourElement = hourInput.getElementsByTagName("input")[0];
1340
1422
  var minuteInput = createNumberInput("flatpickr-minute", {
1423
+ id: idPrefix + "-minute",
1341
1424
  "aria-label": self.l10n.minuteAriaLabel,
1342
1425
  });
1343
1426
  self.minuteElement = minuteInput.getElementsByTagName("input")[0];
@@ -1365,7 +1448,9 @@
1365
1448
  self.timeContainer.classList.add("time24hr");
1366
1449
  if (self.config.enableSeconds) {
1367
1450
  self.timeContainer.classList.add("hasSeconds");
1368
- var secondInput = createNumberInput("flatpickr-second");
1451
+ var secondInput = createNumberInput("flatpickr-second", {
1452
+ id: idPrefix + "-second",
1453
+ });
1369
1454
  self.secondElement = secondInput.getElementsByTagName("input")[0];
1370
1455
  self.secondElement.value = pad(self.latestSelectedDateObj
1371
1456
  ? self.latestSelectedDateObj.getSeconds()
@@ -1388,6 +1473,28 @@
1388
1473
  }
1389
1474
  return self.timeContainer;
1390
1475
  }
1476
+ // Rebuilds the time picker after a time option changes through set(). The
1477
+ // core reads the time elements' presence as "time is enabled", so they are
1478
+ // cleared, not just hidden.
1479
+ function rebuildTime() {
1480
+ if (self.isMobile)
1481
+ return setMobileInputType();
1482
+ var previous = self.timeContainer;
1483
+ self.timeContainer = self.hourElement = self.minuteElement = undefined;
1484
+ self.secondElement = self.amPM = undefined;
1485
+ self.calendarContainer.classList.remove("hasTime");
1486
+ self.calendarContainer.classList.remove("noCalendar");
1487
+ if (self.config.enableTime) {
1488
+ self.calendarContainer.insertBefore(buildTime(), previous ||
1489
+ (self.innerContainer
1490
+ ? self.innerContainer.nextSibling
1491
+ : self.calendarContainer.firstChild));
1492
+ bindTimeEvents();
1493
+ setHoursFromDate();
1494
+ }
1495
+ if (previous)
1496
+ self.calendarContainer.removeChild(previous);
1497
+ }
1391
1498
  function buildWeekdays() {
1392
1499
  if (!self.weekdayContainer)
1393
1500
  self.weekdayContainer = createElement("div", "flatpickr-weekdays");
@@ -1433,8 +1540,8 @@
1433
1540
  return;
1434
1541
  self.currentMonth += delta;
1435
1542
  if (self.currentMonth < 0 || self.currentMonth > 11) {
1436
- self.currentYear += self.currentMonth > 11 ? 1 : -1;
1437
- self.currentMonth = (self.currentMonth + 12) % 12;
1543
+ self.currentYear += Math.floor(self.currentMonth / 12);
1544
+ self.currentMonth = ((self.currentMonth % 12) + 12) % 12;
1438
1545
  triggerEvent("onYearChange");
1439
1546
  buildMonthSwitch();
1440
1547
  }
@@ -1467,6 +1574,7 @@
1467
1574
  }
1468
1575
  function close() {
1469
1576
  self.isOpen = false;
1577
+ unbindOpenEvents();
1470
1578
  if (!self.isMobile) {
1471
1579
  if (self.calendarContainer !== undefined) {
1472
1580
  self.calendarContainer.classList.remove("open");
@@ -1484,6 +1592,7 @@
1484
1592
  self._handlers[i].remove();
1485
1593
  }
1486
1594
  self._handlers = [];
1595
+ unbindOpenEvents();
1487
1596
  if (self.mobileInput) {
1488
1597
  if (self.mobileInput.parentNode)
1489
1598
  self.mobileInput.parentNode.removeChild(self.mobileInput);
@@ -1553,38 +1662,38 @@
1553
1662
  function isCalendarElem(elem) {
1554
1663
  return self.calendarContainer.contains(elem);
1555
1664
  }
1665
+ // Only bound while the calendar is open, by bindOpenEvents()
1556
1666
  function documentClick(e) {
1557
- if (self.isOpen && !self.config.inline) {
1558
- var eventTarget_1 = getEventTarget(e);
1559
- var isCalendarElement = isCalendarElem(eventTarget_1);
1560
- var isInput = eventTarget_1 === self.input ||
1561
- eventTarget_1 === self.altInput ||
1562
- self.element.contains(eventTarget_1);
1563
- var lostFocus = !isInput &&
1564
- !isCalendarElement &&
1565
- !isCalendarElem(e.relatedTarget);
1566
- var isIgnored = !self.config.ignoredFocusElements.some(function (elem) {
1567
- return elem.contains(eventTarget_1);
1568
- });
1569
- if (lostFocus && isIgnored) {
1570
- if (self.config.allowInput) {
1571
- self.setDate(self._input.value, false, self.config.altInput
1572
- ? self.config.altFormat
1573
- : self.config.dateFormat);
1574
- }
1575
- if (self.timeContainer !== undefined &&
1576
- self.minuteElement !== undefined &&
1577
- self.hourElement !== undefined &&
1578
- self.input.value !== "" &&
1579
- self.input.value !== undefined) {
1580
- updateTime();
1581
- }
1582
- self.close();
1583
- if (self.config &&
1584
- self.config.mode === "range" &&
1585
- self.selectedDates.length === 1)
1586
- self.clear(false);
1667
+ var eventTarget = getEventTarget(e);
1668
+ var isCalendarElement = isCalendarElem(eventTarget);
1669
+ var isInput = eventTarget === self.input ||
1670
+ eventTarget === self.altInput ||
1671
+ self.element.contains(eventTarget);
1672
+ var lostFocus = !isInput &&
1673
+ !isCalendarElement &&
1674
+ !isCalendarElem(e.relatedTarget);
1675
+ var isIgnored = !self.config.ignoredFocusElements.some(function (elem) {
1676
+ return elem.contains(eventTarget);
1677
+ });
1678
+ if (lostFocus && isIgnored) {
1679
+ var committed = self.config.allowInput ? commitTypedValue() : undefined;
1680
+ // a typed value has already overwritten the time inputs
1681
+ if (committed === undefined &&
1682
+ self.timeContainer !== undefined &&
1683
+ self.minuteElement !== undefined &&
1684
+ self.hourElement !== undefined &&
1685
+ self.input.value !== "" &&
1686
+ self.input.value !== undefined) {
1687
+ updateTime();
1587
1688
  }
1689
+ self.close();
1690
+ // onClose may have destroyed the instance
1691
+ if (self.config === undefined)
1692
+ return;
1693
+ if (self.config.mode === "range" && self.selectedDates.length === 1)
1694
+ self.clear(false);
1695
+ // announce the outcome of the whole click, like selecting a day does
1696
+ announceCommit(committed);
1588
1697
  }
1589
1698
  }
1590
1699
  function changeYear(newYear) {
@@ -1605,7 +1714,6 @@
1605
1714
  if (isNewYear) {
1606
1715
  self.redraw();
1607
1716
  triggerEvent("onYearChange");
1608
- buildMonthSwitch();
1609
1717
  }
1610
1718
  }
1611
1719
  function isEnabled(date, timeless) {
@@ -1662,15 +1770,44 @@
1662
1770
  return false;
1663
1771
  }
1664
1772
  function onBlur(e) {
1665
- var isInput = e.target === self._input;
1666
- var valueChanged = self._input.value.trimEnd() !== getDateStr();
1667
- if (isInput &&
1668
- valueChanged &&
1669
- !(e.relatedTarget && isCalendarElem(e.relatedTarget))) {
1670
- self.setDate(self._input.value, true, e.target === self.altInput
1671
- ? self.config.altFormat
1672
- : self.config.dateFormat);
1773
+ if (e.target === self._input &&
1774
+ !(e.relatedTarget && isCalendarElem(e.relatedTarget)))
1775
+ announceCommit(commitTypedValue());
1776
+ }
1777
+ /**
1778
+ * Applies what was typed into the input, if anything, without announcing it.
1779
+ * Returns the state to compare against when announcing.
1780
+ */
1781
+ function commitTypedValue() {
1782
+ // nothing typed since flatpickr last wrote the input
1783
+ if (self._input.value.trimEnd() === getDateStr())
1784
+ return undefined;
1785
+ var committed = {
1786
+ times: getSelectedTimes(),
1787
+ year: self.currentYear,
1788
+ month: self.currentMonth,
1789
+ };
1790
+ self.setDate(self._input.value, false, self.config.altInput ? self.config.altFormat : self.config.dateFormat);
1791
+ // hold a typed time within minTime/maxTime, as the time inputs do
1792
+ if (self.hourElement !== undefined) {
1793
+ setHoursFromInputs();
1794
+ updateValue(false);
1673
1795
  }
1796
+ return committed;
1797
+ }
1798
+ function announceCommit(committed) {
1799
+ if (committed === undefined)
1800
+ return;
1801
+ triggerViewChange(committed.year, committed.month);
1802
+ if (getSelectedTimes() !== committed.times) {
1803
+ // an emptied value announces like clear(), which leaves out onValueUpdate
1804
+ if (self.selectedDates.length > 0)
1805
+ triggerEvent("onValueUpdate");
1806
+ triggerEvent("onChange");
1807
+ }
1808
+ }
1809
+ function getSelectedTimes() {
1810
+ return self.selectedDates.map(function (d) { return d.getTime(); }).join();
1674
1811
  }
1675
1812
  function onKeyDown(e) {
1676
1813
  // e.key e.keyCode
@@ -1692,9 +1829,7 @@
1692
1829
  var allowInlineKeydown = self.config.inline && isInput && !allowInput;
1693
1830
  if (e.keyCode === 13 && isInput) {
1694
1831
  if (allowInput) {
1695
- self.setDate(self._input.value, true, eventTarget === self.altInput
1696
- ? self.config.altFormat
1697
- : self.config.dateFormat);
1832
+ announceCommit(commitTypedValue());
1698
1833
  self.close();
1699
1834
  return eventTarget.blur();
1700
1835
  }
@@ -1756,7 +1891,8 @@
1756
1891
  if ((self.daysContainer &&
1757
1892
  eventTarget.$i !== undefined) ||
1758
1893
  eventTarget === self.input ||
1759
- eventTarget === self.altInput) {
1894
+ eventTarget === self.altInput ||
1895
+ eventTarget === self.daysContainer) {
1760
1896
  if (e.ctrlKey) {
1761
1897
  e.stopPropagation();
1762
1898
  changeYear(self.currentYear - delta);
@@ -1904,6 +2040,7 @@
1904
2040
  var wasOpen = self.isOpen;
1905
2041
  self.isOpen = true;
1906
2042
  if (!wasOpen) {
2043
+ bindOpenEvents();
1907
2044
  self.calendarContainer.classList.add("open");
1908
2045
  self._input.classList.add("active");
1909
2046
  triggerEvent("onOpen");
@@ -1928,7 +2065,8 @@
1928
2065
  dateObj.getSeconds() > 0;
1929
2066
  }
1930
2067
  if (self.selectedDates) {
1931
- self.selectedDates = self.selectedDates.filter(function (d) { return isEnabled(d); });
2068
+ if (!self.config.allowInvalidPreload)
2069
+ self.selectedDates = filterEnabledDates(self.selectedDates, true);
1932
2070
  if (!self.selectedDates.length && type === "min")
1933
2071
  setHoursFromDate(dateObj);
1934
2072
  updateValue();
@@ -1963,8 +2101,7 @@
1963
2101
  "enableSeconds",
1964
2102
  "disableMobile",
1965
2103
  ];
1966
- var userConfig = __assign(__assign({}, JSON.parse(JSON.stringify(element.dataset || {}))), instanceConfig);
1967
- var formats = {};
2104
+ Object.assign(userConfig, JSON.parse(JSON.stringify(element.dataset || {})), instanceConfig);
1968
2105
  self.config.parseDate = userConfig.parseDate;
1969
2106
  self.config.formatDate = userConfig.formatDate;
1970
2107
  Object.defineProperty(self.config, "enable", {
@@ -1979,23 +2116,6 @@
1979
2116
  self.config._disable = parseDateRules(dates);
1980
2117
  },
1981
2118
  });
1982
- var timeMode = userConfig.mode === "time";
1983
- if (!userConfig.dateFormat && (userConfig.enableTime || timeMode)) {
1984
- var defaultDateFormat = flatpickr.defaultConfig.dateFormat || defaults.dateFormat;
1985
- formats.dateFormat =
1986
- userConfig.noCalendar || timeMode
1987
- ? "H:i" + (userConfig.enableSeconds ? ":S" : "")
1988
- : defaultDateFormat + " H:i" + (userConfig.enableSeconds ? ":S" : "");
1989
- }
1990
- if (userConfig.altInput &&
1991
- (userConfig.enableTime || timeMode) &&
1992
- !userConfig.altFormat) {
1993
- var defaultAltFormat = flatpickr.defaultConfig.altFormat || defaults.altFormat;
1994
- formats.altFormat =
1995
- userConfig.noCalendar || timeMode
1996
- ? "h:i" + (userConfig.enableSeconds ? ":S K" : " K")
1997
- : defaultAltFormat + (" h:i" + (userConfig.enableSeconds ? ":S" : "") + " K");
1998
- }
1999
2119
  Object.defineProperty(self.config, "minDate", {
2000
2120
  get: function () { return self.config._minDate; },
2001
2121
  set: minMaxDateSetter("min"),
@@ -2019,7 +2139,7 @@
2019
2139
  self.config.noCalendar = true;
2020
2140
  self.config.enableTime = true;
2021
2141
  }
2022
- Object.assign(self.config, formats, userConfig);
2142
+ Object.assign(self.config, defaultFormats(), userConfig);
2023
2143
  for (var i = 0; i < boolOpts.length; i++)
2024
2144
  // https://github.com/microsoft/TypeScript/issues/31663
2025
2145
  self.config[boolOpts[i]] =
@@ -2028,14 +2148,6 @@
2028
2148
  HOOKS.filter(function (hook) { return self.config[hook] !== undefined; }).forEach(function (hook) {
2029
2149
  self.config[hook] = arrayify(self.config[hook] || []).map(bindToInstance);
2030
2150
  });
2031
- self.isMobile =
2032
- !self.config.disableMobile &&
2033
- !self.config.inline &&
2034
- self.config.mode === "single" &&
2035
- !self.config.disable.length &&
2036
- !self.config.enable &&
2037
- !self.config.weekNumbers &&
2038
- /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
2039
2151
  for (var i = 0; i < self.config.plugins.length; i++) {
2040
2152
  var pluginConf = self.config.plugins[i](self) || {};
2041
2153
  for (var key in pluginConf) {
@@ -2048,12 +2160,49 @@
2048
2160
  self.config[key] = pluginConf[key];
2049
2161
  }
2050
2162
  }
2163
+ // after the plugins, whose options (e.g. disableMobile) count too
2164
+ self.isMobile =
2165
+ !self.config.disableMobile &&
2166
+ !self.config.inline &&
2167
+ self.config.mode === "single" &&
2168
+ !self.config.disable.length &&
2169
+ !self.config.enable &&
2170
+ !self.config.weekNumbers &&
2171
+ /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
2051
2172
  if (!userConfig.altInputClass) {
2052
2173
  self.config.altInputClass =
2053
2174
  getInputElem().className + " " + self.config.altInputClass;
2054
2175
  }
2055
2176
  triggerEvent("onParseConfig");
2056
2177
  }
2178
+ // The dateFormat/altFormat the user's time options imply when they don't
2179
+ // give their own
2180
+ function defaultFormats() {
2181
+ var defaultDateFormat = flatpickr.defaultConfig.dateFormat || defaults.dateFormat;
2182
+ var defaultAltFormat = flatpickr.defaultConfig.altFormat || defaults.altFormat;
2183
+ var timeMode = userConfig.mode === "time";
2184
+ if (!userConfig.enableTime && !timeMode)
2185
+ return { dateFormat: defaultDateFormat, altFormat: defaultAltFormat };
2186
+ var timeOnly = userConfig.noCalendar || timeMode;
2187
+ var seconds = userConfig.enableSeconds ? ":S" : "";
2188
+ return {
2189
+ dateFormat: timeOnly
2190
+ ? "H:i" + seconds
2191
+ : defaultDateFormat + " H:i" + seconds,
2192
+ altFormat: !userConfig.altInput
2193
+ ? defaultAltFormat
2194
+ : timeOnly
2195
+ ? "h:i" + seconds + " K"
2196
+ : defaultAltFormat + (" h:i" + seconds + " K"),
2197
+ };
2198
+ }
2199
+ function updateDefaultFormats() {
2200
+ var formats = defaultFormats();
2201
+ if (userConfig.dateFormat === undefined)
2202
+ self.config.dateFormat = formats.dateFormat;
2203
+ if (userConfig.altFormat === undefined)
2204
+ self.config.altFormat = formats.altFormat;
2205
+ }
2057
2206
  function getInputElem() {
2058
2207
  return self.config.wrap
2059
2208
  ? element.querySelector("[data-input]")
@@ -2070,10 +2219,15 @@
2070
2219
  : undefined));
2071
2220
  tokenRegex.D = "(" + self.l10n.weekdays.shorthand.join("|") + ")";
2072
2221
  tokenRegex.l = "(" + self.l10n.weekdays.longhand.join("|") + ")";
2073
- tokenRegex.M = "(" + self.l10n.months.shorthand.join("|") + ")";
2074
- tokenRegex.F = "(" + self.l10n.months.longhand.join("|") + ")";
2222
+ tokenRegex.M = "(" + self.l10n.months.shorthand
2223
+ .slice()
2224
+ .sort(function (a, b) { return b.length - a.length; })
2225
+ .join("|") + ")";
2226
+ tokenRegex.F = "(" + self.l10n.months.longhand
2227
+ .slice()
2228
+ .sort(function (a, b) { return b.length - a.length; })
2229
+ .join("|") + ")";
2075
2230
  tokenRegex.K = "(" + self.l10n.amPM[0] + "|" + self.l10n.amPM[1] + "|" + self.l10n.amPM[0].toLowerCase() + "|" + self.l10n.amPM[1].toLowerCase() + ")";
2076
- var userConfig = __assign(__assign({}, instanceConfig), JSON.parse(JSON.stringify(element.dataset || {})));
2077
2231
  if (userConfig.time_24hr === undefined &&
2078
2232
  flatpickr.defaultConfig.time_24hr === undefined) {
2079
2233
  self.config.time_24hr = self.l10n.time_24hr;
@@ -2089,15 +2243,28 @@
2089
2243
  return;
2090
2244
  triggerEvent("onPreCalendarPosition");
2091
2245
  var positionElement = customPositionElement || self._positionElement;
2092
- var calendarHeight = Array.prototype.reduce.call(self.calendarContainer.children, (function (acc, child) { return acc + child.offsetHeight; }), 0), calendarWidth = self.calendarContainer.offsetWidth, configPos = self.config.position.split(" "), configPosVertical = configPos[0], configPosHorizontal = configPos.length > 1 ? configPos[1] : null, inputBounds = positionElement.getBoundingClientRect(), distanceFromBottom = win.innerHeight - inputBounds.bottom, showOnTop = configPosVertical === "above" ||
2093
- (configPosVertical !== "below" &&
2094
- distanceFromBottom < calendarHeight &&
2095
- inputBounds.top > calendarHeight);
2096
- var top = win.pageYOffset +
2246
+ var calendarHeight = Array.prototype.reduce.call(self.calendarContainer.children, (function (acc, child) { return acc + child.offsetHeight; }), 0), calendarWidth = self.calendarContainer.offsetWidth, configPos = self.config.position.split(" "), configPosVertical = configPos[0], configPosHorizontal = configPos.length > 1 ? configPos[1] : null, inputBounds = positionElement.getBoundingClientRect(), distanceFromBottom = win.innerHeight - inputBounds.bottom, isAutoVertical = configPosVertical !== "above" && configPosVertical !== "below", fitsBelow = distanceFromBottom >= calendarHeight, fitsAbove = inputBounds.top > calendarHeight,
2247
+ // the page can't always be scrolled to what sticks out of the viewport
2248
+ // (scroll-locked modals, touch screens), so a calendar that fits
2249
+ // neither above nor below goes on the roomier side, kept in the viewport
2250
+ keepInViewport = isAutoVertical &&
2251
+ !fitsBelow &&
2252
+ !fitsAbove &&
2253
+ !self.config.static &&
2254
+ !self.config.inline, showOnTop = keepInViewport
2255
+ ? inputBounds.top > distanceFromBottom
2256
+ : configPosVertical === "above" ||
2257
+ (isAutoVertical && !fitsBelow && fitsAbove);
2258
+ var anchoredTop = win.pageYOffset +
2097
2259
  inputBounds.top +
2098
2260
  (!showOnTop ? positionElement.offsetHeight + 2 : -calendarHeight - 2);
2099
- toggleClass(self.calendarContainer, "arrowTop", !showOnTop);
2100
- toggleClass(self.calendarContainer, "arrowBottom", showOnTop);
2261
+ // a calendar taller than the viewport sticks to its top
2262
+ var top = keepInViewport
2263
+ ? Math.max(win.pageYOffset, Math.min(anchoredTop, win.pageYOffset + win.innerHeight - calendarHeight))
2264
+ : anchoredTop;
2265
+ // a calendar kept in the viewport may cover the input it would point at
2266
+ toggleClass(self.calendarContainer, "arrowTop", !keepInViewport && !showOnTop);
2267
+ toggleClass(self.calendarContainer, "arrowBottom", !keepInViewport && showOnTop);
2101
2268
  if (self.config.inline)
2102
2269
  return;
2103
2270
  var left = win.pageXOffset + inputBounds.left;
@@ -2139,24 +2306,32 @@
2139
2306
  }
2140
2307
  function getContainingBlockOrigin() {
2141
2308
  var containingBlock = self.calendarContainer.offsetParent;
2142
- // offsetParent is null while the calendar is hidden, and is <body> both
2143
- // when <body> is positioned and when there is no positioned ancestor
2144
- if (containingBlock === null ||
2145
- (containingBlock === doc.body &&
2146
- win.getComputedStyle(containingBlock).position === "static"))
2309
+ // offsetParent is null while the calendar is hidden, and stops at <body>
2310
+ // even when <body> is static: the containing block is then <html> if that
2311
+ // is positioned (scroll locks fix it at -scrollY), or else the page
2312
+ if (containingBlock === doc.body && isStatic(doc.body))
2313
+ containingBlock = isStatic(doc.documentElement)
2314
+ ? null
2315
+ : doc.documentElement;
2316
+ if (containingBlock === null)
2147
2317
  return { top: 0, left: 0 };
2148
2318
  var bounds = containingBlock.getBoundingClientRect();
2319
+ // <html>'s scroll offsets are the page's, already in pageX/YOffset
2320
+ var isRoot = containingBlock === doc.documentElement;
2149
2321
  return {
2150
2322
  top: win.pageYOffset +
2151
2323
  bounds.top +
2152
2324
  containingBlock.clientTop -
2153
- containingBlock.scrollTop,
2325
+ (isRoot ? 0 : containingBlock.scrollTop),
2154
2326
  left: win.pageXOffset +
2155
2327
  bounds.left +
2156
2328
  containingBlock.clientLeft -
2157
- containingBlock.scrollLeft,
2329
+ (isRoot ? 0 : containingBlock.scrollLeft),
2158
2330
  };
2159
2331
  }
2332
+ function isStatic(elem) {
2333
+ return win.getComputedStyle(elem).position === "static";
2334
+ }
2160
2335
  function redraw() {
2161
2336
  if (self.config.noCalendar || self.isMobile)
2162
2337
  return;
@@ -2255,6 +2430,9 @@
2255
2430
  minDate: [jumpToDate],
2256
2431
  maxDate: [jumpToDate],
2257
2432
  positionElement: [updatePositionElement],
2433
+ enableTime: [updateDefaultFormats, rebuildTime],
2434
+ enableSeconds: [updateDefaultFormats, rebuildTime],
2435
+ time_24hr: [rebuildTime],
2258
2436
  clickOpens: [
2259
2437
  function () {
2260
2438
  if (self.config.clickOpens === true) {
@@ -2271,6 +2449,7 @@
2271
2449
  function set(option, value) {
2272
2450
  if (option !== null && typeof option === "object") {
2273
2451
  Object.assign(self.config, option);
2452
+ Object.assign(userConfig, option);
2274
2453
  for (var key in option) {
2275
2454
  if (CALLBACKS[key] !== undefined)
2276
2455
  CALLBACKS[key].forEach(function (x) { return x(); });
@@ -2278,6 +2457,7 @@
2278
2457
  }
2279
2458
  else {
2280
2459
  self.config[option] = value;
2460
+ userConfig[option] = value;
2281
2461
  if (CALLBACKS[option] !== undefined)
2282
2462
  CALLBACKS[option].forEach(function (x) { return x(); });
2283
2463
  else if (HOOKS.indexOf(option) > -1)
@@ -2286,6 +2466,17 @@
2286
2466
  self.redraw();
2287
2467
  updateValue(true);
2288
2468
  }
2469
+ /**
2470
+ * Leaves out the dates minDate, maxDate, enable or disable rule out. The two
2471
+ * ends of a range go together: either on its own reads as a range whose
2472
+ * start was just picked (#3108)
2473
+ */
2474
+ function filterEnabledDates(dates, timeless) {
2475
+ var enabled = dates.filter(function (d) { return isEnabled(d, timeless); });
2476
+ return self.config.mode === "range" && enabled.length < dates.length
2477
+ ? []
2478
+ : enabled;
2479
+ }
2289
2480
  function setSelectedDate(inputDate, format) {
2290
2481
  var dates = [];
2291
2482
  if (inputDate instanceof Array)
@@ -2312,9 +2503,9 @@
2312
2503
  }
2313
2504
  else
2314
2505
  self.config.errorHandler(new Error("Invalid date supplied: " + JSON.stringify(inputDate)));
2315
- self.selectedDates = (self.config.allowInvalidPreload
2506
+ self.selectedDates = self.config.allowInvalidPreload
2316
2507
  ? dates
2317
- : dates.filter(function (d) { return d instanceof Date && isEnabled(d, false); }));
2508
+ : filterEnabledDates(dates.filter(function (d) { return d instanceof Date; }), false);
2318
2509
  if (self.config.mode === "range")
2319
2510
  self.selectedDates.sort(function (a, b) { return a.getTime() - b.getTime(); });
2320
2511
  }
@@ -2431,24 +2622,27 @@
2431
2622
  function updatePositionElement() {
2432
2623
  self._positionElement = self.config.positionElement || self._input;
2433
2624
  }
2434
- function setupMobile() {
2625
+ function setMobileInputType() {
2435
2626
  var inputType = self.config.enableTime
2436
2627
  ? self.config.noCalendar
2437
2628
  ? "time"
2438
2629
  : "datetime-local"
2439
2630
  : "date";
2440
- self.mobileInput = createElement("input", self.input.className + " flatpickr-mobile");
2441
- self.mobileInput.tabIndex = 1;
2442
2631
  self.mobileInput.type = inputType;
2443
- self.mobileInput.disabled = self.input.disabled;
2444
- self.mobileInput.required = self.input.required;
2445
- self.mobileInput.placeholder = self.input.placeholder;
2446
2632
  self.mobileFormatStr =
2447
2633
  inputType === "datetime-local"
2448
2634
  ? "Y-m-d\\TH:i:S"
2449
2635
  : inputType === "date"
2450
2636
  ? "Y-m-d"
2451
2637
  : "H:i:S";
2638
+ }
2639
+ function setupMobile() {
2640
+ self.mobileInput = createElement("input", self.input.className + " flatpickr-mobile");
2641
+ self.mobileInput.tabIndex = 1;
2642
+ setMobileInputType();
2643
+ self.mobileInput.disabled = self.input.disabled;
2644
+ self.mobileInput.required = self.input.required;
2645
+ self.mobileInput.placeholder = self.input.placeholder;
2452
2646
  if (self.selectedDates.length > 0) {
2453
2647
  self.mobileInput.defaultValue = self.mobileInput.value = self.formatDate(self.selectedDates[0], self.mobileFormatStr);
2454
2648
  }
@@ -2559,10 +2753,15 @@
2559
2753
  function updateValue(triggerChange) {
2560
2754
  if (triggerChange === void 0) { triggerChange = true; }
2561
2755
  if (self.mobileInput !== undefined && self.mobileFormatStr) {
2562
- self.mobileInput.value =
2563
- self.latestSelectedDateObj !== undefined
2564
- ? self.formatDate(self.latestSelectedDateObj, self.mobileFormatStr)
2565
- : "";
2756
+ var mobileValue = self.latestSelectedDateObj !== undefined
2757
+ ? self.formatDate(self.latestSelectedDateObj, self.mobileFormatStr)
2758
+ : "";
2759
+ var currentDate = self.parseDate(self.mobileInput.value, self.mobileFormatStr);
2760
+ // Browsers leave out zero seconds ("15:05", not "15:05:00"). Writing an
2761
+ // equivalent value back resets the field the user is typing in (#3144)
2762
+ if (currentDate === undefined ||
2763
+ self.formatDate(currentDate, self.mobileFormatStr) !== mobileValue)
2764
+ self.mobileInput.value = mobileValue;
2566
2765
  }
2567
2766
  self.input.value = getDateStr(self.config.dateFormat);
2568
2767
  if (self.altInput !== undefined) {