/
jquery.datepicker.js
543 lines (414 loc) · 16.3 KB
/
jquery.datepicker.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
(function ($, window) {
/**
* Global date picker component.
* Renders a calender dialog to select a single date, multiple dates or a date range.
* Is used on an input element which holds the value of the picker.
* By default there will be generated a display value field and a hidden input field.
* The display value field holds the alternate date format for better usability.
*/
$.plugin('swDatePicker', {
defaults: {
/**
* Modes:
* single - A single date selection
* multiple - Select multiple dates in one picker
* range - Select a date range in one picker
*/
mode: 'single',
/**
* If true, dates will be parsed, formatted, and displayed in UTC.
* Pre loading date strings with timezones is recommended but not necessary.
*/
utc: false,
/**
* Wrap: see https://chmln.github.io/flatpickr/options/
*/
wrap: false,
/**
* Position the calendar inside the wrapper and next to the input element.
*/
'static': false,
/**
* Enables week numbers
*/
weekNumbers: false,
/**
* Allow manual datetime input
*/
allowInput: false,
/**
* Clicking on input opens the date picker.
* Disable if you wish to open the calendar manually with the open() method.
*/
clickOpens: true,
/**
* Display time picker in 24 hour mode
*/
time_24hr: true,
/**
* Enables the time picker functionality
*/
enableTime: false,
/**
* Set to true to hide the calendar.
* Use for a time picker along with enableTime.
*/
noCalendar: false,
/**
* More date format chars at https://chmln.github.io/flatpickr/#dateformat
*/
dateFormat: 'Y-m-d',
/**
* The date format for the time.
* Is added to dateFormat when enableTime option is set to true.
*/
timeFormat: ' H:i:S',
/**
* Hides the original input and creates a new one for a different display value.
*/
altInput: true,
/**
* The name attribute of an additional input field for storing the single start value of a range.
* Only working with mode "range".
*/
rangeStartInput: null,
/**
* The name attribute of an additional input field for storing the single end value of a range.
* Only working with mode "range".
*/
rangeEndInput: null,
/**
* The created altInput element will have this class.
*/
altInputClass: 'flatpickr-input form-control input',
/**
* Used as the displayed value when altInput is set to true.
*/
altFormat: 'F j, Y',
/**
* Used as the displayed value when altInput is set to true.
*/
altTimeFormat: ' - H:i',
/**
* Define the symbol which is used to separate multiple dates.
* Only necessary for mode "multiple".
* The default separator of the flatpickr is ";".
*/
multiDateSeparator: null,
/**
* Either a date string or a date object.
* Used for initial value.
*/
defaultDate: null,
/**
* The minimum date that user can pick (inclusive).
*/
minDate: null,
/**
* The maximum date that user can pick (inclusive).
*/
maxDate: null,
/**
* Define an array of dates which can be selected.
* You can also pass a coma separated list via data attribute.
* All other dates are disabled.
*/
enabledDates: null,
/**
* Date parser that transforms a given string to a date object.
*/
parseDate: null,
/**
* Submit the parent form of the date picker input on date change.
*/
autoSubmit: false
},
init: function (el, options) {
var me = this,
globalConfig = window.datePickerGlobalConfig || {};
/**
* The defaults are additionally set by global configs including localization.
*/
me.opts = $.extend({}, me.defaults, globalConfig, options);
me.applyDataAttributes(true);
/**
* Holds the suspend events status.
*/
me.suspended = false;
/**
* Fix for the flatpickr plugin to handle datetime formatting correctly.
*/
if (me.opts.enableTime) {
me.opts.dateFormat = me.opts.dateFormat + me.opts.timeFormat;
me.opts.altFormat = me.opts.altFormat + me.opts.altTimeFormat;
}
/**
* On range mode the min and max values can be stored separately in additional hidden inputs.
*/
if (me.opts.mode === 'range' && me.opts.rangeStartInput !== null) {
me.$rangeStartInput = $('[name="' + me.opts.rangeStartInput + '"]');
}
if (me.opts.mode === 'range' && me.opts.rangeEndInput !== null) {
me.$rangeEndInput = $('[name="' + me.opts.rangeEndInput + '"]');
}
me.initFlatpickr();
me.registerEvents();
$.publish('plugin/swDatePicker/onInit', [ me ]);
},
/**
* Prepares the config for the flatpickr plugin and initializes it.
*/
initFlatpickr: function () {
var me = this,
config = $.extend({}, me.opts);
/**
* Convert the initial value to flatpickr friendly format if custom separator is used.
*/
if (me.opts.mode === 'multiple' && me.opts.multiDateSeparator !== null) {
me.$el.val(me.convertMultiSeparatorToFlatpickr(me.$el.val()));
}
/**
* Prepares the enabled dates.
* You can also pass a coma separated list via data attribute.
*/
if (me.opts.enabledDates !== null) {
if (typeof me.opts.enabledDates === 'string') {
me.opts.enabledDates = me.opts.enabledDates.split(',');
}
config['enable'] = me.opts.enabledDates;
}
/**
* Event handler api of the flatpickr plugin.
*/
config['onReady'] = $.proxy(me.onPickerReady, me);
config['onChange'] = $.proxy(me.onPickerChange, me);
config['onOpen'] = $.proxy(me.onPickerOpen, me);
config['onClose'] = $.proxy(me.onPickerClose, me);
me.flatpickr = me.$el.flatpickr(config);
/**
* Convert value to custom separator after flatpickr was integrated.
*/
if (me.opts.mode === 'multiple' && me.opts.multiDateSeparator !== null) {
me.$el.val(me.convertMultiSeparator(me.$el.val()));
}
/**
* Set the flatpickr range value from the separate min and max inputs.
*/
if (me.opts.mode === 'range') {
me.setDatePickerValFromInputs();
me.setStartInputVal();
me.setEndInputVal();
}
$.publish('plugin/swDatePicker/onInitFlatpickr', [ me, me.flatpickr, config ]);
},
registerEvents: function () {
var me = this;
me._on(me.$el, 'clear', $.proxy(me.onInputClear, me));
me._on(me.$el, 'change', $.proxy(me.onInputChange, me));
if (me.$rangeStartInput) {
me._on(me.$rangeStartInput, 'clear', $.proxy(me.onRangeInputClear, me, me.$rangeStartInput));
me._on(me.$rangeStartInput, 'change', $.proxy(me.onInputChange, me));
}
if (me.$rangeEndInput) {
me._on(me.$rangeEndInput, 'clear', $.proxy(me.onRangeInputClear, me, me.$rangeEndInput));
me._on(me.$rangeEndInput, 'change', $.proxy(me.onInputChange, me));
}
$.subscribe(me.getEventName('plugin/swOffcanvasMenu/onCloseMenu'), $.proxy(me.close, me));
$.subscribe(me.getEventName('plugin/swOffcanvasMenu/onBeforeOpenMenu'), $.proxy(me.close, me));
$.publish('plugin/swDatePicker/onRegisterEvents', [ me ]);
},
open: function () {
var me = this;
me.flatpickr.open();
},
close: function () {
var me = this;
me.flatpickr.close();
},
onInputClear: function () {
var me = this;
me.flatpickr.clear();
$.publish('plugin/swDatePicker/onInputClear', [ me ]);
},
/**
* Prevents change events on the inputs when events are suspended.
*/
onInputChange: function (event) {
var me = this;
if (me.suspended) {
event.preventDefault();
event.stopPropagation();
return false;
}
$.publish('plugin/swDatePicker/onInputChange', [ me ]);
},
/**
* Clears the separate range input fields and resets the picker values.
* Especially used via the clear event by the filter facets.
*/
onRangeInputClear: function ($input) {
var me = this;
$input.val('');
me.$el.trigger('change');
me.setDatePickerValFromInputs();
$.publish('plugin/swDatePicker/onRangeInputClear', [ me ]);
},
onPickerReady: function () {
var me = this;
$.publish('plugin/swDatePicker/onPickerReady', [ me ]);
},
onPickerChange: function () {
var me = this;
if (me.opts.mode === 'range') {
me.setStartInputVal();
me.setEndInputVal();
}
/**
* Convert value to custom separator.
*/
if (me.opts.mode === 'multiple' && me.opts.multiDateSeparator !== null) {
me.$el.val(me.convertMultiSeparator(me.$el.val()));
}
$.publish('plugin/swDatePicker/onPickerChange', [ me ]);
},
onPickerOpen: function () {
var me = this;
me.currentValue = me.$el.val();
$.publish('plugin/swDatePicker/onPickerOpen', [ me ]);
},
onPickerClose: function () {
var me = this;
/**
* Submits the parent form when the autoSubmit option is set.
*/
if (me.opts.autoSubmit && me.currentValue !== me.$el.val()) {
me.$el.parents('form').submit();
}
$.publish('plugin/swDatePicker/onPickerClose', [ me ]);
},
setStartInputVal: function (value) {
var me = this;
if (me.$rangeStartInput) {
var val = value || me.flatpickr.selectedDates[0] || '',
altVal = val;
if (val && val !== '') {
val = me.formatDate(val);
altVal = me.formatDate(altVal, me.opts.altFormat);
}
me.$rangeStartInput.val(val);
/**
* Stores the visual display value in an additional data attribute.
*/
if (me.opts.altFormat) {
me.$rangeStartInput.attr('data-display-value', altVal);
}
}
$.publish('plugin/swDatePicker/onSetStartInputVal', [ me ]);
},
setEndInputVal: function (value) {
var me = this;
if (me.$rangeEndInput) {
var val = value || me.flatpickr.selectedDates[1] || '',
altVal = val;
if (val && val !== '') {
val = me.formatDate(val);
altVal = me.formatDate(altVal, me.opts.altFormat);
}
me.$rangeEndInput.val(val);
/**
* Stores the visual display value in an additional data attribute.
*/
if (me.opts.altFormat) {
me.$rangeEndInput.attr('data-display-value', altVal);
}
}
$.publish('plugin/swDatePicker/onSetEndInputVal', [ me ]);
},
setDatePickerValFromInputs: function () {
var me = this,
values = [];
if (me.$rangeStartInput && me.$rangeStartInput.val().length > 0) {
values.push(me.$rangeStartInput.val());
}
if (me.$rangeEndInput && me.$rangeEndInput.val().length > 0) {
values.push(me.$rangeEndInput.val());
}
me.flatpickr.setDate(values);
$.publish('plugin/swDatePicker/onSetDatePickerValFromInputs', [ me ]);
},
getRangeStartValue: function () {
var me = this;
if (!me.$rangeStartInput) {
return null;
}
$.publish('plugin/swDatePicker/onGetRangeStartValue', [ me ]);
return me.$rangeStartInput.val();
},
getRangeEndValue: function () {
var me = this;
if (!me.$rangeEndInput) {
return null;
}
$.publish('plugin/swDatePicker/onGetRangeEndValue', [ me ]);
return me.$rangeEndInput.val();
},
/**
* Suspend change events from firing on all picker input fields.
*/
suspendEvents: function () {
var me = this;
me.suspended = true;
$.publish('plugin/swDatePicker/onSuspendEvents', [ me ]);
},
/**
* Resume change events firing on all picker input fields.
*/
resumeEvents: function () {
var me = this;
me.suspended = false;
$.publish('plugin/swDatePicker/onResumeEvents', [ me ]);
},
/**
* Converts the submit value format from flatpickr with multi selection to system friendly format.
*/
convertMultiSeparator: function (value) {
var me = this;
if (me.opts.multiDateSeparator === null) {
return value;
}
var convertValue = value.split('; ').join(me.opts.multiDateSeparator);
$.publish('plugin/swDatePicker/onConvertMultiSeparator', [ me, convertValue ]);
return convertValue;
},
/**
* Converts the submit value with multi selection back to flatpickr friendly format.
*/
convertMultiSeparatorToFlatpickr: function (value) {
var me = this;
if (me.opts.multiDateSeparator === null) {
return value;
}
var convertValue = value.split(me.opts.multiDateSeparator).join('; ');
$.publish('plugin/swDatePicker/onConvertMultiSeparatorToFlatpickr', [ me, convertValue ]);
return convertValue;
},
formatDate: function (date, dateFormat) {
var me = this;
if (!date) {
return false;
}
dateFormat = dateFormat || me.opts.dateFormat;
var formattedDate = me.flatpickr.formatDate(dateFormat, date);
$.publish('plugin/swDatePicker/onFormatDate', [ me, formattedDate, dateFormat, date ]);
return formattedDate;
},
destroy: function () {
var me = this;
$.unsubscribe(me.getEventName('plugin/swOffcanvasMenu/onCloseMenu'));
$.unsubscribe(me.getEventName('plugin/swOffcanvasMenu/onBeforeOpenMenu'));
me.flatpickr.destroy();
me._destroy();
$.publish('plugin/swDatePicker/onDestroy', [ me ]);
}
});
})(jQuery, window);