-
Notifications
You must be signed in to change notification settings - Fork 0
/
justcal.js
1867 lines (1587 loc) · 62.1 KB
/
justcal.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
/**
********************
** justcal.js **
********************
JavaScript Ultimate Slick Tiny Calendar (datetime picker)
Draggable multilingual keyboard-able calendar - date and time - picker
It is easy to use, simple yet powerful
and highly configurable datetime picker
with seconds, AM-PM support, multilingual, keyboard-able
and wide ranged format of input/output date/time.
It requires no extra files - no JavaScript libraries,
no any css or image files, it works just out of the box
(c) beotiger at beotiger.com | [Andrey Tzar]
Email: [email protected] | Web: http://beotiger.com/justcal
License: MIT License
-----------------------------------------------
Please notice: this program was inspired by
the script DateTimePicker_css.js
written by TengYong Ng on 16-Nov-2003 23:19
Website: http://www.rainforestnet.com
Copyright (c) 2003 TengYong Ng
Great thanks to TengYong and other contributors
-----------------------------------------------
**/
/**
version 1.0b
Last modified: 2013-11-25 01:04 UTC+04:00
**/
(function() {
window.justcal = null; // global variable for calendar object
var jcWidget; // widget element
/**
The main function for creating and calling
justCal (datetimepicker) widget
**/
window.justCal = function(element, options)
{
// respect persistent option
if(jcWidget && jcWidget.style.visibility != "hidden"
&& justcal.opts.persistent)
return false;
justcal = new JustCal(element, options);
return true;
}
/** ***********************
JustCal prototype
** **********************/
function JustCal(element, options)
{
// first of all test the given element
// if it is invalid, raise error and do nothing
// element isn't necessary a dom element
// it may be a jQuery object or an element id
if (element.jquery){
// jQuery object was passed
element = element[0];
} else if (typeof element == "string") {
if (/^#.*/.test(element))
// if jQuery user passes #elementId don't break it
element = element.slice(1);
element = document.getElementById(element);
}
if (!element || element.nodeType !== 1)
throw new Error("JustCal: please make sure that you're passing a valid element");
this.Control = element; // our element for DateTime value
this.setNow(); // set current day and time
// Default options
this.opts = {
format: '', // format of date/time
// resembles format of PHP function `date()`
// with some restrictions
// will be fetched from defaultFormat of current lang
// if is not set by user options
value: '', // predefined value for starting date/time
// which will be used instead of the value
// in target element (if it is not an empty string)
showTime: false, // use time in picker
time12Mode: false, // AM/PM availability and 12-hours mode
showSeconds: false, // use time with seconds
showExtraPane: true, // extra panel w. OK/Cancel buttons
navbar: 'both', // type of navigation bar: step, select, both or compact
dateLimit: '', // may be: future, past or an empty string
// (lets us pick dates in the future or in the past only)
fromDate: '', // values in format Y-m-d (e.g. 2010-05-23) (YYYY-MM-DD)
toDate: '', // which define limit of available dates for picking
// by default all dates from fromYear to toYear, or dateLimit's
// are available to be picked
// years could be total numbers or offsets from the current year
// (w. `+` sign for toYear and `-` sign for fromYear)
fromYear: '-12', // offset for the first year in a drop down year selection
toYear: '+7', // offset for the latest year in a drop down year selection
dow: -1, // first day of the week: 0 - Sun, 1 - Mon ... 6 - Sat
weekChars: 2, //number of characters for a week day (Su or Sun, Mo or Mon)
autoTime: true, // auto turn on showTime, time12Mode and showSeconds
// according to output format
onlyTime: false, // use only time picker
closeOnESC: true, // hide widget when ESC key is pressed
persistent: false, // if true, prevents hiding widget on clicking outside it
draggable: true, // allows out widget to be dragged
leftOffset: 12, // left offset relative to current pos, can be negative value
topOffset: -12, // top offset relative to current pos, , can be negative value
lang: 'en', // localization, see this.lang object above for available langs
theme: 'jungle' // theme to use, see this.theme object above for available themes
};
// reset new added options
for (var prop in options)
this.opts[prop] = options[prop];
this.lang = JustCal.getLang(this.opts.lang);
this.theme = JustCal.getTheme(this.opts.theme);
// default text for meridiem
if(!('textAMPM' in this.lang))
this.lang.textAMPM = ['AM','PM'];
if(this.opts.autoTime) {
if(/[gGhHis]/.test(this.opts.format))
this.opts.showTime = true; // force showTime
if(/s/.test(this.opts.format))
this.opts.showSeconds = true; // force showSeconds
if(/[aAgh]/.test(this.opts.format))
this.opts.time12Mode = true; // force time12Mode
}
// get default format of lang if not specified explicitly
if(!this.opts.format)
this.opts.format = this.lang.defaultFormat;
this.opts.dow = +this.opts.dow;
if(this.opts.dow < 0 || this.opts.dow > 6)
if('dow' in this.lang)
this.opts.dow = this.lang.dow;
else
this.opts.dow = 1;
// normalize weekChars
if(this.opts.weekChars < 1)
this.opts.weekChars = 1;
else if(this.opts.weekChars > this.lang.shortWeekdays[0].length)
this.opts.weekChars = this.lang.shortWeekdays[0].length;
// normalize start and end years
if(this.opts.fromYear.toString().charAt(0) == '-')
this.opts.fromYear = this.Year - Number(this.opts.fromYear.toString().slice(1));
if(this.opts.toYear.toString().charAt(0) == '+')
this.opts.toYear = this.Year + Number(this.opts.toYear.toString().slice(1));
// make sure strings are in proper case
this.opts.navbar = this.opts.navbar.toLowerCase();
this.opts.dateLimit = this.opts.dateLimit.toLowerCase();
// type of navigation bar: `step`, `select`, `both` or `compact`
if(!(this.opts.navbar == 'step' || this.opts.navbar == 'select' || this.opts.navbar == 'compact'))
this.opts.navbar = 'both';
// type of dateLimit should be `future`, `past` or an empty string
if(!(this.opts.dateLimit == 'future' || this.opts.dateLimit == 'past'))
this.opts.dateLimit = '';
// allowed months & dates numbers for date limits
// default: allow all months and dates
this.fromMonth = 0; // Jan
this.toMonth = 11; // Dec
var dayFrom = 1,
dayTo = 31;
if(this.opts.dateLimit == 'future') {
this.opts.fromYear = this.today.getFullYear();
this.fromMonth = this.today.getMonth();
dayFrom = this.today.getDate();
}
else if(this.opts.dateLimit == 'past') {
this.opts.toYear = this.today.getFullYear();
this.toMonth = this.today.getMonth();
dayTo = this.today.getDate();
}
// parse fromDate,toDate options
// they should be in YYYY-MM-DD format for year, month, day
if(/^\d{4}.\d{2}.\d{2}$/.test(this.opts.fromDate)) {
this.opts.fromYear = this.opts.fromDate.substring(0,4);
this.fromMonth = this.opts.fromDate.substring(5,7) - 1;
dayFrom = this.opts.fromDate.substring(8);
}
if(/^\d{4}.\d{2}.\d{2}$/.test(this.opts.toDate)) {
this.opts.toYear = this.opts.toDate.substring(0,4);
this.toMonth = this.opts.toDate.substring(5,7) - 1;
dayTo = this.opts.toDate.substring(8);
}
// allowed dates to be picked in widget
this.startDate = new Date(this.opts.fromYear, this.fromMonth, dayFrom).valueOf();
this.finalDate = new Date(this.opts.toYear, this.toMonth, dayTo).valueOf();
// existing date and time
if (this.Control.nodeName.toUpperCase() == 'INPUT' ||
this.Control.nodeName.toUpperCase() == 'TEXTAREA')
this.exDateTime = this.Control.value; // value of textbox or textarea elements
else
this.exDateTime = this.Control.innerHTML;
// or this.Control.textContent || this.Control.innerText; ??
if(this.opts.value)
this.exDateTime = this.opts.value; // redefine fetched value
if (this.exDateTime)
// try to parse it and set date/time respectively
this.formatDateIn(this.exDateTime);
// define and normalize selected date
this.selDate = new Date(this.Year, this.Month, this.Date);
if(this.selDate.valueOf() < this.startDate)
this.selDate = new Date(this.startDate);
if(this.selDate.valueOf() > this.finalDate)
this.selDate = new Date(this.finalDate);
// create and render new picker
this.renderPicker(true);
}
JustCal.prototype = {
// set current date and time
setNow: function() {
this.today = new Date();
this.Date = this.today.getDate();
this.Month = this.today.getMonth();
this.Year = this.today.getFullYear();
this.Hours = this.today.getHours();
if (this.Hours < 10)
this.Hours = "0" + this.Hours;
this.Minutes = this.today.getMinutes();
if (this.Minutes < 10)
this.Minutes = "0" + this.Minutes;
this.Seconds = this.today.getSeconds();
if (this.Seconds < 10)
this.Seconds = "0" + this.Seconds;
if (this.Hours < 12)
this.AMorPM = 0;
else
this.AMorPM = 1;
},
incYear: function ()
{
if (this.Year < this.opts.toYear)
this.Year++;
this.renderPicker();
},
decYear: function ()
{
if (this.Year > this.opts.fromYear)
this.Year--;
this.renderPicker();
},
incMonth: function()
{
if(this.Year == this.opts.toYear
&& this.Month >= this.toMonth)
return;
this.Month++;
if (this.Month >= 12) {
this.Month = 0;
this.incYear();
}
else
this.renderPicker();
},
decMonth: function()
{
if(this.Year == this.opts.fromYear
&& this.Month <= this.fromMonth)
return;
this.Month--;
if (this.Month < 0) {
this.Month = 11;
this.decYear();
}
else
this.renderPicker();
},
switchMonth: function (intMth)
{
this.Month = Number(intMth);
this.renderPicker();
},
switchYear: function (intYear)
{
this.Year = Number(intYear);
this.renderPicker();
},
setHours: function(intHour)
{
var MaxHour = 23,
MinHour = 0;
if (this.opts.time12Mode) {
MaxHour = 12;
MinHour = 1;
}
intHour = Number(intHour);
// if intHour is Not a Number then set it to zero value
if(isNaN(intHour))
intHour = 0;
if (intHour > MaxHour)
intHour = MaxHour;
if (intHour < MinHour)
intHour = MinHour;
if ((this.opts.time12Mode) && (this.AMorPM == 1) && (intHour < 12))
intHour += 12;
else if ((this.opts.time12Mode) && (this.AMorPM == 0) && (intHour == 12))
// 12AM = 00:00?
intHour = 0;
// add leading zero sign
if(intHour < 10)
intHour = '0' + intHour;
this.Hours = intHour;
},
setMinutes: function (intMin)
{
var MaxMin = 59,
MinMin = 0;
intMin = Number(intMin);
// if intMin is Not a Number then set it to zero value
if(isNaN(intMin))
intMin = 0;
if (intMin > MaxMin)
intMin = MaxMin;
else if (intMin < MinMin)
intMin = MinMin;
if (intMin < 10)
intMin = '0' + intMin;
this.Minutes = intMin;
},
setSeconds: function (intSec)
{
var MaxSec = 59,
MinSec = 0;
intSec = Number(intSec);
// if intSec is Not a Number then set it to zero value
if(isNaN(intSec))
intSec = 0;
if (intSec > MaxSec)
intSec = MaxSec;
else if (intSec < MinSec)
intSec = MinSec;
if (intSec < 10)
intSec = '0' + intSec;
this.Seconds = intSec;
},
getShowHour: function(modeAMPM)
{
var finalHour;
if(modeAMPM === undefined)
modeAMPM = this.opts.time12Mode;
// modeAMPM true for 12-mode hours
if (modeAMPM) {
finalHour = Number(this.Hours);
if (finalHour == 0) {
this.AMorPM = 0;
finalHour = 12;
}
else if (finalHour == 12)
this.AMorPM = 1;
else if (finalHour > 12) {
this.AMorPM = 1;
finalHour -= 12;
if(finalHour < 10)
finalHour = '0' + finalHour;
}
else {
this.AMorPM = 0;
if (finalHour < 10)
finalHour = '0' + finalHour;
}
}
else
finalHour = this.Hours;
return finalHour;
},
setAmPm: function (value)
{
this.AMorPM = value & 1;
this.Hours = Number(this.Hours);
if (value == "1") { // PM
if(this.Hours < 12)
this.Hours += 12;
}
else if(this.Hours >= 12) // for AM
this.Hours -= 12;
if(this.Hours < 10)
this.Hours = '0' + this.Hours;
},
// parse AM/PM string for current lang
// and set AM or PM accordingly
findAndSetAmPm: function(val)
{
var l = this.lang.textAMPM[0].length;
if(this.lang.textAMPM[0].toUpperCase() === val.substring(0,l)) {
// this is AM
this.setAmPm(0);
return l;
}
// let us do not validate PM string
this.setAmPm(1);
return this.lang.textAMPM[1].length;
},
// get the name of the month
getMonthName: function (isLong, theMonth)
{
var myMonth;
if(theMonth !== undefined)
myMonth = theMonth;
else
myMonth = this.Month;
myMonth = Number(myMonth);
myMonth = this.lang.monthNames[myMonth];
if (isLong)
return myMonth;
return myMonth.substr(0, 3);
},
// get number of days in a month
getDaysInMonth: function(year,month)
{
if(year === undefined)
year = this.Year;
if(month === undefined)
month = this.Month;
var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// for leap year there are 29 days in Februaury
if (this.isLeapYear(year))
daysInMonth[1] = 29;
return daysInMonth[month];
},
isLeapYear: function (year)
{
if(year === undefined)
year = this.Year;
if ((year % 4) == 0) {
if ((year % 100 == 0) && (year % 400) != 0)
return false;
else
return true;
}
return false;
},
getMonthIndex: function (shortMonthName)
{
shortMonthName = shortMonthName.toUpperCase();
for (var i = 0; i < 12; i++)
if (this.lang.monthNames[i].substring(0, 3)
.toUpperCase() == shortMonthName)
return i;
return 0; // may be `return -1` should be better
},
/**
Search for month name in val
and return its length and index
**/
getFullMonthIndex: function (val)
{
val = val.toUpperCase();
for (var i = 0; i < 12; i++)
if (val.search(this.lang.monthNames[i].toUpperCase()) != -1)
return { len: this.lang.monthNames[i].length, idx: i };
return { len: 1, idx: 0};
},
formatDateOut: function (pDate,pMonth,pYear)
{
var theMonth,
MonthDigit,
YearDigit = pYear || this.Year,
week,
tokens;
// we can't use || because pMounth can be equal to 0
if(pMonth !== undefined)
MonthDigit = pMonth;
else
MonthDigit = this.Month;
pDate = Number(pDate); // avoid prefixing extra '0' for values like '09'
if (pDate < 10)
pDate = "0" + pDate;
theMonth = Number(MonthDigit); // js number of month 0..11 for getMonthName calls
// from JavaScript 0..11 to usual 1..12
// also converts MonthDigit to Number
MonthDigit++;
if (MonthDigit < 10)
MonthDigit = '0' + MonthDigit;
// get the # of a weekday 0..6 (0 - Sunday, 1 - Monday, 2 - Tuesday etc.)
week = new Date(YearDigit, theMonth, pDate).getDay();
tokens = {
// hours in two formats: 0..23 and 1..12
'g': Number(this.getShowHour(true)), // 1..12
'G': Number(this.Hours), // 0..23
'h': this.getShowHour(true), // 01..12
'H': this.Hours, // 00..23
// minutes and seconds in two digits
'i': this.Minutes,
's': this.Seconds,
// day of the week
'w': week, // # of a weekday 0..6
// day
'd': pDate, // day of the month 01..31 (always two digits)
'j': Number(pDate), // day of the month 1..31 (one or two digits)
// month
'n': Number(MonthDigit), // # of a month 1..12
'm': MonthDigit, // # of a month 01..12
// year
'Y': YearDigit, // 4 digits: 2013, 1999 etc.
'y': YearDigit.toString().substr(2,2), // 2 digits: 13, 99 etc.
// AM/PM
'a': this.lang.textAMPM[this.AMorPM].toLowerCase(),// AM|PM lowercased
'A': this.lang.textAMPM[this.AMorPM].toUpperCase(),// AM|PM uppercased
// name of a month
'F': this.getMonthName(true, theMonth), // January, February, March etc.
'M': this.getMonthName(false, theMonth), // Jan, Feb, Mar etc.
// day of the week
'D': this.lang.weekdays[week].substr(0,3), // 3 chars of a day of the week (Sun,Mon,Tue etc.)
'l': this.lang.weekdays[week] // full name of a weekday (Sunday,Monday, etc.)
};
/*
replace all known tokens in format string
with their actual values, leaving other chars untouched
We can not use simple replacing of tokens,
for when it comes to replacing names of months and weeks
it will spawn unwanted tokens,
(e.g. December spawns `D` token in a string)
which will be parsed later in example like this:
var output = this.opts.format;
for(var prop in tokens)
output = output.replace(new RegExp(prop,'g'), tokens[prop]);
*/
var ch, // format char
output = ''; // output string
for(var i = 0, j = this.opts.format.length; i < j; i++) {
ch = this.opts.format.charAt(i);
if(ch in tokens)
output += tokens[ch];
else
output += ch; // unknown token just add to output
}
// console.log('formatDateOut out: ' + output);
return output;
},
/**
Try to analyze input value
and set current date/time respectively
**/
formatDateIn: function (val)
{
var myFormat = this.opts.format,
i = 0, // current position in val string
l, // number of chars to skip from myFormat string
ch, // char in myFormat string
ch1,ch2, // current chars in val
digit,
mObj, // for getting month index and string length
day,month,year;
while(myFormat.length > 0 && val.length > 0) {
// console.log('myFormat = ' + myFormat + '\nval = ' + val);
l = 1;
ch1 = val.charAt(0);
digit = false;
if(val.length > 1) {
ch2 = val.charAt(1);
digit = /\d/.test(ch2); // true if second char is a digit
}
ch = myFormat.charAt(0);
switch(ch) {
case 'a':
case 'A': // AM|PM lowercased or uppercased
l = this.findAndSetAmPm(val.toUpperCase());
break;
// hours in two formats: 0..23 and 1..12
case 'g':
case 'G':
if(digit) { ch1 += ch2; l++; }
this.setHours(ch1); // 1..12
break;
case 'h':
case 'H':
this.setHours(ch1+ch2); // 01..12,00..23
l++;
break;
// minutes and seconds in two digits
case 'i':
this.setMinutes(ch1+ch2);
l++;
break;
case 's':
this.setSeconds(ch1+ch2);
l++;
break;
// day of the week
case 'D': l = 3; break; // just skip 3 chars of a day of the week (Sun,Mon,Tue etc.)
// find the full name of a weekday and skip its length
case 'l': l = this.findWeek(val); break;
case 'w': break; // skip the # of a weekday 0..6
// day
case 'd':
day = ch1 + ch2;
l++;
break;// day of the month 01..31 (always two digits)
case 'j':
if(digit) { ch1+=ch2; l++}; // day of the month 1..31 (one or two digits)
day = ch1;
break;
// month
case 'n':
if(digit) { ch1+=ch2; l++}; // # of the month 1..31 (one or two digits)
month = ch1;
break;
case 'm':
month = ch1 + ch2; // # of a month 01..12
l++;
break;
// year
case 'Y': year = val.substr(0,4); l = 4; break; // 4 digits: 2013, 1999 etc.
case 'y': year = ch1 + ch2; l++; break; // 2 digits: 13, 99 etc.
// name of a month
case 'F': // January, February, March etc.
mObj = this.getFullMonthIndex(val); // index of a month
l = mObj.len; // length
month = mObj.idx + 1; // index
break;
case 'M': // Jan, Feb, Mar etc.
month = this.getMonthIndex(val.substr(0,3)) + 1; // index of a month
l = 3;
break;
default:
break; // just skip unknown token
}
val = val.slice(l); // not 1, but l (like L in lowercase)
myFormat = myFormat.slice(1); // 1, not l
}
// test date for correctness
year = Number(year);
if(year < 10)
year = Number('200'+year);
else if(year < 50)
year = Number('20'+year);
else if(year < 100)
year = Number('19'+year);
if(year.toString().length == 4
&& year >= this.opts.fromYear
&& year <= this.opts.toYear)
this.Year = year;
month--;
if (month >= 0 && month < 12)
this.Month = month;
// day depends on current month and year
day = Number(day);
if (day >= 1 && day <= this.getDaysInMonth())
this.Date = day;
},
/**
Find name of a dayweek in val
and return its length
or 1 if nothing has been found
**/
findWeek: function (val)
{
val = val.toUpperCase();
for(var i = 0; i < 7; i++)
if(val.search(this.lang.weekdays[i].toUpperCase()) != -1)
return this.lang.weekdays[i].length;
return 1; // 1 char to skip
},
selectDate: function (date)
{
this.Date = date;
this.selDate = new Date(this.Year, this.Month, date);
this.renderPicker();
},
// sets datetime for the element
setDateTime: function (pDate,pMonth,pYear)
{
var val = this.formatDateOut(pDate,pMonth,pYear);
if (this.Control.nodeName.toUpperCase() == 'INPUT' ||
this.Control.nodeName.toUpperCase() == 'TEXTAREA') {
this.Control.value = val; // value of a textbox or textarea elements
// don't forget to fire an `onchange` handler
// if it exists and the field has been changed
if(typeof this.Control.onchange == 'function' && this.exDateTime !== val)
this.Control.onchange();
}
else
this.Control.innerHTML = val; // inner text for other elements
/*
We are not using
this.Control.innerText = val;
this.Control.textContent = val;
due to possible HTML layout in format option
*/
this.Control.focus(); // focus target element
this.hide(); // hide picker
},
// hide widget
hide: function() {
jcWidget.style.visibility = 'hidden';
},
// change background color of hover element
highlight: function(element, col, oldBgColor)
{
if (!col) {
element.style.background = this.theme.hoverColor;
element.style.cursor = "pointer";
}
else {
if (oldBgColor)
element.style.background = oldBgColor;
else
element.style.background = this.theme.mainBgColor;
element.style.cursor = "inherit";
}
},
// generate table cell with some value
genCell: function (value, color, clickable)
{
var cell;
value = value || '';
color = color || this.theme.mainBgColor;
if(clickable === undefined)
clickable = true;
if (value) {
if (clickable) {
if (this.opts.showExtraPane) {
cell = "<td style='cursor:pointer;background-color:" + color + ";padding:0;margin:0' onmouseover='justcal.highlight(this, 0)' onmouseout=\"justcal.highlight(this, 1,'" + color + "')\" onmousedown='justcal.selectDate(" + value + ")'>" + value + "</td>"; }
else {
cell = "<td style='cursor:pointer;background-color:" + color + ";padding:0;margin:0' onmouseover='justcal.highlight(this, 0)' onmouseout=\"justcal.highlight(this, 1,'" + color + "')\" onclick=\"justcal.setDateTime('" + value + "')\">" + value + "</td>"; }
}
else
cell = "<td style='background-color:" + color + ";padding:0;margin:0'>"+value+"</td>";
}
else
cell = "<td style='background-color:" + color + ";padding:0;margin:0'> </td>";
return cell;
},
// render new HTML for widget with current year and month
// create jcWidget only first time when called
renderPicker: function(refresh)
{
var html,
curDate,
today,
i,
vDayCount = 0,
vFirstDay,
cell,
selectAm,
selectPm;
html = "<table style='width:100%;padding:0;margin:5px auto 5px auto;font-size:12px;text-align:center;cursor:auto'><tbody>";
html += "<tr><td style='padding:0;margin:0'>\n\n";
if ((this.opts.navbar == "select" || this.opts.navbar == "both") && !this.opts.onlyTime) {
html += "<table style='border:none;width:100%'><tr><td style='padding:0;margin:0;text-align:center'><select onchange='justcal.switchMonth(this.selectedIndex);'>";
for (i = 0; i < 12; i++) {
if (i == this.Month)
selectAm = 'selected';
else
selectAm = '';
html += "<option " + selectAm + " value=" + i + ">" + this.lang.monthNames[i] + "</option>";
}
html += "</select></td>";
html += "<td style='padding:0;margin:0;text-align:center'><select onchange='justcal.switchYear(this.value)'>";
for (i = this.opts.fromYear; i <= this.opts.toYear; i++) {
if (i == this.Year)
selectAm = 'selected';
else
selectAm = '';
html += "<option " + selectAm + " value=" + i + ">" + i + "</option>\n";
}
html += "</select></td></tr></table>\n";
}
if ((this.opts.navbar == "step" || this.opts.navbar == "both") && !this.opts.onlyTime)
{
html += "<table style='border:none;width:100%'><tr><td style='padding:0;margin:0'><span title='" + this.lang.textPrevYear + "' onmousedown='justcal.decYear()' onmouseover='justcal.highlight(this,0)' onmouseout='justcal.highlight(this,1)' style='font-size:14px;color:" + this.theme.cycleColor + "'><<</span></td>";// decrease 1 year
html += "<td style='padding:0;margin:0'><span title='" + this.lang.textPrevMonth + "' onmousedown='justcal.decMonth()' onmouseover='justcal.highlight(this,0)' onmouseout='justcal.highlight(this,1)' style='font-size:14px;color:" + this.theme.cycleColor + "'> < </span></td>\n";// decrease 1 month
html += "<td style='width:70%;font-family:Verdana;font-weight:bold;color:" + this.theme.cycleColor + ";padding:0;margin:0'>" + this.getMonthName(true) + " " + this.Year + "</td>\n"; // month and year
html += "<td style='padding:0;margin:0'><span title='" + this.lang.textNextMonth + "' onmousedown='justcal.incMonth()' onmouseover='justcal.highlight(this,0)' onmouseout='justcal.highlight(this,1)' style='font-size:14px;color:" + this.theme.cycleColor + "'> > </span></td>\n";// increase 1 month
html += "<td style='padding:0;margin:0'><span title='" + this.lang.textNextYear + "' onmousedown='justcal.incYear()' onmouseover='justcal.highlight(this,0)' onmouseout='justcal.highlight(this,1)' style='font-size:14px;color:" + this.theme.cycleColor + "'>>></span></td></tr></table>\n";// increase 1 year
}
if (this.opts.navbar == "compact" && !this.opts.onlyTime) {
html += "<table style='border:none;width:100%'><tr>";
html += "<td style='padding:0;margin:0'><span title='" + this.lang.textPrevMonth + "' onmousedown='justcal.decMonth()' onmouseover='justcal.highlight(this,0)' onmouseout='justcal.highlight(this,1)' style='font-size:14px;color:" + this.theme.cycleColor + "'> < </span></td>\n";// decrease 1 month
html += "<td style='width:70%;font-family:Verdana;font-weight:bold;color:" + this.theme.cycleColor + ";padding:0;margin:0'>" + this.getMonthName(true) + "</td>\n"; // month and year
html += "<td style='padding:0;margin:0'><span title='" + this.lang.textNextMonth + "' onmousedown='justcal.incMonth()' onmouseover='justcal.highlight(this,0)' onmouseout='justcal.highlight(this,1)' style='font-size:14px;color:" + this.theme.cycleColor + "'> > </span></td>\n";// increase 1 month
html += "<td style='padding:0;margin:0;text-align:center'><select onchange='justcal.switchYear(this.value)'>";
for (i = this.opts.fromYear; i <= this.opts.toYear; i++) {
if (i == this.Year)
selectAm = 'selected';
else
selectAm = '';
html += "<option " + selectAm + " value=" + i + ">" + i + "</option>\n";
}
html += "</select></td></tr></table>\n";
}
html += "\n\n</td></tr>\n";
if(!this.opts.onlyTime)
{
// weekday header
html += "<tr><td style='padding:0;margin:0'><table style='font-family:Verdana;border-spacing:1px;border-collapse:separate'><tr>";
var dow,
dowsun = [6,0,1,2,3,4,5]; // for Sunday definition
// swap Sunday and Monday
dow = this.opts.dow;
while (dow--)
this.lang.shortWeekdays
.push(this.lang.shortWeekdays.shift());
for (i = 0; i < 7; i++)
html += "<td style='background-color:" + this.theme.weekHeaderColor + ";width:30px;color:" + this.theme.weekFontColor + ";padding:2px 0;margin:0'>" + this.lang.shortWeekdays[i].substr(0, this.opts.weekChars) + "</td>";
// swap back Sunday and Monday
dow = this.opts.dow;
while (dow--)
this.lang.shortWeekdays
.unshift(this.lang.shortWeekdays.pop());
html += "</tr>\n";
curDate = new Date(this.Year, this.Month, 1);
vFirstDay = curDate.getDay();
// let us do some more arithmetic
dow = this.opts.dow;
if (dow)
while (dow--)
if (--vFirstDay < 0)
vFirstDay = 6;
html += "<tr>";
for (i = 0; i < vFirstDay; i++) {
html += this.genCell();
vDayCount++;
}
// today value without time
today = new Date(this.today.getFullYear(), this.today.getMonth(),
this.today.getDate()).valueOf();
dow = this.opts.dow; // first day of the week
for (i = 1; i <= this.getDaysInMonth(); i++)
{
// value of the rendering day (without time)
curDate = new Date(this.Year, this.Month, i).valueOf();
if ((vDayCount % 7 == 0) && (i > 1))
html += "<tr>";