| 1 |
(function (global, factory) { |
| 2 |
typeof exports === "object" && typeof module !== "undefined" |
| 3 |
? (module.exports = factory()) |
| 4 |
: typeof define === "function" && define.amd |
| 5 |
? define(factory) |
| 6 |
: ((global = global || self), (global.rangePlugin = factory())); |
| 7 |
})(this, function () { |
| 8 |
"use strict"; |
| 9 |
|
| 10 |
function rangePlugin(config) { |
| 11 |
if (config === void 0) { |
| 12 |
config = {}; |
| 13 |
} |
| 14 |
return function (fp) { |
| 15 |
var dateFormat = "", |
| 16 |
secondInput, |
| 17 |
_secondInputFocused, |
| 18 |
_prevDates; |
| 19 |
var createSecondInput = function () { |
| 20 |
if (config.input) { |
| 21 |
secondInput = |
| 22 |
config.input instanceof Element |
| 23 |
? config.input |
| 24 |
: window.document.querySelector(config.input); |
| 25 |
if (!secondInput) { |
| 26 |
fp.config.errorHandler( |
| 27 |
new Error("Invalid input element specified") |
| 28 |
); |
| 29 |
return; |
| 30 |
} |
| 31 |
if (fp.config.wrap) { |
| 32 |
secondInput = secondInput.querySelector("[data-input]"); |
| 33 |
} |
| 34 |
} else { |
| 35 |
secondInput = fp._input.cloneNode(); |
| 36 |
secondInput.removeAttribute("id"); |
| 37 |
secondInput._flatpickr = undefined; |
| 38 |
} |
| 39 |
if (secondInput.value) { |
| 40 |
var parsedDate = fp.parseDate(secondInput.value); |
| 41 |
if (parsedDate) fp.selectedDates.push(parsedDate); |
| 42 |
} |
| 43 |
secondInput.setAttribute("data-fp-omit", ""); |
| 44 |
fp._bind(secondInput, ["focus", "click"], function () { |
| 45 |
if (fp.selectedDates[1]) { |
| 46 |
fp.latestSelectedDateObj = fp.selectedDates[1]; |
| 47 |
fp._setHoursFromDate(fp.selectedDates[1]); |
| 48 |
fp.jumpToDate(fp.selectedDates[1]); |
| 49 |
} |
| 50 |
_secondInputFocused = true; |
| 51 |
fp.isOpen = false; |
| 52 |
fp.open( |
| 53 |
undefined, |
| 54 |
config.position === "left" ? fp._input : secondInput |
| 55 |
); |
| 56 |
}); |
| 57 |
fp._bind(fp._input, ["focus", "click"], function (e) { |
| 58 |
e.preventDefault(); |
| 59 |
fp.isOpen = false; |
| 60 |
fp.open(); |
| 61 |
}); |
| 62 |
if (fp.config.allowInput) |
| 63 |
fp._bind(secondInput, "keydown", function (e) { |
| 64 |
if (e.key === "Enter") { |
| 65 |
fp.setDate( |
| 66 |
[fp.selectedDates[0], secondInput.value], |
| 67 |
true, |
| 68 |
dateFormat |
| 69 |
); |
| 70 |
secondInput.click(); |
| 71 |
} |
| 72 |
}); |
| 73 |
if (!config.input) |
| 74 |
fp._input.parentNode && |
| 75 |
fp._input.parentNode.insertBefore( |
| 76 |
secondInput, |
| 77 |
fp._input.nextSibling |
| 78 |
); |
| 79 |
}; |
| 80 |
var plugin = { |
| 81 |
onParseConfig: function () { |
| 82 |
fp.config.mode = "range"; |
| 83 |
dateFormat = fp.config.altInput |
| 84 |
? fp.config.altFormat |
| 85 |
: fp.config.dateFormat; |
| 86 |
}, |
| 87 |
onReady: function () { |
| 88 |
createSecondInput(); |
| 89 |
fp.config.ignoredFocusElements.push(secondInput); |
| 90 |
if (fp.config.allowInput) { |
| 91 |
fp._input.removeAttribute("readonly"); |
| 92 |
secondInput.removeAttribute("readonly"); |
| 93 |
} else { |
| 94 |
secondInput.setAttribute("readonly", "readonly"); |
| 95 |
} |
| 96 |
fp._bind(fp._input, "focus", function () { |
| 97 |
fp.latestSelectedDateObj = fp.selectedDates[0]; |
| 98 |
fp._setHoursFromDate(fp.selectedDates[0]); |
| 99 |
_secondInputFocused = false; |
| 100 |
fp.jumpToDate(fp.selectedDates[0]); |
| 101 |
}); |
| 102 |
if (fp.config.allowInput) |
| 103 |
fp._bind(fp._input, "keydown", function (e) { |
| 104 |
if (e.key === "Enter") |
| 105 |
fp.setDate( |
| 106 |
[fp._input.value, fp.selectedDates[1]], |
| 107 |
true, |
| 108 |
dateFormat |
| 109 |
); |
| 110 |
}); |
| 111 |
fp.setDate(fp.selectedDates, false); |
| 112 |
plugin.onValueUpdate(fp.selectedDates); |
| 113 |
fp.loadedPlugins.push("range"); |
| 114 |
}, |
| 115 |
onPreCalendarPosition: function () { |
| 116 |
if (_secondInputFocused) { |
| 117 |
fp._positionElement = secondInput; |
| 118 |
setTimeout(function () { |
| 119 |
fp._positionElement = fp._input; |
| 120 |
}, 0); |
| 121 |
} |
| 122 |
}, |
| 123 |
onChange: function () { |
| 124 |
if (!fp.selectedDates.length) { |
| 125 |
setTimeout(function () { |
| 126 |
if (fp.selectedDates.length) 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) return; |
| 145 |
_prevDates = |
| 146 |
!_prevDates || selDates.length >= _prevDates.length |
| 147 |
? selDates.slice() |
| 148 |
: _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 = newDates.slice(); |
| 156 |
} |
| 157 |
(_a = fp.selectedDates.map(function (d) { |
| 158 |
return fp.formatDate(d, dateFormat); |
| 159 |
})), |
| 160 |
(_b = _a[0]), |
| 161 |
(fp._input.value = _b === void 0 ? "" : _b), |
| 162 |
(_c = _a[1]), |
| 163 |
(secondInput.value = _c === void 0 ? "" : _c); |
| 164 |
}, |
| 165 |
}; |
| 166 |
return plugin; |
| 167 |
}; |
| 168 |
} |
| 169 |
|
| 170 |
return rangePlugin; |
| 171 |
}); |
| 172 |
|