You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2720 lines
121 KiB

  1. /* flatpickr v4.6.13, @license MIT */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  4. typeof define === 'function' && define.amd ? define(factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.flatpickr = factory());
  6. }(this, (function () { 'use strict';
  7. /*! *****************************************************************************
  8. Copyright (c) Microsoft Corporation.
  9. Permission to use, copy, modify, and/or distribute this software for any
  10. purpose with or without fee is hereby granted.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  12. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  13. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  14. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  16. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. PERFORMANCE OF THIS SOFTWARE.
  18. ***************************************************************************** */
  19. var __assign = function() {
  20. __assign = Object.assign || function __assign(t) {
  21. for (var s, i = 1, n = arguments.length; i < n; i++) {
  22. s = arguments[i];
  23. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  24. }
  25. return t;
  26. };
  27. return __assign.apply(this, arguments);
  28. };
  29. function __spreadArrays() {
  30. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  31. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  32. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  33. r[k] = a[j];
  34. return r;
  35. }
  36. var HOOKS = [
  37. "onChange",
  38. "onClose",
  39. "onDayCreate",
  40. "onDestroy",
  41. "onKeyDown",
  42. "onMonthChange",
  43. "onOpen",
  44. "onParseConfig",
  45. "onReady",
  46. "onValueUpdate",
  47. "onYearChange",
  48. "onPreCalendarPosition",
  49. ];
  50. var defaults = {
  51. _disable: [],
  52. allowInput: false,
  53. allowInvalidPreload: false,
  54. altFormat: "F j, Y",
  55. altInput: false,
  56. altInputClass: "form-control input",
  57. animate: typeof window === "object" &&
  58. window.navigator.userAgent.indexOf("MSIE") === -1,
  59. ariaDateFormat: "F j, Y",
  60. autoFillDefaultTime: true,
  61. clickOpens: true,
  62. closeOnSelect: true,
  63. conjunction: ", ",
  64. dateFormat: "Y-m-d",
  65. defaultHour: 12,
  66. defaultMinute: 0,
  67. defaultSeconds: 0,
  68. disable: [],
  69. disableMobile: false,
  70. enableSeconds: false,
  71. enableTime: false,
  72. errorHandler: function (err) {
  73. return typeof console !== "undefined" && console.warn(err);
  74. },
  75. getWeek: function (givenDate) {
  76. var date = new Date(givenDate.getTime());
  77. date.setHours(0, 0, 0, 0);
  78. // Thursday in current week decides the year.
  79. date.setDate(date.getDate() + 3 - ((date.getDay() + 6) % 7));
  80. // January 4 is always in week 1.
  81. var week1 = new Date(date.getFullYear(), 0, 4);
  82. // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  83. return (1 +
  84. Math.round(((date.getTime() - week1.getTime()) / 86400000 -
  85. 3 +
  86. ((week1.getDay() + 6) % 7)) /
  87. 7));
  88. },
  89. hourIncrement: 1,
  90. ignoredFocusElements: [],
  91. inline: false,
  92. locale: "default",
  93. minuteIncrement: 5,
  94. mode: "single",
  95. monthSelectorType: "dropdown",
  96. 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>",
  97. noCalendar: false,
  98. now: new Date(),
  99. onChange: [],
  100. onClose: [],
  101. onDayCreate: [],
  102. onDestroy: [],
  103. onKeyDown: [],
  104. onMonthChange: [],
  105. onOpen: [],
  106. onParseConfig: [],
  107. onReady: [],
  108. onValueUpdate: [],
  109. onYearChange: [],
  110. onPreCalendarPosition: [],
  111. plugins: [],
  112. position: "auto",
  113. positionElement: undefined,
  114. 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>",
  115. shorthandCurrentMonth: false,
  116. showMonths: 1,
  117. static: false,
  118. time_24hr: false,
  119. weekNumbers: false,
  120. wrap: false,
  121. };
  122. var english = {
  123. weekdays: {
  124. shorthand: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  125. longhand: [
  126. "Sunday",
  127. "Monday",
  128. "Tuesday",
  129. "Wednesday",
  130. "Thursday",
  131. "Friday",
  132. "Saturday",
  133. ],
  134. },
  135. months: {
  136. shorthand: [
  137. "Jan",
  138. "Feb",
  139. "Mar",
  140. "Apr",
  141. "May",
  142. "Jun",
  143. "Jul",
  144. "Aug",
  145. "Sep",
  146. "Oct",
  147. "Nov",
  148. "Dec",
  149. ],
  150. longhand: [
  151. "January",
  152. "February",
  153. "March",
  154. "April",
  155. "May",
  156. "June",
  157. "July",
  158. "August",
  159. "September",
  160. "October",
  161. "November",
  162. "December",
  163. ],
  164. },
  165. daysInMonth: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  166. firstDayOfWeek: 0,
  167. ordinal: function (nth) {
  168. var s = nth % 100;
  169. if (s > 3 && s < 21)
  170. return "th";
  171. switch (s % 10) {
  172. case 1:
  173. return "st";
  174. case 2:
  175. return "nd";
  176. case 3:
  177. return "rd";
  178. default:
  179. return "th";
  180. }
  181. },
  182. rangeSeparator: " to ",
  183. weekAbbreviation: "Wk",
  184. scrollTitle: "Scroll to increment",
  185. toggleTitle: "Click to toggle",
  186. amPM: ["AM", "PM"],
  187. yearAriaLabel: "Year",
  188. monthAriaLabel: "Month",
  189. hourAriaLabel: "Hour",
  190. minuteAriaLabel: "Minute",
  191. time_24hr: false,
  192. };
  193. var pad = function (number, length) {
  194. if (length === void 0) { length = 2; }
  195. return ("000" + number).slice(length * -1);
  196. };
  197. var int = function (bool) { return (bool === true ? 1 : 0); };
  198. /* istanbul ignore next */
  199. function debounce(fn, wait) {
  200. var t;
  201. return function () {
  202. var _this = this;
  203. var args = arguments;
  204. clearTimeout(t);
  205. t = setTimeout(function () { return fn.apply(_this, args); }, wait);
  206. };
  207. }
  208. var arrayify = function (obj) {
  209. return obj instanceof Array ? obj : [obj];
  210. };
  211. function toggleClass(elem, className, bool) {
  212. if (bool === true)
  213. return elem.classList.add(className);
  214. elem.classList.remove(className);
  215. }
  216. function createElement(tag, className, content) {
  217. var e = window.document.createElement(tag);
  218. className = className || "";
  219. content = content || "";
  220. e.className = className;
  221. if (content !== undefined)
  222. e.textContent = content;
  223. return e;
  224. }
  225. function clearNode(node) {
  226. while (node.firstChild)
  227. node.removeChild(node.firstChild);
  228. }
  229. function findParent(node, condition) {
  230. if (condition(node))
  231. return node;
  232. else if (node.parentNode)
  233. return findParent(node.parentNode, condition);
  234. return undefined; // nothing found
  235. }
  236. function createNumberInput(inputClassName, opts) {
  237. var wrapper = createElement("div", "numInputWrapper"), numInput = createElement("input", "numInput " + inputClassName), arrowUp = createElement("span", "arrowUp"), arrowDown = createElement("span", "arrowDown");
  238. if (navigator.userAgent.indexOf("MSIE 9.0") === -1) {
  239. numInput.type = "number";
  240. }
  241. else {
  242. numInput.type = "text";
  243. numInput.pattern = "\\d*";
  244. }
  245. if (opts !== undefined)
  246. for (var key in opts)
  247. numInput.setAttribute(key, opts[key]);
  248. wrapper.appendChild(numInput);
  249. wrapper.appendChild(arrowUp);
  250. wrapper.appendChild(arrowDown);
  251. return wrapper;
  252. }
  253. function getEventTarget(event) {
  254. try {
  255. if (typeof event.composedPath === "function") {
  256. var path = event.composedPath();
  257. return path[0];
  258. }
  259. return event.target;
  260. }
  261. catch (error) {
  262. return event.target;
  263. }
  264. }
  265. var doNothing = function () { return undefined; };
  266. var monthToStr = function (monthNumber, shorthand, locale) { return locale.months[shorthand ? "shorthand" : "longhand"][monthNumber]; };
  267. var revFormat = {
  268. D: doNothing,
  269. F: function (dateObj, monthName, locale) {
  270. dateObj.setMonth(locale.months.longhand.indexOf(monthName));
  271. },
  272. G: function (dateObj, hour) {
  273. dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
  274. },
  275. H: function (dateObj, hour) {
  276. dateObj.setHours(parseFloat(hour));
  277. },
  278. J: function (dateObj, day) {
  279. dateObj.setDate(parseFloat(day));
  280. },
  281. K: function (dateObj, amPM, locale) {
  282. dateObj.setHours((dateObj.getHours() % 12) +
  283. 12 * int(new RegExp(locale.amPM[1], "i").test(amPM)));
  284. },
  285. M: function (dateObj, shortMonth, locale) {
  286. dateObj.setMonth(locale.months.shorthand.indexOf(shortMonth));
  287. },
  288. S: function (dateObj, seconds) {
  289. dateObj.setSeconds(parseFloat(seconds));
  290. },
  291. U: function (_, unixSeconds) { return new Date(parseFloat(unixSeconds) * 1000); },
  292. W: function (dateObj, weekNum, locale) {
  293. var weekNumber = parseInt(weekNum);
  294. var date = new Date(dateObj.getFullYear(), 0, 2 + (weekNumber - 1) * 7, 0, 0, 0, 0);
  295. date.setDate(date.getDate() - date.getDay() + locale.firstDayOfWeek);
  296. return date;
  297. },
  298. Y: function (dateObj, year) {
  299. dateObj.setFullYear(parseFloat(year));
  300. },
  301. Z: function (_, ISODate) { return new Date(ISODate); },
  302. d: function (dateObj, day) {
  303. dateObj.setDate(parseFloat(day));
  304. },
  305. h: function (dateObj, hour) {
  306. dateObj.setHours((dateObj.getHours() >= 12 ? 12 : 0) + parseFloat(hour));
  307. },
  308. i: function (dateObj, minutes) {
  309. dateObj.setMinutes(parseFloat(minutes));
  310. },
  311. j: function (dateObj, day) {
  312. dateObj.setDate(parseFloat(day));
  313. },
  314. l: doNothing,
  315. m: function (dateObj, month) {
  316. dateObj.setMonth(parseFloat(month) - 1);
  317. },
  318. n: function (dateObj, month) {
  319. dateObj.setMonth(parseFloat(month) - 1);
  320. },
  321. s: function (dateObj, seconds) {
  322. dateObj.setSeconds(parseFloat(seconds));
  323. },
  324. u: function (_, unixMillSeconds) {
  325. return new Date(parseFloat(unixMillSeconds));
  326. },
  327. w: doNothing,
  328. y: function (dateObj, year) {
  329. dateObj.setFullYear(2000 + parseFloat(year));
  330. },
  331. };
  332. var tokenRegex = {
  333. D: "",
  334. F: "",
  335. G: "(\\d\\d|\\d)",
  336. H: "(\\d\\d|\\d)",
  337. J: "(\\d\\d|\\d)\\w+",
  338. K: "",
  339. M: "",
  340. S: "(\\d\\d|\\d)",
  341. U: "(.+)",
  342. W: "(\\d\\d|\\d)",
  343. Y: "(\\d{4})",
  344. Z: "(.+)",
  345. d: "(\\d\\d|\\d)",
  346. h: "(\\d\\d|\\d)",
  347. i: "(\\d\\d|\\d)",
  348. j: "(\\d\\d|\\d)",
  349. l: "",
  350. m: "(\\d\\d|\\d)",
  351. n: "(\\d\\d|\\d)",
  352. s: "(\\d\\d|\\d)",
  353. u: "(.+)",
  354. w: "(\\d\\d|\\d)",
  355. y: "(\\d{2})",
  356. };
  357. var formats = {
  358. // get the date in UTC
  359. Z: function (date) { return date.toISOString(); },
  360. // weekday name, short, e.g. Thu
  361. D: function (date, locale, options) {
  362. return locale.weekdays.shorthand[formats.w(date, locale, options)];
  363. },
  364. // full month name e.g. January
  365. F: function (date, locale, options) {
  366. return monthToStr(formats.n(date, locale, options) - 1, false, locale);
  367. },
  368. // padded hour 1-12
  369. G: function (date, locale, options) {
  370. return pad(formats.h(date, locale, options));
  371. },
  372. // hours with leading zero e.g. 03
  373. H: function (date) { return pad(date.getHours()); },
  374. // day (1-30) with ordinal suffix e.g. 1st, 2nd
  375. J: function (date, locale) {
  376. return locale.ordinal !== undefined
  377. ? date.getDate() + locale.ordinal(date.getDate())
  378. : date.getDate();
  379. },
  380. // AM/PM
  381. K: function (date, locale) { return locale.amPM[int(date.getHours() > 11)]; },
  382. // shorthand month e.g. Jan, Sep, Oct, etc
  383. M: function (date, locale) {
  384. return monthToStr(date.getMonth(), true, locale);
  385. },
  386. // seconds 00-59
  387. S: function (date) { return pad(date.getSeconds()); },
  388. // unix timestamp
  389. U: function (date) { return date.getTime() / 1000; },
  390. W: function (date, _, options) {
  391. return options.getWeek(date);
  392. },
  393. // full year e.g. 2016, padded (0001-9999)
  394. Y: function (date) { return pad(date.getFullYear(), 4); },
  395. // day in month, padded (01-30)
  396. d: function (date) { return pad(date.getDate()); },
  397. // hour from 1-12 (am/pm)
  398. h: function (date) { return (date.getHours() % 12 ? date.getHours() % 12 : 12); },
  399. // minutes, padded with leading zero e.g. 09
  400. i: function (date) { return pad(date.getMinutes()); },
  401. // day in month (1-30)
  402. j: function (date) { return date.getDate(); },
  403. // weekday name, full, e.g. Thursday
  404. l: function (date, locale) {
  405. return locale.weekdays.longhand[date.getDay()];
  406. },
  407. // padded month number (01-12)
  408. m: function (date) { return pad(date.getMonth() + 1); },
  409. // the month number (1-12)
  410. n: function (date) { return date.getMonth() + 1; },
  411. // seconds 0-59
  412. s: function (date) { return date.getSeconds(); },
  413. // Unix Milliseconds
  414. u: function (date) { return date.getTime(); },
  415. // number of the day of the week
  416. w: function (date) { return date.getDay(); },
  417. // last two digits of year e.g. 16 for 2016
  418. y: function (date) { return String(date.getFullYear()).substring(2); },
  419. };
  420. var createDateFormatter = function (_a) {
  421. 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;
  422. return function (dateObj, frmt, overrideLocale) {
  423. var locale = overrideLocale || l10n;
  424. if (config.formatDate !== undefined && !isMobile) {
  425. return config.formatDate(dateObj, frmt, locale);
  426. }
  427. return frmt
  428. .split("")
  429. .map(function (c, i, arr) {
  430. return formats[c] && arr[i - 1] !== "\\"
  431. ? formats[c](dateObj, locale, config)
  432. : c !== "\\"
  433. ? c
  434. : "";
  435. })
  436. .join("");
  437. };
  438. };
  439. var createDateParser = function (_a) {
  440. var _b = _a.config, config = _b === void 0 ? defaults : _b, _c = _a.l10n, l10n = _c === void 0 ? english : _c;
  441. return function (date, givenFormat, timeless, customLocale) {
  442. if (date !== 0 && !date)
  443. return undefined;
  444. var locale = customLocale || l10n;
  445. var parsedDate;
  446. var dateOrig = date;
  447. if (date instanceof Date)
  448. parsedDate = new Date(date.getTime());
  449. else if (typeof date !== "string" &&
  450. date.toFixed !== undefined // timestamp
  451. )
  452. // create a copy
  453. parsedDate = new Date(date);
  454. else if (typeof date === "string") {
  455. // date string
  456. var format = givenFormat || (config || defaults).dateFormat;
  457. var datestr = String(date).trim();
  458. if (datestr === "today") {
  459. parsedDate = new Date();
  460. timeless = true;
  461. }
  462. else if (config && config.parseDate) {
  463. parsedDate = config.parseDate(date, format);
  464. }
  465. else if (/Z$/.test(datestr) ||
  466. /GMT$/.test(datestr) // datestrings w/ timezone
  467. ) {
  468. parsedDate = new Date(date);
  469. }
  470. else {
  471. var matched = void 0, ops = [];
  472. for (var i = 0, matchIndex = 0, regexStr = ""; i < format.length; i++) {
  473. var token_1 = format[i];
  474. var isBackSlash = token_1 === "\\";
  475. var escaped = format[i - 1] === "\\" || isBackSlash;
  476. if (tokenRegex[token_1] && !escaped) {
  477. regexStr += tokenRegex[token_1];
  478. var match = new RegExp(regexStr).exec(date);
  479. if (match && (matched = true)) {
  480. ops[token_1 !== "Y" ? "push" : "unshift"]({
  481. fn: revFormat[token_1],
  482. val: match[++matchIndex],
  483. });
  484. }
  485. }
  486. else if (!isBackSlash)
  487. regexStr += "."; // don't really care
  488. }
  489. parsedDate =
  490. !config || !config.noCalendar
  491. ? new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0)
  492. : new Date(new Date().setHours(0, 0, 0, 0));
  493. ops.forEach(function (_a) {
  494. var fn = _a.fn, val = _a.val;
  495. return (parsedDate = fn(parsedDate, val, locale) || parsedDate);
  496. });
  497. parsedDate = matched ? parsedDate : undefined;
  498. }
  499. }
  500. /* istanbul ignore next */
  501. if (!(parsedDate instanceof Date && !isNaN(parsedDate.getTime()))) {
  502. config.errorHandler(new Error("Invalid date provided: " + dateOrig));
  503. return undefined;
  504. }
  505. if (timeless === true)
  506. parsedDate.setHours(0, 0, 0, 0);
  507. return parsedDate;
  508. };
  509. };
  510. /**
  511. * Compute the difference in dates, measured in ms
  512. */
  513. function compareDates(date1, date2, timeless) {
  514. if (timeless === void 0) { timeless = true; }
  515. if (timeless !== false) {
  516. return (new Date(date1.getTime()).setHours(0, 0, 0, 0) -
  517. new Date(date2.getTime()).setHours(0, 0, 0, 0));
  518. }
  519. return date1.getTime() - date2.getTime();
  520. }
  521. var isBetween = function (ts, ts1, ts2) {
  522. return ts > Math.min(ts1, ts2) && ts < Math.max(ts1, ts2);
  523. };
  524. var calculateSecondsSinceMidnight = function (hours, minutes, seconds) {
  525. return hours * 3600 + minutes * 60 + seconds;
  526. };
  527. var parseSeconds = function (secondsSinceMidnight) {
  528. var hours = Math.floor(secondsSinceMidnight / 3600), minutes = (secondsSinceMidnight - hours * 3600) / 60;
  529. return [hours, minutes, secondsSinceMidnight - hours * 3600 - minutes * 60];
  530. };
  531. var duration = {
  532. DAY: 86400000,
  533. };
  534. function getDefaultHours(config) {
  535. var hours = config.defaultHour;
  536. var minutes = config.defaultMinute;
  537. var seconds = config.defaultSeconds;
  538. if (config.minDate !== undefined) {
  539. var minHour = config.minDate.getHours();
  540. var minMinutes = config.minDate.getMinutes();
  541. var minSeconds = config.minDate.getSeconds();
  542. if (hours < minHour) {
  543. hours = minHour;
  544. }
  545. if (hours === minHour && minutes < minMinutes) {
  546. minutes = minMinutes;
  547. }
  548. if (hours === minHour && minutes === minMinutes && seconds < minSeconds)
  549. seconds = config.minDate.getSeconds();
  550. }
  551. if (config.maxDate !== undefined) {
  552. var maxHr = config.maxDate.getHours();
  553. var maxMinutes = config.maxDate.getMinutes();
  554. hours = Math.min(hours, maxHr);
  555. if (hours === maxHr)
  556. minutes = Math.min(maxMinutes, minutes);
  557. if (hours === maxHr && minutes === maxMinutes)
  558. seconds = config.maxDate.getSeconds();
  559. }
  560. return { hours: hours, minutes: minutes, seconds: seconds };
  561. }
  562. if (typeof Object.assign !== "function") {
  563. Object.assign = function (target) {
  564. var args = [];
  565. for (var _i = 1; _i < arguments.length; _i++) {
  566. args[_i - 1] = arguments[_i];
  567. }
  568. if (!target) {
  569. throw TypeError("Cannot convert undefined or null to object");
  570. }
  571. var _loop_1 = function (source) {
  572. if (source) {
  573. Object.keys(source).forEach(function (key) { return (target[key] = source[key]); });
  574. }
  575. };
  576. for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
  577. var source = args_1[_a];
  578. _loop_1(source);
  579. }
  580. return target;
  581. };
  582. }
  583. var DEBOUNCED_CHANGE_MS = 300;
  584. function FlatpickrInstance(element, instanceConfig) {
  585. var self = {
  586. config: __assign(__assign({}, defaults), flatpickr.defaultConfig),
  587. l10n: english,
  588. };
  589. self.parseDate = createDateParser({ config: self.config, l10n: self.l10n });
  590. self._handlers = [];
  591. self.pluginElements = [];
  592. self.loadedPlugins = [];
  593. self._bind = bind;
  594. self._setHoursFromDate = setHoursFromDate;
  595. self._positionCalendar = positionCalendar;
  596. self.changeMonth = changeMonth;
  597. self.changeYear = changeYear;
  598. self.clear = clear;
  599. self.close = close;
  600. self.onMouseOver = onMouseOver;
  601. self._createElement = createElement;
  602. self.createDay = createDay;
  603. self.destroy = destroy;
  604. self.isEnabled = isEnabled;
  605. self.jumpToDate = jumpToDate;
  606. self.updateValue = updateValue;
  607. self.open = open;
  608. self.redraw = redraw;
  609. self.set = set;
  610. self.setDate = setDate;
  611. self.toggle = toggle;
  612. function setupHelperFunctions() {
  613. self.utils = {
  614. getDaysInMonth: function (month, yr) {
  615. if (month === void 0) { month = self.currentMonth; }
  616. if (yr === void 0) { yr = self.currentYear; }
  617. if (month === 1 && ((yr % 4 === 0 && yr % 100 !== 0) || yr % 400 === 0))
  618. return 29;
  619. return self.l10n.daysInMonth[month];
  620. },
  621. };
  622. }
  623. function init() {
  624. self.element = self.input = element;
  625. self.isOpen = false;
  626. parseConfig();
  627. setupLocale();
  628. setupInputs();
  629. setupDates();
  630. setupHelperFunctions();
  631. if (!self.isMobile)
  632. build();
  633. bindEvents();
  634. if (self.selectedDates.length || self.config.noCalendar) {
  635. if (self.config.enableTime) {
  636. setHoursFromDate(self.config.noCalendar ? self.latestSelectedDateObj : undefined);
  637. }
  638. updateValue(false);
  639. }
  640. setCalendarWidth();
  641. var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  642. /* TODO: investigate this further
  643. Currently, there is weird positioning behavior in safari causing pages
  644. to scroll up. https://github.com/chmln/flatpickr/issues/563
  645. However, most browsers are not Safari and positioning is expensive when used
  646. in scale. https://github.com/chmln/flatpickr/issues/1096
  647. */
  648. if (!self.isMobile && isSafari) {
  649. positionCalendar();
  650. }
  651. triggerEvent("onReady");
  652. }
  653. function getClosestActiveElement() {
  654. var _a;
  655. return (((_a = self.calendarContainer) === null || _a === void 0 ? void 0 : _a.getRootNode())
  656. .activeElement || document.activeElement);
  657. }
  658. function bindToInstance(fn) {
  659. return fn.bind(self);
  660. }
  661. function setCalendarWidth() {
  662. var config = self.config;
  663. if (config.weekNumbers === false && config.showMonths === 1) {
  664. return;
  665. }
  666. else if (config.noCalendar !== true) {
  667. window.requestAnimationFrame(function () {
  668. if (self.calendarContainer !== undefined) {
  669. self.calendarContainer.style.visibility = "hidden";
  670. self.calendarContainer.style.display = "block";
  671. }
  672. if (self.daysContainer !== undefined) {
  673. var daysWidth = (self.days.offsetWidth + 1) * config.showMonths;
  674. self.daysContainer.style.width = daysWidth + "px";
  675. self.calendarContainer.style.width =
  676. daysWidth +
  677. (self.weekWrapper !== undefined
  678. ? self.weekWrapper.offsetWidth
  679. : 0) +
  680. "px";
  681. self.calendarContainer.style.removeProperty("visibility");
  682. self.calendarContainer.style.removeProperty("display");
  683. }
  684. });
  685. }
  686. }
  687. /**
  688. * The handler for all events targeting the time inputs
  689. */
  690. function updateTime(e) {
  691. if (self.selectedDates.length === 0) {
  692. var defaultDate = self.config.minDate === undefined ||
  693. compareDates(new Date(), self.config.minDate) >= 0
  694. ? new Date()
  695. : new Date(self.config.minDate.getTime());
  696. var defaults = getDefaultHours(self.config);
  697. defaultDate.setHours(defaults.hours, defaults.minutes, defaults.seconds, defaultDate.getMilliseconds());
  698. self.selectedDates = [defaultDate];
  699. self.latestSelectedDateObj = defaultDate;
  700. }
  701. if (e !== undefined && e.type !== "blur") {
  702. timeWrapper(e);
  703. }
  704. var prevValue = self._input.value;
  705. setHoursFromInputs();
  706. updateValue();
  707. if (self._input.value !== prevValue) {
  708. self._debouncedChange();
  709. }
  710. }
  711. function ampm2military(hour, amPM) {
  712. return (hour % 12) + 12 * int(amPM === self.l10n.amPM[1]);
  713. }
  714. function military2ampm(hour) {
  715. switch (hour % 24) {
  716. case 0:
  717. case 12:
  718. return 12;
  719. default:
  720. return hour % 12;
  721. }
  722. }
  723. /**
  724. * Syncs the selected date object time with user's time input
  725. */
  726. function setHoursFromInputs() {
  727. if (self.hourElement === undefined || self.minuteElement === undefined)
  728. return;
  729. var hours = (parseInt(self.hourElement.value.slice(-2), 10) || 0) % 24, minutes = (parseInt(self.minuteElement.value, 10) || 0) % 60, seconds = self.secondElement !== undefined
  730. ? (parseInt(self.secondElement.value, 10) || 0) % 60
  731. : 0;
  732. if (self.amPM !== undefined) {
  733. hours = ampm2military(hours, self.amPM.textContent);
  734. }
  735. var limitMinHours = self.config.minTime !== undefined ||
  736. (self.config.minDate &&
  737. self.minDateHasTime &&
  738. self.latestSelectedDateObj &&
  739. compareDates(self.latestSelectedDateObj, self.config.minDate, true) ===
  740. 0);
  741. var limitMaxHours = self.config.maxTime !== undefined ||
  742. (self.config.maxDate &&
  743. self.maxDateHasTime &&
  744. self.latestSelectedDateObj &&
  745. compareDates(self.latestSelectedDateObj, self.config.maxDate, true) ===
  746. 0);
  747. if (self.config.maxTime !== undefined &&
  748. self.config.minTime !== undefined &&
  749. self.config.minTime > self.config.maxTime) {
  750. var minBound = calculateSecondsSinceMidnight(self.config.minTime.getHours(), self.config.minTime.getMinutes(), self.config.minTime.getSeconds());
  751. var maxBound = calculateSecondsSinceMidnight(self.config.maxTime.getHours(), self.config.maxTime.getMinutes(), self.config.maxTime.getSeconds());
  752. var currentTime = calculateSecondsSinceMidnight(hours, minutes, seconds);
  753. if (currentTime > maxBound && currentTime < minBound) {
  754. var result = parseSeconds(minBound);
  755. hours = result[0];
  756. minutes = result[1];
  757. seconds = result[2];
  758. }
  759. }
  760. else {
  761. if (limitMaxHours) {
  762. var maxTime = self.config.maxTime !== undefined
  763. ? self.config.maxTime
  764. : self.config.maxDate;
  765. hours = Math.min(hours, maxTime.getHours());
  766. if (hours === maxTime.getHours())
  767. minutes = Math.min(minutes, maxTime.getMinutes());
  768. if (minutes === maxTime.getMinutes())
  769. seconds = Math.min(seconds, maxTime.getSeconds());
  770. }
  771. if (limitMinHours) {
  772. var minTime = self.config.minTime !== undefined
  773. ? self.config.minTime
  774. : self.config.minDate;
  775. hours = Math.max(hours, minTime.getHours());
  776. if (hours === minTime.getHours() && minutes < minTime.getMinutes())
  777. minutes = minTime.getMinutes();
  778. if (minutes === minTime.getMinutes())
  779. seconds = Math.max(seconds, minTime.getSeconds());
  780. }
  781. }
  782. setHours(hours, minutes, seconds);
  783. }
  784. /**
  785. * Syncs time input values with a date
  786. */
  787. function setHoursFromDate(dateObj) {
  788. var date = dateObj || self.latestSelectedDateObj;
  789. if (date && date instanceof Date) {
  790. setHours(date.getHours(), date.getMinutes(), date.getSeconds());
  791. }
  792. }
  793. /**
  794. * Sets the hours, minutes, and optionally seconds
  795. * of the latest selected date object and the
  796. * corresponding time inputs
  797. * @param {Number} hours the hour. whether its military
  798. * or am-pm gets inferred from config
  799. * @param {Number} minutes the minutes
  800. * @param {Number} seconds the seconds (optional)
  801. */
  802. function setHours(hours, minutes, seconds) {
  803. if (self.latestSelectedDateObj !== undefined) {
  804. self.latestSelectedDateObj.setHours(hours % 24, minutes, seconds || 0, 0);
  805. }
  806. if (!self.hourElement || !self.minuteElement || self.isMobile)
  807. return;
  808. self.hourElement.value = pad(!self.config.time_24hr
  809. ? ((12 + hours) % 12) + 12 * int(hours % 12 === 0)
  810. : hours);
  811. self.minuteElement.value = pad(minutes);
  812. if (self.amPM !== undefined)
  813. self.amPM.textContent = self.l10n.amPM[int(hours >= 12)];
  814. if (self.secondElement !== undefined)
  815. self.secondElement.value = pad(seconds);
  816. }
  817. /**
  818. * Handles the year input and incrementing events
  819. * @param {Event} event the keyup or increment event
  820. */
  821. function onYearInput(event) {
  822. var eventTarget = getEventTarget(event);
  823. var year = parseInt(eventTarget.value) + (event.delta || 0);
  824. if (year / 1000 > 1 ||
  825. (event.key === "Enter" && !/[^\d]/.test(year.toString()))) {
  826. changeYear(year);
  827. }
  828. }
  829. /**
  830. * Essentially addEventListener + tracking
  831. * @param {Element} element the element to addEventListener to
  832. * @param {String} event the event name
  833. * @param {Function} handler the event handler
  834. */
  835. function bind(element, event, handler, options) {
  836. if (event instanceof Array)
  837. return event.forEach(function (ev) { return bind(element, ev, handler, options); });
  838. if (element instanceof Array)
  839. return element.forEach(function (el) { return bind(el, event, handler, options); });
  840. element.addEventListener(event, handler, options);
  841. self._handlers.push({
  842. remove: function () { return element.removeEventListener(event, handler, options); },
  843. });
  844. }
  845. function triggerChange() {
  846. triggerEvent("onChange");
  847. }
  848. /**
  849. * Adds all the necessary event listeners
  850. */
  851. function bindEvents() {
  852. if (self.config.wrap) {
  853. ["open", "close", "toggle", "clear"].forEach(function (evt) {
  854. Array.prototype.forEach.call(self.element.querySelectorAll("[data-" + evt + "]"), function (el) {
  855. return bind(el, "click", self[evt]);
  856. });
  857. });
  858. }
  859. if (self.isMobile) {
  860. setupMobile();
  861. return;
  862. }
  863. var debouncedResize = debounce(onResize, 50);
  864. self._debouncedChange = debounce(triggerChange, DEBOUNCED_CHANGE_MS);
  865. if (self.daysContainer && !/iPhone|iPad|iPod/i.test(navigator.userAgent))
  866. bind(self.daysContainer, "mouseover", function (e) {
  867. if (self.config.mode === "range")
  868. onMouseOver(getEventTarget(e));
  869. });
  870. bind(self._input, "keydown", onKeyDown);
  871. if (self.calendarContainer !== undefined) {
  872. bind(self.calendarContainer, "keydown", onKeyDown);
  873. }
  874. if (!self.config.inline && !self.config.static)
  875. bind(window, "resize", debouncedResize);
  876. if (window.ontouchstart !== undefined)
  877. bind(window.document, "touchstart", documentClick);
  878. else
  879. bind(window.document, "mousedown", documentClick);
  880. bind(window.document, "focus", documentClick, { capture: true });
  881. if (self.config.clickOpens === true) {
  882. bind(self._input, "focus", self.open);
  883. bind(self._input, "click", self.open);
  884. }
  885. if (self.daysContainer !== undefined) {
  886. bind(self.monthNav, "click", onMonthNavClick);
  887. bind(self.monthNav, ["keyup", "increment"], onYearInput);
  888. bind(self.daysContainer, "click", selectDate);
  889. }
  890. if (self.timeContainer !== undefined &&
  891. self.minuteElement !== undefined &&
  892. self.hourElement !== undefined) {
  893. var selText = function (e) {
  894. return getEventTarget(e).select();
  895. };
  896. bind(self.timeContainer, ["increment"], updateTime);
  897. bind(self.timeContainer, "blur", updateTime, { capture: true });
  898. bind(self.timeContainer, "click", timeIncrement);
  899. bind([self.hourElement, self.minuteElement], ["focus", "click"], selText);
  900. if (self.secondElement !== undefined)
  901. bind(self.secondElement, "focus", function () { return self.secondElement && self.secondElement.select(); });
  902. if (self.amPM !== undefined) {
  903. bind(self.amPM, "click", function (e) {
  904. updateTime(e);
  905. });
  906. }
  907. }
  908. if (self.config.allowInput) {
  909. bind(self._input, "blur", onBlur);
  910. }
  911. }
  912. /**
  913. * Set the calendar view to a particular date.
  914. * @param {Date} jumpDate the date to set the view to
  915. * @param {boolean} triggerChange if change events should be triggered
  916. */
  917. function jumpToDate(jumpDate, triggerChange) {
  918. var jumpTo = jumpDate !== undefined
  919. ? self.parseDate(jumpDate)
  920. : self.latestSelectedDateObj ||
  921. (self.config.minDate && self.config.minDate > self.now
  922. ? self.config.minDate
  923. : self.config.maxDate && self.config.maxDate < self.now
  924. ? self.config.maxDate
  925. : self.now);
  926. var oldYear = self.currentYear;
  927. var oldMonth = self.currentMonth;
  928. try {
  929. if (jumpTo !== undefined) {
  930. self.currentYear = jumpTo.getFullYear();
  931. self.currentMonth = jumpTo.getMonth();
  932. }
  933. }
  934. catch (e) {
  935. /* istanbul ignore next */
  936. e.message = "Invalid date supplied: " + jumpTo;
  937. self.config.errorHandler(e);
  938. }
  939. if (triggerChange && self.currentYear !== oldYear) {
  940. triggerEvent("onYearChange");
  941. buildMonthSwitch();
  942. }
  943. if (triggerChange &&
  944. (self.currentYear !== oldYear || self.currentMonth !== oldMonth)) {
  945. triggerEvent("onMonthChange");
  946. }
  947. self.redraw();
  948. }
  949. /**
  950. * The up/down arrow handler for time inputs
  951. * @param {Event} e the click event
  952. */
  953. function timeIncrement(e) {
  954. var eventTarget = getEventTarget(e);
  955. if (~eventTarget.className.indexOf("arrow"))
  956. incrementNumInput(e, eventTarget.classList.contains("arrowUp") ? 1 : -1);
  957. }
  958. /**
  959. * Increments/decrements the value of input associ-
  960. * ated with the up/down arrow by dispatching an
  961. * "increment" event on the input.
  962. *
  963. * @param {Event} e the click event
  964. * @param {Number} delta the diff (usually 1 or -1)
  965. * @param {Element} inputElem the input element
  966. */
  967. function incrementNumInput(e, delta, inputElem) {
  968. var target = e && getEventTarget(e);
  969. var input = inputElem ||
  970. (target && target.parentNode && target.parentNode.firstChild);
  971. var event = createEvent("increment");
  972. event.delta = delta;
  973. input && input.dispatchEvent(event);
  974. }
  975. function build() {
  976. var fragment = window.document.createDocumentFragment();
  977. self.calendarContainer = createElement("div", "flatpickr-calendar");
  978. self.calendarContainer.tabIndex = -1;
  979. if (!self.config.noCalendar) {
  980. fragment.appendChild(buildMonthNav());
  981. self.innerContainer = createElement("div", "flatpickr-innerContainer");
  982. if (self.config.weekNumbers) {
  983. var _a = buildWeeks(), weekWrapper = _a.weekWrapper, weekNumbers = _a.weekNumbers;
  984. self.innerContainer.appendChild(weekWrapper);
  985. self.weekNumbers = weekNumbers;
  986. self.weekWrapper = weekWrapper;
  987. }
  988. self.rContainer = createElement("div", "flatpickr-rContainer");
  989. self.rContainer.appendChild(buildWeekdays());
  990. if (!self.daysContainer) {
  991. self.daysContainer = createElement("div", "flatpickr-days");
  992. self.daysContainer.tabIndex = -1;
  993. }
  994. buildDays();
  995. self.rContainer.appendChild(self.daysContainer);
  996. self.innerContainer.appendChild(self.rContainer);
  997. fragment.appendChild(self.innerContainer);
  998. }
  999. if (self.config.enableTime) {
  1000. fragment.appendChild(buildTime());
  1001. }
  1002. toggleClass(self.calendarContainer, "rangeMode", self.config.mode === "range");
  1003. toggleClass(self.calendarContainer, "animate", self.config.animate === true);
  1004. toggleClass(self.calendarContainer, "multiMonth", self.config.showMonths > 1);
  1005. self.calendarContainer.appendChild(fragment);
  1006. var customAppend = self.config.appendTo !== undefined &&
  1007. self.config.appendTo.nodeType !== undefined;
  1008. if (self.config.inline || self.config.static) {
  1009. self.calendarContainer.classList.add(self.config.inline ? "inline" : "static");
  1010. if (self.config.inline) {
  1011. if (!customAppend && self.element.parentNode)
  1012. self.element.parentNode.insertBefore(self.calendarContainer, self._input.nextSibling);
  1013. else if (self.config.appendTo !== undefined)
  1014. self.config.appendTo.appendChild(self.calendarContainer);
  1015. }
  1016. if (self.config.static) {
  1017. var wrapper = createElement("div", "flatpickr-wrapper");
  1018. if (self.element.parentNode)
  1019. self.element.parentNode.insertBefore(wrapper, self.element);
  1020. wrapper.appendChild(self.element);
  1021. if (self.altInput)
  1022. wrapper.appendChild(self.altInput);
  1023. wrapper.appendChild(self.calendarContainer);
  1024. }
  1025. }
  1026. if (!self.config.static && !self.config.inline)
  1027. (self.config.appendTo !== undefined
  1028. ? self.config.appendTo
  1029. : window.document.body).appendChild(self.calendarContainer);
  1030. }
  1031. function createDay(className, date, _dayNumber, i) {
  1032. var dateIsEnabled = isEnabled(date, true), dayElement = createElement("span", className, date.getDate().toString());
  1033. dayElement.dateObj = date;
  1034. dayElement.$i = i;
  1035. dayElement.setAttribute("aria-label", self.formatDate(date, self.config.ariaDateFormat));
  1036. if (className.indexOf("hidden") === -1 &&
  1037. compareDates(date, self.now) === 0) {
  1038. self.todayDateElem = dayElement;
  1039. dayElement.classList.add("today");
  1040. dayElement.setAttribute("aria-current", "date");
  1041. }
  1042. if (dateIsEnabled) {
  1043. dayElement.tabIndex = -1;
  1044. if (isDateSelected(date)) {
  1045. dayElement.classList.add("selected");
  1046. self.selectedDateElem = dayElement;
  1047. if (self.config.mode === "range") {
  1048. toggleClass(dayElement, "startRange", self.selectedDates[0] &&
  1049. compareDates(date, self.selectedDates[0], true) === 0);
  1050. toggleClass(dayElement, "endRange", self.selectedDates[1] &&
  1051. compareDates(date, self.selectedDates[1], true) === 0);
  1052. if (className === "nextMonthDay")
  1053. dayElement.classList.add("inRange");
  1054. }
  1055. }
  1056. }
  1057. else {
  1058. dayElement.classList.add("flatpickr-disabled");
  1059. }
  1060. if (self.config.mode === "range") {
  1061. if (isDateInRange(date) && !isDateSelected(date))
  1062. dayElement.classList.add("inRange");
  1063. }
  1064. if (self.weekNumbers &&
  1065. self.config.showMonths === 1 &&
  1066. className !== "prevMonthDay" &&
  1067. i % 7 === 6) {
  1068. self.weekNumbers.insertAdjacentHTML("beforeend", "<span class='flatpickr-day'>" + self.config.getWeek(date) + "</span>");
  1069. }
  1070. triggerEvent("onDayCreate", dayElement);
  1071. return dayElement;
  1072. }
  1073. function focusOnDayElem(targetNode) {
  1074. targetNode.focus();
  1075. if (self.config.mode === "range")
  1076. onMouseOver(targetNode);
  1077. }
  1078. function getFirstAvailableDay(delta) {
  1079. var startMonth = delta > 0 ? 0 : self.config.showMonths - 1;
  1080. var endMonth = delta > 0 ? self.config.showMonths : -1;
  1081. for (var m = startMonth; m != endMonth; m += delta) {
  1082. var month = self.daysContainer.children[m];
  1083. var startIndex = delta > 0 ? 0 : month.children.length - 1;
  1084. var endIndex = delta > 0 ? month.children.length : -1;
  1085. for (var i = startIndex; i != endIndex; i += delta) {
  1086. var c = month.children[i];
  1087. if (c.className.indexOf("hidden") === -1 && isEnabled(c.dateObj))
  1088. return c;
  1089. }
  1090. }
  1091. return undefined;
  1092. }
  1093. function getNextAvailableDay(current, delta) {
  1094. var givenMonth = current.className.indexOf("Month") === -1
  1095. ? current.dateObj.getMonth()
  1096. : self.currentMonth;
  1097. var endMonth = delta > 0 ? self.config.showMonths : -1;
  1098. var loopDelta = delta > 0 ? 1 : -1;
  1099. for (var m = givenMonth - self.currentMonth; m != endMonth; m += loopDelta) {
  1100. var month = self.daysContainer.children[m];
  1101. var startIndex = givenMonth - self.currentMonth === m
  1102. ? current.$i + delta
  1103. : delta < 0
  1104. ? month.children.length - 1
  1105. : 0;
  1106. var numMonthDays = month.children.length;
  1107. for (var i = startIndex; i >= 0 && i < numMonthDays && i != (delta > 0 ? numMonthDays : -1); i += loopDelta) {
  1108. var c = month.children[i];
  1109. if (c.className.indexOf("hidden") === -1 &&
  1110. isEnabled(c.dateObj) &&
  1111. Math.abs(current.$i - i) >= Math.abs(delta))
  1112. return focusOnDayElem(c);
  1113. }
  1114. }
  1115. self.changeMonth(loopDelta);
  1116. focusOnDay(getFirstAvailableDay(loopDelta), 0);
  1117. return undefined;
  1118. }
  1119. function focusOnDay(current, offset) {
  1120. var activeElement = getClosestActiveElement();
  1121. var dayFocused = isInView(activeElement || document.body);
  1122. var startElem = current !== undefined
  1123. ? current
  1124. : dayFocused
  1125. ? activeElement
  1126. : self.selectedDateElem !== undefined && isInView(self.selectedDateElem)
  1127. ? self.selectedDateElem
  1128. : self.todayDateElem !== undefined && isInView(self.todayDateElem)
  1129. ? self.todayDateElem
  1130. : getFirstAvailableDay(offset > 0 ? 1 : -1);
  1131. if (startElem === undefined) {
  1132. self._input.focus();
  1133. }
  1134. else if (!dayFocused) {
  1135. focusOnDayElem(startElem);
  1136. }
  1137. else {
  1138. getNextAvailableDay(startElem, offset);
  1139. }
  1140. }
  1141. function buildMonthDays(year, month) {
  1142. var firstOfMonth = (new Date(year, month, 1).getDay() - self.l10n.firstDayOfWeek + 7) % 7;
  1143. var prevMonthDays = self.utils.getDaysInMonth((month - 1 + 12) % 12, year);
  1144. var daysInMonth = self.utils.getDaysInMonth(month, year), days = window.document.createDocumentFragment(), isMultiMonth = self.config.showMonths > 1, prevMonthDayClass = isMultiMonth ? "prevMonthDay hidden" : "prevMonthDay", nextMonthDayClass = isMultiMonth ? "nextMonthDay hidden" : "nextMonthDay";
  1145. var dayNumber = prevMonthDays + 1 - firstOfMonth, dayIndex = 0;
  1146. // prepend days from the ending of previous month
  1147. for (; dayNumber <= prevMonthDays; dayNumber++, dayIndex++) {
  1148. days.appendChild(createDay("flatpickr-day " + prevMonthDayClass, new Date(year, month - 1, dayNumber), dayNumber, dayIndex));
  1149. }
  1150. // Start at 1 since there is no 0th day
  1151. for (dayNumber = 1; dayNumber <= daysInMonth; dayNumber++, dayIndex++) {
  1152. days.appendChild(createDay("flatpickr-day", new Date(year, month, dayNumber), dayNumber, dayIndex));
  1153. }
  1154. // append days from the next month
  1155. for (var dayNum = daysInMonth + 1; dayNum <= 42 - firstOfMonth &&
  1156. (self.config.showMonths === 1 || dayIndex % 7 !== 0); dayNum++, dayIndex++) {
  1157. days.appendChild(createDay("flatpickr-day " + nextMonthDayClass, new Date(year, month + 1, dayNum % daysInMonth), dayNum, dayIndex));
  1158. }
  1159. //updateNavigationCurrentMonth();
  1160. var dayContainer = createElement("div", "dayContainer");
  1161. dayContainer.appendChild(days);
  1162. return dayContainer;
  1163. }
  1164. function buildDays() {
  1165. if (self.daysContainer === undefined) {
  1166. return;
  1167. }
  1168. clearNode(self.daysContainer);
  1169. // TODO: week numbers for each month
  1170. if (self.weekNumbers)
  1171. clearNode(self.weekNumbers);
  1172. var frag = document.createDocumentFragment();
  1173. for (var i = 0; i < self.config.showMonths; i++) {
  1174. var d = new Date(self.currentYear, self.currentMonth, 1);
  1175. d.setMonth(self.currentMonth + i);
  1176. frag.appendChild(buildMonthDays(d.getFullYear(), d.getMonth()));
  1177. }
  1178. self.daysContainer.appendChild(frag);
  1179. self.days = self.daysContainer.firstChild;
  1180. if (self.config.mode === "range" && self.selectedDates.length === 1) {
  1181. onMouseOver();
  1182. }
  1183. }
  1184. function buildMonthSwitch() {
  1185. if (self.config.showMonths > 1 ||
  1186. self.config.monthSelectorType !== "dropdown")
  1187. return;
  1188. var shouldBuildMonth = function (month) {
  1189. if (self.config.minDate !== undefined &&
  1190. self.currentYear === self.config.minDate.getFullYear() &&
  1191. month < self.config.minDate.getMonth()) {
  1192. return false;
  1193. }
  1194. return !(self.config.maxDate !== undefined &&
  1195. self.currentYear === self.config.maxDate.getFullYear() &&
  1196. month > self.config.maxDate.getMonth());
  1197. };
  1198. self.monthsDropdownContainer.tabIndex = -1;
  1199. self.monthsDropdownContainer.innerHTML = "";
  1200. for (var i = 0; i < 12; i++) {
  1201. if (!shouldBuildMonth(i))
  1202. continue;
  1203. var month = createElement("option", "flatpickr-monthDropdown-month");
  1204. month.value = new Date(self.currentYear, i).getMonth().toString();
  1205. month.textContent = monthToStr(i, self.config.shorthandCurrentMonth, self.l10n);
  1206. month.tabIndex = -1;
  1207. if (self.currentMonth === i) {
  1208. month.selected = true;
  1209. }
  1210. self.monthsDropdownContainer.appendChild(month);
  1211. }
  1212. }
  1213. function buildMonth() {
  1214. var container = createElement("div", "flatpickr-month");
  1215. var monthNavFragment = window.document.createDocumentFragment();
  1216. var monthElement;
  1217. if (self.config.showMonths > 1 ||
  1218. self.config.monthSelectorType === "static") {
  1219. monthElement = createElement("span", "cur-month");
  1220. }
  1221. else {
  1222. self.monthsDropdownContainer = createElement("select", "flatpickr-monthDropdown-months");
  1223. self.monthsDropdownContainer.setAttribute("aria-label", self.l10n.monthAriaLabel);
  1224. bind(self.monthsDropdownContainer, "change", function (e) {
  1225. var target = getEventTarget(e);
  1226. var selectedMonth = parseInt(target.value, 10);
  1227. self.changeMonth(selectedMonth - self.currentMonth);
  1228. triggerEvent("onMonthChange");
  1229. });
  1230. buildMonthSwitch();
  1231. monthElement = self.monthsDropdownContainer;
  1232. }
  1233. var yearInput = createNumberInput("cur-year", { tabindex: "-1" });
  1234. var yearElement = yearInput.getElementsByTagName("input")[0];
  1235. yearElement.setAttribute("aria-label", self.l10n.yearAriaLabel);
  1236. if (self.config.minDate) {
  1237. yearElement.setAttribute("min", self.config.minDate.getFullYear().toString());
  1238. }
  1239. if (self.config.maxDate) {
  1240. yearElement.setAttribute("max", self.config.maxDate.getFullYear().toString());
  1241. yearElement.disabled =
  1242. !!self.config.minDate &&
  1243. self.config.minDate.getFullYear() === self.config.maxDate.getFullYear();
  1244. }
  1245. var currentMonth = createElement("div", "flatpickr-current-month");
  1246. currentMonth.appendChild(monthElement);
  1247. currentMonth.appendChild(yearInput);
  1248. monthNavFragment.appendChild(currentMonth);
  1249. container.appendChild(monthNavFragment);
  1250. return {
  1251. container: container,
  1252. yearElement: yearElement,
  1253. monthElement: monthElement,
  1254. };
  1255. }
  1256. function buildMonths() {
  1257. clearNode(self.monthNav);
  1258. self.monthNav.appendChild(self.prevMonthNav);
  1259. if (self.config.showMonths) {
  1260. self.yearElements = [];
  1261. self.monthElements = [];
  1262. }
  1263. for (var m = self.config.showMonths; m--;) {
  1264. var month = buildMonth();
  1265. self.yearElements.push(month.yearElement);
  1266. self.monthElements.push(month.monthElement);
  1267. self.monthNav.appendChild(month.container);
  1268. }
  1269. self.monthNav.appendChild(self.nextMonthNav);
  1270. }
  1271. function buildMonthNav() {
  1272. self.monthNav = createElement("div", "flatpickr-months");
  1273. self.yearElements = [];
  1274. self.monthElements = [];
  1275. self.prevMonthNav = createElement("span", "flatpickr-prev-month");
  1276. self.prevMonthNav.innerHTML = self.config.prevArrow;
  1277. self.nextMonthNav = createElement("span", "flatpickr-next-month");
  1278. self.nextMonthNav.innerHTML = self.config.nextArrow;
  1279. buildMonths();
  1280. Object.defineProperty(self, "_hidePrevMonthArrow", {
  1281. get: function () { return self.__hidePrevMonthArrow; },
  1282. set: function (bool) {
  1283. if (self.__hidePrevMonthArrow !== bool) {
  1284. toggleClass(self.prevMonthNav, "flatpickr-disabled", bool);
  1285. self.__hidePrevMonthArrow = bool;
  1286. }
  1287. },
  1288. });
  1289. Object.defineProperty(self, "_hideNextMonthArrow", {
  1290. get: function () { return self.__hideNextMonthArrow; },
  1291. set: function (bool) {
  1292. if (self.__hideNextMonthArrow !== bool) {
  1293. toggleClass(self.nextMonthNav, "flatpickr-disabled", bool);
  1294. self.__hideNextMonthArrow = bool;
  1295. }
  1296. },
  1297. });
  1298. self.currentYearElement = self.yearElements[0];
  1299. updateNavigationCurrentMonth();
  1300. return self.monthNav;
  1301. }
  1302. function buildTime() {
  1303. self.calendarContainer.classList.add("hasTime");
  1304. if (self.config.noCalendar)
  1305. self.calendarContainer.classList.add("noCalendar");
  1306. var defaults = getDefaultHours(self.config);
  1307. self.timeContainer = createElement("div", "flatpickr-time");
  1308. self.timeContainer.tabIndex = -1;
  1309. var separator = createElement("span", "flatpickr-time-separator", ":");
  1310. var hourInput = createNumberInput("flatpickr-hour", {
  1311. "aria-label": self.l10n.hourAriaLabel,
  1312. });
  1313. self.hourElement = hourInput.getElementsByTagName("input")[0];
  1314. var minuteInput = createNumberInput("flatpickr-minute", {
  1315. "aria-label": self.l10n.minuteAriaLabel,
  1316. });
  1317. self.minuteElement = minuteInput.getElementsByTagName("input")[0];
  1318. self.hourElement.tabIndex = self.minuteElement.tabIndex = -1;
  1319. self.hourElement.value = pad(self.latestSelectedDateObj
  1320. ? self.latestSelectedDateObj.getHours()
  1321. : self.config.time_24hr
  1322. ? defaults.hours
  1323. : military2ampm(defaults.hours));
  1324. self.minuteElement.value = pad(self.latestSelectedDateObj
  1325. ? self.latestSelectedDateObj.getMinutes()
  1326. : defaults.minutes);
  1327. self.hourElement.setAttribute("step", self.config.hourIncrement.toString());
  1328. self.minuteElement.setAttribute("step", self.config.minuteIncrement.toString());
  1329. self.hourElement.setAttribute("min", self.config.time_24hr ? "0" : "1");
  1330. self.hourElement.setAttribute("max", self.config.time_24hr ? "23" : "12");
  1331. self.hourElement.setAttribute("maxlength", "2");
  1332. self.minuteElement.setAttribute("min", "0");
  1333. self.minuteElement.setAttribute("max", "59");
  1334. self.minuteElement.setAttribute("maxlength", "2");
  1335. self.timeContainer.appendChild(hourInput);
  1336. self.timeContainer.appendChild(separator);
  1337. self.timeContainer.appendChild(minuteInput);
  1338. if (self.config.time_24hr)
  1339. self.timeContainer.classList.add("time24hr");
  1340. if (self.config.enableSeconds) {
  1341. self.timeContainer.classList.add("hasSeconds");
  1342. var secondInput = createNumberInput("flatpickr-second");
  1343. self.secondElement = secondInput.getElementsByTagName("input")[0];
  1344. self.secondElement.value = pad(self.latestSelectedDateObj
  1345. ? self.latestSelectedDateObj.getSeconds()
  1346. : defaults.seconds);
  1347. self.secondElement.setAttribute("step", self.minuteElement.getAttribute("step"));
  1348. self.secondElement.setAttribute("min", "0");
  1349. self.secondElement.setAttribute("max", "59");
  1350. self.secondElement.setAttribute("maxlength", "2");
  1351. self.timeContainer.appendChild(createElement("span", "flatpickr-time-separator", ":"));
  1352. self.timeContainer.appendChild(secondInput);
  1353. }
  1354. if (!self.config.time_24hr) {
  1355. // add self.amPM if appropriate
  1356. self.amPM = createElement("span", "flatpickr-am-pm", self.l10n.amPM[int((self.latestSelectedDateObj
  1357. ? self.hourElement.value
  1358. : self.config.defaultHour) > 11)]);
  1359. self.amPM.title = self.l10n.toggleTitle;
  1360. self.amPM.tabIndex = -1;
  1361. self.timeContainer.appendChild(self.amPM);
  1362. }
  1363. return self.timeContainer;
  1364. }
  1365. function buildWeekdays() {
  1366. if (!self.weekdayContainer)
  1367. self.weekdayContainer = createElement("div", "flatpickr-weekdays");
  1368. else
  1369. clearNode(self.weekdayContainer);
  1370. for (var i = self.config.showMonths; i--;) {
  1371. var container = createElement("div", "flatpickr-weekdaycontainer");
  1372. self.weekdayContainer.appendChild(container);
  1373. }
  1374. updateWeekdays();
  1375. return self.weekdayContainer;
  1376. }
  1377. function updateWeekdays() {
  1378. if (!self.weekdayContainer) {
  1379. return;
  1380. }
  1381. var firstDayOfWeek = self.l10n.firstDayOfWeek;
  1382. var weekdays = __spreadArrays(self.l10n.weekdays.shorthand);
  1383. if (firstDayOfWeek > 0 && firstDayOfWeek < weekdays.length) {
  1384. weekdays = __spreadArrays(weekdays.splice(firstDayOfWeek, weekdays.length), weekdays.splice(0, firstDayOfWeek));
  1385. }
  1386. for (var i = self.config.showMonths; i--;) {
  1387. self.weekdayContainer.children[i].innerHTML = "\n <span class='flatpickr-weekday'>\n " + weekdays.join("</span><span class='flatpickr-weekday'>") + "\n </span>\n ";
  1388. }
  1389. }
  1390. /* istanbul ignore next */
  1391. function buildWeeks() {
  1392. self.calendarContainer.classList.add("hasWeeks");
  1393. var weekWrapper = createElement("div", "flatpickr-weekwrapper");
  1394. weekWrapper.appendChild(createElement("span", "flatpickr-weekday", self.l10n.weekAbbreviation));
  1395. var weekNumbers = createElement("div", "flatpickr-weeks");
  1396. weekWrapper.appendChild(weekNumbers);
  1397. return {
  1398. weekWrapper: weekWrapper,
  1399. weekNumbers: weekNumbers,
  1400. };
  1401. }
  1402. function changeMonth(value, isOffset) {
  1403. if (isOffset === void 0) { isOffset = true; }
  1404. var delta = isOffset ? value : value - self.currentMonth;
  1405. if ((delta < 0 && self._hidePrevMonthArrow === true) ||
  1406. (delta > 0 && self._hideNextMonthArrow === true))
  1407. return;
  1408. self.currentMonth += delta;
  1409. if (self.currentMonth < 0 || self.currentMonth > 11) {
  1410. self.currentYear += self.currentMonth > 11 ? 1 : -1;
  1411. self.currentMonth = (self.currentMonth + 12) % 12;
  1412. triggerEvent("onYearChange");
  1413. buildMonthSwitch();
  1414. }
  1415. buildDays();
  1416. triggerEvent("onMonthChange");
  1417. updateNavigationCurrentMonth();
  1418. }
  1419. function clear(triggerChangeEvent, toInitial) {
  1420. if (triggerChangeEvent === void 0) { triggerChangeEvent = true; }
  1421. if (toInitial === void 0) { toInitial = true; }
  1422. self.input.value = "";
  1423. if (self.altInput !== undefined)
  1424. self.altInput.value = "";
  1425. if (self.mobileInput !== undefined)
  1426. self.mobileInput.value = "";
  1427. self.selectedDates = [];
  1428. self.latestSelectedDateObj = undefined;
  1429. if (toInitial === true) {
  1430. self.currentYear = self._initialDate.getFullYear();
  1431. self.currentMonth = self._initialDate.getMonth();
  1432. }
  1433. if (self.config.enableTime === true) {
  1434. var _a = getDefaultHours(self.config), hours = _a.hours, minutes = _a.minutes, seconds = _a.seconds;
  1435. setHours(hours, minutes, seconds);
  1436. }
  1437. self.redraw();
  1438. if (triggerChangeEvent)
  1439. // triggerChangeEvent is true (default) or an Event
  1440. triggerEvent("onChange");
  1441. }
  1442. function close() {
  1443. self.isOpen = false;
  1444. if (!self.isMobile) {
  1445. if (self.calendarContainer !== undefined) {
  1446. self.calendarContainer.classList.remove("open");
  1447. }
  1448. if (self._input !== undefined) {
  1449. self._input.classList.remove("active");
  1450. }
  1451. }
  1452. triggerEvent("onClose");
  1453. }
  1454. function destroy() {
  1455. if (self.config !== undefined)
  1456. triggerEvent("onDestroy");
  1457. for (var i = self._handlers.length; i--;) {
  1458. self._handlers[i].remove();
  1459. }
  1460. self._handlers = [];
  1461. if (self.mobileInput) {
  1462. if (self.mobileInput.parentNode)
  1463. self.mobileInput.parentNode.removeChild(self.mobileInput);
  1464. self.mobileInput = undefined;
  1465. }
  1466. else if (self.calendarContainer && self.calendarContainer.parentNode) {
  1467. if (self.config.static && self.calendarContainer.parentNode) {
  1468. var wrapper = self.calendarContainer.parentNode;
  1469. wrapper.lastChild && wrapper.removeChild(wrapper.lastChild);
  1470. if (wrapper.parentNode) {
  1471. while (wrapper.firstChild)
  1472. wrapper.parentNode.insertBefore(wrapper.firstChild, wrapper);
  1473. wrapper.parentNode.removeChild(wrapper);
  1474. }
  1475. }
  1476. else
  1477. self.calendarContainer.parentNode.removeChild(self.calendarContainer);
  1478. }
  1479. if (self.altInput) {
  1480. self.input.type = "text";
  1481. if (self.altInput.parentNode)
  1482. self.altInput.parentNode.removeChild(self.altInput);
  1483. delete self.altInput;
  1484. }
  1485. if (self.input) {
  1486. self.input.type = self.input._type;
  1487. self.input.classList.remove("flatpickr-input");
  1488. self.input.removeAttribute("readonly");
  1489. }
  1490. [
  1491. "_showTimeInput",
  1492. "latestSelectedDateObj",
  1493. "_hideNextMonthArrow",
  1494. "_hidePrevMonthArrow",
  1495. "__hideNextMonthArrow",
  1496. "__hidePrevMonthArrow",
  1497. "isMobile",
  1498. "isOpen",
  1499. "selectedDateElem",
  1500. "minDateHasTime",
  1501. "maxDateHasTime",
  1502. "days",
  1503. "daysContainer",
  1504. "_input",
  1505. "_positionElement",
  1506. "innerContainer",
  1507. "rContainer",
  1508. "monthNav",
  1509. "todayDateElem",
  1510. "calendarContainer",
  1511. "weekdayContainer",
  1512. "prevMonthNav",
  1513. "nextMonthNav",
  1514. "monthsDropdownContainer",
  1515. "currentMonthElement",
  1516. "currentYearElement",
  1517. "navigationCurrentMonth",
  1518. "selectedDateElem",
  1519. "config",
  1520. ].forEach(function (k) {
  1521. try {
  1522. delete self[k];
  1523. }
  1524. catch (_) { }
  1525. });
  1526. }
  1527. function isCalendarElem(elem) {
  1528. return self.calendarContainer.contains(elem);
  1529. }
  1530. function documentClick(e) {
  1531. if (self.isOpen && !self.config.inline) {
  1532. var eventTarget_1 = getEventTarget(e);
  1533. var isCalendarElement = isCalendarElem(eventTarget_1);
  1534. var isInput = eventTarget_1 === self.input ||
  1535. eventTarget_1 === self.altInput ||
  1536. self.element.contains(eventTarget_1) ||
  1537. // web components
  1538. // e.path is not present in all browsers. circumventing typechecks
  1539. (e.path &&
  1540. e.path.indexOf &&
  1541. (~e.path.indexOf(self.input) ||
  1542. ~e.path.indexOf(self.altInput)));
  1543. var lostFocus = !isInput &&
  1544. !isCalendarElement &&
  1545. !isCalendarElem(e.relatedTarget);
  1546. var isIgnored = !self.config.ignoredFocusElements.some(function (elem) {
  1547. return elem.contains(eventTarget_1);
  1548. });
  1549. if (lostFocus && isIgnored) {
  1550. if (self.config.allowInput) {
  1551. self.setDate(self._input.value, false, self.config.altInput
  1552. ? self.config.altFormat
  1553. : self.config.dateFormat);
  1554. }
  1555. if (self.timeContainer !== undefined &&
  1556. self.minuteElement !== undefined &&
  1557. self.hourElement !== undefined &&
  1558. self.input.value !== "" &&
  1559. self.input.value !== undefined) {
  1560. updateTime();
  1561. }
  1562. self.close();
  1563. if (self.config &&
  1564. self.config.mode === "range" &&
  1565. self.selectedDates.length === 1)
  1566. self.clear(false);
  1567. }
  1568. }
  1569. }
  1570. function changeYear(newYear) {
  1571. if (!newYear ||
  1572. (self.config.minDate && newYear < self.config.minDate.getFullYear()) ||
  1573. (self.config.maxDate && newYear > self.config.maxDate.getFullYear()))
  1574. return;
  1575. var newYearNum = newYear, isNewYear = self.currentYear !== newYearNum;
  1576. self.currentYear = newYearNum || self.currentYear;
  1577. if (self.config.maxDate &&
  1578. self.currentYear === self.config.maxDate.getFullYear()) {
  1579. self.currentMonth = Math.min(self.config.maxDate.getMonth(), self.currentMonth);
  1580. }
  1581. else if (self.config.minDate &&
  1582. self.currentYear === self.config.minDate.getFullYear()) {
  1583. self.currentMonth = Math.max(self.config.minDate.getMonth(), self.currentMonth);
  1584. }
  1585. if (isNewYear) {
  1586. self.redraw();
  1587. triggerEvent("onYearChange");
  1588. buildMonthSwitch();
  1589. }
  1590. }
  1591. function isEnabled(date, timeless) {
  1592. var _a;
  1593. if (timeless === void 0) { timeless = true; }
  1594. var dateToCheck = self.parseDate(date, undefined, timeless); // timeless
  1595. if ((self.config.minDate &&
  1596. dateToCheck &&
  1597. compareDates(dateToCheck, self.config.minDate, timeless !== undefined ? timeless : !self.minDateHasTime) < 0) ||
  1598. (self.config.maxDate &&
  1599. dateToCheck &&
  1600. compareDates(dateToCheck, self.config.maxDate, timeless !== undefined ? timeless : !self.maxDateHasTime) > 0))
  1601. return false;
  1602. if (!self.config.enable && self.config.disable.length === 0)
  1603. return true;
  1604. if (dateToCheck === undefined)
  1605. return false;
  1606. var bool = !!self.config.enable, array = (_a = self.config.enable) !== null && _a !== void 0 ? _a : self.config.disable;
  1607. for (var i = 0, d = void 0; i < array.length; i++) {
  1608. d = array[i];
  1609. if (typeof d === "function" &&
  1610. d(dateToCheck) // disabled by function
  1611. )
  1612. return bool;
  1613. else if (d instanceof Date &&
  1614. dateToCheck !== undefined &&
  1615. d.getTime() === dateToCheck.getTime())
  1616. // disabled by date
  1617. return bool;
  1618. else if (typeof d === "string") {
  1619. // disabled by date string
  1620. var parsed = self.parseDate(d, undefined, true);
  1621. return parsed && parsed.getTime() === dateToCheck.getTime()
  1622. ? bool
  1623. : !bool;
  1624. }
  1625. else if (
  1626. // disabled by range
  1627. typeof d === "object" &&
  1628. dateToCheck !== undefined &&
  1629. d.from &&
  1630. d.to &&
  1631. dateToCheck.getTime() >= d.from.getTime() &&
  1632. dateToCheck.getTime() <= d.to.getTime())
  1633. return bool;
  1634. }
  1635. return !bool;
  1636. }
  1637. function isInView(elem) {
  1638. if (self.daysContainer !== undefined)
  1639. return (elem.className.indexOf("hidden") === -1 &&
  1640. elem.className.indexOf("flatpickr-disabled") === -1 &&
  1641. self.daysContainer.contains(elem));
  1642. return false;
  1643. }
  1644. function onBlur(e) {
  1645. var isInput = e.target === self._input;
  1646. var valueChanged = self._input.value.trimEnd() !== getDateStr();
  1647. if (isInput &&
  1648. valueChanged &&
  1649. !(e.relatedTarget && isCalendarElem(e.relatedTarget))) {
  1650. self.setDate(self._input.value, true, e.target === self.altInput
  1651. ? self.config.altFormat
  1652. : self.config.dateFormat);
  1653. }
  1654. }
  1655. function onKeyDown(e) {
  1656. // e.key e.keyCode
  1657. // "Backspace" 8
  1658. // "Tab" 9
  1659. // "Enter" 13
  1660. // "Escape" (IE "Esc") 27
  1661. // "ArrowLeft" (IE "Left") 37
  1662. // "ArrowUp" (IE "Up") 38
  1663. // "ArrowRight" (IE "Right") 39
  1664. // "ArrowDown" (IE "Down") 40
  1665. // "Delete" (IE "Del") 46
  1666. var eventTarget = getEventTarget(e);
  1667. var isInput = self.config.wrap
  1668. ? element.contains(eventTarget)
  1669. : eventTarget === self._input;
  1670. var allowInput = self.config.allowInput;
  1671. var allowKeydown = self.isOpen && (!allowInput || !isInput);
  1672. var allowInlineKeydown = self.config.inline && isInput && !allowInput;
  1673. if (e.keyCode === 13 && isInput) {
  1674. if (allowInput) {
  1675. self.setDate(self._input.value, true, eventTarget === self.altInput
  1676. ? self.config.altFormat
  1677. : self.config.dateFormat);
  1678. self.close();
  1679. return eventTarget.blur();
  1680. }
  1681. else {
  1682. self.open();
  1683. }
  1684. }
  1685. else if (isCalendarElem(eventTarget) ||
  1686. allowKeydown ||
  1687. allowInlineKeydown) {
  1688. var isTimeObj = !!self.timeContainer &&
  1689. self.timeContainer.contains(eventTarget);
  1690. switch (e.keyCode) {
  1691. case 13:
  1692. if (isTimeObj) {
  1693. e.preventDefault();
  1694. updateTime();
  1695. focusAndClose();
  1696. }
  1697. else
  1698. selectDate(e);
  1699. break;
  1700. case 27: // escape
  1701. e.preventDefault();
  1702. focusAndClose();
  1703. break;
  1704. case 8:
  1705. case 46:
  1706. if (isInput && !self.config.allowInput) {
  1707. e.preventDefault();
  1708. self.clear();
  1709. }
  1710. break;
  1711. case 37:
  1712. case 39:
  1713. if (!isTimeObj && !isInput) {
  1714. e.preventDefault();
  1715. var activeElement = getClosestActiveElement();
  1716. if (self.daysContainer !== undefined &&
  1717. (allowInput === false ||
  1718. (activeElement && isInView(activeElement)))) {
  1719. var delta_1 = e.keyCode === 39 ? 1 : -1;
  1720. if (!e.ctrlKey)
  1721. focusOnDay(undefined, delta_1);
  1722. else {
  1723. e.stopPropagation();
  1724. changeMonth(delta_1);
  1725. focusOnDay(getFirstAvailableDay(1), 0);
  1726. }
  1727. }
  1728. }
  1729. else if (self.hourElement)
  1730. self.hourElement.focus();
  1731. break;
  1732. case 38:
  1733. case 40:
  1734. e.preventDefault();
  1735. var delta = e.keyCode === 40 ? 1 : -1;
  1736. if ((self.daysContainer &&
  1737. eventTarget.$i !== undefined) ||
  1738. eventTarget === self.input ||
  1739. eventTarget === self.altInput) {
  1740. if (e.ctrlKey) {
  1741. e.stopPropagation();
  1742. changeYear(self.currentYear - delta);
  1743. focusOnDay(getFirstAvailableDay(1), 0);
  1744. }
  1745. else if (!isTimeObj)
  1746. focusOnDay(undefined, delta * 7);
  1747. }
  1748. else if (eventTarget === self.currentYearElement) {
  1749. changeYear(self.currentYear - delta);
  1750. }
  1751. else if (self.config.enableTime) {
  1752. if (!isTimeObj && self.hourElement)
  1753. self.hourElement.focus();
  1754. updateTime(e);
  1755. self._debouncedChange();
  1756. }
  1757. break;
  1758. case 9:
  1759. if (isTimeObj) {
  1760. var elems = [
  1761. self.hourElement,
  1762. self.minuteElement,
  1763. self.secondElement,
  1764. self.amPM,
  1765. ]
  1766. .concat(self.pluginElements)
  1767. .filter(function (x) { return x; });
  1768. var i = elems.indexOf(eventTarget);
  1769. if (i !== -1) {
  1770. var target = elems[i + (e.shiftKey ? -1 : 1)];
  1771. e.preventDefault();
  1772. (target || self._input).focus();
  1773. }
  1774. }
  1775. else if (!self.config.noCalendar &&
  1776. self.daysContainer &&
  1777. self.daysContainer.contains(eventTarget) &&
  1778. e.shiftKey) {
  1779. e.preventDefault();
  1780. self._input.focus();
  1781. }
  1782. break;
  1783. }
  1784. }
  1785. if (self.amPM !== undefined && eventTarget === self.amPM) {
  1786. switch (e.key) {
  1787. case self.l10n.amPM[0].charAt(0):
  1788. case self.l10n.amPM[0].charAt(0).toLowerCase():
  1789. self.amPM.textContent = self.l10n.amPM[0];
  1790. setHoursFromInputs();
  1791. updateValue();
  1792. break;
  1793. case self.l10n.amPM[1].charAt(0):
  1794. case self.l10n.amPM[1].charAt(0).toLowerCase():
  1795. self.amPM.textContent = self.l10n.amPM[1];
  1796. setHoursFromInputs();
  1797. updateValue();
  1798. break;
  1799. }
  1800. }
  1801. if (isInput || isCalendarElem(eventTarget)) {
  1802. triggerEvent("onKeyDown", e);
  1803. }
  1804. }
  1805. function onMouseOver(elem, cellClass) {
  1806. if (cellClass === void 0) { cellClass = "flatpickr-day"; }
  1807. if (self.selectedDates.length !== 1 ||
  1808. (elem &&
  1809. (!elem.classList.contains(cellClass) ||
  1810. elem.classList.contains("flatpickr-disabled"))))
  1811. return;
  1812. var hoverDate = elem
  1813. ? elem.dateObj.getTime()
  1814. : self.days.firstElementChild.dateObj.getTime(), initialDate = self.parseDate(self.selectedDates[0], undefined, true).getTime(), rangeStartDate = Math.min(hoverDate, self.selectedDates[0].getTime()), rangeEndDate = Math.max(hoverDate, self.selectedDates[0].getTime());
  1815. var containsDisabled = false;
  1816. var minRange = 0, maxRange = 0;
  1817. for (var t = rangeStartDate; t < rangeEndDate; t += duration.DAY) {
  1818. if (!isEnabled(new Date(t), true)) {
  1819. containsDisabled =
  1820. containsDisabled || (t > rangeStartDate && t < rangeEndDate);
  1821. if (t < initialDate && (!minRange || t > minRange))
  1822. minRange = t;
  1823. else if (t > initialDate && (!maxRange || t < maxRange))
  1824. maxRange = t;
  1825. }
  1826. }
  1827. var hoverableCells = Array.from(self.rContainer.querySelectorAll("*:nth-child(-n+" + self.config.showMonths + ") > ." + cellClass));
  1828. hoverableCells.forEach(function (dayElem) {
  1829. var date = dayElem.dateObj;
  1830. var timestamp = date.getTime();
  1831. var outOfRange = (minRange > 0 && timestamp < minRange) ||
  1832. (maxRange > 0 && timestamp > maxRange);
  1833. if (outOfRange) {
  1834. dayElem.classList.add("notAllowed");
  1835. ["inRange", "startRange", "endRange"].forEach(function (c) {
  1836. dayElem.classList.remove(c);
  1837. });
  1838. return;
  1839. }
  1840. else if (containsDisabled && !outOfRange)
  1841. return;
  1842. ["startRange", "inRange", "endRange", "notAllowed"].forEach(function (c) {
  1843. dayElem.classList.remove(c);
  1844. });
  1845. if (elem !== undefined) {
  1846. elem.classList.add(hoverDate <= self.selectedDates[0].getTime()
  1847. ? "startRange"
  1848. : "endRange");
  1849. if (initialDate < hoverDate && timestamp === initialDate)
  1850. dayElem.classList.add("startRange");
  1851. else if (initialDate > hoverDate && timestamp === initialDate)
  1852. dayElem.classList.add("endRange");
  1853. if (timestamp >= minRange &&
  1854. (maxRange === 0 || timestamp <= maxRange) &&
  1855. isBetween(timestamp, initialDate, hoverDate))
  1856. dayElem.classList.add("inRange");
  1857. }
  1858. });
  1859. }
  1860. function onResize() {
  1861. if (self.isOpen && !self.config.static && !self.config.inline)
  1862. positionCalendar();
  1863. }
  1864. function open(e, positionElement) {
  1865. if (positionElement === void 0) { positionElement = self._positionElement; }
  1866. if (self.isMobile === true) {
  1867. if (e) {
  1868. e.preventDefault();
  1869. var eventTarget = getEventTarget(e);
  1870. if (eventTarget) {
  1871. eventTarget.blur();
  1872. }
  1873. }
  1874. if (self.mobileInput !== undefined) {
  1875. self.mobileInput.focus();
  1876. self.mobileInput.click();
  1877. }
  1878. triggerEvent("onOpen");
  1879. return;
  1880. }
  1881. else if (self._input.disabled || self.config.inline) {
  1882. return;
  1883. }
  1884. var wasOpen = self.isOpen;
  1885. self.isOpen = true;
  1886. if (!wasOpen) {
  1887. self.calendarContainer.classList.add("open");
  1888. self._input.classList.add("active");
  1889. triggerEvent("onOpen");
  1890. positionCalendar(positionElement);
  1891. }
  1892. if (self.config.enableTime === true && self.config.noCalendar === true) {
  1893. if (self.config.allowInput === false &&
  1894. (e === undefined ||
  1895. !self.timeContainer.contains(e.relatedTarget))) {
  1896. setTimeout(function () { return self.hourElement.select(); }, 50);
  1897. }
  1898. }
  1899. }
  1900. function minMaxDateSetter(type) {
  1901. return function (date) {
  1902. var dateObj = (self.config["_" + type + "Date"] = self.parseDate(date, self.config.dateFormat));
  1903. var inverseDateObj = self.config["_" + (type === "min" ? "max" : "min") + "Date"];
  1904. if (dateObj !== undefined) {
  1905. self[type === "min" ? "minDateHasTime" : "maxDateHasTime"] =
  1906. dateObj.getHours() > 0 ||
  1907. dateObj.getMinutes() > 0 ||
  1908. dateObj.getSeconds() > 0;
  1909. }
  1910. if (self.selectedDates) {
  1911. self.selectedDates = self.selectedDates.filter(function (d) { return isEnabled(d); });
  1912. if (!self.selectedDates.length && type === "min")
  1913. setHoursFromDate(dateObj);
  1914. updateValue();
  1915. }
  1916. if (self.daysContainer) {
  1917. redraw();
  1918. if (dateObj !== undefined)
  1919. self.currentYearElement[type] = dateObj.getFullYear().toString();
  1920. else
  1921. self.currentYearElement.removeAttribute(type);
  1922. self.currentYearElement.disabled =
  1923. !!inverseDateObj &&
  1924. dateObj !== undefined &&
  1925. inverseDateObj.getFullYear() === dateObj.getFullYear();
  1926. }
  1927. };
  1928. }
  1929. function parseConfig() {
  1930. var boolOpts = [
  1931. "wrap",
  1932. "weekNumbers",
  1933. "allowInput",
  1934. "allowInvalidPreload",
  1935. "clickOpens",
  1936. "time_24hr",
  1937. "enableTime",
  1938. "noCalendar",
  1939. "altInput",
  1940. "shorthandCurrentMonth",
  1941. "inline",
  1942. "static",
  1943. "enableSeconds",
  1944. "disableMobile",
  1945. ];
  1946. var userConfig = __assign(__assign({}, JSON.parse(JSON.stringify(element.dataset || {}))), instanceConfig);
  1947. var formats = {};
  1948. self.config.parseDate = userConfig.parseDate;
  1949. self.config.formatDate = userConfig.formatDate;
  1950. Object.defineProperty(self.config, "enable", {
  1951. get: function () { return self.config._enable; },
  1952. set: function (dates) {
  1953. self.config._enable = parseDateRules(dates);
  1954. },
  1955. });
  1956. Object.defineProperty(self.config, "disable", {
  1957. get: function () { return self.config._disable; },
  1958. set: function (dates) {
  1959. self.config._disable = parseDateRules(dates);
  1960. },
  1961. });
  1962. var timeMode = userConfig.mode === "time";
  1963. if (!userConfig.dateFormat && (userConfig.enableTime || timeMode)) {
  1964. var defaultDateFormat = flatpickr.defaultConfig.dateFormat || defaults.dateFormat;
  1965. formats.dateFormat =
  1966. userConfig.noCalendar || timeMode
  1967. ? "H:i" + (userConfig.enableSeconds ? ":S" : "")
  1968. : defaultDateFormat + " H:i" + (userConfig.enableSeconds ? ":S" : "");
  1969. }
  1970. if (userConfig.altInput &&
  1971. (userConfig.enableTime || timeMode) &&
  1972. !userConfig.altFormat) {
  1973. var defaultAltFormat = flatpickr.defaultConfig.altFormat || defaults.altFormat;
  1974. formats.altFormat =
  1975. userConfig.noCalendar || timeMode
  1976. ? "h:i" + (userConfig.enableSeconds ? ":S K" : " K")
  1977. : defaultAltFormat + (" h:i" + (userConfig.enableSeconds ? ":S" : "") + " K");
  1978. }
  1979. Object.defineProperty(self.config, "minDate", {
  1980. get: function () { return self.config._minDate; },
  1981. set: minMaxDateSetter("min"),
  1982. });
  1983. Object.defineProperty(self.config, "maxDate", {
  1984. get: function () { return self.config._maxDate; },
  1985. set: minMaxDateSetter("max"),
  1986. });
  1987. var minMaxTimeSetter = function (type) { return function (val) {
  1988. self.config[type === "min" ? "_minTime" : "_maxTime"] = self.parseDate(val, "H:i:S");
  1989. }; };
  1990. Object.defineProperty(self.config, "minTime", {
  1991. get: function () { return self.config._minTime; },
  1992. set: minMaxTimeSetter("min"),
  1993. });
  1994. Object.defineProperty(self.config, "maxTime", {
  1995. get: function () { return self.config._maxTime; },
  1996. set: minMaxTimeSetter("max"),
  1997. });
  1998. if (userConfig.mode === "time") {
  1999. self.config.noCalendar = true;
  2000. self.config.enableTime = true;
  2001. }
  2002. Object.assign(self.config, formats, userConfig);
  2003. for (var i = 0; i < boolOpts.length; i++)
  2004. // https://github.com/microsoft/TypeScript/issues/31663
  2005. self.config[boolOpts[i]] =
  2006. self.config[boolOpts[i]] === true ||
  2007. self.config[boolOpts[i]] === "true";
  2008. HOOKS.filter(function (hook) { return self.config[hook] !== undefined; }).forEach(function (hook) {
  2009. self.config[hook] = arrayify(self.config[hook] || []).map(bindToInstance);
  2010. });
  2011. self.isMobile =
  2012. !self.config.disableMobile &&
  2013. !self.config.inline &&
  2014. self.config.mode === "single" &&
  2015. !self.config.disable.length &&
  2016. !self.config.enable &&
  2017. !self.config.weekNumbers &&
  2018. /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  2019. for (var i = 0; i < self.config.plugins.length; i++) {
  2020. var pluginConf = self.config.plugins[i](self) || {};
  2021. for (var key in pluginConf) {
  2022. if (HOOKS.indexOf(key) > -1) {
  2023. self.config[key] = arrayify(pluginConf[key])
  2024. .map(bindToInstance)
  2025. .concat(self.config[key]);
  2026. }
  2027. else if (typeof userConfig[key] === "undefined")
  2028. self.config[key] = pluginConf[key];
  2029. }
  2030. }
  2031. if (!userConfig.altInputClass) {
  2032. self.config.altInputClass =
  2033. getInputElem().className + " " + self.config.altInputClass;
  2034. }
  2035. triggerEvent("onParseConfig");
  2036. }
  2037. function getInputElem() {
  2038. return self.config.wrap
  2039. ? element.querySelector("[data-input]")
  2040. : element;
  2041. }
  2042. function setupLocale() {
  2043. if (typeof self.config.locale !== "object" &&
  2044. typeof flatpickr.l10ns[self.config.locale] === "undefined")
  2045. self.config.errorHandler(new Error("flatpickr: invalid locale " + self.config.locale));
  2046. self.l10n = __assign(__assign({}, flatpickr.l10ns.default), (typeof self.config.locale === "object"
  2047. ? self.config.locale
  2048. : self.config.locale !== "default"
  2049. ? flatpickr.l10ns[self.config.locale]
  2050. : undefined));
  2051. tokenRegex.D = "(" + self.l10n.weekdays.shorthand.join("|") + ")";
  2052. tokenRegex.l = "(" + self.l10n.weekdays.longhand.join("|") + ")";
  2053. tokenRegex.M = "(" + self.l10n.months.shorthand.join("|") + ")";
  2054. tokenRegex.F = "(" + self.l10n.months.longhand.join("|") + ")";
  2055. tokenRegex.K = "(" + self.l10n.amPM[0] + "|" + self.l10n.amPM[1] + "|" + self.l10n.amPM[0].toLowerCase() + "|" + self.l10n.amPM[1].toLowerCase() + ")";
  2056. var userConfig = __assign(__assign({}, instanceConfig), JSON.parse(JSON.stringify(element.dataset || {})));
  2057. if (userConfig.time_24hr === undefined &&
  2058. flatpickr.defaultConfig.time_24hr === undefined) {
  2059. self.config.time_24hr = self.l10n.time_24hr;
  2060. }
  2061. self.formatDate = createDateFormatter(self);
  2062. self.parseDate = createDateParser({ config: self.config, l10n: self.l10n });
  2063. }
  2064. function positionCalendar(customPositionElement) {
  2065. if (typeof self.config.position === "function") {
  2066. return void self.config.position(self, customPositionElement);
  2067. }
  2068. if (self.calendarContainer === undefined)
  2069. return;
  2070. triggerEvent("onPreCalendarPosition");
  2071. var positionElement = customPositionElement || self._positionElement;
  2072. 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 = window.innerHeight - inputBounds.bottom, showOnTop = configPosVertical === "above" ||
  2073. (configPosVertical !== "below" &&
  2074. distanceFromBottom < calendarHeight &&
  2075. inputBounds.top > calendarHeight);
  2076. var top = window.pageYOffset +
  2077. inputBounds.top +
  2078. (!showOnTop ? positionElement.offsetHeight + 2 : -calendarHeight - 2);
  2079. toggleClass(self.calendarContainer, "arrowTop", !showOnTop);
  2080. toggleClass(self.calendarContainer, "arrowBottom", showOnTop);
  2081. if (self.config.inline)
  2082. return;
  2083. var left = window.pageXOffset + inputBounds.left;
  2084. var isCenter = false;
  2085. var isRight = false;
  2086. if (configPosHorizontal === "center") {
  2087. left -= (calendarWidth - inputBounds.width) / 2;
  2088. isCenter = true;
  2089. }
  2090. else if (configPosHorizontal === "right") {
  2091. left -= calendarWidth - inputBounds.width;
  2092. isRight = true;
  2093. }
  2094. toggleClass(self.calendarContainer, "arrowLeft", !isCenter && !isRight);
  2095. toggleClass(self.calendarContainer, "arrowCenter", isCenter);
  2096. toggleClass(self.calendarContainer, "arrowRight", isRight);
  2097. var right = window.document.body.offsetWidth -
  2098. (window.pageXOffset + inputBounds.right);
  2099. var rightMost = left + calendarWidth > window.document.body.offsetWidth;
  2100. var centerMost = right + calendarWidth > window.document.body.offsetWidth;
  2101. toggleClass(self.calendarContainer, "rightMost", rightMost);
  2102. if (self.config.static)
  2103. return;
  2104. self.calendarContainer.style.top = top + "px";
  2105. if (!rightMost) {
  2106. self.calendarContainer.style.left = left + "px";
  2107. self.calendarContainer.style.right = "auto";
  2108. }
  2109. else if (!centerMost) {
  2110. self.calendarContainer.style.left = "auto";
  2111. self.calendarContainer.style.right = right + "px";
  2112. }
  2113. else {
  2114. var doc = getDocumentStyleSheet();
  2115. // some testing environments don't have css support
  2116. if (doc === undefined)
  2117. return;
  2118. var bodyWidth = window.document.body.offsetWidth;
  2119. var centerLeft = Math.max(0, bodyWidth / 2 - calendarWidth / 2);
  2120. var centerBefore = ".flatpickr-calendar.centerMost:before";
  2121. var centerAfter = ".flatpickr-calendar.centerMost:after";
  2122. var centerIndex = doc.cssRules.length;
  2123. var centerStyle = "{left:" + inputBounds.left + "px;right:auto;}";
  2124. toggleClass(self.calendarContainer, "rightMost", false);
  2125. toggleClass(self.calendarContainer, "centerMost", true);
  2126. doc.insertRule(centerBefore + "," + centerAfter + centerStyle, centerIndex);
  2127. self.calendarContainer.style.left = centerLeft + "px";
  2128. self.calendarContainer.style.right = "auto";
  2129. }
  2130. }
  2131. function getDocumentStyleSheet() {
  2132. var editableSheet = null;
  2133. for (var i = 0; i < document.styleSheets.length; i++) {
  2134. var sheet = document.styleSheets[i];
  2135. if (!sheet.cssRules)
  2136. continue;
  2137. try {
  2138. sheet.cssRules;
  2139. }
  2140. catch (err) {
  2141. continue;
  2142. }
  2143. editableSheet = sheet;
  2144. break;
  2145. }
  2146. return editableSheet != null ? editableSheet : createStyleSheet();
  2147. }
  2148. function createStyleSheet() {
  2149. var style = document.createElement("style");
  2150. document.head.appendChild(style);
  2151. return style.sheet;
  2152. }
  2153. function redraw() {
  2154. if (self.config.noCalendar || self.isMobile)
  2155. return;
  2156. buildMonthSwitch();
  2157. updateNavigationCurrentMonth();
  2158. buildDays();
  2159. }
  2160. function focusAndClose() {
  2161. self._input.focus();
  2162. if (window.navigator.userAgent.indexOf("MSIE") !== -1 ||
  2163. navigator.msMaxTouchPoints !== undefined) {
  2164. // hack - bugs in the way IE handles focus keeps the calendar open
  2165. setTimeout(self.close, 0);
  2166. }
  2167. else {
  2168. self.close();
  2169. }
  2170. }
  2171. function selectDate(e) {
  2172. e.preventDefault();
  2173. e.stopPropagation();
  2174. var isSelectable = function (day) {
  2175. return day.classList &&
  2176. day.classList.contains("flatpickr-day") &&
  2177. !day.classList.contains("flatpickr-disabled") &&
  2178. !day.classList.contains("notAllowed");
  2179. };
  2180. var t = findParent(getEventTarget(e), isSelectable);
  2181. if (t === undefined)
  2182. return;
  2183. var target = t;
  2184. var selectedDate = (self.latestSelectedDateObj = new Date(target.dateObj.getTime()));
  2185. var shouldChangeMonth = (selectedDate.getMonth() < self.currentMonth ||
  2186. selectedDate.getMonth() >
  2187. self.currentMonth + self.config.showMonths - 1) &&
  2188. self.config.mode !== "range";
  2189. self.selectedDateElem = target;
  2190. if (self.config.mode === "single")
  2191. self.selectedDates = [selectedDate];
  2192. else if (self.config.mode === "multiple") {
  2193. var selectedIndex = isDateSelected(selectedDate);
  2194. if (selectedIndex)
  2195. self.selectedDates.splice(parseInt(selectedIndex), 1);
  2196. else
  2197. self.selectedDates.push(selectedDate);
  2198. }
  2199. else if (self.config.mode === "range") {
  2200. if (self.selectedDates.length === 2) {
  2201. self.clear(false, false);
  2202. }
  2203. self.latestSelectedDateObj = selectedDate;
  2204. self.selectedDates.push(selectedDate);
  2205. // unless selecting same date twice, sort ascendingly
  2206. if (compareDates(selectedDate, self.selectedDates[0], true) !== 0)
  2207. self.selectedDates.sort(function (a, b) { return a.getTime() - b.getTime(); });
  2208. }
  2209. setHoursFromInputs();
  2210. if (shouldChangeMonth) {
  2211. var isNewYear = self.currentYear !== selectedDate.getFullYear();
  2212. self.currentYear = selectedDate.getFullYear();
  2213. self.currentMonth = selectedDate.getMonth();
  2214. if (isNewYear) {
  2215. triggerEvent("onYearChange");
  2216. buildMonthSwitch();
  2217. }
  2218. triggerEvent("onMonthChange");
  2219. }
  2220. updateNavigationCurrentMonth();
  2221. buildDays();
  2222. updateValue();
  2223. // maintain focus
  2224. if (!shouldChangeMonth &&
  2225. self.config.mode !== "range" &&
  2226. self.config.showMonths === 1)
  2227. focusOnDayElem(target);
  2228. else if (self.selectedDateElem !== undefined &&
  2229. self.hourElement === undefined) {
  2230. self.selectedDateElem && self.selectedDateElem.focus();
  2231. }
  2232. if (self.hourElement !== undefined)
  2233. self.hourElement !== undefined && self.hourElement.focus();
  2234. if (self.config.closeOnSelect) {
  2235. var single = self.config.mode === "single" && !self.config.enableTime;
  2236. var range = self.config.mode === "range" &&
  2237. self.selectedDates.length === 2 &&
  2238. !self.config.enableTime;
  2239. if (single || range) {
  2240. focusAndClose();
  2241. }
  2242. }
  2243. triggerChange();
  2244. }
  2245. var CALLBACKS = {
  2246. locale: [setupLocale, updateWeekdays],
  2247. showMonths: [buildMonths, setCalendarWidth, buildWeekdays],
  2248. minDate: [jumpToDate],
  2249. maxDate: [jumpToDate],
  2250. positionElement: [updatePositionElement],
  2251. clickOpens: [
  2252. function () {
  2253. if (self.config.clickOpens === true) {
  2254. bind(self._input, "focus", self.open);
  2255. bind(self._input, "click", self.open);
  2256. }
  2257. else {
  2258. self._input.removeEventListener("focus", self.open);
  2259. self._input.removeEventListener("click", self.open);
  2260. }
  2261. },
  2262. ],
  2263. };
  2264. function set(option, value) {
  2265. if (option !== null && typeof option === "object") {
  2266. Object.assign(self.config, option);
  2267. for (var key in option) {
  2268. if (CALLBACKS[key] !== undefined)
  2269. CALLBACKS[key].forEach(function (x) { return x(); });
  2270. }
  2271. }
  2272. else {
  2273. self.config[option] = value;
  2274. if (CALLBACKS[option] !== undefined)
  2275. CALLBACKS[option].forEach(function (x) { return x(); });
  2276. else if (HOOKS.indexOf(option) > -1)
  2277. self.config[option] = arrayify(value);
  2278. }
  2279. self.redraw();
  2280. updateValue(true);
  2281. }
  2282. function setSelectedDate(inputDate, format) {
  2283. var dates = [];
  2284. if (inputDate instanceof Array)
  2285. dates = inputDate.map(function (d) { return self.parseDate(d, format); });
  2286. else if (inputDate instanceof Date || typeof inputDate === "number")
  2287. dates = [self.parseDate(inputDate, format)];
  2288. else if (typeof inputDate === "string") {
  2289. switch (self.config.mode) {
  2290. case "single":
  2291. case "time":
  2292. dates = [self.parseDate(inputDate, format)];
  2293. break;
  2294. case "multiple":
  2295. dates = inputDate
  2296. .split(self.config.conjunction)
  2297. .map(function (date) { return self.parseDate(date, format); });
  2298. break;
  2299. case "range":
  2300. dates = inputDate
  2301. .split(self.l10n.rangeSeparator)
  2302. .map(function (date) { return self.parseDate(date, format); });
  2303. break;
  2304. }
  2305. }
  2306. else
  2307. self.config.errorHandler(new Error("Invalid date supplied: " + JSON.stringify(inputDate)));
  2308. self.selectedDates = (self.config.allowInvalidPreload
  2309. ? dates
  2310. : dates.filter(function (d) { return d instanceof Date && isEnabled(d, false); }));
  2311. if (self.config.mode === "range")
  2312. self.selectedDates.sort(function (a, b) { return a.getTime() - b.getTime(); });
  2313. }
  2314. function setDate(date, triggerChange, format) {
  2315. if (triggerChange === void 0) { triggerChange = false; }
  2316. if (format === void 0) { format = self.config.dateFormat; }
  2317. if ((date !== 0 && !date) || (date instanceof Array && date.length === 0))
  2318. return self.clear(triggerChange);
  2319. setSelectedDate(date, format);
  2320. self.latestSelectedDateObj =
  2321. self.selectedDates[self.selectedDates.length - 1];
  2322. self.redraw();
  2323. jumpToDate(undefined, triggerChange);
  2324. setHoursFromDate();
  2325. if (self.selectedDates.length === 0) {
  2326. self.clear(false);
  2327. }
  2328. updateValue(triggerChange);
  2329. if (triggerChange)
  2330. triggerEvent("onChange");
  2331. }
  2332. function parseDateRules(arr) {
  2333. return arr
  2334. .slice()
  2335. .map(function (rule) {
  2336. if (typeof rule === "string" ||
  2337. typeof rule === "number" ||
  2338. rule instanceof Date) {
  2339. return self.parseDate(rule, undefined, true);
  2340. }
  2341. else if (rule &&
  2342. typeof rule === "object" &&
  2343. rule.from &&
  2344. rule.to)
  2345. return {
  2346. from: self.parseDate(rule.from, undefined),
  2347. to: self.parseDate(rule.to, undefined),
  2348. };
  2349. return rule;
  2350. })
  2351. .filter(function (x) { return x; }); // remove falsy values
  2352. }
  2353. function setupDates() {
  2354. self.selectedDates = [];
  2355. self.now = self.parseDate(self.config.now) || new Date();
  2356. // Workaround IE11 setting placeholder as the input's value
  2357. var preloadedDate = self.config.defaultDate ||
  2358. ((self.input.nodeName === "INPUT" ||
  2359. self.input.nodeName === "TEXTAREA") &&
  2360. self.input.placeholder &&
  2361. self.input.value === self.input.placeholder
  2362. ? null
  2363. : self.input.value);
  2364. if (preloadedDate)
  2365. setSelectedDate(preloadedDate, self.config.dateFormat);
  2366. self._initialDate =
  2367. self.selectedDates.length > 0
  2368. ? self.selectedDates[0]
  2369. : self.config.minDate &&
  2370. self.config.minDate.getTime() > self.now.getTime()
  2371. ? self.config.minDate
  2372. : self.config.maxDate &&
  2373. self.config.maxDate.getTime() < self.now.getTime()
  2374. ? self.config.maxDate
  2375. : self.now;
  2376. self.currentYear = self._initialDate.getFullYear();
  2377. self.currentMonth = self._initialDate.getMonth();
  2378. if (self.selectedDates.length > 0)
  2379. self.latestSelectedDateObj = self.selectedDates[0];
  2380. if (self.config.minTime !== undefined)
  2381. self.config.minTime = self.parseDate(self.config.minTime, "H:i");
  2382. if (self.config.maxTime !== undefined)
  2383. self.config.maxTime = self.parseDate(self.config.maxTime, "H:i");
  2384. self.minDateHasTime =
  2385. !!self.config.minDate &&
  2386. (self.config.minDate.getHours() > 0 ||
  2387. self.config.minDate.getMinutes() > 0 ||
  2388. self.config.minDate.getSeconds() > 0);
  2389. self.maxDateHasTime =
  2390. !!self.config.maxDate &&
  2391. (self.config.maxDate.getHours() > 0 ||
  2392. self.config.maxDate.getMinutes() > 0 ||
  2393. self.config.maxDate.getSeconds() > 0);
  2394. }
  2395. function setupInputs() {
  2396. self.input = getInputElem();
  2397. /* istanbul ignore next */
  2398. if (!self.input) {
  2399. self.config.errorHandler(new Error("Invalid input element specified"));
  2400. return;
  2401. }
  2402. // hack: store previous type to restore it after destroy()
  2403. self.input._type = self.input.type;
  2404. self.input.type = "text";
  2405. self.input.classList.add("flatpickr-input");
  2406. self._input = self.input;
  2407. if (self.config.altInput) {
  2408. // replicate self.element
  2409. self.altInput = createElement(self.input.nodeName, self.config.altInputClass);
  2410. self._input = self.altInput;
  2411. self.altInput.placeholder = self.input.placeholder;
  2412. self.altInput.disabled = self.input.disabled;
  2413. self.altInput.required = self.input.required;
  2414. self.altInput.tabIndex = self.input.tabIndex;
  2415. self.altInput.type = "text";
  2416. self.input.setAttribute("type", "hidden");
  2417. if (!self.config.static && self.input.parentNode)
  2418. self.input.parentNode.insertBefore(self.altInput, self.input.nextSibling);
  2419. }
  2420. if (!self.config.allowInput)
  2421. self._input.setAttribute("readonly", "readonly");
  2422. updatePositionElement();
  2423. }
  2424. function updatePositionElement() {
  2425. self._positionElement = self.config.positionElement || self._input;
  2426. }
  2427. function setupMobile() {
  2428. var inputType = self.config.enableTime
  2429. ? self.config.noCalendar
  2430. ? "time"
  2431. : "datetime-local"
  2432. : "date";
  2433. self.mobileInput = createElement("input", self.input.className + " flatpickr-mobile");
  2434. self.mobileInput.tabIndex = 1;
  2435. self.mobileInput.type = inputType;
  2436. self.mobileInput.disabled = self.input.disabled;
  2437. self.mobileInput.required = self.input.required;
  2438. self.mobileInput.placeholder = self.input.placeholder;
  2439. self.mobileFormatStr =
  2440. inputType === "datetime-local"
  2441. ? "Y-m-d\\TH:i:S"
  2442. : inputType === "date"
  2443. ? "Y-m-d"
  2444. : "H:i:S";
  2445. if (self.selectedDates.length > 0) {
  2446. self.mobileInput.defaultValue = self.mobileInput.value = self.formatDate(self.selectedDates[0], self.mobileFormatStr);
  2447. }
  2448. if (self.config.minDate)
  2449. self.mobileInput.min = self.formatDate(self.config.minDate, "Y-m-d");
  2450. if (self.config.maxDate)
  2451. self.mobileInput.max = self.formatDate(self.config.maxDate, "Y-m-d");
  2452. if (self.input.getAttribute("step"))
  2453. self.mobileInput.step = String(self.input.getAttribute("step"));
  2454. self.input.type = "hidden";
  2455. if (self.altInput !== undefined)
  2456. self.altInput.type = "hidden";
  2457. try {
  2458. if (self.input.parentNode)
  2459. self.input.parentNode.insertBefore(self.mobileInput, self.input.nextSibling);
  2460. }
  2461. catch (_a) { }
  2462. bind(self.mobileInput, "change", function (e) {
  2463. self.setDate(getEventTarget(e).value, false, self.mobileFormatStr);
  2464. triggerEvent("onChange");
  2465. triggerEvent("onClose");
  2466. });
  2467. }
  2468. function toggle(e) {
  2469. if (self.isOpen === true)
  2470. return self.close();
  2471. self.open(e);
  2472. }
  2473. function triggerEvent(event, data) {
  2474. // If the instance has been destroyed already, all hooks have been removed
  2475. if (self.config === undefined)
  2476. return;
  2477. var hooks = self.config[event];
  2478. if (hooks !== undefined && hooks.length > 0) {
  2479. for (var i = 0; hooks[i] && i < hooks.length; i++)
  2480. hooks[i](self.selectedDates, self.input.value, self, data);
  2481. }
  2482. if (event === "onChange") {
  2483. self.input.dispatchEvent(createEvent("change"));
  2484. // many front-end frameworks bind to the input event
  2485. self.input.dispatchEvent(createEvent("input"));
  2486. }
  2487. }
  2488. function createEvent(name) {
  2489. var e = document.createEvent("Event");
  2490. e.initEvent(name, true, true);
  2491. return e;
  2492. }
  2493. function isDateSelected(date) {
  2494. for (var i = 0; i < self.selectedDates.length; i++) {
  2495. var selectedDate = self.selectedDates[i];
  2496. if (selectedDate instanceof Date &&
  2497. compareDates(selectedDate, date) === 0)
  2498. return "" + i;
  2499. }
  2500. return false;
  2501. }
  2502. function isDateInRange(date) {
  2503. if (self.config.mode !== "range" || self.selectedDates.length < 2)
  2504. return false;
  2505. return (compareDates(date, self.selectedDates[0]) >= 0 &&
  2506. compareDates(date, self.selectedDates[1]) <= 0);
  2507. }
  2508. function updateNavigationCurrentMonth() {
  2509. if (self.config.noCalendar || self.isMobile || !self.monthNav)
  2510. return;
  2511. self.yearElements.forEach(function (yearElement, i) {
  2512. var d = new Date(self.currentYear, self.currentMonth, 1);
  2513. d.setMonth(self.currentMonth + i);
  2514. if (self.config.showMonths > 1 ||
  2515. self.config.monthSelectorType === "static") {
  2516. self.monthElements[i].textContent =
  2517. monthToStr(d.getMonth(), self.config.shorthandCurrentMonth, self.l10n) + " ";
  2518. }
  2519. else {
  2520. self.monthsDropdownContainer.value = d.getMonth().toString();
  2521. }
  2522. yearElement.value = d.getFullYear().toString();
  2523. });
  2524. self._hidePrevMonthArrow =
  2525. self.config.minDate !== undefined &&
  2526. (self.currentYear === self.config.minDate.getFullYear()
  2527. ? self.currentMonth <= self.config.minDate.getMonth()
  2528. : self.currentYear < self.config.minDate.getFullYear());
  2529. self._hideNextMonthArrow =
  2530. self.config.maxDate !== undefined &&
  2531. (self.currentYear === self.config.maxDate.getFullYear()
  2532. ? self.currentMonth + 1 > self.config.maxDate.getMonth()
  2533. : self.currentYear > self.config.maxDate.getFullYear());
  2534. }
  2535. function getDateStr(specificFormat) {
  2536. var format = specificFormat ||
  2537. (self.config.altInput ? self.config.altFormat : self.config.dateFormat);
  2538. return self.selectedDates
  2539. .map(function (dObj) { return self.formatDate(dObj, format); })
  2540. .filter(function (d, i, arr) {
  2541. return self.config.mode !== "range" ||
  2542. self.config.enableTime ||
  2543. arr.indexOf(d) === i;
  2544. })
  2545. .join(self.config.mode !== "range"
  2546. ? self.config.conjunction
  2547. : self.l10n.rangeSeparator);
  2548. }
  2549. /**
  2550. * Updates the values of inputs associated with the calendar
  2551. */
  2552. function updateValue(triggerChange) {
  2553. if (triggerChange === void 0) { triggerChange = true; }
  2554. if (self.mobileInput !== undefined && self.mobileFormatStr) {
  2555. self.mobileInput.value =
  2556. self.latestSelectedDateObj !== undefined
  2557. ? self.formatDate(self.latestSelectedDateObj, self.mobileFormatStr)
  2558. : "";
  2559. }
  2560. self.input.value = getDateStr(self.config.dateFormat);
  2561. if (self.altInput !== undefined) {
  2562. self.altInput.value = getDateStr(self.config.altFormat);
  2563. }
  2564. if (triggerChange !== false)
  2565. triggerEvent("onValueUpdate");
  2566. }
  2567. function onMonthNavClick(e) {
  2568. var eventTarget = getEventTarget(e);
  2569. var isPrevMonth = self.prevMonthNav.contains(eventTarget);
  2570. var isNextMonth = self.nextMonthNav.contains(eventTarget);
  2571. if (isPrevMonth || isNextMonth) {
  2572. changeMonth(isPrevMonth ? -1 : 1);
  2573. }
  2574. else if (self.yearElements.indexOf(eventTarget) >= 0) {
  2575. eventTarget.select();
  2576. }
  2577. else if (eventTarget.classList.contains("arrowUp")) {
  2578. self.changeYear(self.currentYear + 1);
  2579. }
  2580. else if (eventTarget.classList.contains("arrowDown")) {
  2581. self.changeYear(self.currentYear - 1);
  2582. }
  2583. }
  2584. function timeWrapper(e) {
  2585. e.preventDefault();
  2586. var isKeyDown = e.type === "keydown", eventTarget = getEventTarget(e), input = eventTarget;
  2587. if (self.amPM !== undefined && eventTarget === self.amPM) {
  2588. self.amPM.textContent =
  2589. self.l10n.amPM[int(self.amPM.textContent === self.l10n.amPM[0])];
  2590. }
  2591. var min = parseFloat(input.getAttribute("min")), max = parseFloat(input.getAttribute("max")), step = parseFloat(input.getAttribute("step")), curValue = parseInt(input.value, 10), delta = e.delta ||
  2592. (isKeyDown ? (e.which === 38 ? 1 : -1) : 0);
  2593. var newValue = curValue + step * delta;
  2594. if (typeof input.value !== "undefined" && input.value.length === 2) {
  2595. var isHourElem = input === self.hourElement, isMinuteElem = input === self.minuteElement;
  2596. if (newValue < min) {
  2597. newValue =
  2598. max +
  2599. newValue +
  2600. int(!isHourElem) +
  2601. (int(isHourElem) && int(!self.amPM));
  2602. if (isMinuteElem)
  2603. incrementNumInput(undefined, -1, self.hourElement);
  2604. }
  2605. else if (newValue > max) {
  2606. newValue =
  2607. input === self.hourElement ? newValue - max - int(!self.amPM) : min;
  2608. if (isMinuteElem)
  2609. incrementNumInput(undefined, 1, self.hourElement);
  2610. }
  2611. if (self.amPM &&
  2612. isHourElem &&
  2613. (step === 1
  2614. ? newValue + curValue === 23
  2615. : Math.abs(newValue - curValue) > step)) {
  2616. self.amPM.textContent =
  2617. self.l10n.amPM[int(self.amPM.textContent === self.l10n.amPM[0])];
  2618. }
  2619. input.value = pad(newValue);
  2620. }
  2621. }
  2622. init();
  2623. return self;
  2624. }
  2625. /* istanbul ignore next */
  2626. function _flatpickr(nodeList, config) {
  2627. // static list
  2628. var nodes = Array.prototype.slice
  2629. .call(nodeList)
  2630. .filter(function (x) { return x instanceof HTMLElement; });
  2631. var instances = [];
  2632. for (var i = 0; i < nodes.length; i++) {
  2633. var node = nodes[i];
  2634. try {
  2635. if (node.getAttribute("data-fp-omit") !== null)
  2636. continue;
  2637. if (node._flatpickr !== undefined) {
  2638. node._flatpickr.destroy();
  2639. node._flatpickr = undefined;
  2640. }
  2641. node._flatpickr = FlatpickrInstance(node, config || {});
  2642. instances.push(node._flatpickr);
  2643. }
  2644. catch (e) {
  2645. console.error(e);
  2646. }
  2647. }
  2648. return instances.length === 1 ? instances[0] : instances;
  2649. }
  2650. /* istanbul ignore next */
  2651. if (typeof HTMLElement !== "undefined" &&
  2652. typeof HTMLCollection !== "undefined" &&
  2653. typeof NodeList !== "undefined") {
  2654. // browser env
  2655. HTMLCollection.prototype.flatpickr = NodeList.prototype.flatpickr = function (config) {
  2656. return _flatpickr(this, config);
  2657. };
  2658. HTMLElement.prototype.flatpickr = function (config) {
  2659. return _flatpickr([this], config);
  2660. };
  2661. }
  2662. /* istanbul ignore next */
  2663. var flatpickr = function (selector, config) {
  2664. if (typeof selector === "string") {
  2665. return _flatpickr(window.document.querySelectorAll(selector), config);
  2666. }
  2667. else if (selector instanceof Node) {
  2668. return _flatpickr([selector], config);
  2669. }
  2670. else {
  2671. return _flatpickr(selector, config);
  2672. }
  2673. };
  2674. /* istanbul ignore next */
  2675. flatpickr.defaultConfig = {};
  2676. flatpickr.l10ns = {
  2677. en: __assign({}, english),
  2678. default: __assign({}, english),
  2679. };
  2680. flatpickr.localize = function (l10n) {
  2681. flatpickr.l10ns.default = __assign(__assign({}, flatpickr.l10ns.default), l10n);
  2682. };
  2683. flatpickr.setDefaults = function (config) {
  2684. flatpickr.defaultConfig = __assign(__assign({}, flatpickr.defaultConfig), config);
  2685. };
  2686. flatpickr.parseDate = createDateParser({});
  2687. flatpickr.formatDate = createDateFormatter({});
  2688. flatpickr.compareDates = compareDates;
  2689. /* istanbul ignore next */
  2690. if (typeof jQuery !== "undefined" && typeof jQuery.fn !== "undefined") {
  2691. jQuery.fn.flatpickr = function (config) {
  2692. return _flatpickr(this, config);
  2693. };
  2694. }
  2695. Date.prototype.fp_incr = function (days) {
  2696. return new Date(this.getFullYear(), this.getMonth(), this.getDate() + (typeof days === "string" ? parseInt(days, 10) : days));
  2697. };
  2698. if (typeof window !== "undefined") {
  2699. window.flatpickr = flatpickr;
  2700. }
  2701. return flatpickr;
  2702. })));