forked from kbwood/datepick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.datepick.js
More file actions
2259 lines (2176 loc) · 92 KB
/
jquery.datepick.js
File metadata and controls
2259 lines (2176 loc) · 92 KB
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* http://keith-wood.name/datepick.html
Date picker for jQuery v5.0.0.
Written by Keith Wood (kbwood{at}iinet.com.au) February 2010.
Licensed under the MIT (https://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt) licence.
Please attribute the author if you use it. */
(function($) { // Hide scope, no $ conflict
var pluginName = 'datepick';
/** Create the datepicker plugin.
<p>Sets an input field to popup a calendar for date entry,
or a <code>div</code> or <code>span</code> to show an inline calendar.</p>
<p>Expects HTML like:</p>
<pre><input type="text"> or <div></div></pre>
<p>Provide inline configuration like:</p>
<pre><input type="text" data-datepick="name: 'value'"/></pre>
@module Datepick
@augments JQPlugin
@example $(selector).datepick()
$(selector).datepick({minDate: 0, maxDate: '+1m +1w'}) */
$.JQPlugin.createPlugin({
/** The name of the plugin. */
name: pluginName,
/** Default template for generating a datepicker.
Insert anywhere: '{l10n:name}' to insert localised value for name,
'{link:name}' to insert a link trigger for command name,
'{button:name}' to insert a button trigger for command name,
'{popup:start}...{popup:end}' to mark a section for inclusion in a popup datepicker only,
'{inline:start}...{inline:end}' to mark a section for inclusion in an inline datepicker only.
firstDayOfMonth: 1,
@property picker {string} Overall structure: '{months}' to insert calendar months.
@property monthRow {string} One row of months: '{months}' to insert calendar months.
@property month {string} A single month: '{monthHeader<em>:dateFormat</em>}' to insert the month header -
<em>dateFormat</em> is optional and defaults to 'MM yyyy',
'{weekHeader}' to insert a week header, '{weeks}' to insert the month's weeks.
@property weekHeader {string} A week header: '{days}' to insert individual day names.
@property dayHeader {string} Individual day header: '{day}' to insert day name.
@property week {string} One week of the month: '{days}' to insert the week's days,
'{weekOfYear}' to insert week of year.
@property day {string} An individual day: '{day}' to insert day value.
@property monthSelector {string} jQuery selector, relative to picker, for a single month.
@property daySelector {string} jQuery selector, relative to picker, for individual days.
@property rtlClass {string} Class for right-to-left (RTL) languages.
@property multiClass {string} Class for multi-month datepickers.
@property defaultClass {string} Class for selectable dates.
@property selectedClass {string} Class for currently selected dates.
@property highlightedClass {string} Class for highlighted dates.
@property todayClass {string} Class for today.
@property otherMonthClass {string} Class for days from other months.
@property weekendClass {string} Class for days on weekends.
@property commandClass {string} Class prefix for commands.
@property commandButtonClass {string} Extra class(es) for commands that are buttons.
@property commandLinkClass {string} Extra class(es) for commands that are links.
@property disabledClass {string} Class for disabled commands. */
defaultRenderer: {
picker: '<div class="datepick">' +
'<div class="datepick-nav">{link:prev}{link:today}{link:next}</div>{months}' +
'{popup:start}<div class="datepick-ctrl">{link:clear}{link:close}</div>{popup:end}' +
'<div class="datepick-clear-fix"></div></div>',
monthRow: '<div class="datepick-month-row">{months}</div>',
month: '<div class="datepick-month"><div class="datepick-month-header">{monthHeader}</div>' +
'<table><thead>{weekHeader}</thead><tbody>{weeks}</tbody></table></div>',
weekHeader: '<tr>{days}</tr>',
dayHeader: '<th>{day}</th>',
week: '<tr>{days}</tr>',
day: '<td>{day}</td>',
monthSelector: '.datepick-month',
daySelector: 'td',
rtlClass: 'datepick-rtl',
multiClass: 'datepick-multi',
defaultClass: '',
selectedClass: 'datepick-selected',
highlightedClass: 'datepick-highlight',
todayClass: 'datepick-today',
otherMonthClass: 'datepick-other-month',
weekendClass: 'datepick-weekend',
commandClass: 'datepick-cmd',
commandButtonClass: '',
commandLinkClass: '',
disabledClass: 'datepick-disabled'
},
/** Command actions that may be added to a layout by name.
<ul>
<li>prev - Show the previous month (based on <code>monthsToStep</code> option) - <em>PageUp</em></li>
<li>prevJump - Show the previous year (based on <code>monthsToJump</code> option) - <em>Ctrl+PageUp</em></li>
<li>next - Show the next month (based on <code>monthsToStep</code> option) - <em>PageDown</em></li>
<li>nextJump - Show the next year (based on <code>monthsToJump</code> option) - <em>Ctrl+PageDown</em></li>
<li>current - Show the currently selected month or today's if none selected - <em>Ctrl+Home</em></li>
<li>today - Show today's month - <em>Ctrl+Home</em></li>
<li>clear - Erase the date and close the datepicker popup - <em>Ctrl+End</em></li>
<li>close - Close the datepicker popup - <em>Esc</em></li>
<li>prevWeek - Move the cursor to the previous week - <em>Ctrl+Up</em></li>
<li>prevDay - Move the cursor to the previous day - <em>Ctrl+Left</em></li>
<li>nextDay - Move the cursor to the next day - <em>Ctrl+Right</em></li>
<li>nextWeek - Move the cursor to the next week - <em>Ctrl+Down</em></li>
</ul>
The command name is the key name and is used to add the command to a layout
with '{button:name}' or '{link:name}'. Each has the following attributes.
@property text {string} The field in the regional settings for the displayed text.
@property status {string} The field in the regional settings for the status text.
@property keystroke {object} The keystroke to trigger the action, with attributes:
<code>keyCode</code> {number} the code for the keystroke,
<code>ctrlKey</code> {boolean} <code>true</code> if <em>Ctrl</em> is required,
<code>altKey</code> {boolean} <code>true</code> if <em>Alt</em> is required,
<code>shiftKey</code> {boolean} <code>true</code> if <em>Shift</em> is required.
@property enabled {DatepickCommandEnabled} The function that indicates the command is enabled.
@property date {DatepickCommandDate} The function to get the date associated with this action.
@property action {DatepickCommandAction} The function that implements the action. */
commands: {
prev: {text: 'prevText', status: 'prevStatus', // Previous month
keystroke: {keyCode: 33}, // Page up
enabled: function(inst) {
var minDate = inst.curMinDate();
return (!minDate || plugin.add(plugin.day(
plugin._applyMonthsOffset(plugin.add(plugin.newDate(inst.drawDate),
1 - inst.options.monthsToStep, 'm'), inst), 1), -1, 'd').
getTime() >= minDate.getTime()); },
date: function(inst) {
return plugin.day(plugin._applyMonthsOffset(plugin.add(
plugin.newDate(inst.drawDate), -inst.options.monthsToStep, 'm'), inst), 1); },
action: function(inst) {
plugin.changeMonth(this, -inst.options.monthsToStep); }
},
prevJump: {text: 'prevJumpText', status: 'prevJumpStatus', // Previous year
keystroke: {keyCode: 33, ctrlKey: true}, // Ctrl + Page up
enabled: function(inst) {
var minDate = inst.curMinDate();
return (!minDate || plugin.add(plugin.day(
plugin._applyMonthsOffset(plugin.add(plugin.newDate(inst.drawDate),
1 - inst.options.monthsToJump, 'm'), inst), 1), -1, 'd').
getTime() >= minDate.getTime()); },
date: function(inst) {
return plugin.day(plugin._applyMonthsOffset(plugin.add(
plugin.newDate(inst.drawDate), -inst.options.monthsToJump, 'm'), inst), 1); },
action: function(inst) {
plugin.changeMonth(this, -inst.options.monthsToJump); }
},
next: {text: 'nextText', status: 'nextStatus', // Next month
keystroke: {keyCode: 34}, // Page down
enabled: function(inst) {
var maxDate = inst.get('maxDate');
return (!maxDate || plugin.day(plugin._applyMonthsOffset(plugin.add(
plugin.newDate(inst.drawDate), inst.options.monthsToStep, 'm'), inst), 1).
getTime() <= maxDate.getTime()); },
date: function(inst) {
return plugin.day(plugin._applyMonthsOffset(plugin.add(
plugin.newDate(inst.drawDate), inst.options.monthsToStep, 'm'), inst), 1); },
action: function(inst) {
plugin.changeMonth(this, inst.options.monthsToStep); }
},
nextJump: {text: 'nextJumpText', status: 'nextJumpStatus', // Next year
keystroke: {keyCode: 34, ctrlKey: true}, // Ctrl + Page down
enabled: function(inst) {
var maxDate = inst.get('maxDate');
return (!maxDate || plugin.day(plugin._applyMonthsOffset(plugin.add(
plugin.newDate(inst.drawDate), inst.options.monthsToJump, 'm'), inst), 1).
getTime() <= maxDate.getTime()); },
date: function(inst) {
return plugin.day(plugin._applyMonthsOffset(plugin.add(
plugin.newDate(inst.drawDate), inst.options.monthsToJump, 'm'), inst), 1); },
action: function(inst) {
plugin.changeMonth(this, inst.options.monthsToJump); }
},
current: {text: 'currentText', status: 'currentStatus', // Current month
keystroke: {keyCode: 36, ctrlKey: true}, // Ctrl + Home
enabled: function(inst) {
var minDate = inst.curMinDate();
var maxDate = inst.get('maxDate');
var curDate = inst.selectedDates[0] || plugin.today();
return (!minDate || curDate.getTime() >= minDate.getTime()) &&
(!maxDate || curDate.getTime() <= maxDate.getTime()); },
date: function(inst) {
return inst.selectedDates[0] || plugin.today(); },
action: function(inst) {
var curDate = inst.selectedDates[0] || plugin.today();
plugin.showMonth(this, curDate.getFullYear(), curDate.getMonth() + 1); }
},
today: {text: 'todayText', status: 'todayStatus', // Today's month
keystroke: {keyCode: 36, ctrlKey: true}, // Ctrl + Home
enabled: function(inst) {
var minDate = inst.curMinDate();
var maxDate = inst.get('maxDate');
return (!minDate || plugin.today().getTime() >= minDate.getTime()) &&
(!maxDate || plugin.today().getTime() <= maxDate.getTime()); },
date: function(inst) { return plugin.today(); },
action: function(inst) { plugin.showMonth(this); }
},
clear: {text: 'clearText', status: 'clearStatus', // Clear the datepicker
keystroke: {keyCode: 35, ctrlKey: true}, // Ctrl + End
enabled: function(inst) { return true; },
date: function(inst) { return null; },
action: function(inst) { plugin.clear(this); }
},
close: {text: 'closeText', status: 'closeStatus', // Close the datepicker
keystroke: {keyCode: 27}, // Escape
enabled: function(inst) { return true; },
date: function(inst) { return null; },
action: function(inst) { plugin.hide(this); }
},
prevWeek: {text: 'prevWeekText', status: 'prevWeekStatus', // Previous week
keystroke: {keyCode: 38, ctrlKey: true}, // Ctrl + Up
enabled: function(inst) {
var minDate = inst.curMinDate();
return (!minDate || plugin.add(plugin.newDate(inst.drawDate), -7, 'd').
getTime() >= minDate.getTime()); },
date: function(inst) { return plugin.add(plugin.newDate(inst.drawDate), -7, 'd'); },
action: function(inst) { plugin.changeDay(this, -7); }
},
prevDay: {text: 'prevDayText', status: 'prevDayStatus', // Previous day
keystroke: {keyCode: 37, ctrlKey: true}, // Ctrl + Left
enabled: function(inst) {
var minDate = inst.curMinDate();
return (!minDate || plugin.add(plugin.newDate(inst.drawDate), -1, 'd').
getTime() >= minDate.getTime()); },
date: function(inst) { return plugin.add(plugin.newDate(inst.drawDate), -1, 'd'); },
action: function(inst) { plugin.changeDay(this, -1); }
},
nextDay: {text: 'nextDayText', status: 'nextDayStatus', // Next day
keystroke: {keyCode: 39, ctrlKey: true}, // Ctrl + Right
enabled: function(inst) {
var maxDate = inst.get('maxDate');
return (!maxDate || plugin.add(plugin.newDate(inst.drawDate), 1, 'd').
getTime() <= maxDate.getTime()); },
date: function(inst) { return plugin.add(plugin.newDate(inst.drawDate), 1, 'd'); },
action: function(inst) { plugin.changeDay(this, 1); }
},
nextWeek: {text: 'nextWeekText', status: 'nextWeekStatus', // Next week
keystroke: {keyCode: 40, ctrlKey: true}, // Ctrl + Down
enabled: function(inst) {
var maxDate = inst.get('maxDate');
return (!maxDate || plugin.add(plugin.newDate(inst.drawDate), 7, 'd').
getTime() <= maxDate.getTime()); },
date: function(inst) { return plugin.add(plugin.newDate(inst.drawDate), 7, 'd'); },
action: function(inst) { plugin.changeDay(this, 7); }
}
},
/** Determine whether a command is enabled.
@callback DatepickCommandEnabled
@param inst {object} The current instance settings.
@return {boolean} <code>true</code> if this command is enabled, <code>false</code> if not.
@example enabled: function(inst) {
return !!inst.curMinDate();
} */
/** Calculate the representative date for a command.
@callback DatepickCommandDate
@param inst {object} The current instance settings.
@return {Date} A date appropriate for this command.
@example date: function(inst) {
return inst.curMinDate();
} */
/** Perform the action for a command.
@callback DatepickCommandAction
@param inst {object} The current instance settings.
@example date: function(inst) {
$.datepick.setDate(inst.elem, inst.curMinDate());
} */
/** Calculate the week of the year for a date.
@callback DatepickCalculateWeek
@param date {Date} The date to evaluate.
@return {number} The week of the year.
@example calculateWeek: function(date) {
return Math.floor(($.datepick.dayOfYear(date) - 1) / 7) + 1;
} */
/** Provide information about an individual date shown in the calendar.
@callback DatepickOnDate
@param date {Date} The date to evaluate.
@return {object} Information about that date, with the properties above.
@property selectable {boolean} <code>true</code> if this date can be selected.
@property dateClass {string} Class(es) to be applied to the date.
@property content {string} The date cell content.
@property tooltip {string} A popup tooltip for the date.
@example onDate: function(date) {
return {selectable: date.getDay() > 0 && date.getDay() < 5,
dateClass: date.getDay() == 4 ? 'last-day' : ''};
} */
/** Update the datepicker display.
@callback DatepickOnShow
@param picker {jQuery} The datepicker <code>div</code> to be shown.
@param inst {object} The current instance settings.
@example onShow: function(picker, inst) {
picker.append('<button type="button">Hi</button>').
find('button:last').click(function() {
alert('Hi!');
});
} */
/** React to navigating through the months/years.
@callback DatepickOnChangeMonthYear
@param year {number} The new year.
@param month {number} The new month (1 to 12).
@example onChangeMonthYear: function(year, month) {
alert('Now in ' + month + '/' + year);
} */
/** Datepicker on select callback.
Triggered when a date is selected.
@callback DatepickOnSelect
@param dates {Date[]} The selected date(s).
@example onSelect: function(dates) {
alert('Selected ' + dates);
} */
/** Datepicker on close callback.
Triggered when a popup calendar is closed.
@callback DatepickOnClose
@param dates {Date[]} The selected date(s).
@example onClose: function(dates) {
alert('Selected ' + dates);
} */
/** Default settings for the plugin.
@property [pickerClass=''] {string} CSS class to add to this instance of the datepicker.
@property [showOnFocus=true] {boolean} <code>true</code> for popup on focus, <code>false</code> for not.
@property [showTrigger=null] {string|Element|jQuery} Element to be cloned for a trigger, <code>null</code> for none.
@property [showAnim='show'] {string} Name of jQuery animation for popup, '' for no animation.
@property [showOptions=null] {object} Options for enhanced animations.
@property [showSpeed='normal'] {string} Duration of display/closure.
@property [popupContainer=null] {string|Element|jQuery} The element to which a popup calendar is added, <code>null</code> for body.
@property [alignment='bottom'] {string} Alignment of popup - with nominated corner of input:
'top' or 'bottom' aligns depending on language direction,
'topLeft', 'topRight', 'bottomLeft', 'bottomRight'.
@property [fixedWeeks=false] {boolean} <code>true</code> to always show 6 weeks, <code>false</code> to only show as many as are needed.
@property [firstDay=0] {number} First day of the week, 0 = Sunday, 1 = Monday, etc.
@property [calculateWeek=this.iso8601Week] {DatepickCalculateWeek} Calculate week of the year from a date, <code>null</code> for ISO8601.
@property [monthsToShow=1] {number|number[]} How many months to show, cols or [rows, cols].
@property [monthsOffset=0] {number} How many months to offset the primary month by;
may be a function that takes the date and returns the offset.
@property [monthsToStep=1] {number} How many months to move when prev/next clicked.
@property [monthsToJump=12] {number} How many months to move when large prev/next clicked.
@property [useMouseWheel=true] {boolean} <code>true</code> to use mousewheel if available, <code>false</code> to never use it.
@property [changeMonth=true] {boolean} <code>true</code> to change month/year via drop-down, <code>false</code> for navigation only.
@property [yearRange='c-10:c+10'] {string} Range of years to show in drop-down: 'any' for direct text entry
or 'start:end', where start/end are '+-nn' for relative to today
or 'c+-nn' for relative to the currently selected date
or 'nnnn' for an absolute year.
@property [shortYearCutoff='+10'] {string} Cutoff for two-digit year in the current century.
@property [showOtherMonths=false] {boolean} <code>true</code> to show dates from other months, <code>false</code> to not show them.
@property [selectOtherMonths=false] {boolean} <code>true</code> to allow selection of dates from other months too.
@property [defaultDate=null] {string|number|Date} Date to show if no other selected.
@property [selectDefaultDate=false] {boolean} <code>true</code> to pre-select the default date if no other is chosen.
@property [minDate=null] {string|number|Date} The minimum selectable date.
@property [maxDate=null] {string|number|Date} The maximum selectable date.
@property [dateFormat='mm/dd/yyyy'] {string} Format for dates.
@property [autoSize=false] {boolean} <code>true</code> to size the input field according to the date format.
@property [rangeSelect=false] {boolean} Allows for selecting a date range on one date picker.
@property [rangeSeparator=' - '] {string} Text between two dates in a range.
@property [multiSelect=0] {number} Maximum number of selectable dates, zero for single select.
@property [multiSeparator=','] {string} Text between multiple dates.
@property [onDate=null] {DatepickOnDate} Callback as a date is added to the datepicker.
@property [onShow=null] {DatepickOnShow} Callback just before a datepicker is shown.
@property [onChangeMonthYear=null] {DatepickOnChangeMonthYear} Callback when a new month/year is selected.
@property [onSelect=null] {DatepickOnSelect} Callback when a date is selected.
@property [onClose=null] {DatepickOnClose} Callback when a datepicker is closed.
@property [altField=null] {string|Element|jQuery} Alternate field to update in synch with the datepicker.
@property [altFormat=null] {string} Date format for alternate field, defaults to <code>dateFormat</code>.
@property [constrainInput=true] {boolean} <code>true</code> to constrain typed input to <code>dateFormat</code> allowed characters.
@property [commandsAsDateFormat=false] {boolean} <code>true</code> to apply
<code><a href="#formatDate">formatDate</a></code> to the command texts.
@property [commands=this.commands] {object} Command actions that may be added to a layout by name. */
defaultOptions: {
pickerClass: '',
showOnFocus: true,
showTrigger: null,
showAnim: 'show',
showOptions: {},
showSpeed: 'normal',
popupContainer: null,
alignment: 'bottom',
fixedWeeks: false,
firstDay: 0,
calculateWeek: null, // this.iso8601Week,
monthsToShow: 1,
monthsOffset: 0,
monthsToStep: 1,
monthsToJump: 12,
useMouseWheel: true,
changeMonth: true,
yearRange: 'c-10:c+10',
shortYearCutoff: '+10',
showOtherMonths: false,
selectOtherMonths: false,
defaultDate: null,
selectDefaultDate: false,
minDate: null,
maxDate: null,
dateFormat: 'mm/dd/yyyy',
autoSize: false,
rangeSelect: false,
rangeSeparator: ' - ',
multiSelect: 0,
multiSeparator: ',',
onDate: null,
onShow: null,
onChangeMonthYear: null,
onSelect: null,
onClose: null,
altField: null,
altFormat: null,
constrainInput: true,
commandsAsDateFormat: false,
commands: {} // this.commands
},
/** Localisations for the plugin.
Entries are objects indexed by the language code ('' being the default US/English).
Each object has the following attributes.
@property [monthNames=['January','February','March','April','May','June','July','August','September','October','November','December']]
The long names of the months.
@property [monthNamesShort=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']]
The short names of the months.
@property [dayNames=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']]
The long names of the days of the week.
@property [dayNamesShort=['Sun','Mon','Tue','Wed','Thu','Fri','Sat']] The short names of the days of the week.
@property [dayNamesMin=['Su','Mo','Tu','We','Th','Fr','Sa']] The minimal names of the days of the week.
@property [dateFormat='mm/dd/yyyy'] {string} See options on <code><a href="#formatDate">formatDate</a></code>.
@property [firstDay=0] {number} The first day of the week, Sun = 0, Mon = 1, etc.
@property [renderer=this.defaultRenderer] {string} The rendering templates.
@property [prevText='<Prev'] {string} Text for the previous month command.
@property [prevStatus='Show the previous month'] {string} Status text for the previous month command.
@property [prevJumpText='<<'] {string} Text for the previous year command.
@property [prevJumpStatus='Show the previous year'] {string} Status text for the previous year command.
@property [nextText='Next>'] {string} Text for the next month command.
@property [nextStatus='Show the next month'] {string} Status text for the next month command.
@property [nextJumpText='>>'] {string} Text for the next year command.
@property [nextJumpStatus='Show the next year'] {string} Status text for the next year command.
@property [currentText='Current'] {string} Text for the current month command.
@property [currentStatus='Show the current month'] {string} Status text for the current month command.
@property [todayText='Today'] {string} Text for the today's month command.
@property [todayStatus='Show today\'s month'] {string} Status text for the today's month command.
@property [clearText='Clear'] {string} Text for the clear command.
@property [clearStatus='Clear all the dates'] {string} Status text for the clear command.
@property [closeText='Close'] {string} Text for the close command.
@property [closeStatus='Close the datepicker'] {string} Status text for the close command.
@property [yearStatus='Change the year'] {string} Status text for year selection.
@property [monthStatus='Change the month'] {string} Status text for month selection.
@property [weekText='Wk'] {string} Text for week of the year column header.
@property [weekStatus='Week of the year'] {string} Status text for week of the year column header.
@property [dayStatus='Select DD, M d, yyyy'] {string} Status text for selectable days.
@property [defaultStatus='Select a date'] {string} Status text shown by default.
@property [isRTL=false] {boolean} <code>true</code> if language is right-to-left. */
regionalOptions: { // Available regional settings, indexed by language/country code
'': { // Default regional settings - English/US
monthNames: ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
dateFormat: 'mm/dd/yyyy',
firstDay: 0,
renderer: {}, // this.defaultRenderer
prevText: '<Prev',
prevStatus: 'Show the previous month',
prevJumpText: '<<',
prevJumpStatus: 'Show the previous year',
nextText: 'Next>',
nextStatus: 'Show the next month',
nextJumpText: '>>',
nextJumpStatus: 'Show the next year',
currentText: 'Current',
currentStatus: 'Show the current month',
todayText: 'Today',
todayStatus: 'Show today\'s month',
clearText: 'Clear',
clearStatus: 'Clear all the dates',
closeText: 'Close',
closeStatus: 'Close the datepicker',
yearStatus: 'Change the year',
monthStatus: 'Change the month',
weekText: 'Wk',
weekStatus: 'Week of the year',
dayStatus: 'Select DD, M d, yyyy',
defaultStatus: 'Select a date',
isRTL: false
}
},
/** Names of getter methods - those that can't be chained. */
_getters: ['getDate', 'isDisabled', 'isSelectable', 'retrieveDate'],
_disabled: [],
_popupClass: pluginName + '-popup', // Marker for popup division
_triggerClass: pluginName + '-trigger', // Marker for trigger element
_disableClass: pluginName + '-disable', // Marker for disabled element
_monthYearClass: pluginName + '-month-year', // Marker for month/year inputs
_curMonthClass: pluginName + '-month-', // Marker for current month/year
_anyYearClass: pluginName + '-any-year', // Marker for year direct input
_curDoWClass: pluginName + '-dow-', // Marker for day of week
_ticksTo1970: (((1970 - 1) * 365 + Math.floor(1970 / 4) - Math.floor(1970 / 100) +
Math.floor(1970 / 400)) * 24 * 60 * 60 * 10000000),
_msPerDay: 24 * 60 * 60 * 1000,
/** The date format for use with Atom (RFC 3339/ISO 8601). */
ATOM: 'yyyy-mm-dd',
/** The date format for use with cookies. */
COOKIE: 'D, dd M yyyy',
/** The date format for full display. */
FULL: 'DD, MM d, yyyy',
/** The date format for use with ISO 8601. */
ISO_8601: 'yyyy-mm-dd',
/** The date format for Julian dates. */
JULIAN: 'J',
/** The date format for use with RFC 822. */
RFC_822: 'D, d M yy',
/** The date format for use with RFC 850. */
RFC_850: 'DD, dd-M-yy',
/** The date format for use with RFC 1036. */
RFC_1036: 'D, d M yy',
/** The date format for use with RFC 1123. */
RFC_1123: 'D, d M yyyy',
/** The date format for use with RFC 2822. */
RFC_2822: 'D, d M yyyy',
/** The date format for use with RSS (RFC 822). */
RSS: 'D, d M yy',
/** The date format for Windows ticks. */
TICKS: '!',
/** The date format for Unix timestamp. */
TIMESTAMP: '@',
/** The date format for use with W3C (ISO 8601). */
W3C: 'yyyy-mm-dd',
/** Format a date object into a string value.
The format can be combinations of the following:
<ul>
<li>d - day of month (no leading zero)</li>
<li>dd - day of month (two digit)</li>
<li>o - day of year (no leading zeros)</li>
<li>oo - day of year (three digit)</li>
<li>D - day name short</li>
<li>DD - day name long</li>
<li>w - week of year (no leading zero)</li>
<li>ww - week of year (two digit)</li>
<li>m - month of year (no leading zero)</li>
<li>mm - month of year (two digit)</li>
<li>M - month name short</li>
<li>MM - month name long</li>
<li>yy - year (two digit)</li>
<li>yyyy - year (four digit)</li>
<li>@ - Unix timestamp (s since 01/01/1970)</li>
<li>! - Windows ticks (100ns since 01/01/0001)</li>
<li>'...' - literal text</li>
<li>'' - single quote</li>
</ul>
@param [format=dateFormat] {string} The desired format of the date.
@param date {Date} The date value to format.
@param [settings] {object} With the properties shown below.
@property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday.
@property [dayNames] {string[]} Names of the days from Sunday.
@property [monthNamesShort] {string[]} Abbreviated names of the months.
@property [monthNames] {string[]} Names of the months.
@property [calculateWeek] {DatepickCalculateWeek} Function that determines week of the year.
@return {string} The date in the above format.
@example var display = $.datepick.formatDate('yyyy-mm-dd', new Date(2014, 12-1, 25)) */
formatDate: function(format, date, settings) {
if (typeof format !== 'string') {
settings = date;
date = format;
format = '';
}
if (!date) {
return '';
}
format = format || this.defaultOptions.dateFormat;
settings = settings || {};
var dayNamesShort = settings.dayNamesShort || this.defaultOptions.dayNamesShort;
var dayNames = settings.dayNames || this.defaultOptions.dayNames;
var monthNamesShort = settings.monthNamesShort || this.defaultOptions.monthNamesShort;
var monthNames = settings.monthNames || this.defaultOptions.monthNames;
var calculateWeek = settings.calculateWeek || this.defaultOptions.calculateWeek;
// Check whether a format character is doubled
var doubled = function(match, step) {
var matches = 1;
while (iFormat + matches < format.length && format.charAt(iFormat + matches) === match) {
matches++;
}
iFormat += matches - 1;
return Math.floor(matches / (step || 1)) > 1;
};
// Format a number, with leading zeroes if necessary
var formatNumber = function(match, value, len, step) {
var num = '' + value;
if (doubled(match, step)) {
while (num.length < len) {
num = '0' + num;
}
}
return num;
};
// Format a name, short or long as requested
var formatName = function(match, value, shortNames, longNames) {
return (doubled(match) ? longNames[value] : shortNames[value]);
};
var output = '';
var literal = false;
for (var iFormat = 0; iFormat < format.length; iFormat++) {
if (literal) {
if (format.charAt(iFormat) === "'" && !doubled("'")) {
literal = false;
}
else {
output += format.charAt(iFormat);
}
}
else {
switch (format.charAt(iFormat)) {
case 'd': output += formatNumber('d', date.getDate(), 2); break;
case 'D': output += formatName('D', date.getDay(),
dayNamesShort, dayNames); break;
case 'o': output += formatNumber('o', this.dayOfYear(date), 3); break;
case 'w': output += formatNumber('w', calculateWeek(date), 2); break;
case 'm': output += formatNumber('m', date.getMonth() + 1, 2); break;
case 'M': output += formatName('M', date.getMonth(),
monthNamesShort, monthNames); break;
case 'y':
output += (doubled('y', 2) ? date.getFullYear() :
(date.getFullYear() % 100 < 10 ? '0' : '') + date.getFullYear() % 100);
break;
case '@': output += Math.floor(date.getTime() / 1000); break;
case '!': output += date.getTime() * 10000 + this._ticksTo1970; break;
case "'":
if (doubled("'")) {
output += "'";
}
else {
literal = true;
}
break;
default:
output += format.charAt(iFormat);
}
}
}
return output;
},
/** Parse a string value into a date object.
See <code><a href="#formatDate">formatDate</a></code> for the possible formats, plus:
<ul>
<li>* - ignore rest of string</li>
</ul>
@param format {string} The expected format of the date ('' for default datepicker format).
@param value {string} The date in the above format.
@param [settings] {object} With the properties shown above.
@property [shortYearCutoff] {number} the cutoff year for determining the century.
@property [dayNamesShort] {string[]} abbreviated names of the days from Sunday.
@property [dayNames] {string[]} names of the days from Sunday.
@property [monthNamesShort] {string[]} abbreviated names of the months.
@property [monthNames] {string[]} names of the months.
@return {Date} The extracted date value or <code>null</code> if value is blank.
@throws Errors if the format and/or value are missing, if the value doesn't match the format,
or if the date is invalid.
@example var date = $.datepick.parseDate('dd/mm/yyyy', '25/12/2014') */
parseDate: function(format, value, settings) {
if (value == null) {
throw 'Invalid arguments';
}
value = (typeof value === 'object' ? value.toString() : value + '');
if (value === '') {
return null;
}
format = format || this.defaultOptions.dateFormat;
settings = settings || {};
var shortYearCutoff = settings.shortYearCutoff || this.defaultOptions.shortYearCutoff;
shortYearCutoff = (typeof shortYearCutoff !== 'string' ? shortYearCutoff :
this.today().getFullYear() % 100 + parseInt(shortYearCutoff, 10));
var dayNamesShort = settings.dayNamesShort || this.defaultOptions.dayNamesShort;
var dayNames = settings.dayNames || this.defaultOptions.dayNames;
var monthNamesShort = settings.monthNamesShort || this.defaultOptions.monthNamesShort;
var monthNames = settings.monthNames || this.defaultOptions.monthNames;
var year = -1;
var month = -1;
var day = -1;
var doy = -1;
var shortYear = false;
var literal = false;
// Check whether a format character is doubled
var doubled = function(match, step) {
var matches = 1;
while (iFormat + matches < format.length && format.charAt(iFormat + matches) === match) {
matches++;
}
iFormat += matches - 1;
return Math.floor(matches / (step || 1)) > 1;
};
// Extract a number from the string value
var getNumber = function(match, step) {
var isDoubled = doubled(match, step);
var size = [2, 3, isDoubled ? 4 : 2, 11, 20]['oy@!'.indexOf(match) + 1];
var digits = new RegExp('^-?\\d{1,' + size + '}');
var num = value.substring(iValue).match(digits);
if (!num) {
throw 'Missing number at position {0}'.replace(/\{0\}/, iValue);
}
iValue += num[0].length;
return parseInt(num[0], 10);
};
// Extract a name from the string value and convert to an index
var getName = function(match, shortNames, longNames, step) {
var names = (doubled(match, step) ? longNames : shortNames);
for (var i = 0; i < names.length; i++) {
if (value.substr(iValue, names[i].length).toLowerCase() === names[i].toLowerCase()) {
iValue += names[i].length;
return i + 1;
}
}
throw 'Unknown name at position {0}'.replace(/\{0\}/, iValue);
};
// Confirm that a literal character matches the string value
var checkLiteral = function() {
if (value.charAt(iValue) !== format.charAt(iFormat)) {
throw 'Unexpected literal at position {0}'.replace(/\{0\}/, iValue);
}
iValue++;
};
var iValue = 0;
for (var iFormat = 0; iFormat < format.length; iFormat++) {
if (literal) {
if (format.charAt(iFormat) === "'" && !doubled("'")) {
literal = false;
}
else {
checkLiteral();
}
}
else {
switch (format.charAt(iFormat)) {
case 'd': day = getNumber('d'); break;
case 'D': getName('D', dayNamesShort, dayNames); break;
case 'o': doy = getNumber('o'); break;
case 'w': getNumber('w'); break;
case 'm': month = getNumber('m'); break;
case 'M': month = getName('M', monthNamesShort, monthNames); break;
case 'y':
var iSave = iFormat;
shortYear = !doubled('y', 2);
iFormat = iSave;
year = getNumber('y', 2);
break;
case '@':
var date = this._normaliseDate(new Date(getNumber('@') * 1000));
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
break;
case '!':
var date = this._normaliseDate(
new Date((getNumber('!') - this._ticksTo1970) / 10000));
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
break;
case '*': iValue = value.length; break;
case "'":
if (doubled("'")) {
checkLiteral();
}
else {
literal = true;
}
break;
default: checkLiteral();
}
}
}
if (iValue < value.length) {
throw 'Additional text found at end';
}
if (year === -1) {
year = this.today().getFullYear();
}
else if (year < 100 && shortYear) {
year += (shortYearCutoff === -1 ? 1900 : this.today().getFullYear() -
this.today().getFullYear() % 100 - (year <= shortYearCutoff ? 0 : 100));
}
if (doy > -1) {
month = 1;
day = doy;
for (var dim = this.daysInMonth(year, month); day > dim;
dim = this.daysInMonth(year, month)) {
month++;
day -= dim;
}
}
var date = this.newDate(year, month, day);
if (date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day) {
throw 'Invalid date';
}
return date;
},
/** A date may be specified as an exact value or a relative one.
@param dateSpec {Date|number|string} The date as an object or string
in the given format or an offset - numeric days from today,
or string amounts and periods, e.g. '+1m +2w'.
@param defaultDate {Date} The date to use if no other supplied, may be <code>null</code>.
@param [currentDate] {Date} The current date as a possible basis for relative dates,
if <code>null</code> today is used.
@param dateFormat {string} The expected date format - see <code><a href="#formatDate">formatDate</a></code>.
@param settings {object} With the properties shown above.
@property [shortYearCutoff] {number} The cutoff year for determining the century.
@property [dayNamesShort] {string[]} Abbreviated names of the days from Sunday.
@property [dayNames] {string[]} Names of the days from Sunday.
@property [monthNamesShort] {string[]} Abbreviated names of the months.
@property [monthNames] {string[]} Names of the months.
@return {Date} The decoded date.
@example $.datepick.determineDate('+1m +2w', new Date()) */
determineDate: function(dateSpec, defaultDate, currentDate, dateFormat, settings) {
if (currentDate && typeof currentDate !== 'object') {
settings = dateFormat;
dateFormat = currentDate;
currentDate = null;
}
if (typeof dateFormat !== 'string') {
settings = dateFormat;
dateFormat = '';
}
var offsetString = function(offset) {
try {
return plugin.parseDate(dateFormat, offset, settings);
}
catch (e) {
// Ignore
}
offset = offset.toLowerCase();
var date = (offset.match(/^c/) && currentDate ? plugin.newDate(currentDate) : null) ||
plugin.today();
var pattern = /([+-]?[0-9]+)\s*(d|w|m|y)?/g;
var matches = null;
while (matches = pattern.exec(offset)) {
date = plugin.add(date, parseInt(matches[1], 10), matches[2] || 'd');
}
return date;
};
defaultDate = (defaultDate ? plugin.newDate(defaultDate) : null);
dateSpec = (dateSpec == null ? defaultDate :
(typeof dateSpec === 'string' ? offsetString(dateSpec) : (typeof dateSpec === 'number' ?
(isNaN(dateSpec) || dateSpec === Infinity || dateSpec === -Infinity ? defaultDate :
plugin.add(plugin.today(), dateSpec, 'd')) : plugin.newDate(dateSpec))));
return dateSpec;
},
/** Find the number of days in a given month.
@param year {Date|number} The date to get days for or the full year.
@param month {number} The month (1 to 12).
@return {number} The number of days in this month.
@example var days = $.datepick.daysInMonth(2014, 12) */
daysInMonth: function(year, month) {
month = (year.getFullYear ? year.getMonth() + 1 : month);
year = (year.getFullYear ? year.getFullYear() : year);
return this.newDate(year, month + 1, 0).getDate();
},
/** Calculate the day of the year for a date.
@param year {Date|number} The date to get the day-of-year for or the full year.
@param month {number} The month (1-12).
@param day {number} The day.
@return {number} The day of the year.
@example var doy = $.datepick.dayOfYear(2014, 12, 25) */
dayOfYear: function(year, month, day) {
var date = (year.getFullYear ? year : plugin.newDate(year, month, day));
var newYear = plugin.newDate(date.getFullYear(), 1, 1);
return Math.floor((date.getTime() - newYear.getTime()) / plugin._msPerDay) + 1;
},
/** Set as <code>calculateWeek</code> to determine the week of the year based on the ISO 8601 definition.
@param year {Date|number} The date to get the week for or the full year.
@param month {number} The month (1-12).
@param day {number} The day.
@return {number} The number of the week within the year that contains this date.
@example var week = $.datepick.iso8601Week(2014, 12, 25) */
iso8601Week: function(year, month, day) {
var checkDate = (year.getFullYear ?
new Date(year.getTime()) : plugin.newDate(year, month, day));
// Find Thursday of this week starting on Monday
checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay() || 7));
var time = checkDate.getTime();
checkDate.setMonth(0, 1); // Compare with Jan 1
return Math.floor(Math.round((time - checkDate) / plugin._msPerDay) / 7) + 1;
},
/** Return today's date.
@return {Date} Today.
@example $.datepick.today() */
today: function() {
return this._normaliseDate(new Date());
},
/** Return a new date.
@param year {Date|number} The date to clone or the year.
@param month {number} The month (1-12).
@param day {number} The day.
@return {Date} The date.
@example $.datepick.newDate(oldDate)
$.datepick.newDate(2014, 12, 25) */
newDate: function(year, month, day) {
return (!year ? null : (year.getFullYear ? this._normaliseDate(new Date(year.getTime())) :
new Date(year, month - 1, day, 12)));
},
/** Standardise a date into a common format - time portion is 12 noon.
@private
@param date {Date} The date to standardise.
@return {Date} The normalised date. */
_normaliseDate: function(date) {
if (date) {
date.setHours(12, 0, 0, 0);
}
return date;
},
/** Set the year for a date.
@param date {Date} The original date.
@param year {number} The new year.
@return {Date} The updated date.
@example $.datepick.year(date, 2014) */
year: function(date, year) {
date.setFullYear(year);
return this._normaliseDate(date);
},
/** Set the month for a date.
@param date {Date} The original date.
@param month {number} The new month (1-12).
@return {Date} The updated date.
@example $.datepick.month(date, 12) */
month: function(date, month) {
date.setMonth(month - 1);
return this._normaliseDate(date);
},
/** Set the day for a date.
@param date {Date} The original date.
@param day {number} The new day of the month.
@return {Date} The updated date.
@example $.datepick.day(date, 25) */
day: function(date, day) {
date.setDate(day);
return this._normaliseDate(date);
},
/** Add a number of periods to a date.
@param date {Date} The original date.
@param amount {number} The number of periods.
@param period {string} The type of period d/w/m/y.
@return {Date} The updated date.
@example $.datepick.add(date, 10, 'd') */
add: function(date, amount, period) {
if (period === 'd' || period === 'w') {
this._normaliseDate(date);
date.setDate(date.getDate() + amount * (period === 'w' ? 7 : 1));
}
else {
var year = date.getFullYear() + (period === 'y' ? amount : 0);
var month = date.getMonth() + (period === 'm' ? amount : 0);
date.setTime(plugin.newDate(year, month + 1,
Math.min(date.getDate(), this.daysInMonth(year, month + 1))).getTime());
}
return date;
},
/** Apply the months offset value to a date.
@private
@param date {Date} The original date.
@param inst {object} The current instance settings.
@return {Date} The updated date. */
_applyMonthsOffset: function(date, inst) {
var monthsOffset = inst.options.monthsOffset;
if ($.isFunction(monthsOffset)) {
monthsOffset = monthsOffset.apply(inst.elem[0], [date]);
}
return plugin.add(date, -monthsOffset, 'm');
},
_init: function() {
this.defaultOptions.commands = this.commands;
this.defaultOptions.calculateWeek = this.iso8601Week;
this.regionalOptions[''].renderer = this.defaultRenderer;
this._super();
},
_instSettings: function(elem, options) {
return {selectedDates: [], drawDate: null, pickingRange: false,
inline: ($.inArray(elem[0].nodeName.toLowerCase(), ['div', 'span']) > -1),
get: function(name) { // Get a setting value, computing if necessary
if ($.inArray(name, ['defaultDate', 'minDate', 'maxDate']) > -1) { // Decode date settings
return plugin.determineDate(this.options[name], null,
this.selectedDates[0], this.options.dateFormat, this.getConfig());