-
Notifications
You must be signed in to change notification settings - Fork 29
/
stimulus-flatpickr.js
273 lines (230 loc) 路 6.09 KB
/
stimulus-flatpickr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var stimulus = require('stimulus');
var flatpickr = _interopDefault(require('flatpickr'));
var kebabCase = function (string) { return string
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_]+/g, "-")
.toLowerCase(); };
var capitalize = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
var booleanOptions = [
"allowInput",
"altInput",
"animate",
"clickOpens",
"closeOnSelect",
"disableMobile",
"enableSeconds",
"enableTime",
"inline",
"noCalendar",
"shorthandCurrentMonth",
"static",
"time_24hr",
"weekNumbers",
"wrap"
];
var stringOptions = [
"altInputClass",
"conjunction",
"defaultDate",
"mode",
"nextArrow",
"position",
"prevArrow"
];
var numberOptions = [
"defaultHour",
"defaultMinute",
"defaultSeconds",
"hourIncrement",
"minuteIncrement",
"showMonths"
];
var arrayOptions = [
"disable",
"enable",
"disableDaysOfWeek",
"enableDaysOfWeek"
];
var dateOptions = ["maxDate", "minDate", "maxTime", "minTime", "now"];
var dateFormats = ["altFormat", "ariaDateFormat", "dateFormat"];
var options = {
string: stringOptions,
boolean: booleanOptions,
date: dateOptions,
array: arrayOptions,
number: numberOptions
};
var events = [
"change",
"open",
"close",
"monthChange",
"yearChange",
"ready",
"valueUpdate",
"dayCreate"
];
var elements = [
"calendarContainer",
"currentYearElement",
"days",
"daysContainer",
"input",
"nextMonthNav",
"monthNav",
"prevMonthNav",
"rContainer",
"selectedDateElem",
"todayDateElem",
"weekdayContainer"
];
var mapping = {
"%Y": "Y",
"%y": "y",
"%C": "Y",
"%m": "m",
"%-m": "n",
"%_m": "n",
"%B": "F",
"%^B": "F",
"%b": "M",
"%^b": "M",
"%h": "M",
"%^h": "M",
"%d": "d",
"%-d": "j",
"%e": "j",
"%H": "H",
"%k": "H",
"%I": "h",
"%l": "h",
"%P": "K",
"%p": "K",
"%M": "i",
"%S": "S",
"%A": "l",
"%a": "D",
"%w": "w"
};
var strftimeRegex = new RegExp(
Object.keys(mapping)
.join("|")
.replace(new RegExp("\\^", "g"), "\\^"),
"g"
);
var convertDateFormat = function (format) {
return format.replace(strftimeRegex, function (match) {
return mapping[match];
});
};
var Flatpickr = (function (Controller) {
function Flatpickr () {
Controller.apply(this, arguments);
}
if ( Controller ) Flatpickr.__proto__ = Controller;
Flatpickr.prototype = Object.create( Controller && Controller.prototype );
Flatpickr.prototype.constructor = Flatpickr;
Flatpickr.prototype.initialize = function initialize () {
this.config = {};
};
Flatpickr.prototype.connect = function connect () {
this._initializeEvents();
this._initializeOptions();
this._initializeDateFormats();
this.fp = flatpickr(this.element, Object.assign({}, this.config));
this._initializeElements();
};
Flatpickr.prototype.disconnect = function disconnect () {
this.fp.destroy();
};
Flatpickr.prototype._initializeEvents = function _initializeEvents () {
var this$1 = this;
events.forEach(function (event) {
if (this$1[event]) {
var hook = "on" + (capitalize(event));
this$1.config[hook] = this$1[event].bind(this$1);
}
});
};
Flatpickr.prototype._initializeOptions = function _initializeOptions () {
var this$1 = this;
Object.keys(options).forEach(function (optionType) {
var optionsCamelCase = options[optionType];
optionsCamelCase.forEach(function (option) {
var optionKebab = kebabCase(option);
if (this$1.data.has(optionKebab)) {
this$1.config[option] = this$1[("_" + optionType)](optionKebab);
}
});
});
this._handleDaysOfWeek();
};
Flatpickr.prototype._handleDaysOfWeek = function _handleDaysOfWeek () {
if (this.config.disableDaysOfWeek) {
this.config.disableDaysOfWeek = this._validateDaysOfWeek(
this.config.disableDaysOfWeek
);
this.config.disable = (this.config.disable || []).concat( [this._disable.bind(this)]
);
}
if (this.config.enableDaysOfWeek) {
this.config.enableDaysOfWeek = this._validateDaysOfWeek(
this.config.enableDaysOfWeek
);
this.config.enable = (this.config.enable || []).concat( [this._enable.bind(this)]
);
}
};
Flatpickr.prototype._validateDaysOfWeek = function _validateDaysOfWeek (days) {
if (Array.isArray(days)) {
return days.map(function (day) { return parseInt(day); });
} else {
console.error("days of week must be a valid array");
return [];
}
};
Flatpickr.prototype._disable = function _disable (date) {
var disabledDays = this.config.disableDaysOfWeek;
return disabledDays.includes(date.getDay());
};
Flatpickr.prototype._enable = function _enable (date) {
var enabledDays = this.config.enableDaysOfWeek;
return enabledDays.includes(date.getDay());
};
Flatpickr.prototype._initializeDateFormats = function _initializeDateFormats () {
var this$1 = this;
dateFormats.forEach(function (dateFormat) {
if (this$1.data.has(dateFormat)) {
this$1.config[dateFormat] = convertDateFormat(this$1.data.get(dateFormat));
}
});
};
Flatpickr.prototype._initializeElements = function _initializeElements () {
var this$1 = this;
elements.forEach(function (element) {
this$1[(element + "Target")] = this$1.fp[element];
});
};
Flatpickr.prototype._string = function _string (option) {
return this.data.get(option);
};
Flatpickr.prototype._date = function _date (option) {
return this.data.get(option);
};
Flatpickr.prototype._boolean = function _boolean (option) {
return this.data.get(option) === "true";
};
Flatpickr.prototype._array = function _array (option) {
return JSON.parse(this.data.get(option));
};
Flatpickr.prototype._number = function _number (option) {
return parseInt(this.data.get(option));
};
return Flatpickr;
}(stimulus.Controller));
module.exports = Flatpickr;
//# sourceMappingURL=stimulus-flatpickr.js.map