rangePlugin.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rangePlugin = factory());
  5. }(this, (function () { 'use strict';
  6. /*! *****************************************************************************
  7. Copyright (c) Microsoft Corporation.
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  11. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  12. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  13. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  14. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  15. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. PERFORMANCE OF THIS SOFTWARE.
  17. ***************************************************************************** */
  18. function __spreadArrays() {
  19. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  20. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  21. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  22. r[k] = a[j];
  23. return r;
  24. }
  25. function rangePlugin(config) {
  26. if (config === void 0) { config = {}; }
  27. return function (fp) {
  28. var dateFormat = "", secondInput, _secondInputFocused, _prevDates;
  29. var createSecondInput = function () {
  30. if (config.input) {
  31. secondInput =
  32. config.input instanceof Element
  33. ? config.input
  34. : window.document.querySelector(config.input);
  35. if (!secondInput) {
  36. fp.config.errorHandler(new Error("Invalid input element specified"));
  37. return;
  38. }
  39. if (fp.config.wrap) {
  40. secondInput = secondInput.querySelector("[data-input]");
  41. }
  42. }
  43. else {
  44. secondInput = fp._input.cloneNode();
  45. secondInput.removeAttribute("id");
  46. secondInput._flatpickr = undefined;
  47. }
  48. if (secondInput.value) {
  49. var parsedDate = fp.parseDate(secondInput.value);
  50. if (parsedDate)
  51. fp.selectedDates.push(parsedDate);
  52. }
  53. secondInput.setAttribute("data-fp-omit", "");
  54. if (fp.config.clickOpens) {
  55. fp._bind(secondInput, ["focus", "click"], function () {
  56. if (fp.selectedDates[1]) {
  57. fp.latestSelectedDateObj = fp.selectedDates[1];
  58. fp._setHoursFromDate(fp.selectedDates[1]);
  59. fp.jumpToDate(fp.selectedDates[1]);
  60. }
  61. _secondInputFocused = true;
  62. fp.isOpen = false;
  63. fp.open(undefined, config.position === "left" ? fp._input : secondInput);
  64. });
  65. fp._bind(fp._input, ["focus", "click"], function (e) {
  66. e.preventDefault();
  67. fp.isOpen = false;
  68. fp.open();
  69. });
  70. }
  71. if (fp.config.allowInput)
  72. fp._bind(secondInput, "keydown", function (e) {
  73. if (e.key === "Enter") {
  74. fp.setDate([fp.selectedDates[0], secondInput.value], true, dateFormat);
  75. secondInput.click();
  76. }
  77. });
  78. if (!config.input)
  79. fp._input.parentNode &&
  80. fp._input.parentNode.insertBefore(secondInput, fp._input.nextSibling);
  81. };
  82. var plugin = {
  83. onParseConfig: function () {
  84. fp.config.mode = "range";
  85. dateFormat = fp.config.altInput
  86. ? fp.config.altFormat
  87. : fp.config.dateFormat;
  88. },
  89. onReady: function () {
  90. createSecondInput();
  91. fp.config.ignoredFocusElements.push(secondInput);
  92. if (fp.config.allowInput) {
  93. fp._input.removeAttribute("readonly");
  94. secondInput.removeAttribute("readonly");
  95. }
  96. else {
  97. secondInput.setAttribute("readonly", "readonly");
  98. }
  99. fp._bind(fp._input, "focus", function () {
  100. fp.latestSelectedDateObj = fp.selectedDates[0];
  101. fp._setHoursFromDate(fp.selectedDates[0]);
  102. _secondInputFocused = false;
  103. fp.jumpToDate(fp.selectedDates[0]);
  104. });
  105. if (fp.config.allowInput)
  106. fp._bind(fp._input, "keydown", function (e) {
  107. if (e.key === "Enter")
  108. fp.setDate([fp._input.value, fp.selectedDates[1]], true, dateFormat);
  109. });
  110. fp.setDate(fp.selectedDates, false);
  111. plugin.onValueUpdate(fp.selectedDates);
  112. fp.loadedPlugins.push("range");
  113. },
  114. onPreCalendarPosition: function () {
  115. if (_secondInputFocused) {
  116. fp._positionElement = secondInput;
  117. setTimeout(function () {
  118. fp._positionElement = fp._input;
  119. }, 0);
  120. }
  121. },
  122. onChange: function () {
  123. if (!fp.selectedDates.length) {
  124. setTimeout(function () {
  125. if (fp.selectedDates.length)
  126. return;
  127. secondInput.value = "";
  128. _prevDates = [];
  129. }, 10);
  130. }
  131. if (_secondInputFocused) {
  132. setTimeout(function () {
  133. secondInput.focus();
  134. }, 0);
  135. }
  136. },
  137. onDestroy: function () {
  138. if (!config.input)
  139. secondInput.parentNode &&
  140. secondInput.parentNode.removeChild(secondInput);
  141. },
  142. onValueUpdate: function (selDates) {
  143. var _a, _b, _c;
  144. if (!secondInput)
  145. return;
  146. _prevDates =
  147. !_prevDates || selDates.length >= _prevDates.length
  148. ? __spreadArrays(selDates) : _prevDates;
  149. if (_prevDates.length > selDates.length) {
  150. var newSelectedDate = selDates[0];
  151. var newDates = _secondInputFocused
  152. ? [_prevDates[0], newSelectedDate]
  153. : [newSelectedDate, _prevDates[1]];
  154. fp.setDate(newDates, false);
  155. _prevDates = __spreadArrays(newDates);
  156. }
  157. _a = fp.selectedDates.map(function (d) { return fp.formatDate(d, dateFormat); }), _b = _a[0], fp._input.value = _b === void 0 ? "" : _b, _c = _a[1], secondInput.value = _c === void 0 ? "" : _c;
  158. },
  159. };
  160. return plugin;
  161. };
  162. }
  163. return rangePlugin;
  164. })));