/
dataTables.dateTime.js
1702 lines (1443 loc) · 40.2 KB
/
dataTables.dateTime.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
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
/*! DateTime picker for DataTables.net v1.5.2
*
* © SpryMedia Ltd, all rights reserved.
* License: MIT datatables.net/license/mit
*/
/**
* @summary DateTime picker for DataTables.net
* @version 1.5.2
* @file dataTables.dateTime.js
* @author SpryMedia Ltd
* @contact www.datatables.net/contact
*/
// Supported formatting and parsing libraries:
// * Moment
// * Luxon
// * DayJS
var dateLib;
/*
* This file provides a DateTime GUI picker (calendar and time input). Only the
* format YYYY-MM-DD is supported without additional software, but the end user
* experience can be greatly enhanced by including the momentjs, dayjs or luxon library
* which provide date / time parsing and formatting options.
*
* This functionality is required because the HTML5 date and datetime input
* types are not widely supported in desktop browsers.
*
* Constructed by using:
*
* new DateTime( input, opts )
*
* where `input` is the HTML input element to use and `opts` is an object of
* options based on the `DateTime.defaults` object.
*/
var DateTime = function (input, opts) {
// Check if called with a window or jQuery object for DOM less applications
// This is for backwards compatibility with CommonJS loader
if (DateTime.factory(input, opts)) {
return DateTime;
}
// Attempt to auto detect the formatting library (if there is one). Having it in
// the constructor allows load order independence.
if (typeof dateLib === 'undefined') {
dateLib = window.moment
? window.moment
: window.dayjs
? window.dayjs
: window.luxon
? window.luxon
: null;
}
this.c = $.extend(true, {}, DateTime.defaults, opts);
var classPrefix = this.c.classPrefix;
// Only IS8601 dates are supported without moment, dayjs or luxon
if (!dateLib && this.c.format !== 'YYYY-MM-DD') {
throw "DateTime: Without momentjs, dayjs or luxon only the format 'YYYY-MM-DD' can be used";
}
// Min and max need to be `Date` objects in the config
if (typeof this.c.minDate === 'string') {
this.c.minDate = new Date(this.c.minDate);
}
if (typeof this.c.maxDate === 'string') {
this.c.maxDate = new Date(this.c.maxDate);
}
// DOM structure
var structure = $(
'<div class="' + classPrefix + '">' +
'<div class="' + classPrefix + '-date">' +
'<div class="' + classPrefix + '-title">' +
'<div class="' + classPrefix + '-iconLeft">' +
'<button type="button"></button>' +
'</div>' +
'<div class="' + classPrefix + '-iconRight">' +
'<button type="button"></button>' +
'</div>' +
'<div class="' + classPrefix + '-label">' +
'<span></span>' +
'<select class="' + classPrefix + '-month"></select>' +
'</div>' +
'<div class="' + classPrefix + '-label">' +
'<span></span>' +
'<select class="' + classPrefix + '-year"></select>' +
'</div>' +
'</div>' +
'<div class="' + classPrefix + '-buttons">' +
'<a class="' + classPrefix + '-clear"></a>' +
'<a class="' + classPrefix + '-today"></a>' +
'</div>' +
'<div class="' + classPrefix + '-calendar"></div>' +
'</div>' +
'<div class="' + classPrefix + '-time">' +
'<div class="' + classPrefix + '-hours"></div>' +
'<div class="' + classPrefix + '-minutes"></div>' +
'<div class="' + classPrefix + '-seconds"></div>' +
'</div>' +
'<div class="' + classPrefix + '-error"></div>' +
'</div>'
);
this.dom = {
container: structure,
date: structure.find('.' + classPrefix + '-date'),
title: structure.find('.' + classPrefix + '-title'),
calendar: structure.find('.' + classPrefix + '-calendar'),
time: structure.find('.' + classPrefix + '-time'),
error: structure.find('.' + classPrefix + '-error'),
buttons: structure.find('.' + classPrefix + '-buttons'),
clear: structure.find('.' + classPrefix + '-clear'),
today: structure.find('.' + classPrefix + '-today'),
previous: structure.find('.' + classPrefix + '-iconLeft'),
next: structure.find('.' + classPrefix + '-iconRight'),
input: $(input)
};
this.s = {
/** @type {Date} Date value that the picker has currently selected */
d: null,
/** @type {Date} Date of the calendar - might not match the value */
display: null,
/** @type {number} Used to select minutes in a range where the range base is itself unavailable */
minutesRange: null,
/** @type {number} Used to select minutes in a range where the range base is itself unavailable */
secondsRange: null,
/** @type {String} Unique namespace string for this instance */
namespace: 'dateime-' + (DateTime._instance++),
/** @type {Object} Parts of the picker that should be shown */
parts: {
date: this.c.format.match(/[YMD]|L(?!T)|l/) !== null,
time: this.c.format.match(/[Hhm]|LT|LTS/) !== null,
seconds: this.c.format.indexOf('s') !== -1,
hours12: this.c.format.match(/[haA]/) !== null
}
};
this.dom.container
.append(this.dom.date)
.append(this.dom.time)
.append(this.dom.error);
this.dom.date
.append(this.dom.title)
.append(this.dom.buttons)
.append(this.dom.calendar);
this.dom.input.addClass('dt-datetime');
this._constructor();
};
$.extend(DateTime.prototype, {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public
*/
/**
* Destroy the control
*/
destroy: function () {
this._hide(true);
this.dom.container.off().empty();
this.dom.input
.removeClass('dt-datetime')
.removeAttr('autocomplete')
.off('.datetime');
},
display: function (year, month) {
if (year !== undefined) {
this.s.display.setUTCFullYear(year);
}
if (month !== undefined) {
this.s.display.setUTCMonth(month - 1);
}
if (year !== undefined || month !== undefined) {
this._setTitle();
this._setCalander();
return this;
}
return {
month: this.s.display.getUTCMonth() + 1,
year: this.s.display.getUTCFullYear()
};
},
errorMsg: function (msg) {
var error = this.dom.error;
if (msg) {
error.html(msg);
}
else {
error.empty();
}
return this;
},
hide: function () {
this._hide();
return this;
},
max: function (date) {
this.c.maxDate = typeof date === 'string'
? new Date(date)
: date;
this._optionsTitle();
this._setCalander();
return this;
},
min: function (date) {
this.c.minDate = typeof date === 'string'
? new Date(date)
: date;
this._optionsTitle();
this._setCalander();
return this;
},
/**
* Check if an element belongs to this control
*
* @param {node} node Element to check
* @return {boolean} true if owned by this control, false otherwise
*/
owns: function (node) {
return $(node).parents().filter(this.dom.container).length > 0;
},
/**
* Get / set the value
*
* @param {string|Date} set Value to set
* @param {boolean} [write=true] Flag to indicate if the formatted value
* should be written into the input element
*/
val: function (set, write) {
if (set === undefined) {
return this.s.d;
}
if (set instanceof Date) {
this.s.d = this._dateToUtc(set);
}
else if (set === null || set === '') {
this.s.d = null;
}
else if (set === '--now') {
this.s.d = this._dateToUtc(new Date());
}
else if (typeof set === 'string') {
this.s.d = this._dateToUtc(
this._convert(set, this.c.format, null)
);
}
if (write || write === undefined) {
if (this.s.d) {
this._writeOutput();
}
else {
// The input value was not valid...
this.dom.input.val(set);
}
}
// Need something to display
this.s.display = this.s.d
? new Date(this.s.d.toString())
: new Date();
// Set the day of the month to be 1 so changing between months doesn't
// run into issues when going from day 31 to 28 (for example)
this.s.display.setUTCDate(1);
// Update the display elements for the new value
this._setTitle();
this._setCalander();
this._setTime();
return this;
},
/**
* Similar to `val()` but uses a given date / time format
*
* @param format Format to get the data as (getter) or that is input (setter)
* @param val Value to write (if undefined, used as a getter)
* @returns
*/
valFormat: function (format, val) {
if (!val) {
return this._convert(this.val(), null, format);
}
// Convert from the format given here to the instance's configured format
this.val(
this._convert(val, format, null)
);
return this;
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
/**
* Build the control and assign initial event handlers
*
* @private
*/
_constructor: function () {
var that = this;
var classPrefix = this.c.classPrefix;
var last = this.dom.input.val();
var onChange = function () {
var curr = that.dom.input.val();
if (curr !== last) {
that.c.onChange.call(that, curr, that.s.d, that.dom.input);
last = curr;
}
};
if (!this.s.parts.date) {
this.dom.date.css('display', 'none');
}
if (!this.s.parts.time) {
this.dom.time.css('display', 'none');
}
if (!this.s.parts.seconds) {
this.dom.time.children('div.' + classPrefix + '-seconds').remove();
this.dom.time.children('span').eq(1).remove();
}
if (!this.c.buttons.clear) {
this.dom.clear.css('display', 'none');
}
if (!this.c.buttons.today) {
this.dom.today.css('display', 'none');
}
// Render the options
this._optionsTitle();
$(document).on('i18n.dt', function (e, settings) {
if (settings.oLanguage.datetime) {
$.extend(true, that.c.i18n, settings.oLanguage.datetime);
that._optionsTitle();
}
});
// When attached to a hidden input, we always show the input picker, and
// do so inline
if (this.dom.input.attr('type') === 'hidden') {
this.dom.container.addClass('inline');
this.c.attachTo = 'input';
this.val(this.dom.input.val(), false);
this._show();
}
// Set the initial value
if (last) {
this.val(last, false);
}
// Trigger the display of the widget when clicking or focusing on the
// input element
this.dom.input
.attr('autocomplete', 'off')
.on('focus.datetime click.datetime', function () {
// If already visible - don't do anything
if (that.dom.container.is(':visible') || that.dom.input.is(':disabled')) {
return;
}
// In case the value has changed by text
that.val(that.dom.input.val(), false);
that._show();
})
.on('keyup.datetime', function () {
// Update the calendar's displayed value as the user types
if (that.dom.container.is(':visible')) {
that.val(that.dom.input.val(), false);
}
});
// Want to prevent the focus bubbling up the document to account for
// focus capture in modals (e.g. Editor and Bootstrap). They can see
// the focus as outside the modal and thus immediately blur focus on
// the picker. Need to use a native addEL since jQuery changes the
// focusin to focus for some reason! focusin bubbles, focus does not.
this.dom.container[0].addEventListener('focusin', function (e) {
e.stopPropagation();
});
// Main event handlers for input in the widget
this.dom.container
.on('change', 'select', function () {
var select = $(this);
var val = select.val();
if (select.hasClass(classPrefix + '-month')) {
// Month select
that._correctMonth(that.s.display, val);
that._setTitle();
that._setCalander();
}
else if (select.hasClass(classPrefix + '-year')) {
// Year select
that.s.display.setUTCFullYear(val);
that._setTitle();
that._setCalander();
}
else if (select.hasClass(classPrefix + '-hours') || select.hasClass(classPrefix + '-ampm')) {
// Hours - need to take account of AM/PM input if present
if (that.s.parts.hours12) {
var hours = $(that.dom.container).find('.' + classPrefix + '-hours').val() * 1;
var pm = $(that.dom.container).find('.' + classPrefix + '-ampm').val() === 'pm';
that.s.d.setUTCHours(hours === 12 && !pm ?
0 :
pm && hours !== 12 ?
hours + 12 :
hours
);
}
else {
that.s.d.setUTCHours(val);
}
that._setTime();
that._writeOutput(true);
onChange();
}
else if (select.hasClass(classPrefix + '-minutes')) {
// Minutes select
that.s.d.setUTCMinutes(val);
that._setTime();
that._writeOutput(true);
onChange();
}
else if (select.hasClass(classPrefix + '-seconds')) {
// Seconds select
that.s.d.setSeconds(val);
that._setTime();
that._writeOutput(true);
onChange();
}
that.dom.input.focus();
that._position();
})
.on('click', function (e) {
var d = that.s.d;
var nodeName = e.target.nodeName.toLowerCase();
var target = nodeName === 'span' ?
e.target.parentNode :
e.target;
nodeName = target.nodeName.toLowerCase();
if (nodeName === 'select') {
return;
}
e.stopPropagation();
if (nodeName === 'a') {
e.preventDefault();
if ($(target).hasClass(classPrefix + '-clear')) {
// Clear the value and don't change the display
that.s.d = null;
that.dom.input.val('');
that._writeOutput();
that._setCalander();
that._setTime();
onChange();
}
else if ($(target).hasClass(classPrefix + '-today')) {
// Don't change the value, but jump to the month
// containing today
that.s.display = new Date();
that._setTitle();
that._setCalander();
}
}
if (nodeName === 'button') {
var button = $(target);
var parent = button.parent();
if (parent.hasClass('disabled') && !parent.hasClass('range')) {
button.blur();
return;
}
if (parent.hasClass(classPrefix + '-iconLeft')) {
// Previous month
that.s.display.setUTCMonth(that.s.display.getUTCMonth() - 1);
that._setTitle();
that._setCalander();
that.dom.input.focus();
}
else if (parent.hasClass(classPrefix + '-iconRight')) {
// Next month
that._correctMonth(that.s.display, that.s.display.getUTCMonth() + 1);
that._setTitle();
that._setCalander();
that.dom.input.focus();
}
else if (button.parents('.' + classPrefix + '-time').length) {
var val = button.data('value');
var unit = button.data('unit');
d = that._needValue();
if (unit === 'minutes') {
if (parent.hasClass('disabled') && parent.hasClass('range')) {
that.s.minutesRange = val;
that._setTime();
return;
}
else {
that.s.minutesRange = null;
}
}
if (unit === 'seconds') {
if (parent.hasClass('disabled') && parent.hasClass('range')) {
that.s.secondsRange = val;
that._setTime();
return;
}
else {
that.s.secondsRange = null;
}
}
// Specific to hours for 12h clock
if (val === 'am') {
if (d.getUTCHours() >= 12) {
val = d.getUTCHours() - 12;
}
else {
return;
}
}
else if (val === 'pm') {
if (d.getUTCHours() < 12) {
val = d.getUTCHours() + 12;
}
else {
return;
}
}
var set = unit === 'hours' ?
'setUTCHours' :
unit === 'minutes' ?
'setUTCMinutes' :
'setSeconds';
d[set](val);
that._setCalander();
that._setTime();
that._writeOutput(true);
onChange();
}
else {
// Calendar click
d = that._needValue();
// Can't be certain that the current day will exist in
// the new month, and likewise don't know that the
// new day will exist in the old month, But 1 always
// does, so we can change the month without worry of a
// recalculation being done automatically by `Date`
d.setUTCDate(1);
d.setUTCFullYear(button.data('year'));
d.setUTCMonth(button.data('month'));
d.setUTCDate(button.data('day'));
that._writeOutput(true);
// Don't hide if there is a time picker, since we want to
// be able to select a time as well.
if (!that.s.parts.time) {
// This is annoying but IE has some kind of async
// behaviour with focus and the focus from the above
// write would occur after this hide - resulting in the
// calendar opening immediately
setTimeout(function () {
that._hide();
}, 10);
}
else {
that._setCalander();
that._setTime();
}
onChange();
}
}
else {
// Click anywhere else in the widget - return focus to the
// input element
that.dom.input.focus();
}
});
},
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Private
*/
/**
* Compare the date part only of two dates - this is made super easy by the
* toDateString method!
*
* @param {Date} a Date 1
* @param {Date} b Date 2
* @private
*/
_compareDates: function (a, b) {
// Can't use toDateString as that converts to local time
// luxon uses different method names so need to be able to call them
return this._isLuxon()
? dateLib.DateTime.fromJSDate(a).toUTC().toISODate() === dateLib.DateTime.fromJSDate(b).toUTC().toISODate()
: this._dateToUtcString(a) === this._dateToUtcString(b);
},
/**
* Convert from one format to another
*
* @param {string|Date} val Value
* @param {string|null} from Format to convert from. If null a `Date` must be given
* @param {string|null} to Format to convert to. If null a `Date` will be returned
* @returns {string|Date} Converted value
*/
_convert: function (val, from, to) {
if (!val) {
return val;
}
if (!dateLib) {
// Note that in here from and to can either be null or YYYY-MM-DD
// They cannot be anything else
if ((!from && !to) || (from && to)) {
// No conversion
return val;
}
else if (!from) {
// Date in, string back
return val.getUTCFullYear() + '-' +
this._pad(val.getUTCMonth() + 1) + '-' +
this._pad(val.getUTCDate());
}
else { // (! to)
// String in, date back
var match = val.match(/(\d{4})\-(\d{2})\-(\d{2})/);
return match ?
new Date(match[1], match[2] - 1, match[3]) :
null;
}
}
else if (this._isLuxon()) {
// Luxon
var dtLux = val instanceof Date
? dateLib.DateTime.fromJSDate(val).toUTC()
: dateLib.DateTime.fromFormat(val, from);
if (!dtLux.isValid) {
return null;
}
return to
? dtLux.toFormat(to)
: dtLux.toJSDate();
}
else {
// Moment / DayJS
var dtMo = val instanceof Date
? dateLib.utc(val, undefined, this.c.locale, this.c.strict)
: dateLib(val, from, this.c.locale, this.c.strict);
if (!dtMo.isValid()) {
return null;
}
return to
? dtMo.format(to)
: dtMo.toDate();
}
},
/**
* When changing month, take account of the fact that some months don't have
* the same number of days. For example going from January to February you
* can have the 31st of Jan selected and just add a month since the date
* would still be 31, and thus drop you into March.
*
* @param {Date} date Date - will be modified
* @param {integer} month Month to set
* @private
*/
_correctMonth: function (date, month) {
var days = this._daysInMonth(date.getUTCFullYear(), month);
var correctDays = date.getUTCDate() > days;
date.setUTCMonth(month);
if (correctDays) {
date.setUTCDate(days);
date.setUTCMonth(month);
}
},
/**
* Get the number of days in a method. Based on
* http://stackoverflow.com/a/4881951 by Matti Virkkunen
*
* @param {integer} year Year
* @param {integer} month Month (starting at 0)
* @private
*/
_daysInMonth: function (year, month) {
//
var isLeap = ((year % 4) === 0 && ((year % 100) !== 0 || (year % 400) === 0));
var months = [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return months[month];
},
/**
* Create a new date object which has the UTC values set to the local time.
* This allows the local time to be used directly for the library which
* always bases its calculations and display on UTC.
*
* @param {Date} s Date to "convert"
* @return {Date} Shifted date
*/
_dateToUtc: function (s) {
if (!s) {
return s;
}
return new Date(Date.UTC(
s.getFullYear(), s.getMonth(), s.getDate(),
s.getHours(), s.getMinutes(), s.getSeconds()
));
},
/**
* Create a UTC ISO8601 date part from a date object
*
* @param {Date} d Date to "convert"
* @return {string} ISO formatted date
*/
_dateToUtcString: function (d) {
// luxon uses different method names so need to be able to call them
return this._isLuxon()
? dateLib.DateTime.fromJSDate(d).toUTC().toISODate()
: d.getUTCFullYear() + '-' +
this._pad(d.getUTCMonth() + 1) + '-' +
this._pad(d.getUTCDate());
},
/**
* Hide the control and remove events related to its display
*
* @private
*/
_hide: function (destroy) {
if (!destroy && this.dom.input.attr('type') === 'hidden') {
return;
}
var namespace = this.s.namespace;
this.dom.container.detach();
$(window).off('.' + namespace);
$(document).off('keydown.' + namespace);
$('div.dataTables_scrollBody').off('scroll.' + namespace);
$('div.DTE_Body_Content').off('scroll.' + namespace);
$('body').off('click.' + namespace);
$(this.dom.input[0].offsetParent).off('.' + namespace);
},
/**
* Convert a 24 hour value to a 12 hour value
*
* @param {integer} val 24 hour value
* @return {integer} 12 hour value
* @private
*/
_hours24To12: function (val) {
return val === 0 ?
12 :
val > 12 ?
val - 12 :
val;
},
/**
* Generate the HTML for a single day in the calendar - this is basically
* and HTML cell with a button that has data attributes so we know what was
* clicked on (if it is clicked on) and a bunch of classes for styling.
*
* @param {object} day Day object from the `_htmlMonth` method
* @return {string} HTML cell
*/
_htmlDay: function (day) {
if (day.empty) {
return '<td class="empty"></td>';
}
var classes = ['selectable'];
var classPrefix = this.c.classPrefix;
if (day.disabled) {
classes.push('disabled');
}
if (day.today) {
classes.push('now');
}
if (day.selected) {
classes.push('selected');
}
return '<td data-day="' + day.day + '" class="' + classes.join(' ') + '">' +
'<button class="' + classPrefix + '-button ' + classPrefix + '-day" type="button" ' + 'data-year="' + day.year + '" data-month="' + day.month + '" data-day="' + day.day + '">' +
'<span>' + day.day + '</span>' +
'</button>' +
'</td>';
},
/**
* Create the HTML for a month to be displayed in the calendar table.
*
* Based upon the logic used in Pikaday - MIT licensed
* Copyright (c) 2014 David Bushell
* https://github.com/dbushell/Pikaday
*
* @param {integer} year Year
* @param {integer} month Month (starting at 0)
* @return {string} Calendar month HTML
* @private
*/
_htmlMonth: function (year, month) {
var now = this._dateToUtc(new Date()),
days = this._daysInMonth(year, month),
before = new Date(Date.UTC(year, month, 1)).getUTCDay(),
data = [],
row = [];
if (this.c.firstDay > 0) {
before -= this.c.firstDay;
if (before < 0) {
before += 7;
}
}
var cells = days + before,
after = cells;
while (after > 7) {
after -= 7;
}
cells += 7 - after;
var minDate = this.c.minDate;
var maxDate = this.c.maxDate;
if (minDate) {
minDate.setUTCHours(0);
minDate.setUTCMinutes(0);
minDate.setSeconds(0);
}
if (maxDate) {
maxDate.setUTCHours(23);
maxDate.setUTCMinutes(59);
maxDate.setSeconds(59);
}
for (var i = 0, r = 0; i < cells; i++) {
var day = new Date(Date.UTC(year, month, 1 + (i - before))),
selected = this.s.d ? this._compareDates(day, this.s.d) : false,
today = this._compareDates(day, now),
empty = i < before || i >= (days + before),
disabled = (minDate && day < minDate) ||
(maxDate && day > maxDate);
var disableDays = this.c.disableDays;
if (Array.isArray(disableDays) && $.inArray(day.getUTCDay(), disableDays) !== -1) {
disabled = true;
}
else if (typeof disableDays === 'function' && disableDays(day) === true) {
disabled = true;
}
var dayConfig = {
day: 1 + (i - before),
month: month,
year: year,
selected: selected,
today: today,
disabled: disabled,
empty: empty
};
row.push(this._htmlDay(dayConfig));
if (++r === 7) {
if (this.c.showWeekNumber) {
row.unshift(this._htmlWeekOfYear(i - before, month, year));
}
data.push('<tr>' + row.join('') + '</tr>');
row = [];
r = 0;
}
}
var classPrefix = this.c.classPrefix;
var className = classPrefix + '-table';
if (this.c.showWeekNumber) {
className += ' weekNumber';
}
// Show / hide month icons based on min/max
if (minDate) {
var underMin = minDate >= new Date(Date.UTC(year, month, 1, 0, 0, 0));
this.dom.title.find('div.' + classPrefix + '-iconLeft')
.css('display', underMin ? 'none' : 'block');
}
if (maxDate) {
var overMax = maxDate < new Date(Date.UTC(year, month + 1, 1, 0, 0, 0));
this.dom.title.find('div.' + classPrefix + '-iconRight')
.css('display', overMax ? 'none' : 'block');
}
return '<table class="' + className + '">' +
'<thead>' +
this._htmlMonthHead() +
'</thead>' +
'<tbody>' +
data.join('') +
'</tbody>' +
'</table>';
},
/**