Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

339 linhas
11 KiB

  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("flatpickr"), require("vue"));
  4. else if(typeof define === 'function' && define.amd)
  5. define("VueFlatpickr", ["flatpickr", "vue"], factory);
  6. else if(typeof exports === 'object')
  7. exports["VueFlatpickr"] = factory(require("flatpickr"), require("vue"));
  8. else
  9. root["VueFlatpickr"] = factory(root["flatpickr"], root["Vue"]);
  10. })(this, (__WEBPACK_EXTERNAL_MODULE__311__, __WEBPACK_EXTERNAL_MODULE__976__) => {
  11. return /******/ (() => { // webpackBootstrap
  12. /******/ "use strict";
  13. /******/ var __webpack_modules__ = ({
  14. /***/ 311:
  15. /***/ ((module) => {
  16. module.exports = __WEBPACK_EXTERNAL_MODULE__311__;
  17. /***/ }),
  18. /***/ 976:
  19. /***/ ((module) => {
  20. module.exports = __WEBPACK_EXTERNAL_MODULE__976__;
  21. /***/ })
  22. /******/ });
  23. /************************************************************************/
  24. /******/ // The module cache
  25. /******/ var __webpack_module_cache__ = {};
  26. /******/
  27. /******/ // The require function
  28. /******/ function __webpack_require__(moduleId) {
  29. /******/ // Check if module is in cache
  30. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  31. /******/ if (cachedModule !== undefined) {
  32. /******/ return cachedModule.exports;
  33. /******/ }
  34. /******/ // Create a new module (and put it into the cache)
  35. /******/ var module = __webpack_module_cache__[moduleId] = {
  36. /******/ // no module.id needed
  37. /******/ // no module.loaded needed
  38. /******/ exports: {}
  39. /******/ };
  40. /******/
  41. /******/ // Execute the module function
  42. /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  43. /******/
  44. /******/ // Return the exports of the module
  45. /******/ return module.exports;
  46. /******/ }
  47. /******/
  48. /************************************************************************/
  49. /******/ /* webpack/runtime/compat get default export */
  50. /******/ (() => {
  51. /******/ // getDefaultExport function for compatibility with non-harmony modules
  52. /******/ __webpack_require__.n = (module) => {
  53. /******/ var getter = module && module.__esModule ?
  54. /******/ () => (module['default']) :
  55. /******/ () => (module);
  56. /******/ __webpack_require__.d(getter, { a: getter });
  57. /******/ return getter;
  58. /******/ };
  59. /******/ })();
  60. /******/
  61. /******/ /* webpack/runtime/define property getters */
  62. /******/ (() => {
  63. /******/ // define getter functions for harmony exports
  64. /******/ __webpack_require__.d = (exports, definition) => {
  65. /******/ for(var key in definition) {
  66. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  67. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  68. /******/ }
  69. /******/ }
  70. /******/ };
  71. /******/ })();
  72. /******/
  73. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  74. /******/ (() => {
  75. /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  76. /******/ })();
  77. /******/
  78. /******/ /* webpack/runtime/make namespace object */
  79. /******/ (() => {
  80. /******/ // define __esModule on exports
  81. /******/ __webpack_require__.r = (exports) => {
  82. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  83. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  84. /******/ }
  85. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  86. /******/ };
  87. /******/ })();
  88. /******/
  89. /************************************************************************/
  90. var __webpack_exports__ = {};
  91. // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
  92. (() => {
  93. // ESM COMPAT FLAG
  94. __webpack_require__.r(__webpack_exports__);
  95. // EXPORTS
  96. __webpack_require__.d(__webpack_exports__, {
  97. "default": () => (/* binding */ src)
  98. });
  99. // EXTERNAL MODULE: external "flatpickr"
  100. var external_flatpickr_ = __webpack_require__(311);
  101. var external_flatpickr_default = /*#__PURE__*/__webpack_require__.n(external_flatpickr_);
  102. // EXTERNAL MODULE: external {"commonjs":"vue","commonjs2":"vue","amd":"vue","root":"Vue"}
  103. var external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_ = __webpack_require__(976);
  104. ;// CONCATENATED MODULE: ./src/events.ts
  105. const includedEvents = [
  106. 'onChange',
  107. 'onClose',
  108. 'onDestroy',
  109. 'onMonthChange',
  110. 'onOpen',
  111. 'onYearChange',
  112. ];
  113. // Let's not emit these events by default
  114. const excludedEvents = [
  115. 'onValueUpdate',
  116. 'onDayCreate',
  117. 'onParseConfig',
  118. 'onReady',
  119. 'onPreCalendarPosition',
  120. 'onKeyDown',
  121. ];
  122. ;// CONCATENATED MODULE: ./src/util.ts
  123. function camelToKebab(string) {
  124. return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
  125. }
  126. function arrayify(obj) {
  127. return obj instanceof Array
  128. ? obj
  129. : [obj];
  130. }
  131. function nullify(value) {
  132. return (value && value.length)
  133. ? value
  134. : null;
  135. }
  136. ;// CONCATENATED MODULE: ./src/component.ts
  137. // Keep a copy of all events for later use
  138. const allEvents = [...includedEvents, ...excludedEvents];
  139. // Passing these properties in `fp.set()` method will cause flatpickr to trigger some callbacks
  140. const configCallbacks = ['locale', 'showMonths'];
  141. /* harmony default export */ const component = ((0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.defineComponent)({
  142. name: 'FlatPickr',
  143. compatConfig: {
  144. MODE: 3,
  145. },
  146. render() {
  147. return (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.h)('input', {
  148. type: 'text',
  149. 'data-input': true,
  150. disabled: this.disabled,
  151. onInput: this.onInput,
  152. });
  153. },
  154. emits: [
  155. 'blur',
  156. 'update:modelValue',
  157. ...allEvents.map(camelToKebab)
  158. ],
  159. props: {
  160. modelValue: {
  161. type: [String, Number, Date, Array, null],
  162. required: true,
  163. },
  164. // https://flatpickr.js.org/options/
  165. config: {
  166. type: Object,
  167. default: () => ({
  168. defaultDate: null,
  169. wrap: false,
  170. })
  171. },
  172. events: {
  173. type: Array,
  174. default: () => includedEvents
  175. },
  176. disabled: {
  177. type: Boolean,
  178. default: false
  179. },
  180. },
  181. data() {
  182. return {
  183. fp: null, //todo make it non-reactive
  184. };
  185. },
  186. mounted() {
  187. // Return early if flatpickr is already loaded
  188. /* istanbul ignore if */
  189. if (this.fp)
  190. return;
  191. // Init flatpickr
  192. this.fp = external_flatpickr_default()(this.getElem(), this.prepareConfig());
  193. // Attach blur event
  194. this.fpInput().addEventListener('blur', this.onBlur);
  195. // Immediate watch will fail before fp is set,
  196. // so we need to start watching after mount
  197. this.$watch('disabled', this.watchDisabled, {
  198. immediate: true
  199. });
  200. },
  201. methods: {
  202. prepareConfig() {
  203. // Don't mutate original object on parent component
  204. let safeConfig = Object.assign({}, this.config);
  205. this.events.forEach((hook) => {
  206. // Respect global callbacks registered via setDefault() method
  207. let globalCallbacks = (external_flatpickr_default()).defaultConfig[hook] || [];
  208. // Inject our own method along with user's callbacks
  209. let localCallback = (...args) => {
  210. this.$emit(camelToKebab(hook), ...args);
  211. };
  212. // Overwrite with merged array
  213. safeConfig[hook] = arrayify(safeConfig[hook] || []).concat(globalCallbacks, localCallback);
  214. });
  215. const onCloseCb = this.onClose.bind(this);
  216. safeConfig['onClose'] = arrayify(safeConfig['onClose'] || []).concat(onCloseCb);
  217. // Set initial date without emitting any event
  218. safeConfig.defaultDate = this.modelValue || safeConfig.defaultDate;
  219. return safeConfig;
  220. },
  221. /**
  222. * Get the HTML node where flatpickr to be attached
  223. * Bind on parent element if wrap is true
  224. */
  225. getElem() {
  226. return this.config.wrap ? this.$el.parentNode : this.$el;
  227. },
  228. /**
  229. * Watch for value changed by date-picker itself and notify parent component
  230. */
  231. onInput(event) {
  232. const input = event.target;
  233. // Let's wait for DOM to be updated
  234. (0,external_commonjs_vue_commonjs2_vue_amd_vue_root_Vue_.nextTick)().then(() => {
  235. this.$emit('update:modelValue', nullify(input.value));
  236. });
  237. },
  238. fpInput() {
  239. return this.fp.altInput || this.fp.input;
  240. },
  241. /**
  242. * Blur event is required by many validation libraries
  243. */
  244. onBlur(event) {
  245. this.$emit('blur', nullify(event.target.value));
  246. },
  247. /**
  248. * Flatpickr does not emit input event in some cases
  249. */
  250. onClose(selectedDates, dateStr) {
  251. this.$emit('update:modelValue', dateStr);
  252. },
  253. /**
  254. * Watch for the disabled property and sets the value to the real input.
  255. */
  256. watchDisabled(newState) {
  257. if (newState) {
  258. this.fpInput().setAttribute('disabled', '');
  259. }
  260. else {
  261. this.fpInput().removeAttribute('disabled');
  262. }
  263. }
  264. },
  265. watch: {
  266. /**
  267. * Watch for any config changes and redraw date-picker
  268. */
  269. config: {
  270. deep: true,
  271. handler(newConfig) {
  272. if (!this.fp)
  273. return;
  274. let safeConfig = Object.assign({}, newConfig);
  275. // Workaround: Don't pass hooks to configs again otherwise
  276. // previously registered hooks will stop working
  277. // Notice: we are looping through all events
  278. // This also means that new callbacks can not be passed once component has been initialized
  279. allEvents.forEach((hook) => {
  280. delete safeConfig[hook];
  281. });
  282. this.fp.set(safeConfig);
  283. // Workaround: Allow to change locale dynamically
  284. configCallbacks.forEach((name) => {
  285. if (typeof safeConfig[name] !== 'undefined') {
  286. this.fp.set(name, safeConfig[name]);
  287. }
  288. });
  289. }
  290. },
  291. /**
  292. * Watch for changes from parent component and update DOM
  293. */
  294. modelValue(newValue) {
  295. var _a;
  296. // Prevent updates if v-model value is same as input's current value
  297. if (!this.$el || newValue === nullify(this.$el.value))
  298. return;
  299. // Notify flatpickr instance that there is a change in value
  300. (_a = this.fp) === null || _a === void 0 ? void 0 : _a.setDate(newValue, true);
  301. }
  302. },
  303. beforeUnmount() {
  304. /* istanbul ignore else */
  305. if (!this.fp)
  306. return;
  307. this.fpInput().removeEventListener('blur', this.onBlur);
  308. this.fp.destroy();
  309. this.fp = null;
  310. }
  311. }));
  312. ;// CONCATENATED MODULE: ./src/index.ts
  313. /* harmony default export */ const src = (component);
  314. })();
  315. /******/ return __webpack_exports__;
  316. /******/ })()
  317. ;
  318. });