├── .babelrc.js ├── .circleci └── config.yml ├── .eslintrc.js ├── .gitignore ├── .npmignore ├── .prettierrc.json ├── .vscode └── settings.json ├── LICENSE ├── dist ├── index.js ├── index.js.map ├── index.m.js ├── index.m.js.map ├── index.umd.js └── index.umd.js.map ├── images ├── datetime-days-of-week.png ├── datetime-picker-black-fr.png ├── datetime-picker-formats.png └── datetime-picker.png ├── karma.conf.js ├── package.json ├── playground ├── public │ ├── index.html │ └── style.css ├── src │ ├── controllers │ │ └── flatpickr_controller.js │ └── index.js └── webpack.config.js ├── readme.md ├── rollup.config.js ├── spec ├── controllers │ ├── flatpickr_controller.js │ └── flatpickr_hook_controller.js ├── fixtures │ ├── index-custom.html │ └── index.html ├── flatpickr_controller_events_spec.js ├── flatpickr_controller_spec.js ├── flatpickr_controller_targets_spec.js ├── flatpickr_custom_element_spec.js ├── helpers.js ├── mapping_spec.js └── utils_spec.js ├── src ├── config_options.js ├── elements.js ├── events.js ├── index.js ├── strftime_mapping.js └── utils.js └── yarn.lock /.babelrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | test: { 4 | plugins: [ 5 | [ 6 | 'istanbul', 7 | { 8 | exclude: ['spec/**/*.js'] 9 | } 10 | ] 11 | ] 12 | } 13 | }, 14 | presets: [ 15 | [ 16 | '@babel/preset-env', 17 | { 18 | targets: { 19 | node: 'current' 20 | } 21 | } 22 | ] 23 | ], 24 | plugins: [ 25 | '@babel/plugin-proposal-class-properties', 26 | '@babel/plugin-transform-classes', 27 | '@babel/plugin-proposal-object-rest-spread' 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Javascript Node CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-javascript/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/node:latest-browsers 11 | 12 | # Specify service dependencies here if necessary 13 | # CircleCI maintains a library of pre-built images 14 | # documented at https://circleci.com/docs/2.0/circleci-images/ 15 | # - image: circleci/mongo:3.4.4 16 | 17 | # working_directory: ~/repo 18 | 19 | steps: 20 | - checkout 21 | 22 | # Download and cache dependencies 23 | - restore_cache: 24 | keys: 25 | - v1-dependencies-{{ checksum "package.json" }} 26 | # fallback to using the latest cache if no exact match is found 27 | - v1-dependencies- 28 | 29 | - run: yarn install 30 | 31 | - save_cache: 32 | paths: 33 | - node_modules 34 | key: v1-dependencies-{{ checksum "package.json" }} 35 | 36 | # run tests! 37 | - run: yarn test 38 | - run: yarn codecov 39 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | browser: true, 4 | es6: true, 5 | mocha: true 6 | }, 7 | plugins: ['mocha'], 8 | extends: ['plugin:mocha/recommended'], 9 | globals: { 10 | Atomics: 'readonly', 11 | SharedArrayBuffer: 'readonly' 12 | }, 13 | parser: 'babel-eslint', 14 | parserOptions: { 15 | ecmaVersion: 2018, 16 | sourceType: 'module' 17 | }, 18 | rules: { 19 | 'space-before-function-paren': 'off', 20 | 'no-unused-expressions': 'off', 21 | 'mocha/no-setup-in-describe': 'off', 22 | 'mocha/no-top-level-hooks': 'off', 23 | 'mocha/no-hooks-for-single-case': 'off' 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | yarn-error.log 3 | .DS_Store 4 | notes.md 5 | /coverage 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .babelrc 2 | .DS_Store 3 | .gitignore 4 | .yarn.lock 5 | /.git 6 | /node_modules 7 | /playground 8 | /coverage 9 | /.circleci 10 | /images 11 | /spec 12 | karma.conf.js 13 | rollup.config.js 14 | .babelrc.js 15 | .eslintrc.js 16 | .prettierrc.json 17 | /.vscode -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "arrowParens": "always", 3 | "bracketSpacing": true, 4 | "printWidth": 120, 5 | "semi": false, 6 | "singleQuote": true, 7 | "tabWidth": 2, 8 | "trailingComma": "none", 9 | "useTabs": false, 10 | "overrides": [ 11 | { 12 | "files": "*.json", 13 | "options": { 14 | "printWidth": 200 15 | } 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true 3 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Adrien POLY 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } 4 | 5 | var stimulus = require('stimulus'); 6 | var flatpickr = _interopDefault(require('flatpickr')); 7 | 8 | function _classCallCheck(instance, Constructor) { 9 | if (!(instance instanceof Constructor)) { 10 | throw new TypeError("Cannot call a class as a function"); 11 | } 12 | } 13 | 14 | function _defineProperties(target, props) { 15 | for (var i = 0; i < props.length; i++) { 16 | var descriptor = props[i]; 17 | descriptor.enumerable = descriptor.enumerable || false; 18 | descriptor.configurable = true; 19 | if ("value" in descriptor) descriptor.writable = true; 20 | Object.defineProperty(target, descriptor.key, descriptor); 21 | } 22 | } 23 | 24 | function _createClass(Constructor, protoProps, staticProps) { 25 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); 26 | if (staticProps) _defineProperties(Constructor, staticProps); 27 | return Constructor; 28 | } 29 | 30 | function _defineProperty(obj, key, value) { 31 | if (key in obj) { 32 | Object.defineProperty(obj, key, { 33 | value: value, 34 | enumerable: true, 35 | configurable: true, 36 | writable: true 37 | }); 38 | } else { 39 | obj[key] = value; 40 | } 41 | 42 | return obj; 43 | } 44 | 45 | function ownKeys(object, enumerableOnly) { 46 | var keys = Object.keys(object); 47 | 48 | if (Object.getOwnPropertySymbols) { 49 | var symbols = Object.getOwnPropertySymbols(object); 50 | if (enumerableOnly) symbols = symbols.filter(function (sym) { 51 | return Object.getOwnPropertyDescriptor(object, sym).enumerable; 52 | }); 53 | keys.push.apply(keys, symbols); 54 | } 55 | 56 | return keys; 57 | } 58 | 59 | function _objectSpread2(target) { 60 | for (var i = 1; i < arguments.length; i++) { 61 | var source = arguments[i] != null ? arguments[i] : {}; 62 | 63 | if (i % 2) { 64 | ownKeys(Object(source), true).forEach(function (key) { 65 | _defineProperty(target, key, source[key]); 66 | }); 67 | } else if (Object.getOwnPropertyDescriptors) { 68 | Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); 69 | } else { 70 | ownKeys(Object(source)).forEach(function (key) { 71 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); 72 | }); 73 | } 74 | } 75 | 76 | return target; 77 | } 78 | 79 | function _inherits(subClass, superClass) { 80 | if (typeof superClass !== "function" && superClass !== null) { 81 | throw new TypeError("Super expression must either be null or a function"); 82 | } 83 | 84 | subClass.prototype = Object.create(superClass && superClass.prototype, { 85 | constructor: { 86 | value: subClass, 87 | writable: true, 88 | configurable: true 89 | } 90 | }); 91 | if (superClass) _setPrototypeOf(subClass, superClass); 92 | } 93 | 94 | function _getPrototypeOf(o) { 95 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { 96 | return o.__proto__ || Object.getPrototypeOf(o); 97 | }; 98 | return _getPrototypeOf(o); 99 | } 100 | 101 | function _setPrototypeOf(o, p) { 102 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { 103 | o.__proto__ = p; 104 | return o; 105 | }; 106 | 107 | return _setPrototypeOf(o, p); 108 | } 109 | 110 | function _isNativeReflectConstruct() { 111 | if (typeof Reflect === "undefined" || !Reflect.construct) return false; 112 | if (Reflect.construct.sham) return false; 113 | if (typeof Proxy === "function") return true; 114 | 115 | try { 116 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); 117 | return true; 118 | } catch (e) { 119 | return false; 120 | } 121 | } 122 | 123 | function _assertThisInitialized(self) { 124 | if (self === void 0) { 125 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); 126 | } 127 | 128 | return self; 129 | } 130 | 131 | function _possibleConstructorReturn(self, call) { 132 | if (call && (typeof call === "object" || typeof call === "function")) { 133 | return call; 134 | } 135 | 136 | return _assertThisInitialized(self); 137 | } 138 | 139 | function _createSuper(Derived) { 140 | return function () { 141 | var Super = _getPrototypeOf(Derived), 142 | result; 143 | 144 | if (_isNativeReflectConstruct()) { 145 | var NewTarget = _getPrototypeOf(this).constructor; 146 | 147 | result = Reflect.construct(Super, arguments, NewTarget); 148 | } else { 149 | result = Super.apply(this, arguments); 150 | } 151 | 152 | return _possibleConstructorReturn(this, result); 153 | }; 154 | } 155 | 156 | const kebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase(); 157 | const capitalize = string => { 158 | return string.charAt(0).toUpperCase() + string.slice(1); 159 | }; 160 | 161 | const booleanOptions = ['allowInput', 'altInput', 'animate', 'clickOpens', 'closeOnSelect', 'disableMobile', 'enableSeconds', 'enableTime', 'inline', 'noCalendar', 'shorthandCurrentMonth', 'static', 'time_24hr', 'weekNumbers', 'wrap']; 162 | const stringOptions = ['altInputClass', 'conjunction', 'mode', 'nextArrow', 'position', 'prevArrow', 'monthSelectorType']; 163 | const numberOptions = ['defaultHour', 'defaultMinute', 'defaultSeconds', 'hourIncrement', 'minuteIncrement', 'showMonths']; 164 | const arrayOptions = ['disable', 'enable', 'disableDaysOfWeek', 'enableDaysOfWeek']; 165 | const arrayOrStringOptions = ['defaultDate']; 166 | const dateOptions = ['maxDate', 'minDate', 'maxTime', 'minTime', 'now']; 167 | const dateFormats = ['altFormat', 'ariaDateFormat', 'dateFormat']; 168 | const options = { 169 | string: stringOptions, 170 | boolean: booleanOptions, 171 | date: dateOptions, 172 | array: arrayOptions, 173 | number: numberOptions, 174 | arrayOrString: arrayOrStringOptions 175 | }; 176 | 177 | const events = ['change', 'open', 'close', 'monthChange', 'yearChange', 'ready', 'valueUpdate', 'dayCreate']; 178 | 179 | const elements = ['calendarContainer', 'currentYearElement', 'days', 'daysContainer', 'input', 'nextMonthNav', 'monthNav', 'prevMonthNav', 'rContainer', 'selectedDateElem', 'todayDateElem', 'weekdayContainer']; 180 | 181 | const mapping = { 182 | '%Y': 'Y', 183 | '%y': 'y', 184 | '%C': 'Y', 185 | '%m': 'm', 186 | '%-m': 'n', 187 | '%_m': 'n', 188 | '%B': 'F', 189 | '%^B': 'F', 190 | '%b': 'M', 191 | '%^b': 'M', 192 | '%h': 'M', 193 | '%^h': 'M', 194 | '%d': 'd', 195 | '%-d': 'j', 196 | '%e': 'j', 197 | '%H': 'H', 198 | '%k': 'H', 199 | '%I': 'h', 200 | '%l': 'h', 201 | '%-l': 'h', 202 | '%P': 'K', 203 | '%p': 'K', 204 | '%M': 'i', 205 | '%S': 'S', 206 | '%A': 'l', 207 | '%a': 'D', 208 | '%w': 'w' 209 | }; 210 | const strftimeRegex = new RegExp(Object.keys(mapping).join('|').replace(new RegExp('\\^', 'g'), '\\^'), 'g'); 211 | const convertDateFormat = format => { 212 | return format.replace(strftimeRegex, match => { 213 | return mapping[match]; 214 | }); 215 | }; 216 | 217 | let StimulusFlatpickr = /*#__PURE__*/function (_Controller) { 218 | _inherits(StimulusFlatpickr, _Controller); 219 | 220 | var _super = _createSuper(StimulusFlatpickr); 221 | 222 | function StimulusFlatpickr() { 223 | _classCallCheck(this, StimulusFlatpickr); 224 | 225 | return _super.apply(this, arguments); 226 | } 227 | 228 | _createClass(StimulusFlatpickr, [{ 229 | key: "initialize", 230 | value: function initialize() { 231 | this.config = {}; 232 | } 233 | }, { 234 | key: "connect", 235 | value: function connect() { 236 | this._initializeEvents(); 237 | 238 | this._initializeOptions(); 239 | 240 | this._initializeDateFormats(); 241 | 242 | this.fp = flatpickr(this.flatpickrElement, _objectSpread2({}, this.config)); 243 | 244 | this._initializeElements(); 245 | } 246 | }, { 247 | key: "disconnect", 248 | value: function disconnect() { 249 | const value = this.inputTarget.value; 250 | this.fp.destroy(); 251 | this.inputTarget.value = value; 252 | } 253 | }, { 254 | key: "_initializeEvents", 255 | value: function _initializeEvents() { 256 | events.forEach(event => { 257 | if (this[event]) { 258 | const hook = `on${capitalize(event)}`; 259 | this.config[hook] = this[event].bind(this); 260 | } 261 | }); 262 | } 263 | }, { 264 | key: "_initializeOptions", 265 | value: function _initializeOptions() { 266 | Object.keys(options).forEach(optionType => { 267 | const optionsCamelCase = options[optionType]; 268 | optionsCamelCase.forEach(option => { 269 | const optionKebab = kebabCase(option); 270 | 271 | if (this.data.has(optionKebab)) { 272 | this.config[option] = this[`_${optionType}`](optionKebab); 273 | } 274 | }); 275 | }); 276 | 277 | this._handleDaysOfWeek(); 278 | } 279 | }, { 280 | key: "_handleDaysOfWeek", 281 | value: function _handleDaysOfWeek() { 282 | if (this.config.disableDaysOfWeek) { 283 | this.config.disableDaysOfWeek = this._validateDaysOfWeek(this.config.disableDaysOfWeek); 284 | this.config.disable = [...(this.config.disable || []), this._disable.bind(this)]; 285 | } 286 | 287 | if (this.config.enableDaysOfWeek) { 288 | this.config.enableDaysOfWeek = this._validateDaysOfWeek(this.config.enableDaysOfWeek); 289 | this.config.enable = [...(this.config.enable || []), this._enable.bind(this)]; 290 | } 291 | } 292 | }, { 293 | key: "_validateDaysOfWeek", 294 | value: function _validateDaysOfWeek(days) { 295 | if (Array.isArray(days)) { 296 | return days.map(day => parseInt(day)); 297 | } else { 298 | console.error('days of week must be a valid array'); 299 | return []; 300 | } 301 | } 302 | }, { 303 | key: "_disable", 304 | value: function _disable(date) { 305 | const disabledDays = this.config.disableDaysOfWeek; 306 | return disabledDays.includes(date.getDay()); 307 | } 308 | }, { 309 | key: "_enable", 310 | value: function _enable(date) { 311 | const enabledDays = this.config.enableDaysOfWeek; 312 | return enabledDays.includes(date.getDay()); 313 | } 314 | }, { 315 | key: "_initializeDateFormats", 316 | value: function _initializeDateFormats() { 317 | dateFormats.forEach(dateFormat => { 318 | if (this.data.has(dateFormat)) { 319 | this.config[dateFormat] = convertDateFormat(this.data.get(dateFormat)); 320 | } 321 | }); 322 | } 323 | }, { 324 | key: "_initializeElements", 325 | value: function _initializeElements() { 326 | elements.forEach(element => { 327 | this[`${element}Target`] = this.fp[element]; 328 | }); 329 | } 330 | }, { 331 | key: "_string", 332 | value: function _string(option) { 333 | return this.data.get(option); 334 | } 335 | }, { 336 | key: "_date", 337 | value: function _date(option) { 338 | return this.data.get(option); 339 | } 340 | }, { 341 | key: "_boolean", 342 | value: function _boolean(option) { 343 | return !(this.data.get(option) == '0' || this.data.get(option) == 'false'); 344 | } 345 | }, { 346 | key: "_array", 347 | value: function _array(option) { 348 | return JSON.parse(this.data.get(option)); 349 | } 350 | }, { 351 | key: "_number", 352 | value: function _number(option) { 353 | return parseInt(this.data.get(option)); 354 | } 355 | }, { 356 | key: "_arrayOrString", 357 | value: function _arrayOrString(option) { 358 | const val = this.data.get(option); 359 | 360 | try { 361 | return JSON.parse(val); 362 | } catch (e) { 363 | return val; 364 | } 365 | } 366 | }, { 367 | key: "flatpickrElement", 368 | get: function () { 369 | return this.hasInstanceTarget && this.instanceTarget || this.element; 370 | } 371 | }]); 372 | 373 | return StimulusFlatpickr; 374 | }(stimulus.Controller); 375 | 376 | _defineProperty(StimulusFlatpickr, "targets", ['instance']); 377 | 378 | module.exports = StimulusFlatpickr; 379 | //# sourceMappingURL=index.js.map 380 | -------------------------------------------------------------------------------- /dist/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.js","sources":["../src/utils.js","../src/config_options.js","../src/events.js","../src/elements.js","../src/strftime_mapping.js","../src/index.js"],"sourcesContent":["export const kebabCase = string =>\n string\n .replace(/([a-z])([A-Z])/g, \"$1-$2\")\n .replace(/[\\s_]+/g, \"-\")\n .toLowerCase();\n\nexport const capitalize = string => {\n return string.charAt(0).toUpperCase() + string.slice(1);\n};\n","const booleanOptions = [\n 'allowInput',\n 'altInput',\n 'animate',\n 'clickOpens',\n 'closeOnSelect',\n 'disableMobile',\n 'enableSeconds',\n 'enableTime',\n 'inline',\n 'noCalendar',\n 'shorthandCurrentMonth',\n 'static',\n 'time_24hr',\n 'weekNumbers',\n 'wrap'\n]\n\nconst stringOptions = [\n 'altInputClass',\n 'conjunction',\n 'mode',\n 'nextArrow',\n 'position',\n 'prevArrow',\n 'monthSelectorType'\n]\n\nconst numberOptions = [\n 'defaultHour',\n 'defaultMinute',\n 'defaultSeconds',\n 'hourIncrement',\n 'minuteIncrement',\n 'showMonths'\n]\n\nconst arrayOptions = ['disable', 'enable', 'disableDaysOfWeek', 'enableDaysOfWeek']\n\nconst arrayOrStringOptions = ['defaultDate']\n\nconst dateOptions = ['maxDate', 'minDate', 'maxTime', 'minTime', 'now']\n\nexport const dateFormats = ['altFormat', 'ariaDateFormat', 'dateFormat']\n\nexport const options = {\n string: stringOptions,\n boolean: booleanOptions,\n date: dateOptions,\n array: arrayOptions,\n number: numberOptions,\n arrayOrString: arrayOrStringOptions\n}\n","export const events = ['change', 'open', 'close', 'monthChange', 'yearChange', 'ready', 'valueUpdate', 'dayCreate']\n","export const elements = [\n 'calendarContainer',\n 'currentYearElement',\n 'days',\n 'daysContainer',\n 'input',\n 'nextMonthNav',\n 'monthNav',\n 'prevMonthNav',\n 'rContainer',\n 'selectedDateElem',\n 'todayDateElem',\n 'weekdayContainer'\n]\n","export const mapping = {\n '%Y': 'Y',\n '%y': 'y',\n '%C': 'Y',\n '%m': 'm',\n '%-m': 'n',\n '%_m': 'n',\n '%B': 'F',\n '%^B': 'F',\n '%b': 'M',\n '%^b': 'M',\n '%h': 'M',\n '%^h': 'M',\n '%d': 'd',\n '%-d': 'j',\n '%e': 'j',\n '%H': 'H',\n '%k': 'H',\n '%I': 'h',\n '%l': 'h',\n '%-l': 'h',\n '%P': 'K',\n '%p': 'K',\n '%M': 'i',\n '%S': 'S',\n '%A': 'l',\n '%a': 'D',\n '%w': 'w'\n}\n\nconst strftimeRegex = new RegExp(\n Object.keys(mapping)\n .join('|')\n .replace(new RegExp('\\\\^', 'g'), '\\\\^'),\n 'g'\n)\n\nexport const convertDateFormat = (format) => {\n return format.replace(strftimeRegex, (match) => {\n return mapping[match]\n })\n}\n","import { Controller } from 'stimulus'\nimport flatpickr from 'flatpickr'\nimport { kebabCase, capitalize } from './utils'\nimport { options, dateFormats } from './config_options'\nimport { events } from './events'\nimport { elements } from './elements'\nimport { convertDateFormat } from './strftime_mapping'\n\nclass StimulusFlatpickr extends Controller {\n static targets = ['instance']\n\n initialize() {\n this.config = {}\n }\n\n connect() {\n this._initializeEvents()\n this._initializeOptions()\n this._initializeDateFormats()\n\n this.fp = flatpickr(this.flatpickrElement, {\n ...this.config\n })\n\n this._initializeElements()\n }\n\n disconnect() {\n const value = this.inputTarget.value\n this.fp.destroy()\n this.inputTarget.value = value\n }\n\n _initializeEvents() {\n events.forEach((event) => {\n if (this[event]) {\n const hook = `on${capitalize(event)}`\n this.config[hook] = this[event].bind(this)\n }\n })\n }\n\n _initializeOptions() {\n Object.keys(options).forEach((optionType) => {\n const optionsCamelCase = options[optionType]\n optionsCamelCase.forEach((option) => {\n const optionKebab = kebabCase(option)\n if (this.data.has(optionKebab)) {\n this.config[option] = this[`_${optionType}`](optionKebab)\n }\n })\n })\n this._handleDaysOfWeek()\n }\n\n _handleDaysOfWeek() {\n if (this.config.disableDaysOfWeek) {\n this.config.disableDaysOfWeek = this._validateDaysOfWeek(this.config.disableDaysOfWeek)\n this.config.disable = [...(this.config.disable || []), this._disable.bind(this)]\n }\n\n if (this.config.enableDaysOfWeek) {\n this.config.enableDaysOfWeek = this._validateDaysOfWeek(this.config.enableDaysOfWeek)\n this.config.enable = [...(this.config.enable || []), this._enable.bind(this)]\n }\n }\n\n _validateDaysOfWeek(days) {\n if (Array.isArray(days)) {\n return days.map((day) => parseInt(day))\n } else {\n console.error('days of week must be a valid array')\n return []\n }\n }\n\n _disable(date) {\n const disabledDays = this.config.disableDaysOfWeek\n return disabledDays.includes(date.getDay())\n }\n\n _enable(date) {\n const enabledDays = this.config.enableDaysOfWeek\n return enabledDays.includes(date.getDay())\n }\n\n _initializeDateFormats() {\n dateFormats.forEach((dateFormat) => {\n if (this.data.has(dateFormat)) {\n this.config[dateFormat] = convertDateFormat(this.data.get(dateFormat))\n }\n })\n }\n\n _initializeElements() {\n elements.forEach((element) => {\n this[`${element}Target`] = this.fp[element]\n })\n }\n\n _string(option) {\n return this.data.get(option)\n }\n\n _date(option) {\n return this.data.get(option)\n }\n\n _boolean(option) {\n return !(this.data.get(option) == '0' || this.data.get(option) == 'false')\n }\n\n _array(option) {\n return JSON.parse(this.data.get(option))\n }\n\n _number(option) {\n return parseInt(this.data.get(option))\n }\n\n _arrayOrString(option) {\n const val = this.data.get(option)\n try {\n return JSON.parse(val)\n } catch (e) {\n return val\n }\n }\n\n get flatpickrElement() {\n return (this.hasInstanceTarget && this.instanceTarget) || this.element\n }\n}\n\nexport default StimulusFlatpickr\n"],"names":["kebabCase","string","replace","toLowerCase","capitalize","charAt","toUpperCase","slice","booleanOptions","stringOptions","numberOptions","arrayOptions","arrayOrStringOptions","dateOptions","dateFormats","options","boolean","date","array","number","arrayOrString","events","elements","mapping","strftimeRegex","RegExp","Object","keys","join","convertDateFormat","format","match","StimulusFlatpickr","config","_initializeEvents","_initializeOptions","_initializeDateFormats","fp","flatpickr","flatpickrElement","_initializeElements","value","inputTarget","destroy","forEach","event","hook","bind","optionType","optionsCamelCase","option","optionKebab","data","has","_handleDaysOfWeek","disableDaysOfWeek","_validateDaysOfWeek","disable","_disable","enableDaysOfWeek","enable","_enable","days","Array","isArray","map","day","parseInt","console","error","disabledDays","includes","getDay","enabledDays","dateFormat","get","element","JSON","parse","val","e","hasInstanceTarget","instanceTarget","Controller"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAO,MAAMA,SAAS,GAAGC,MAAM,IAC7BA,MAAM,CACHC,OADH,CACW,iBADX,EAC8B,OAD9B,EAEGA,OAFH,CAEW,SAFX,EAEsB,GAFtB,EAGGC,WAHH,EADK;AAMA,MAAMC,UAAU,GAAGH,MAAM,IAAI;AAClC,SAAOA,MAAM,CAACI,MAAP,CAAc,CAAd,EAAiBC,WAAjB,KAAiCL,MAAM,CAACM,KAAP,CAAa,CAAb,CAAxC;AACD,CAFM;;ACNP,MAAMC,cAAc,GAAG,CACrB,YADqB,EAErB,UAFqB,EAGrB,SAHqB,EAIrB,YAJqB,EAKrB,eALqB,EAMrB,eANqB,EAOrB,eAPqB,EAQrB,YARqB,EASrB,QATqB,EAUrB,YAVqB,EAWrB,uBAXqB,EAYrB,QAZqB,EAarB,WAbqB,EAcrB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3B;AACD,OAFD;AAGD;;;4BAEO1B,QAAQ;AACd,aAAO,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAP;AACD;;;0BAEKA,QAAQ;AACZ,aAAO,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAP;AACD;;;6BAEQA,QAAQ;AACf,aAAO,EAAE,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,KAAyB,GAAzB,IAAgC,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,KAAyB,OAA3D,CAAP;AACD;;;2BAEMA,QAAQ;AACb,aAAO2B,IAAI,CAACC,KAAL,CAAW,KAAK1B,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAX,CAAP;AACD;;;4BAEOA,QAAQ;AACd,aAAOiB,QAAQ,CAAC,KAAKf,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAD,CAAf;AACD;;;mCAEcA,QAAQ;AACrB,YAAM6B,GAAG,GAAG,KAAK3B,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAZ;;AACA,UAAI;AACF,eAAO2B,IAAI,CAACC,KAAL,CAAWC,GAAX,CAAP;AACD,OAFD,CAEE,OAAOC,CAAP,EAAU;AACV,eAAOD,GAAP;AACD;AACF;;;qBAEsB;AACrB,aAAQ,KAAKE,iBAAL,IAA0B,KAAKC,cAAhC,IAAmD,KAAKN,OAA/D;AACD;;;;EA3H6BO;;gBAA1BnD,8BACa,CAAC,UAAD;;;;"} -------------------------------------------------------------------------------- /dist/index.m.js: -------------------------------------------------------------------------------- 1 | import { Controller } from 'stimulus'; 2 | import flatpickr from 'flatpickr'; 3 | 4 | function _classCallCheck(instance, Constructor) { 5 | if (!(instance instanceof Constructor)) { 6 | throw new TypeError("Cannot call a class as a function"); 7 | } 8 | } 9 | 10 | function _defineProperties(target, props) { 11 | for (var i = 0; i < props.length; i++) { 12 | var descriptor = props[i]; 13 | descriptor.enumerable = descriptor.enumerable || false; 14 | descriptor.configurable = true; 15 | if ("value" in descriptor) descriptor.writable = true; 16 | Object.defineProperty(target, descriptor.key, descriptor); 17 | } 18 | } 19 | 20 | function _createClass(Constructor, protoProps, staticProps) { 21 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); 22 | if (staticProps) _defineProperties(Constructor, staticProps); 23 | return Constructor; 24 | } 25 | 26 | function _defineProperty(obj, key, value) { 27 | if (key in obj) { 28 | Object.defineProperty(obj, key, { 29 | value: value, 30 | enumerable: true, 31 | configurable: true, 32 | writable: true 33 | }); 34 | } else { 35 | obj[key] = value; 36 | } 37 | 38 | return obj; 39 | } 40 | 41 | function ownKeys(object, enumerableOnly) { 42 | var keys = Object.keys(object); 43 | 44 | if (Object.getOwnPropertySymbols) { 45 | var symbols = Object.getOwnPropertySymbols(object); 46 | if (enumerableOnly) symbols = symbols.filter(function (sym) { 47 | return Object.getOwnPropertyDescriptor(object, sym).enumerable; 48 | }); 49 | keys.push.apply(keys, symbols); 50 | } 51 | 52 | return keys; 53 | } 54 | 55 | function _objectSpread2(target) { 56 | for (var i = 1; i < arguments.length; i++) { 57 | var source = arguments[i] != null ? arguments[i] : {}; 58 | 59 | if (i % 2) { 60 | ownKeys(Object(source), true).forEach(function (key) { 61 | _defineProperty(target, key, source[key]); 62 | }); 63 | } else if (Object.getOwnPropertyDescriptors) { 64 | Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); 65 | } else { 66 | ownKeys(Object(source)).forEach(function (key) { 67 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); 68 | }); 69 | } 70 | } 71 | 72 | return target; 73 | } 74 | 75 | function _inherits(subClass, superClass) { 76 | if (typeof superClass !== "function" && superClass !== null) { 77 | throw new TypeError("Super expression must either be null or a function"); 78 | } 79 | 80 | subClass.prototype = Object.create(superClass && superClass.prototype, { 81 | constructor: { 82 | value: subClass, 83 | writable: true, 84 | configurable: true 85 | } 86 | }); 87 | if (superClass) _setPrototypeOf(subClass, superClass); 88 | } 89 | 90 | function _getPrototypeOf(o) { 91 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { 92 | return o.__proto__ || Object.getPrototypeOf(o); 93 | }; 94 | return _getPrototypeOf(o); 95 | } 96 | 97 | function _setPrototypeOf(o, p) { 98 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { 99 | o.__proto__ = p; 100 | return o; 101 | }; 102 | 103 | return _setPrototypeOf(o, p); 104 | } 105 | 106 | function _isNativeReflectConstruct() { 107 | if (typeof Reflect === "undefined" || !Reflect.construct) return false; 108 | if (Reflect.construct.sham) return false; 109 | if (typeof Proxy === "function") return true; 110 | 111 | try { 112 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); 113 | return true; 114 | } catch (e) { 115 | return false; 116 | } 117 | } 118 | 119 | function _assertThisInitialized(self) { 120 | if (self === void 0) { 121 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); 122 | } 123 | 124 | return self; 125 | } 126 | 127 | function _possibleConstructorReturn(self, call) { 128 | if (call && (typeof call === "object" || typeof call === "function")) { 129 | return call; 130 | } 131 | 132 | return _assertThisInitialized(self); 133 | } 134 | 135 | function _createSuper(Derived) { 136 | return function () { 137 | var Super = _getPrototypeOf(Derived), 138 | result; 139 | 140 | if (_isNativeReflectConstruct()) { 141 | var NewTarget = _getPrototypeOf(this).constructor; 142 | 143 | result = Reflect.construct(Super, arguments, NewTarget); 144 | } else { 145 | result = Super.apply(this, arguments); 146 | } 147 | 148 | return _possibleConstructorReturn(this, result); 149 | }; 150 | } 151 | 152 | const kebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase(); 153 | const capitalize = string => { 154 | return string.charAt(0).toUpperCase() + string.slice(1); 155 | }; 156 | 157 | const booleanOptions = ['allowInput', 'altInput', 'animate', 'clickOpens', 'closeOnSelect', 'disableMobile', 'enableSeconds', 'enableTime', 'inline', 'noCalendar', 'shorthandCurrentMonth', 'static', 'time_24hr', 'weekNumbers', 'wrap']; 158 | const stringOptions = ['altInputClass', 'conjunction', 'mode', 'nextArrow', 'position', 'prevArrow', 'monthSelectorType']; 159 | const numberOptions = ['defaultHour', 'defaultMinute', 'defaultSeconds', 'hourIncrement', 'minuteIncrement', 'showMonths']; 160 | const arrayOptions = ['disable', 'enable', 'disableDaysOfWeek', 'enableDaysOfWeek']; 161 | const arrayOrStringOptions = ['defaultDate']; 162 | const dateOptions = ['maxDate', 'minDate', 'maxTime', 'minTime', 'now']; 163 | const dateFormats = ['altFormat', 'ariaDateFormat', 'dateFormat']; 164 | const options = { 165 | string: stringOptions, 166 | boolean: booleanOptions, 167 | date: dateOptions, 168 | array: arrayOptions, 169 | number: numberOptions, 170 | arrayOrString: arrayOrStringOptions 171 | }; 172 | 173 | const events = ['change', 'open', 'close', 'monthChange', 'yearChange', 'ready', 'valueUpdate', 'dayCreate']; 174 | 175 | const elements = ['calendarContainer', 'currentYearElement', 'days', 'daysContainer', 'input', 'nextMonthNav', 'monthNav', 'prevMonthNav', 'rContainer', 'selectedDateElem', 'todayDateElem', 'weekdayContainer']; 176 | 177 | const mapping = { 178 | '%Y': 'Y', 179 | '%y': 'y', 180 | '%C': 'Y', 181 | '%m': 'm', 182 | '%-m': 'n', 183 | '%_m': 'n', 184 | '%B': 'F', 185 | '%^B': 'F', 186 | '%b': 'M', 187 | '%^b': 'M', 188 | '%h': 'M', 189 | '%^h': 'M', 190 | '%d': 'd', 191 | '%-d': 'j', 192 | '%e': 'j', 193 | '%H': 'H', 194 | '%k': 'H', 195 | '%I': 'h', 196 | '%l': 'h', 197 | '%-l': 'h', 198 | '%P': 'K', 199 | '%p': 'K', 200 | '%M': 'i', 201 | '%S': 'S', 202 | '%A': 'l', 203 | '%a': 'D', 204 | '%w': 'w' 205 | }; 206 | const strftimeRegex = new RegExp(Object.keys(mapping).join('|').replace(new RegExp('\\^', 'g'), '\\^'), 'g'); 207 | const convertDateFormat = format => { 208 | return format.replace(strftimeRegex, match => { 209 | return mapping[match]; 210 | }); 211 | }; 212 | 213 | let StimulusFlatpickr = /*#__PURE__*/function (_Controller) { 214 | _inherits(StimulusFlatpickr, _Controller); 215 | 216 | var _super = _createSuper(StimulusFlatpickr); 217 | 218 | function StimulusFlatpickr() { 219 | _classCallCheck(this, StimulusFlatpickr); 220 | 221 | return _super.apply(this, arguments); 222 | } 223 | 224 | _createClass(StimulusFlatpickr, [{ 225 | key: "initialize", 226 | value: function initialize() { 227 | this.config = {}; 228 | } 229 | }, { 230 | key: "connect", 231 | value: function connect() { 232 | this._initializeEvents(); 233 | 234 | this._initializeOptions(); 235 | 236 | this._initializeDateFormats(); 237 | 238 | this.fp = flatpickr(this.flatpickrElement, _objectSpread2({}, this.config)); 239 | 240 | this._initializeElements(); 241 | } 242 | }, { 243 | key: "disconnect", 244 | value: function disconnect() { 245 | const value = this.inputTarget.value; 246 | this.fp.destroy(); 247 | this.inputTarget.value = value; 248 | } 249 | }, { 250 | key: "_initializeEvents", 251 | value: function _initializeEvents() { 252 | events.forEach(event => { 253 | if (this[event]) { 254 | const hook = `on${capitalize(event)}`; 255 | this.config[hook] = this[event].bind(this); 256 | } 257 | }); 258 | } 259 | }, { 260 | key: "_initializeOptions", 261 | value: function _initializeOptions() { 262 | Object.keys(options).forEach(optionType => { 263 | const optionsCamelCase = options[optionType]; 264 | optionsCamelCase.forEach(option => { 265 | const optionKebab = kebabCase(option); 266 | 267 | if (this.data.has(optionKebab)) { 268 | this.config[option] = this[`_${optionType}`](optionKebab); 269 | } 270 | }); 271 | }); 272 | 273 | this._handleDaysOfWeek(); 274 | } 275 | }, { 276 | key: "_handleDaysOfWeek", 277 | value: function _handleDaysOfWeek() { 278 | if (this.config.disableDaysOfWeek) { 279 | this.config.disableDaysOfWeek = this._validateDaysOfWeek(this.config.disableDaysOfWeek); 280 | this.config.disable = [...(this.config.disable || []), this._disable.bind(this)]; 281 | } 282 | 283 | if (this.config.enableDaysOfWeek) { 284 | this.config.enableDaysOfWeek = this._validateDaysOfWeek(this.config.enableDaysOfWeek); 285 | this.config.enable = [...(this.config.enable || []), this._enable.bind(this)]; 286 | } 287 | } 288 | }, { 289 | key: "_validateDaysOfWeek", 290 | value: function _validateDaysOfWeek(days) { 291 | if (Array.isArray(days)) { 292 | return days.map(day => parseInt(day)); 293 | } else { 294 | console.error('days of week must be a valid array'); 295 | return []; 296 | } 297 | } 298 | }, { 299 | key: "_disable", 300 | value: function _disable(date) { 301 | const disabledDays = this.config.disableDaysOfWeek; 302 | return disabledDays.includes(date.getDay()); 303 | } 304 | }, { 305 | key: "_enable", 306 | value: function _enable(date) { 307 | const enabledDays = this.config.enableDaysOfWeek; 308 | return enabledDays.includes(date.getDay()); 309 | } 310 | }, { 311 | key: "_initializeDateFormats", 312 | value: function _initializeDateFormats() { 313 | dateFormats.forEach(dateFormat => { 314 | if (this.data.has(dateFormat)) { 315 | this.config[dateFormat] = convertDateFormat(this.data.get(dateFormat)); 316 | } 317 | }); 318 | } 319 | }, { 320 | key: "_initializeElements", 321 | value: function _initializeElements() { 322 | elements.forEach(element => { 323 | this[`${element}Target`] = this.fp[element]; 324 | }); 325 | } 326 | }, { 327 | key: "_string", 328 | value: function _string(option) { 329 | return this.data.get(option); 330 | } 331 | }, { 332 | key: "_date", 333 | value: function _date(option) { 334 | return this.data.get(option); 335 | } 336 | }, { 337 | key: "_boolean", 338 | value: function _boolean(option) { 339 | return !(this.data.get(option) == '0' || this.data.get(option) == 'false'); 340 | } 341 | }, { 342 | key: "_array", 343 | value: function _array(option) { 344 | return JSON.parse(this.data.get(option)); 345 | } 346 | }, { 347 | key: "_number", 348 | value: function _number(option) { 349 | return parseInt(this.data.get(option)); 350 | } 351 | }, { 352 | key: "_arrayOrString", 353 | value: function _arrayOrString(option) { 354 | const val = this.data.get(option); 355 | 356 | try { 357 | return JSON.parse(val); 358 | } catch (e) { 359 | return val; 360 | } 361 | } 362 | }, { 363 | key: "flatpickrElement", 364 | get: function () { 365 | return this.hasInstanceTarget && this.instanceTarget || this.element; 366 | } 367 | }]); 368 | 369 | return StimulusFlatpickr; 370 | }(Controller); 371 | 372 | _defineProperty(StimulusFlatpickr, "targets", ['instance']); 373 | 374 | export default StimulusFlatpickr; 375 | //# sourceMappingURL=index.m.js.map 376 | -------------------------------------------------------------------------------- /dist/index.m.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.m.js","sources":["../src/utils.js","../src/config_options.js","../src/events.js","../src/elements.js","../src/strftime_mapping.js","../src/index.js"],"sourcesContent":["export const kebabCase = string =>\n string\n .replace(/([a-z])([A-Z])/g, \"$1-$2\")\n .replace(/[\\s_]+/g, \"-\")\n .toLowerCase();\n\nexport const capitalize = string => {\n return string.charAt(0).toUpperCase() + string.slice(1);\n};\n","const booleanOptions = [\n 'allowInput',\n 'altInput',\n 'animate',\n 'clickOpens',\n 'closeOnSelect',\n 'disableMobile',\n 'enableSeconds',\n 'enableTime',\n 'inline',\n 'noCalendar',\n 'shorthandCurrentMonth',\n 'static',\n 'time_24hr',\n 'weekNumbers',\n 'wrap'\n]\n\nconst stringOptions = [\n 'altInputClass',\n 'conjunction',\n 'mode',\n 'nextArrow',\n 'position',\n 'prevArrow',\n 'monthSelectorType'\n]\n\nconst numberOptions = [\n 'defaultHour',\n 'defaultMinute',\n 'defaultSeconds',\n 'hourIncrement',\n 'minuteIncrement',\n 'showMonths'\n]\n\nconst arrayOptions = ['disable', 'enable', 'disableDaysOfWeek', 'enableDaysOfWeek']\n\nconst arrayOrStringOptions = ['defaultDate']\n\nconst dateOptions = ['maxDate', 'minDate', 'maxTime', 'minTime', 'now']\n\nexport const dateFormats = ['altFormat', 'ariaDateFormat', 'dateFormat']\n\nexport const options = {\n string: stringOptions,\n boolean: booleanOptions,\n date: dateOptions,\n array: arrayOptions,\n number: numberOptions,\n arrayOrString: arrayOrStringOptions\n}\n","export const events = ['change', 'open', 'close', 'monthChange', 'yearChange', 'ready', 'valueUpdate', 'dayCreate']\n","export const elements = [\n 'calendarContainer',\n 'currentYearElement',\n 'days',\n 'daysContainer',\n 'input',\n 'nextMonthNav',\n 'monthNav',\n 'prevMonthNav',\n 'rContainer',\n 'selectedDateElem',\n 'todayDateElem',\n 'weekdayContainer'\n]\n","export const mapping = {\n '%Y': 'Y',\n '%y': 'y',\n '%C': 'Y',\n '%m': 'm',\n '%-m': 'n',\n '%_m': 'n',\n '%B': 'F',\n '%^B': 'F',\n '%b': 'M',\n '%^b': 'M',\n '%h': 'M',\n '%^h': 'M',\n '%d': 'd',\n '%-d': 'j',\n '%e': 'j',\n '%H': 'H',\n '%k': 'H',\n '%I': 'h',\n '%l': 'h',\n '%-l': 'h',\n '%P': 'K',\n '%p': 'K',\n '%M': 'i',\n '%S': 'S',\n '%A': 'l',\n '%a': 'D',\n '%w': 'w'\n}\n\nconst strftimeRegex = new RegExp(\n Object.keys(mapping)\n .join('|')\n .replace(new RegExp('\\\\^', 'g'), '\\\\^'),\n 'g'\n)\n\nexport const convertDateFormat = (format) => {\n return format.replace(strftimeRegex, (match) => {\n return mapping[match]\n })\n}\n","import { Controller } from 'stimulus'\nimport flatpickr from 'flatpickr'\nimport { kebabCase, capitalize } from './utils'\nimport { options, dateFormats } from './config_options'\nimport { events } from './events'\nimport { elements } from './elements'\nimport { convertDateFormat } from './strftime_mapping'\n\nclass StimulusFlatpickr extends Controller {\n static targets = ['instance']\n\n initialize() {\n this.config = {}\n }\n\n connect() {\n this._initializeEvents()\n this._initializeOptions()\n this._initializeDateFormats()\n\n this.fp = flatpickr(this.flatpickrElement, {\n ...this.config\n })\n\n this._initializeElements()\n }\n\n disconnect() {\n const value = this.inputTarget.value\n this.fp.destroy()\n this.inputTarget.value = value\n }\n\n _initializeEvents() {\n events.forEach((event) => {\n if (this[event]) {\n const hook = `on${capitalize(event)}`\n this.config[hook] = this[event].bind(this)\n }\n })\n }\n\n _initializeOptions() {\n Object.keys(options).forEach((optionType) => {\n const optionsCamelCase = options[optionType]\n optionsCamelCase.forEach((option) => {\n const optionKebab = kebabCase(option)\n if (this.data.has(optionKebab)) {\n this.config[option] = this[`_${optionType}`](optionKebab)\n }\n })\n })\n this._handleDaysOfWeek()\n }\n\n _handleDaysOfWeek() {\n if (this.config.disableDaysOfWeek) {\n this.config.disableDaysOfWeek = this._validateDaysOfWeek(this.config.disableDaysOfWeek)\n this.config.disable = [...(this.config.disable || []), this._disable.bind(this)]\n }\n\n if (this.config.enableDaysOfWeek) {\n this.config.enableDaysOfWeek = this._validateDaysOfWeek(this.config.enableDaysOfWeek)\n this.config.enable = [...(this.config.enable || []), this._enable.bind(this)]\n }\n }\n\n _validateDaysOfWeek(days) {\n if (Array.isArray(days)) {\n return days.map((day) => parseInt(day))\n } else {\n console.error('days of week must be a valid array')\n return []\n }\n }\n\n _disable(date) {\n const disabledDays = this.config.disableDaysOfWeek\n return disabledDays.includes(date.getDay())\n }\n\n _enable(date) {\n const enabledDays = this.config.enableDaysOfWeek\n return enabledDays.includes(date.getDay())\n }\n\n _initializeDateFormats() {\n dateFormats.forEach((dateFormat) => {\n if (this.data.has(dateFormat)) {\n this.config[dateFormat] = convertDateFormat(this.data.get(dateFormat))\n }\n })\n }\n\n _initializeElements() {\n elements.forEach((element) => {\n this[`${element}Target`] = this.fp[element]\n })\n }\n\n _string(option) {\n return this.data.get(option)\n }\n\n _date(option) {\n return this.data.get(option)\n }\n\n _boolean(option) {\n return !(this.data.get(option) == '0' || this.data.get(option) == 'false')\n }\n\n _array(option) {\n return JSON.parse(this.data.get(option))\n }\n\n _number(option) {\n return parseInt(this.data.get(option))\n }\n\n _arrayOrString(option) {\n const val = this.data.get(option)\n try {\n return JSON.parse(val)\n } catch (e) {\n return val\n }\n }\n\n get flatpickrElement() {\n return (this.hasInstanceTarget && this.instanceTarget) || this.element\n }\n}\n\nexport default StimulusFlatpickr\n"],"names":["kebabCase","string","replace","toLowerCase","capitalize","charAt","toUpperCase","slice","booleanOptions","stringOptions","numberOptions","arrayOptions","arrayOrStringOptions","dateOptions","dateFormats","options","boolean","date","array","number","arrayOrString","events","elements","mapping","strftimeRegex","RegExp","Object","keys","join","convertDateFormat","format","match","StimulusFlatpickr","config","_initializeEvents","_initializeOptions","_initializeDateFormats","fp","flatpickr","flatpickrElement","_initializeElements","value","inputTarget","destroy","forEach","event","hook","bind","optionType","optionsCamelCase","option","optionKebab","data","has","_handleDaysOfWeek","disableDaysOfWeek","_validateDaysOfWeek","disable","_disable","enableDaysOfWeek","enable","_enable","days","Array","isArray","map","day","parseInt","console","error","disabledDays","includes","getDay","enabledDays","dateFormat","get","element","JSON","parse","val","e","hasInstanceTarget","instanceTarget","Controller"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAO,MAAMA,SAAS,GAAGC,MAAM,IAC7BA,MAAM,CACHC,OADH,CACW,iBADX,EAC8B,OAD9B,EAEGA,OAFH,CAEW,SAFX,EAEsB,GAFtB,EAGGC,WAHH,EADK;AAMA,MAAMC,UAAU,GAAGH,MAAM,IAAI;AAClC,SAAOA,MAAM,CAACI,MAAP,CAAc,CAAd,EAAiBC,WAAjB,KAAiCL,MAAM,CAACM,KAAP,CAAa,CAAb,CAAxC;AACD,CAFM;;ACNP,MAAMC,cAAc,GAAG,CACrB,YADqB,EAErB,UAFqB,EAGrB,SAHqB,EAIrB,YAJqB,EAKrB,eALqB,EAMrB,eANqB,EAOrB,eAPqB,EAQrB,YARqB,EASrB,QATqB,EAUrB,YAVqB,EAWrB,uBAXqB,EAYrB,QAZqB,EAarB,WAbqB,EAcrB,aAdqB,EAerB,MAfqB,CAAvB;AAkBA,MAAMC,aAAa,GAAG,CACpB,eADoB,EAEpB,aAFoB,EAGpB,MAHoB,EAIpB,WAJoB,EAKpB,UALoB,EAMpB,WANoB,EAOpB,mBAPoB,CAAtB;AAUA,MAAMC,aAAa,GAAG,C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6BAEQA,QAAQ;AACf,aAAO,EAAE,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,KAAyB,GAAzB,IAAgC,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,KAAyB,OAA3D,CAAP;AACD;;;2BAEMA,QAAQ;AACb,aAAO2B,IAAI,CAACC,KAAL,CAAW,KAAK1B,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAX,CAAP;AACD;;;4BAEOA,QAAQ;AACd,aAAOiB,QAAQ,CAAC,KAAKf,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAD,CAAf;AACD;;;mCAEcA,QAAQ;AACrB,YAAM6B,GAAG,GAAG,KAAK3B,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAZ;;AACA,UAAI;AACF,eAAO2B,IAAI,CAACC,KAAL,CAAWC,GAAX,CAAP;AACD,OAFD,CAEE,OAAOC,CAAP,EAAU;AACV,eAAOD,GAAP;AACD;AACF;;;qBAEsB;AACrB,aAAQ,KAAKE,iBAAL,IAA0B,KAAKC,cAAhC,IAAmD,KAAKN,OAA/D;AACD;;;;EA3H6BO;;gBAA1BnD,8BACa,CAAC,UAAD;;;;"} -------------------------------------------------------------------------------- /dist/index.umd.js: -------------------------------------------------------------------------------- 1 | (function (global, factory) { 2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('stimulus'), require('flatpickr')) : 3 | typeof define === 'function' && define.amd ? define(['stimulus', 'flatpickr'], factory) : 4 | (global = global || self, global.StimulusFlatpikcr = factory(global.Stimulus, global.Flatpickr)); 5 | }(this, (function (stimulus, flatpickr) { 'use strict'; 6 | 7 | flatpickr = flatpickr && Object.prototype.hasOwnProperty.call(flatpickr, 'default') ? flatpickr['default'] : flatpickr; 8 | 9 | function _classCallCheck(instance, Constructor) { 10 | if (!(instance instanceof Constructor)) { 11 | throw new TypeError("Cannot call a class as a function"); 12 | } 13 | } 14 | 15 | function _defineProperties(target, props) { 16 | for (var i = 0; i < props.length; i++) { 17 | var descriptor = props[i]; 18 | descriptor.enumerable = descriptor.enumerable || false; 19 | descriptor.configurable = true; 20 | if ("value" in descriptor) descriptor.writable = true; 21 | Object.defineProperty(target, descriptor.key, descriptor); 22 | } 23 | } 24 | 25 | function _createClass(Constructor, protoProps, staticProps) { 26 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); 27 | if (staticProps) _defineProperties(Constructor, staticProps); 28 | return Constructor; 29 | } 30 | 31 | function _defineProperty(obj, key, value) { 32 | if (key in obj) { 33 | Object.defineProperty(obj, key, { 34 | value: value, 35 | enumerable: true, 36 | configurable: true, 37 | writable: true 38 | }); 39 | } else { 40 | obj[key] = value; 41 | } 42 | 43 | return obj; 44 | } 45 | 46 | function ownKeys(object, enumerableOnly) { 47 | var keys = Object.keys(object); 48 | 49 | if (Object.getOwnPropertySymbols) { 50 | var symbols = Object.getOwnPropertySymbols(object); 51 | if (enumerableOnly) symbols = symbols.filter(function (sym) { 52 | return Object.getOwnPropertyDescriptor(object, sym).enumerable; 53 | }); 54 | keys.push.apply(keys, symbols); 55 | } 56 | 57 | return keys; 58 | } 59 | 60 | function _objectSpread2(target) { 61 | for (var i = 1; i < arguments.length; i++) { 62 | var source = arguments[i] != null ? arguments[i] : {}; 63 | 64 | if (i % 2) { 65 | ownKeys(Object(source), true).forEach(function (key) { 66 | _defineProperty(target, key, source[key]); 67 | }); 68 | } else if (Object.getOwnPropertyDescriptors) { 69 | Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); 70 | } else { 71 | ownKeys(Object(source)).forEach(function (key) { 72 | Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); 73 | }); 74 | } 75 | } 76 | 77 | return target; 78 | } 79 | 80 | function _inherits(subClass, superClass) { 81 | if (typeof superClass !== "function" && superClass !== null) { 82 | throw new TypeError("Super expression must either be null or a function"); 83 | } 84 | 85 | subClass.prototype = Object.create(superClass && superClass.prototype, { 86 | constructor: { 87 | value: subClass, 88 | writable: true, 89 | configurable: true 90 | } 91 | }); 92 | if (superClass) _setPrototypeOf(subClass, superClass); 93 | } 94 | 95 | function _getPrototypeOf(o) { 96 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { 97 | return o.__proto__ || Object.getPrototypeOf(o); 98 | }; 99 | return _getPrototypeOf(o); 100 | } 101 | 102 | function _setPrototypeOf(o, p) { 103 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { 104 | o.__proto__ = p; 105 | return o; 106 | }; 107 | 108 | return _setPrototypeOf(o, p); 109 | } 110 | 111 | function _isNativeReflectConstruct() { 112 | if (typeof Reflect === "undefined" || !Reflect.construct) return false; 113 | if (Reflect.construct.sham) return false; 114 | if (typeof Proxy === "function") return true; 115 | 116 | try { 117 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); 118 | return true; 119 | } catch (e) { 120 | return false; 121 | } 122 | } 123 | 124 | function _assertThisInitialized(self) { 125 | if (self === void 0) { 126 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); 127 | } 128 | 129 | return self; 130 | } 131 | 132 | function _possibleConstructorReturn(self, call) { 133 | if (call && (typeof call === "object" || typeof call === "function")) { 134 | return call; 135 | } 136 | 137 | return _assertThisInitialized(self); 138 | } 139 | 140 | function _createSuper(Derived) { 141 | return function () { 142 | var Super = _getPrototypeOf(Derived), 143 | result; 144 | 145 | if (_isNativeReflectConstruct()) { 146 | var NewTarget = _getPrototypeOf(this).constructor; 147 | 148 | result = Reflect.construct(Super, arguments, NewTarget); 149 | } else { 150 | result = Super.apply(this, arguments); 151 | } 152 | 153 | return _possibleConstructorReturn(this, result); 154 | }; 155 | } 156 | 157 | const kebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase(); 158 | const capitalize = string => { 159 | return string.charAt(0).toUpperCase() + string.slice(1); 160 | }; 161 | 162 | const booleanOptions = ['allowInput', 'altInput', 'animate', 'clickOpens', 'closeOnSelect', 'disableMobile', 'enableSeconds', 'enableTime', 'inline', 'noCalendar', 'shorthandCurrentMonth', 'static', 'time_24hr', 'weekNumbers', 'wrap']; 163 | const stringOptions = ['altInputClass', 'conjunction', 'mode', 'nextArrow', 'position', 'prevArrow', 'monthSelectorType']; 164 | const numberOptions = ['defaultHour', 'defaultMinute', 'defaultSeconds', 'hourIncrement', 'minuteIncrement', 'showMonths']; 165 | const arrayOptions = ['disable', 'enable', 'disableDaysOfWeek', 'enableDaysOfWeek']; 166 | const arrayOrStringOptions = ['defaultDate']; 167 | const dateOptions = ['maxDate', 'minDate', 'maxTime', 'minTime', 'now']; 168 | const dateFormats = ['altFormat', 'ariaDateFormat', 'dateFormat']; 169 | const options = { 170 | string: stringOptions, 171 | boolean: booleanOptions, 172 | date: dateOptions, 173 | array: arrayOptions, 174 | number: numberOptions, 175 | arrayOrString: arrayOrStringOptions 176 | }; 177 | 178 | const events = ['change', 'open', 'close', 'monthChange', 'yearChange', 'ready', 'valueUpdate', 'dayCreate']; 179 | 180 | const elements = ['calendarContainer', 'currentYearElement', 'days', 'daysContainer', 'input', 'nextMonthNav', 'monthNav', 'prevMonthNav', 'rContainer', 'selectedDateElem', 'todayDateElem', 'weekdayContainer']; 181 | 182 | const mapping = { 183 | '%Y': 'Y', 184 | '%y': 'y', 185 | '%C': 'Y', 186 | '%m': 'm', 187 | '%-m': 'n', 188 | '%_m': 'n', 189 | '%B': 'F', 190 | '%^B': 'F', 191 | '%b': 'M', 192 | '%^b': 'M', 193 | '%h': 'M', 194 | '%^h': 'M', 195 | '%d': 'd', 196 | '%-d': 'j', 197 | '%e': 'j', 198 | '%H': 'H', 199 | '%k': 'H', 200 | '%I': 'h', 201 | '%l': 'h', 202 | '%-l': 'h', 203 | '%P': 'K', 204 | '%p': 'K', 205 | '%M': 'i', 206 | '%S': 'S', 207 | '%A': 'l', 208 | '%a': 'D', 209 | '%w': 'w' 210 | }; 211 | const strftimeRegex = new RegExp(Object.keys(mapping).join('|').replace(new RegExp('\\^', 'g'), '\\^'), 'g'); 212 | const convertDateFormat = format => { 213 | return format.replace(strftimeRegex, match => { 214 | return mapping[match]; 215 | }); 216 | }; 217 | 218 | let StimulusFlatpickr = /*#__PURE__*/function (_Controller) { 219 | _inherits(StimulusFlatpickr, _Controller); 220 | 221 | var _super = _createSuper(StimulusFlatpickr); 222 | 223 | function StimulusFlatpickr() { 224 | _classCallCheck(this, StimulusFlatpickr); 225 | 226 | return _super.apply(this, arguments); 227 | } 228 | 229 | _createClass(StimulusFlatpickr, [{ 230 | key: "initialize", 231 | value: function initialize() { 232 | this.config = {}; 233 | } 234 | }, { 235 | key: "connect", 236 | value: function connect() { 237 | this._initializeEvents(); 238 | 239 | this._initializeOptions(); 240 | 241 | this._initializeDateFormats(); 242 | 243 | this.fp = flatpickr(this.flatpickrElement, _objectSpread2({}, this.config)); 244 | 245 | this._initializeElements(); 246 | } 247 | }, { 248 | key: "disconnect", 249 | value: function disconnect() { 250 | const value = this.inputTarget.value; 251 | this.fp.destroy(); 252 | this.inputTarget.value = value; 253 | } 254 | }, { 255 | key: "_initializeEvents", 256 | value: function _initializeEvents() { 257 | events.forEach(event => { 258 | if (this[event]) { 259 | const hook = `on${capitalize(event)}`; 260 | this.config[hook] = this[event].bind(this); 261 | } 262 | }); 263 | } 264 | }, { 265 | key: "_initializeOptions", 266 | value: function _initializeOptions() { 267 | Object.keys(options).forEach(optionType => { 268 | const optionsCamelCase = options[optionType]; 269 | optionsCamelCase.forEach(option => { 270 | const optionKebab = kebabCase(option); 271 | 272 | if (this.data.has(optionKebab)) { 273 | this.config[option] = this[`_${optionType}`](optionKebab); 274 | } 275 | }); 276 | }); 277 | 278 | this._handleDaysOfWeek(); 279 | } 280 | }, { 281 | key: "_handleDaysOfWeek", 282 | value: function _handleDaysOfWeek() { 283 | if (this.config.disableDaysOfWeek) { 284 | this.config.disableDaysOfWeek = this._validateDaysOfWeek(this.config.disableDaysOfWeek); 285 | this.config.disable = [...(this.config.disable || []), this._disable.bind(this)]; 286 | } 287 | 288 | if (this.config.enableDaysOfWeek) { 289 | this.config.enableDaysOfWeek = this._validateDaysOfWeek(this.config.enableDaysOfWeek); 290 | this.config.enable = [...(this.config.enable || []), this._enable.bind(this)]; 291 | } 292 | } 293 | }, { 294 | key: "_validateDaysOfWeek", 295 | value: function _validateDaysOfWeek(days) { 296 | if (Array.isArray(days)) { 297 | return days.map(day => parseInt(day)); 298 | } else { 299 | console.error('days of week must be a valid array'); 300 | return []; 301 | } 302 | } 303 | }, { 304 | key: "_disable", 305 | value: function _disable(date) { 306 | const disabledDays = this.config.disableDaysOfWeek; 307 | return disabledDays.includes(date.getDay()); 308 | } 309 | }, { 310 | key: "_enable", 311 | value: function _enable(date) { 312 | const enabledDays = this.config.enableDaysOfWeek; 313 | return enabledDays.includes(date.getDay()); 314 | } 315 | }, { 316 | key: "_initializeDateFormats", 317 | value: function _initializeDateFormats() { 318 | dateFormats.forEach(dateFormat => { 319 | if (this.data.has(dateFormat)) { 320 | this.config[dateFormat] = convertDateFormat(this.data.get(dateFormat)); 321 | } 322 | }); 323 | } 324 | }, { 325 | key: "_initializeElements", 326 | value: function _initializeElements() { 327 | elements.forEach(element => { 328 | this[`${element}Target`] = this.fp[element]; 329 | }); 330 | } 331 | }, { 332 | key: "_string", 333 | value: function _string(option) { 334 | return this.data.get(option); 335 | } 336 | }, { 337 | key: "_date", 338 | value: function _date(option) { 339 | return this.data.get(option); 340 | } 341 | }, { 342 | key: "_boolean", 343 | value: function _boolean(option) { 344 | return !(this.data.get(option) == '0' || this.data.get(option) == 'false'); 345 | } 346 | }, { 347 | key: "_array", 348 | value: function _array(option) { 349 | return JSON.parse(this.data.get(option)); 350 | } 351 | }, { 352 | key: "_number", 353 | value: function _number(option) { 354 | return parseInt(this.data.get(option)); 355 | } 356 | }, { 357 | key: "_arrayOrString", 358 | value: function _arrayOrString(option) { 359 | const val = this.data.get(option); 360 | 361 | try { 362 | return JSON.parse(val); 363 | } catch (e) { 364 | return val; 365 | } 366 | } 367 | }, { 368 | key: "flatpickrElement", 369 | get: function () { 370 | return this.hasInstanceTarget && this.instanceTarget || this.element; 371 | } 372 | }]); 373 | 374 | return StimulusFlatpickr; 375 | }(stimulus.Controller); 376 | 377 | _defineProperty(StimulusFlatpickr, "targets", ['instance']); 378 | 379 | return StimulusFlatpickr; 380 | 381 | }))); 382 | //# sourceMappingURL=index.umd.js.map 383 | -------------------------------------------------------------------------------- /dist/index.umd.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.umd.js","sources":["../src/utils.js","../src/config_options.js","../src/events.js","../src/elements.js","../src/strftime_mapping.js","../src/index.js"],"sourcesContent":["export const kebabCase = string =>\n string\n .replace(/([a-z])([A-Z])/g, \"$1-$2\")\n .replace(/[\\s_]+/g, \"-\")\n .toLowerCase();\n\nexport const capitalize = string => {\n return string.charAt(0).toUpperCase() + string.slice(1);\n};\n","const booleanOptions = [\n 'allowInput',\n 'altInput',\n 'animate',\n 'clickOpens',\n 'closeOnSelect',\n 'disableMobile',\n 'enableSeconds',\n 'enableTime',\n 'inline',\n 'noCalendar',\n 'shorthandCurrentMonth',\n 'static',\n 'time_24hr',\n 'weekNumbers',\n 'wrap'\n]\n\nconst stringOptions = [\n 'altInputClass',\n 'conjunction',\n 'mode',\n 'nextArrow',\n 'position',\n 'prevArrow',\n 'monthSelectorType'\n]\n\nconst numberOptions = [\n 'defaultHour',\n 'defaultMinute',\n 'defaultSeconds',\n 'hourIncrement',\n 'minuteIncrement',\n 'showMonths'\n]\n\nconst arrayOptions = ['disable', 'enable', 'disableDaysOfWeek', 'enableDaysOfWeek']\n\nconst arrayOrStringOptions = ['defaultDate']\n\nconst dateOptions = ['maxDate', 'minDate', 'maxTime', 'minTime', 'now']\n\nexport const dateFormats = ['altFormat', 'ariaDateFormat', 'dateFormat']\n\nexport const options = {\n string: stringOptions,\n boolean: booleanOptions,\n date: dateOptions,\n array: arrayOptions,\n number: numberOptions,\n arrayOrString: arrayOrStringOptions\n}\n","export const events = ['change', 'open', 'close', 'monthChange', 'yearChange', 'ready', 'valueUpdate', 'dayCreate']\n","export const elements = [\n 'calendarContainer',\n 'currentYearElement',\n 'days',\n 'daysContainer',\n 'input',\n 'nextMonthNav',\n 'monthNav',\n 'prevMonthNav',\n 'rContainer',\n 'selectedDateElem',\n 'todayDateElem',\n 'weekdayContainer'\n]\n","export const mapping = {\n '%Y': 'Y',\n '%y': 'y',\n '%C': 'Y',\n '%m': 'm',\n '%-m': 'n',\n '%_m': 'n',\n '%B': 'F',\n '%^B': 'F',\n '%b': 'M',\n '%^b': 'M',\n '%h': 'M',\n '%^h': 'M',\n '%d': 'd',\n '%-d': 'j',\n '%e': 'j',\n '%H': 'H',\n '%k': 'H',\n '%I': 'h',\n '%l': 'h',\n '%-l': 'h',\n '%P': 'K',\n '%p': 'K',\n '%M': 'i',\n '%S': 'S',\n '%A': 'l',\n '%a': 'D',\n '%w': 'w'\n}\n\nconst strftimeRegex = new RegExp(\n Object.keys(mapping)\n .join('|')\n .replace(new RegExp('\\\\^', 'g'), '\\\\^'),\n 'g'\n)\n\nexport const convertDateFormat = (format) => {\n return format.replace(strftimeRegex, (match) => {\n return mapping[match]\n })\n}\n","import { Controller } from 'stimulus'\nimport flatpickr from 'flatpickr'\nimport { kebabCase, capitalize } from './utils'\nimport { options, dateFormats } from './config_options'\nimport { events } from './events'\nimport { elements } from './elements'\nimport { convertDateFormat } from './strftime_mapping'\n\nclass StimulusFlatpickr extends Controller {\n static targets = ['instance']\n\n initialize() {\n this.config = {}\n }\n\n connect() {\n this._initializeEvents()\n this._initializeOptions()\n this._initializeDateFormats()\n\n this.fp = flatpickr(this.flatpickrElement, {\n ...this.config\n })\n\n this._initializeElements()\n }\n\n disconnect() {\n const value = this.inputTarget.value\n this.fp.destroy()\n this.inputTarget.value = value\n }\n\n _initializeEvents() {\n events.forEach((event) => {\n if (this[event]) {\n const hook = `on${capitalize(event)}`\n this.config[hook] = this[event].bind(this)\n }\n })\n }\n\n _initializeOptions() {\n Object.keys(options).forEach((optionType) => {\n const optionsCamelCase = options[optionType]\n optionsCamelCase.forEach((option) => {\n const optionKebab = kebabCase(option)\n if (this.data.has(optionKebab)) {\n this.config[option] = this[`_${optionType}`](optionKebab)\n }\n })\n })\n this._handleDaysOfWeek()\n }\n\n _handleDaysOfWeek() {\n if (this.config.disableDaysOfWeek) {\n this.config.disableDaysOfWeek = this._validateDaysOfWeek(this.config.disableDaysOfWeek)\n this.config.disable = [...(this.config.disable || []), this._disable.bind(this)]\n }\n\n if (this.config.enableDaysOfWeek) {\n this.config.enableDaysOfWeek = this._validateDaysOfWeek(this.config.enableDaysOfWeek)\n this.config.enable = [...(this.config.enable || []), this._enable.bind(this)]\n }\n }\n\n _validateDaysOfWeek(days) {\n if (Array.isArray(days)) {\n return days.map((day) => parseInt(day))\n } else {\n console.error('days of week must be a valid array')\n return []\n }\n }\n\n _disable(date) {\n const disabledDays = this.config.disableDaysOfWeek\n return disabledDays.includes(date.getDay())\n }\n\n _enable(date) {\n const enabledDays = this.config.enableDaysOfWeek\n return enabledDays.includes(date.getDay())\n }\n\n _initializeDateFormats() {\n dateFormats.forEach((dateFormat) => {\n if (this.data.has(dateFormat)) {\n this.config[dateFormat] = convertDateFormat(this.data.get(dateFormat))\n }\n })\n }\n\n _initializeElements() {\n elements.forEach((element) => {\n this[`${element}Target`] = this.fp[element]\n })\n }\n\n _string(option) {\n return this.data.get(option)\n }\n\n _date(option) {\n return this.data.get(option)\n }\n\n _boolean(option) {\n return !(this.data.get(option) == '0' || this.data.get(option) == 'false')\n }\n\n _array(option) {\n return JSON.parse(this.data.get(option))\n }\n\n _number(option) {\n return parseInt(this.data.get(option))\n }\n\n _arrayOrString(option) {\n const val = this.data.get(option)\n try {\n return JSON.parse(val)\n } catch (e) {\n return val\n }\n }\n\n get flatpickrElement() {\n return (this.hasInstanceTarget && this.instanceTarget) || this.element\n }\n}\n\nexport default StimulusFlatpickr\n"],"names":["kebabCase","string","replace","toLowerCase","capitalize","charAt","toUpperCase","slice","booleanOptions","stringOptions","numberOptions","arrayOptions","arrayOrStringOptions","dateOptions","dateFormats","options","boolean","date","array","number","arrayOrString","events","elements","mapping","strftimeRegex","RegExp","Object","keys","join","convertDateFormat","format","match","StimulusFlatpickr","config","_initializeEvents","_initializeOptions","_initializeDateFormats","fp","flatpickr","flatpickrElement","_initializeElements","value","inputTarget","destroy","forEach","event","hook","bind","optionType","optionsCamelCase","option","optionKebab","data","has","_handleDaysOfWeek","disableDaysOfWeek","_validateDaysOfWeek","disable","_disable","enableDaysOfWeek","enable","_enable","days","Array","isArray","map","day","parseInt","console","error","disabledDays","includes","getDay","enabledDays","dateFormat","get","element","JSON","parse","val","e","hasInstanceTarget","instanceTarget","Controller"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAAO,MAAMA,SAAS,GAAGC,MAAM,IAC7BA,MAAM,CACHC,OADH,CACW,iBADX,EAC8B,OAD9B,EAEGA,OAFH,CAEW,SAFX,EAEsB,GAFtB,EAGGC,WAHH,EADK;EAMA,MAAMC,UAAU,GAAGH,MAAM,IAAI;EAClC,SAAOA,MAAM,CAACI,MAAP,CAAc,CAAd,EAAiBC,WAAjB,KAAiCL,MAAM,CAACM,KAAP,CAAa,CAAb,CAAxC;EACD,CAFM;;ECNP,MAAMC,cAAc,GAAG,CACrB,YADqB,EAErB,UAFqB,EAGrB,SAHqB,EAIrB,YAJqB,EAKrB,eALqB,EAMrB,eANqB,EAOrB,eAPqB,EAQrB,YARqB,EASrB,QATqB,EAUrB,YAVqB,EAWrB,uBAXqB,EAYrB,QAZqB,EAarB,WAbqB,EAcrB,aAdqB,EAerB,MAfqB,CAAvB;EAkBA,MAAMC,aAAa,GAAG,CACpB,eADoB,EAEpB,aAFoB,EAGpB,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d,CAAP;EACD;;;4BAEKA,QAAQ;EACZ,aAAO,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAP;EACD;;;+BAEQA,QAAQ;EACf,aAAO,EAAE,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,KAAyB,GAAzB,IAAgC,KAAKE,IAAL,CAAUuB,GAAV,CAAczB,MAAd,KAAyB,OAA3D,CAAP;EACD;;;6BAEMA,QAAQ;EACb,aAAO2B,IAAI,CAACC,KAAL,CAAW,KAAK1B,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAX,CAAP;EACD;;;8BAEOA,QAAQ;EACd,aAAOiB,QAAQ,CAAC,KAAKf,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAD,CAAf;EACD;;;qCAEcA,QAAQ;EACrB,YAAM6B,GAAG,GAAG,KAAK3B,IAAL,CAAUuB,GAAV,CAAczB,MAAd,CAAZ;;EACA,UAAI;EACF,eAAO2B,IAAI,CAACC,KAAL,CAAWC,GAAX,CAAP;EACD,OAFD,CAEE,OAAOC,CAAP,EAAU;EACV,eAAOD,GAAP;EACD;EACF;;;uBAEsB;EACrB,aAAQ,KAAKE,iBAAL,IAA0B,KAAKC,cAAhC,IAAmD,KAAKN,OAA/D;EACD;;;;IA3H6BO;;kBAA1BnD,8BACa,CAAC,UAAD;;;;;;;;"} -------------------------------------------------------------------------------- /images/datetime-days-of-week.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrienpoly/stimulus-flatpickr/cf9c7e7f08043999c2a7ca62001a3f61be075ca2/images/datetime-days-of-week.png -------------------------------------------------------------------------------- /images/datetime-picker-black-fr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrienpoly/stimulus-flatpickr/cf9c7e7f08043999c2a7ca62001a3f61be075ca2/images/datetime-picker-black-fr.png -------------------------------------------------------------------------------- /images/datetime-picker-formats.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrienpoly/stimulus-flatpickr/cf9c7e7f08043999c2a7ca62001a3f61be075ca2/images/datetime-picker-formats.png -------------------------------------------------------------------------------- /images/datetime-picker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adrienpoly/stimulus-flatpickr/cf9c7e7f08043999c2a7ca62001a3f61be075ca2/images/datetime-picker.png -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration 2 | // Generated on Mon Sep 03 2018 21:41:15 GMT+0200 (CEST) 3 | 4 | module.exports = function(config) { 5 | config.set({ 6 | // base path that will be used to resolve all patterns (eg. files, exclude) 7 | basePath: '', 8 | 9 | // frameworks to use 10 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 11 | frameworks: ['mocha', 'sinon-chai', 'fixture'], 12 | 13 | // list of files / patterns to load in the browser 14 | files: [ 15 | 'spec/*.js', 16 | 'spec/fixtures/*.html', 17 | { 18 | pattern: '**/*.js.map', 19 | included: false 20 | } 21 | ], 22 | 23 | // list of files / patterns to exclude 24 | exclude: [], 25 | 26 | // preprocess matching files before serving them to the browser 27 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 28 | preprocessors: { 29 | 'spec/**/*.js': ['webpack', 'sourcemap'], 30 | 'src/**/*.js': ['webpack', 'sourcemap'], 31 | 'spec/fixtures/*.html': ['html2js'] 32 | }, 33 | 34 | webpack: { 35 | mode: 'development', 36 | module: { 37 | rules: [ 38 | { 39 | test: /\.js$/, 40 | exclude: [/node_modules/], 41 | use: ['babel-loader'] 42 | }, 43 | { 44 | test: /\.css$/, 45 | use: ['style-loader', 'css-loader'] 46 | } 47 | ] 48 | } 49 | }, 50 | 51 | client: { 52 | captureConsole: true, 53 | chai: { 54 | includeStack: true 55 | }, 56 | clearContext: false 57 | }, 58 | 59 | // test results reporter to use 60 | // possible values: 'dots', 'progress' 61 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter 62 | reporters: ['mocha', 'coverage'], 63 | 64 | coverageReporter: { 65 | reporters: [ 66 | { 67 | type: 'lcov' 68 | }, 69 | { 70 | type: 'text-summary' 71 | }, 72 | { 73 | type: 'text' 74 | } 75 | ] 76 | }, 77 | // web server port 78 | port: 9876, 79 | 80 | // enable / disable colors in the output (reporters and logs) 81 | colors: true, 82 | 83 | // level of logging 84 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 85 | logLevel: config.LOG_INFO, 86 | 87 | // enable / disable watching file and executing tests whenever any file changes 88 | autoWatch: true, 89 | 90 | // start these browsers 91 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 92 | browsers: ['ChromeHeadless'], 93 | // browsers: ['Chrome'], 94 | 95 | // Continuous Integration mode 96 | // if true, Karma captures browsers, runs the tests and exits 97 | singleRun: true, 98 | 99 | // Concurrency level 100 | // how many browser should be started simultaneous 101 | concurrency: 5 102 | }) 103 | } 104 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stimulus-flatpickr", 3 | "version": "1.4.0", 4 | "description": "A Stimulus Wrapper for Flatpickr library", 5 | "keywords": [ 6 | "stimulus", 7 | "stimulusjs", 8 | "flatpickr" 9 | ], 10 | "main": "dist/index.js", 11 | "umd:main": "dist/index.umd.js", 12 | "module": "dist/index.m.js", 13 | "source": "src/index.js", 14 | "amdName": "StimulusFlatpickr", 15 | "author": "@adrienpoly", 16 | "license": "MIT", 17 | "external": "stimulus, flatpickr", 18 | "scripts": { 19 | "start:playground": "webpack-dev-server --mode development --open --config playground/webpack.config.js --hot", 20 | "build": "rollup -c && yarn test", 21 | "dev": "rollup -wc", 22 | "test": "NODE_ENV=test karma start", 23 | "test:watch": "yarn test --auto-watch --no-single-run", 24 | "test:headless": "yarn test --auto-watch --no-single-run --browsers ChromeHeadless", 25 | "codecov": "codecov", 26 | "release": "np" 27 | }, 28 | "peerDependencies": { 29 | "flatpickr": ">=4.6.2", 30 | "stimulus": ">=1.1.1" 31 | }, 32 | "devDependencies": { 33 | "@babel/core": "^7.6.0", 34 | "@babel/plugin-proposal-class-properties": "^7.3.4", 35 | "@babel/plugin-proposal-object-rest-spread": "^7.5.5", 36 | "@babel/plugin-transform-classes": "^7.3.4", 37 | "@babel/plugin-transform-spread": "^7.2.2", 38 | "@babel/preset-env": "^7.6.0", 39 | "babel-eslint": "^10.0.3", 40 | "babel-loader": "^8.0.5", 41 | "babel-plugin-istanbul": "^5.1.1", 42 | "chai": "^4.2.0", 43 | "chai-dom": "^1.8.1", 44 | "codecov": "^3.1.0", 45 | "css-loader": "^1.0.0", 46 | "eslint": "^6.3.0", 47 | "eslint-plugin-import": "^2.18.2", 48 | "eslint-plugin-mocha": "^6.1.0", 49 | "eslint-plugin-node": "^10.0.0", 50 | "eslint-plugin-promise": "^4.2.1", 51 | "eslint-plugin-standard": "^4.0.1", 52 | "flatpickr": "^4.6.3", 53 | "karma": "^4.0.1", 54 | "karma-chai": "^0.1.0", 55 | "karma-chrome-launcher": "^2.2.0", 56 | "karma-coverage": "^2.0.1", 57 | "karma-fixture": "^0.2.6", 58 | "karma-html2js-preprocessor": "^1.1.0", 59 | "karma-mocha": "^1.3.0", 60 | "karma-mocha-reporter": "^2.2.5", 61 | "karma-rollup-preprocessor": "^7.0.2", 62 | "karma-sinon-chai": "^2.0.2", 63 | "karma-sourcemap-loader": "^0.3.7", 64 | "karma-webpack": "^4.0.2", 65 | "minimist": "^1.2.5", 66 | "mocha": "^5.2.0", 67 | "np": "^5.1.3", 68 | "npm-run-all": "^4.1.3", 69 | "rollup": "^1.20.3", 70 | "rollup-plugin-babel": "^4.3.2", 71 | "rollup-plugin-filesize": "^6.0.1", 72 | "rollup-plugin-node-resolve": "^5.2.0", 73 | "sinon": "^6.1.4", 74 | "sinon-chai": "^3.2.0", 75 | "size-plugin": "^1.0.1", 76 | "stimulus": "^1.1.1", 77 | "style-loader": "^0.23.0", 78 | "webpack": "^4.39.3", 79 | "webpack-cli": "^3.2.3", 80 | "webpack-dev-middleware": "^3.6.1", 81 | "webpack-dev-server": "^3.2.1" 82 | }, 83 | "repository": { 84 | "type": "git", 85 | "url": "https://github.com/adrienpoly/stimulus-flatpickr.git" 86 | }, 87 | "resolutions": { 88 | "minimist": "^1.2.5" 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /playground/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |44 | <input 45 | type="text" 46 | placeholder="Select Date.." 47 | data-controller="flatpickr" 48 | data-flatpickr-date-format="Y-m-d" 49 | data-flatpickr-alt-format="F j, Y" 50 | data-flatpickr-alt-input="true" 51 | /> 52 |53 | 54 |
59 | <input 60 | type="text" 61 | placeholder="Select Date.." 62 | data-controller="flatpickr" 63 | data-flatpickr-min-date="2020-01" 64 | /> 65 |66 | 67 |
if you need to wrap custom elements inside the controller you can use two solutions:
69 |77 | <div data-controller="flatpickr" data-flatpickr-wrap="true"> 78 | <input type="text" placeholder="Select Date.." data-input /> 79 | <button title="toggle" data-toggle>open</button> 80 | <button title="clear" data-clear>clear</button> 81 | </div> 82 |83 | 84 |
90 | <div data-controller="flatpickr"> 91 | <input type="text" placeholder="Select Date.." data-target="flatpickr.instance" /> 92 | </div> 93 |94 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
15 | Modest yet powerful wrapper of Flatpickr for Stimulus 16 | Only ~1kb 17 |
18 | 19 |48 | 👇👇👇👇👇👇 49 |
50 |
51 |
52 |
137 | 👇👇👇👇👇👇 138 |
139 |
140 |
141 |
259 | 👇👇👇👇👇👇 260 |
261 |
262 |
263 |
286 | 👇👇👇👇👇👇 287 |
288 |
289 |
290 |
303 | Code 304 | | 305 |306 | Result 307 | | 308 |
---|---|
310 |
311 | <%= form_with model: Appointement.new, authenticity_token: true do |f| %>
312 | <%= f.text_field :start_time,
313 | data: {
314 | controller: "flatpickr",
315 | flatpickr_disable_days_of_week: [5,6], #disables saturdays and sundays
316 | flatpickr_disable: ["2018-09-25", "2018-09-26"] #disables individual dates
317 | } %>
318 | <% end %>
319 |
320 | |
321 |
322 |
323 | |
326 |