-
Notifications
You must be signed in to change notification settings - Fork 56
/
datepicker.js
3547 lines (3088 loc) · 143 KB
/
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
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
/*! DatePicker v6.3.6 MIT/GPL2 @freqdec */
var datePickerController = (function datePickerController() {
"use strict";
var debug = false,
isOpera = Object.prototype.toString.call(window.opera) === "[object Opera]",
describedBy = "",
languageInfo = parseUILanguage(),
nbsp = String.fromCharCode(160),
datePickers = {},
weeksInYearCache = {},
bespokeTitles = {},
uniqueId = 0,
finalOpacity = 100,
cssAnimations = null,
transitionEnd = "",
buttonTabIndex = true,
mouseWheel = true,
deriveLocale = true,
localeImport = false,
nodrag = false,
langFileFolder = false,
returnLocaleDate = false,
kbEvent = false,
dateParseFallback = true,
cellFormat = "%d %F %Y",
titleFormat = "%F %d, %Y",
statusFormat = "",
formatParts = isOpera ? ["%j"] : ["%j", " %F %Y"],
dPartsRegExp = /%([d|j])/,
mPartsRegExp = /%([M|F|m|n])/,
yPartsRegExp = /%[y|Y]/,
noSelectionRegExp = /date-picker-unused|out-of-range|day-disabled|not-selectable/,
formatTestRegExp = /%([d|j|M|F|m|n|Y|y])/,
formatSplitRegExp = /%([d|D|l|j|N|w|S|W|M|F|m|n|t|Y|y])/,
rangeRegExp = /^((\d\d\d\d)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01]))$/,
wcDateRegExp = /^(((\d\d\d\d)|(\*\*\*\*))((0[1-9]|1[012])|(\*\*))(0[1-9]|[12][0-9]|3[01]))$/,
wsCharClass = "\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029",
// https://gist.github.com/padolsey/527683
oldIE = (function(){var undef,v = 3,div = document.createElement('div'),all = div.getElementsByTagName('i');while (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',all[0]); return v > 4 ? v : undef;}());
(function() {
var scriptFiles = document.getElementsByTagName('script'),
json = parseJSON(String(scriptFiles[scriptFiles.length - 1].innerHTML).replace(/[\n\r\s\t]+/g, " ").replace(/^\s+/, "").replace(/\s+$/, ""));
if(typeof json === "object" && !("err" in json)) {
affectJSON(json);
};
if(deriveLocale && typeof(fdLocale) != "object") {
var head = document.getElementsByTagName("head")[0] || document.documentElement,
loc = langFileFolder ? langFileFolder : scriptFiles[scriptFiles.length - 1].src.substr(0, scriptFiles[scriptFiles.length - 1].src.lastIndexOf("/")) + "/lang/",
script,
i;
for(i = 0; i < languageInfo.length; i++) {
script = document.createElement('script');
script.type = "text/javascript";
script.src = loc + languageInfo[i] + ".js";
script.charSet = "utf-8";
if(oldIE && oldIE < 8) {
var bases = document.getElementsByTagName('base');
if (bases.length && bases[0].childNodes.length) {
bases[0].appendChild(script);
} else {
head.appendChild(script);
};
bases = null;
} else {
head.appendChild(script);
};
};
script = null;
} else {
returnLocaleDate = true;
};
})();
function removeChildNodes(elem) {
while(elem.firstChild) {
elem.removeChild(elem.firstChild);
};
};
function addClass(e, c) {
if(new RegExp("(^|[" + wsCharClass + "])" + c + "([" + wsCharClass + "]|$)").test(e.className)) {
return;
};
e.className += ( e.className ? " " : "" ) + c;
};
function removeClass(e, c) {
e.className = !c ? "" : e.className.replace(new RegExp("(^|[" + wsCharClass + "])" + c + "([" + wsCharClass + "]|$)"), " ").replace(new RegExp("/^[" + wsCharClass + "][" + wsCharClass + "]*/"), '').replace(new RegExp("/[" + wsCharClass + "][" + wsCharClass + "]*$/"), '');
};
// Attempts to parse the current language from the HTML element. Defaults to "en" if none given
function parseUILanguage() {
var languageTag = document.getElementsByTagName('html')[0].getAttribute('lang') || document.getElementsByTagName('html')[0].getAttribute('xml:lang');
languageTag = !languageTag ? "en" : languageTag.toLowerCase();
return languageTag.search(/^([a-z]{2,3})-([a-z]{2})$/) != -1 ? [languageTag.match(/^([a-z]{2,3})-([a-z]{2})$/)[1], languageTag] : [languageTag];
};
// Cross browser split from http://blog.stevenlevithan.com/archives/cross-browser-split
var cbSplit = function(str, separator, limit) {
// if `separator` is not a regex, use the native `split`
if(Object.prototype.toString.call(separator) !== "[object RegExp]") {
return cbSplit._nativeSplit.call(str, separator, limit);
};
var output = [],
lastLastIndex = 0,
flags = "",
separator = RegExp(separator.source, "g"),
separator2, match, lastIndex, lastLength;
str = str + "";
if(!cbSplit._compliantExecNpcg) {
separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags);
};
/* behavior for `limit`: if it's...
- `undefined`: no limit.
- `NaN` or zero: return an empty array.
- a positive number: use `Math.floor(limit)`.
- a negative number: no limit.
- other: type-convert, then use the above rules. */
if(limit === undefined || +limit < 0) {
limit = Infinity;
} else {
limit = Math.floor(+limit);
if(!limit) {
return [];
};
};
while(match = separator.exec(str)) {
lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser
if (lastIndex > lastLastIndex) {
output.push(str.slice(lastLastIndex, match.index));
// fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
if(!cbSplit._compliantExecNpcg && match.length > 1) {
match[0].replace(separator2, function () {
for (var i = 1; i < arguments.length - 2; i++) {
if(arguments[i] === undefined) {
match[i] = undefined;
};
};
});
};
if(match.length > 1 && match.index < str.length) {
Array.prototype.push.apply(output, match.slice(1));
};
lastLength = match[0].length;
lastLastIndex = lastIndex;
if(output.length >= limit) {
break;
};
};
if(separator.lastIndex === match.index) {
// avoid an infinite loop
separator.lastIndex++;
};
};
if(lastLastIndex === str.length) {
if (lastLength || !separator.test("")) {
output.push("");
};
} else {
output.push(str.slice(lastLastIndex));
};
return output.length > limit ? output.slice(0, limit) : output;
};
// NPCG: nonparticipating capturing group
cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined;
cbSplit._nativeSplit = String.prototype.split;
// Affects the JSON passed to the script
function affectJSON(json) {
if(!(typeof json === "object")) {
return;
};
var key,
switchObj = {
"debug":function(value) {
debug = !!value;
return true;
},
"lang":function(value) {
if(typeof value === "string" && value.search(/^[a-z]{2,3}(-([a-z]{2}))?$/i) != -1) {
languageInfo = [value.toLowerCase()];
returnLocaleDate = true;
deriveLocale = true;
};
return true;
},
"nodrag":function(value) {
nodrag = !!value;
return true;
},
"buttontabindex":function(value) {
buttonTabIndex = !!value;
return true;
},
"derivelocale":function(value) {
deriveLocale = !!value;
return true;
},
"mousewheel":function(value) {
mouseWheel = !!value;
return true;
},
"cellformat":function(value) {
if(typeof value === "string") {
parseCellFormat(value);
};
return true;
},
"titleformat":function(value) {
if(typeof value === "string") {
titleFormat = value;
};
return true;
},
"statusformat":function(value) {
if(typeof value === "string") {
statusFormat = value;
};
return true;
},
"describedby":function(value) {
if(typeof value === "string") {
describedBy = value;
};
return true;
},
"finalopacity":function(value) {
if(typeof value === 'number' && (+value > 20 && +value <= 100)) {
finalOpacity = parseInt(value, 10);
};
return true;
},
"bespoketitles":function(value) {
if(typeof value === "object") {
bespokeTitles = {};
for(var dt in value) {
if(value.hasOwnProperty(dt) && String(dt).match(wcDateRegExp) != -1) {
bespokeTitles[dt] = String(value[dt]);
};
};
};
return true;
},
"dateparsefallback":function(value) {
dateParseFallback = !!value;
return true;
},
"languagefilelocation":function(value) {
langFileFolder = value;
return true;
},
"_default":function() {
if(debug) {
throw "Unknown key located within JSON data: " + key;
};
return true;
}
};
for(key in json) {
if(!json.hasOwnProperty(key)) {
continue;
};
(switchObj.hasOwnProperty(String(key).toLowerCase()) && switchObj[String(key).toLowerCase()] || switchObj._default)(json[key]);
};
};
// Parses the JSON passed either between the script tags or by using the
// setGlobalOptions method
function parseJSON(str) {
if(!(typeof str === 'string') || str == "") {
return {};
};
try {
// Does a JSON (native or not) Object exist
if(typeof JSON === "object" && JSON.parse) {
return window.JSON.parse(str);
// Genious code taken from: http://kentbrewster.com/badges/
} else if(/debug|lang|nodrag|buttontabindex|derivelocale|mousewheel|cellformat|titleformat|statusformat|describedby|finalopacity|bespoketitles|dateparsefallback/.test(str.toLowerCase())) {
var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
'Array,String,Math,RegExp,Image,ActiveXObject;',
'return (' , str.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function-') , ');'].join(''));
return f();
};
} catch (e) { };
if(debug) {
throw "Could not parse the JSON object";
};
return {"err":1};
};
// Parses the cell format to use whenever the datepicker has keyboard focus
function parseCellFormat(value) {
if(isOpera) {
// Don't use hidden text for opera due to the default
// "blue" browser focus outline stretching outside of the viewport
// and degrading visual accessibility. Harsh & hackish though...
formatParts = ["%j"];
cellFormat = "%j %F %Y";
return;
};
// If no day part stipulated then use presets
if(value.match(/%([d|j])/) == -1) {
return;
};
// Basic split on the %j or %d modifiers
formatParts = cbSplit(value, /%([d|j])/);
cellFormat = value;
};
function pad(value, length) {
length = Math.min(4, length || 2);
return "0000".substr(0,length - Math.min(String(value).length, length)) + value;
};
// Very, very basic event functions
function addEvent(obj, type, fn) {
if(obj.addEventListener) {
obj.addEventListener(type, fn, true);
} else if(obj.attachEvent) {
obj.attachEvent("on"+type, fn);
};
};
function removeEvent(obj, type, fn) {
try {
if(obj.removeEventListener) {
obj.removeEventListener(type, fn, true);
} else if(obj.detachEvent) {
obj.detachEvent("on"+type, fn);
};
} catch(err) {};
};
function stopEvent(e) {
e = e || document.parentWindow.event;
if(e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
};
if(oldIE) {
e.cancelBubble = true;
e.returnValue = false;
};
return false;
};
function setARIARole(element, role) {
if(element && element.tagName) {
element.setAttribute("role", role);
};
};
function setARIAProperty(element, property, value) {
if(element && element.tagName) {
element.setAttribute("aria-" + property, value);
};
};
// Sets a tabindex attribute on an element, bends over for IE.
function setTabIndex(e, i) {
e.setAttribute(oldIE ? "tabIndex" : "tabindex", i);
e.tabIndex = i;
};
function dateToYYYYMMDD(dt) {
return dt instanceof Date && !isNaN(dt) ? dt.getFullYear() + pad(dt.getMonth() + 1) + "" + pad(dt.getDate()) : dt;
};
// The datePicker object itself
function datePicker(options) {
this.dateSet = null;
this.timerSet = false;
this.visible = false;
this.fadeTimer = null;
this.timer = null;
this.yearInc = 0;
this.monthInc = 0;
this.dayInc = 0;
this.mx = 0;
this.my = 0;
this.x = 0;
this.y = 0;
this.created = false;
this.disabled = false;
this.opacity = 0;
this.opacityTo = 100;
this.finalOpacity = 100;
this.inUpdate = false;
this.kbEventsAdded = false;
this.fullCreate = false;
this.selectedTD = null;
this.cursorTD = null;
this.cursorDate = options.cursorDate ? options.cursorDate : "",
this.date = options.cursorDate ? new Date(+options.cursorDate.substr(0,4), +options.cursorDate.substr(4,2) - 1, +options.cursorDate.substr(6,2),5,0,0) : new Date();
this.defaults = {};
this.dynDisabledDates = {};
this.dateList = [];
this.bespokeClass = options.bespokeClass;
this.firstDayOfWeek = localeImport.firstDayOfWeek;
this.interval = new Date();
this.clickActivated = false;
this.showCursor = false;
this.noFocus = true;
this.kbEvent = false;
this.delayedUpdate = false;
this.bespokeTitles = {};
this.bespokeTabIndex = options.bespokeTabIndex;
for(var thing in options) {
if(!options.hasOwnProperty(thing) || String(thing).search(/^(callbacks|formElements|enabledDates|disabledDates)$/) != -1) {
continue;
};
this[thing] = options[thing];
};
if(oldIE) {
this.iePopUp = null;
};
for(var i = 0, prop; prop = ["callbacks", "formElements"][i]; i++) {
this[prop] = {};
if(prop in options) {
for(thing in options[prop]) {
if(options[prop].hasOwnProperty(thing)) {
this[prop][thing] = options[prop][thing];
};
};
};
};
// Adjust time to stop daylight savings madness on windows
this.date.setHours(5);
// Called from an associated form elements onchange event
this.changeHandler = function() {
// In a perfect world this shouldn't ever happen
if(o.disabled) {
return;
};
o.setDateFromInput();
o.callback("dateset", o.createCbArgObj());
};
// Creates the object passed to the callback functions
this.createCbArgObj = function() {
return this.dateSet ? {
"id" :this.id,
"date" :this.dateSet,
"dd" :pad(this.date.getDate()),
"mm" :pad(this.date.getMonth() + 1),
"yyyy" :this.date.getFullYear()
} : {
"id" :this.id,
"date" :null,
"dd" :null,
"mm" :null,
"yyyy" :null
};
};
// Attempts to grab the window scroll offsets
this.getScrollOffsets = function() {
if(typeof(window.pageYOffset) == 'number') {
//Netscape compliant
return [window.pageXOffset, window.pageYOffset];
} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
//DOM compliant
return [document.body.scrollLeft, document.body.scrollTop];
} else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
//IE6 standards compliant mode
return [document.documentElement.scrollLeft, document.documentElement.scrollTop];
};
return [0,0];
};
// Calculates the current list of disabled & enabled dates for a specific year/month
this.getDateExceptions = function(y, m) {
m = pad(m);
var obj = {},
lower = o.firstDateShown,
upper = o.lastDateShown,
rLength = o.dateList.length,
rNumber, workingDt, workingY, workingM, dtLower, dtUpper, i, dt, dt1, dt2, rngLower, rngUpper, cDate;
if(!upper || !lower) {
lower = o.firstDateShown = y + pad(m) + "01";
upper = o.lastDateShown = y + pad(m) + pad(daysInMonth(m, y));
};
dtLower = Number(lower.substr(0,6));
dtUpper = Number(upper.substr(0,6));
workingDt = String(dtLower);
while(+workingDt <= dtUpper) {
workingY = workingDt.substr(0,4);
workingM = workingDt.substr(4,2);
for(rNumber = 0; rNumber < rLength; rNumber++) {
dt1 = String(o.dateList[rNumber].rLow).replace(/^(\*\*\*\*)/, workingY).replace(/^(\d\d\d\d)(\*\*)/, "$1"+workingM);
dt2 = String(o.dateList[rNumber].rHigh).replace(/^(\*\*\*\*)/, workingY).replace(/^(\d\d\d\d)(\*\*)/, "$1"+workingM);
// Single date
if(dt2 == 1) {
if(+dt1 >= +o.firstDateShown && +dt1 <= +o.lastDateShown) {
obj[dt1] = o.dateList[rNumber].type;
};
continue;
};
// Date Range
if(dt1 <= dt2
&&
workingDt >= dt1.substr(0,6)
&&
workingDt <= dt2.substr(0,6)
) {
rngLower = Math.max(dt1, Math.max(String(workingDt) + "01", this.firstDateShown));
rngUpper = Math.min(dt2, Math.min(String(workingDt) + "31", this.lastDateShown));
for(i = rngLower; i <= rngUpper; i++) {
obj[i] = o.dateList[rNumber].type;
};
};
};
// Let the Date Object take care of month overflowss
workingDt = new Date(workingY, +workingM, 2);
workingDt = workingDt.getFullYear()+""+pad(workingDt.getMonth()+1);
};
return obj;
};
// Repositions the datepicker beside the button - to the bottom by
// preference but to the top if there is not enough room to display the
// entire U.I. at the bottom (it really should be updated to favour
// bottom positioning if not enough room to display the entire U.I. at
// the top in that scenario though)
this.reposition = function() {
if(!o.created || o.staticPos) {
return;
};
o.div.style.visibility = "hidden";
o.div.style.left = o.div.style.top = "0px";
o.div.style.display = "block";
var osh = o.div.offsetHeight,
osw = o.div.offsetWidth,
elem = document.getElementById('fd-but-' + o.id),
pos = o.truePosition(elem),
trueBody = (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body,
sOffsets = o.getScrollOffsets(),
scrollTop = sOffsets[1],
scrollLeft = sOffsets[0],
tSpace = parseInt(pos[1] - 2) - parseInt(scrollTop),
bSpace = parseInt(trueBody.clientHeight + scrollTop) - parseInt(pos[1] + elem.offsetHeight + 2);
o.div.style.visibility = "visible";
o.div.style.left = Number(parseInt(trueBody.clientWidth+scrollLeft) < parseInt(osw+pos[0]) ? Math.abs(parseInt((trueBody.clientWidth+scrollLeft) - osw)) : pos[0]) + "px";
o.div.style.top = (bSpace > tSpace) ? Math.abs(parseInt(pos[1] + elem.offsetHeight + 2)) + "px" : Math.abs(parseInt(pos[1] - (osh + 2))) + "px";
if(oldIE === 6) {
o.iePopUp.style.top = o.div.style.top;
o.iePopUp.style.left = o.div.style.left;
o.iePopUp.style.width = osw + "px";
o.iePopUp.style.height = (osh - 2) + "px";
};
};
this.removeCursorHighlight = function() {
var td = document.getElementById(o.id + "-date-picker-hover");
if(td) {
removeClass(td, "date-picker-hover");
};
};
this.addCursorHighlight = function() {
var td = document.getElementById(o.id + "-date-picker-hover");
if(td) {
addClass(td, "date-picker-hover");
};
};
// Resets the tabindex of the previously focused cell
this.removeOldFocus = function() {
var td = document.getElementById(o.id + "-date-picker-hover");
if(td) {
try {
setTabIndex(td, -1);
removeClass(td, "date-picker-hover");
td.id = "";
td.onblur = null;
td.onfocus = null;
} catch(err) {};
};
};
// Sets the tabindex & focus on the currently highlighted cell
this.setNewFocus = function() {
var td = document.getElementById(o.id + "-date-picker-hover");
if(td) {
try {
setTabIndex(td, 0);
if(this.showCursor) {
addClass(td, "date-picker-hover");
};
// If opened with the keyboard then add focus & blur events to the cell
if(!this.clickActivated) {
td.onblur = o.onblur;
td.onfocus = o.onfocus;
};
// If opened with the keyboard (and not in opera) then add a screen-reader friendly date format
if(!isOpera && !this.clickActivated) {
o.addAccessibleDate();
};
// Try to programmatically set focus on the cell
if(!this.noFocus && !this.clickActivated) {
setTimeout(function() { try { td.focus(); } catch(err) {}; }, 0);
};
} catch(err) { };
};
};
// Adds a screen-reader friendly date to the current cell whenever
// the datepicker has been opened with the keyboard
this.addAccessibleDate = function() {
var td = document.getElementById(o.id + "-date-picker-hover");
if(td && !(td.getElementsByTagName("span").length)) {
var ymd = td.className.match(/cd-([\d]{4})([\d]{2})([\d]{2})/),
noS = td.className.search(noSelectionRegExp) != -1,
spn = document.createElement('span'),
spnC;
spn.className = "fd-screen-reader";
removeChildNodes(td);
if(noS) {
spnC = spn.cloneNode(false);
spnC.appendChild(document.createTextNode(getTitleTranslation(13)));
td.appendChild(spnC);
};
for(var pt = 0, part; part = formatParts[pt]; pt++) {
if(part == "%j" || part == "%d") {
td.appendChild(document.createTextNode(printFormattedDate(new Date(ymd[1], +ymd[2]-1, ymd[3], 5, 0, 0), part, true)));
} else {
spnC = spn.cloneNode(false);
spnC.appendChild(document.createTextNode(printFormattedDate(new Date(ymd[1], +ymd[2]-1, ymd[3], 5, 0, 0), part, true)));
td.appendChild(spnC);
};
};
};
};
// Sets the current cursor to a specific date
this.setCursorDate = function(yyyymmdd) {
if(String(yyyymmdd).search(/^([0-9]{8})$/) != -1) {
this.date = new Date(+yyyymmdd.substr(0,4), +yyyymmdd.substr(4,2) - 1, +yyyymmdd.substr(6,2), 5, 0, 0);
this.cursorDate = yyyymmdd;
if(this.staticPos) {
this.updateTable();
};
};
};
// Updates the table used to display the datepicker
this.updateTable = function(noCallback) {
if(!o || o.inUpdate || !o.created) {
return;
};
// We are currently updating (used to stop public methods from firing)
o.inUpdate = true;
// Remove the focus from the currently highlighted cell
o.removeOldFocus();
o.div.dir = localeImport.rtl ? "rtl" : "ltr";
// If the update timer initiated
if(o.timerSet && !o.delayedUpdate) {
// Are we incrementing/decrementing the month
if(o.monthInc) {
var n = o.date.getDate(),
d = new Date(o.date);
d.setDate(2);
d.setMonth(d.getMonth() + o.monthInc * 1);
// Don't go over the days in the month
d.setDate(Math.min(n, daysInMonth(d.getMonth(),d.getFullYear())));
o.date = new Date(d);
} else {
o.date.setDate(Math.min(o.date.getDate()+o.dayInc, daysInMonth(o.date.getMonth()+o.monthInc,o.date.getFullYear()+o.yearInc)));
o.date.setMonth(o.date.getMonth() + o.monthInc);
o.date.setFullYear(o.date.getFullYear() + o.yearInc);
};
};
// Make sure the internal date is within range
o.outOfRange();
// Disable/enable the today button
if(!o.noToday) {
o.disableTodayButton();
};
// Disable/enable the month & year buttons
o.showHideButtons(o.date);
var cd = o.date.getDate(),
cm = o.date.getMonth(),
cy = o.date.getFullYear(),
cursorDate = (String(cy) + pad(cm+1) + pad(cd)),
tmpDate = new Date(cy, cm, 1, 5, 0, 0);
tmpDate.setHours(5);
var dt, dts, cName, row, td, i, currentDate, cellAdded, col, currentStub, abbr, bespokeRenderClass, spnC, dateSetD, selectable, weekDay,
// Weekday of the fist of the month
weekDayC = (tmpDate.getDay() + 6) % 7,
// The column index this weekday will occupy
firstColIndex = (((weekDayC - o.firstDayOfWeek) + 7 ) % 7) - 1,
// The number of days in the current month
dpm = daysInMonth(cm, cy),
// Today as a Date Object
today = new Date(),
// Today as a YYYYMMDD String
today = today.getFullYear() + pad(today.getMonth()+1) + pad(today.getDate()),
// A Sring date stub in a YYYYMM format for the current date
stub = String(tmpDate.getFullYear()) + pad(tmpDate.getMonth()+1),
//
cellAdded = [4,4,4,4,4,4],
// The first day of the previous month as a Date Object
lm = new Date(cy, cm-1, 1, 5, 0, 0),
// The first day of the next month as a Date Object
nm = new Date(cy, cm+1, 1, 5, 0, 0),
// The number of days in the previous month
daySub = daysInMonth(lm.getMonth(), lm.getFullYear()),
// YYYYMM String date stub for the next month
stubN = String(nm.getFullYear()) + pad(nm.getMonth()+1),
// YYYYMM String date stub for the previous month
stubP = String(lm.getFullYear()) + pad(lm.getMonth()+1),
weekDayN = (nm.getDay() + 6) % 7,
weekDayP = (lm.getDay() + 6) % 7,
// A SPAN node to clone when adding dates to individual cells
spn = document.createElement('span');
// Give the "fd-screen-reader" class to the span in order to hide them in the UI
// but keep them accessible to screen-readers
spn.className = "fd-screen-reader";
// The first & last dates shown on the datepicker UI - could be a date from the previous & next month respectively
o.firstDateShown = !o.constrainSelection && o.fillGrid && (0 - firstColIndex < 1) ? String(stubP) + (daySub + (0 - firstColIndex)) : stub + "01";
o.lastDateShown = !o.constrainSelection && o.fillGrid ? stubN + pad(41 - firstColIndex - dpm) : stub + String(dpm);
// Store a reference to the current YYYYMM String representation of the current month
o.currentYYYYMM = stub;
bespokeRenderClass = o.callback("redraw", {id:o.id, dd:pad(cd), mm:pad(cm+1), yyyy:cy, firstDateDisplayed:o.firstDateShown, lastDateDisplayed:o.lastDateShown}) || {};
// An Object of dates that have been explicitly disabled (1) or enabled (0)
dts = o.getDateExceptions(cy, cm+1);
// Double check current date within limits etc
o.checkSelectedDate();
//
dateSetD = (o.dateSet != null) ? o.dateSet.getFullYear() + pad(o.dateSet.getMonth()+1) + pad(o.dateSet.getDate()) : false;
// If we have selected a date then set its ARIA selected property
// to false. We then set the ARIA selected property to true on the
// newly selected cell after redrawing the table
if(this.selectedTD != null) {
setARIAProperty(this.selectedTD, "selected", false);
this.selectedTD = null;
};
// Redraw all of the table cells representing the date parts of the UI
for(var curr = 0; curr < 42; curr++) {
// Current row
row = Math.floor(curr / 7);
// Current TD node
td = o.tds[curr];
// Clone our SPAN node
spnC = spn.cloneNode(false);
// Remove any previous contents from the cell
removeChildNodes(td);
// If the current cell contains a date
if((curr > firstColIndex && curr <= (firstColIndex + dpm)) || o.fillGrid) {
currentStub = stub;
weekDay = weekDayC;
dt = curr - firstColIndex;
cName = [];
selectable = true;
// Are we drawing last month
if(dt < 1) {
dt = daySub + dt;
currentStub = stubP;
weekDay = weekDayP;
selectable = !o.constrainSelection;
cName.push("month-out");
// Are we drawing next month
} else if(dt > dpm) {
dt -= dpm;
currentStub = stubN;
weekDay = weekDayN;
selectable = !o.constrainSelection;
cName.push("month-out");
};
// Calcuate this cells weekday
weekDay = (weekDay + dt + 6) % 7;
// Push a classname representing the weekday e.g. "day-3"
cName.push("day-" + weekDay + " cell-" + curr);
// A YYYYMMDD String representation of this cells date
currentDate = currentStub + String(dt < 10 ? "0" : "") + dt;
// If this cells date is out of range
if(o.rangeLow && +currentDate < +o.rangeLow || o.rangeHigh && +currentDate > +o.rangeHigh) {
// Add a classname to style the cell and stop selection
td.className = "out-of-range";
// Reset this TD nodes title attribute
td.title = "";
// Append the cells date as a text node to the TD
td.appendChild(document.createTextNode(dt));
// Jaysus, what the feck does this line do again...
if(o.showWeeks) {
cellAdded[row] = Math.min(cellAdded[row], 2);
};
// This cells date is within the lower & upper ranges (or no ranges have been defined)
} else {
// If it's a date from last or next month and the "constrainSelection" option
// is false then give the cell a CD-YYYYMMDD class
if(selectable) {
td.title = titleFormat ? printFormattedDate(new Date(+String(currentStub).substr(0,4), +String(currentStub).substr(4, 2) - 1, +dt, 5, 0, 0), titleFormat, true) : "";
cName.push("cd-" + currentDate + " yyyymmdd-" + currentDate + " yyyymm-" + currentStub + " mmdd-" + currentStub.substr(4,2) + pad(dt));
// Otherwise give a "not-selectable" class (which shouldn't be styled in any way, it's for internal use)
} else {
td.title = titleFormat ? getTitleTranslation(13) + " " + printFormattedDate(new Date(+String(currentStub).substr(0,4), +String(currentStub).substr(4, 2) - 1, +dt, 5, 0, 0), titleFormat, true) : "";
cName.push("yyyymmdd-" + currentDate + " yyyymm-" + currentStub + " mmdd-" + currentStub.substr(4,2) + pad(dt) + " not-selectable");
};
// Add a classname if the current cells date is today
if(currentDate == today) {
cName.push("date-picker-today");
};
// If this cell represents the currently selected date
if(dateSetD == currentDate) {
// Add a classname (for styling purposes)
cName.push("date-picker-selected-date");
// Set the ARIA selected property to true
setARIAProperty(td, "selected", "true");
// And cache a reference to the current cell
this.selectedTD = td;
};
// If the current cell has been explicitly disabled
if(((currentDate in dts) && dts[currentDate] == 1)
// or
||
// ... the current weekday has been disabled
(o.disabledDays[weekDay]
&&
// ... and the current date has not been explicitly enabled
!((currentDate in dts) && dts[currentDate] == 0)
)
) {
// Add a classname to style the cell and stop selection
cName.push("day-disabled");
// Update the current cells title to say "Disabled date: ..." (or whatever the translation says)
if(titleFormat && selectable) {
td.title = getTitleTranslation(13) + " " + td.title;
};
};
// Has the redraw callback given us a bespoke classname to add to this cell
if(currentDate in bespokeRenderClass) {
cName.push(bespokeRenderClass[currentDate]);
};
// Do we need to highlight this cells weekday representation
if(o.highlightDays[weekDay]) {
cName.push("date-picker-highlight");
};
// Is the current onscreen cursor set to this cells date
if(cursorDate == currentDate) {
td.id = o.id + "-date-picker-hover";
};
// Add the date to the TD cell as a text node. Note: If the datepicker has been given keyboard
// events, this textnode is replaced by a more screen-reader friendly date during the focus event
td.appendChild(document.createTextNode(dt));
// Add the classnames to the TD node
td.className = cName.join(" ");
// If the UI displays week numbers then update the celladded
if(o.showWeeks) {
cellAdded[row] = Math.min(cName[0] == "month-out" ? 3 : 1, cellAdded[row]);
};
};
// The current TD node is empty i.e. represents no date in the UI
} else {
// Add a classname to style the cell
td.className = "date-picker-unused";
// Add a non-breaking space to unused TD node (for IEs benefit mostly)
td.appendChild(document.createTextNode(nbsp));
// Reset the TD nodes title attribute
td.title = "";
};
// Do we update the week number for this row
if(o.showWeeks && curr - (row * 7) == 6) {
removeChildNodes(o.wkThs[row]);
o.wkThs[row].appendChild(document.createTextNode(cellAdded[row] == 4 && !o.fillGrid ? nbsp : getWeekNumber(cy, cm, curr - firstColIndex - 6)));
o.wkThs[row].className = "date-picker-week-header" + (["",""," out-of-range"," month-out",""][cellAdded[row]]);
};
};
// Update the UI title bar displaying the year & month
var span = o.titleBar.getElementsByTagName("span");
removeChildNodes(span[0]);
removeChildNodes(span[1]);
span[0].appendChild(document.createTextNode(getMonthTranslation(cm, false) + nbsp));
span[1].appendChild(document.createTextNode(cy));
// If we are in an animation
if(o.timerSet) {
// Speed the timer up a little bit to make the pause between updates quicker
o.timerInc = 50 + Math.round(((o.timerInc - 50) / 1.8));
// Recall this function in a timeout
o.timer = window.setTimeout(o.updateTable, o.timerInc);
};
// We are not currently updating the UI
o.inUpdate = o.delayedUpdate = false;
// Focus on the correct TD node
o.setNewFocus();
};
// Removes all scaffold from the DOM & events from memory
this.destroy = function() {
// Remove the button if it exists
if(document.getElementById("fd-but-" + this.id)) {
document.getElementById("fd-but-" + this.id).parentNode.removeChild(document.getElementById("fd-but-" + this.id));
};
if(!this.created) {
return;
};
// Event cleanup for Internet Explorers benefit
removeEvent(this.table, "mousedown", o.onmousedown);
removeEvent(this.table, "mouseover", o.onmouseover);
removeEvent(this.table, "mouseout", o.onmouseout);
removeEvent(document, "mousedown", o.onmousedown);
removeEvent(document, "mouseup", o.clearTimer);
if (window.addEventListener && !window.devicePixelRatio) {
try {