-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatePicker.js
More file actions
824 lines (605 loc) · 23.6 KB
/
Copy pathDatePicker.js
File metadata and controls
824 lines (605 loc) · 23.6 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
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** DatePicker Class
//******************************************************************************
/**
* Calendar component used select dates
*
******************************************************************************/
javaxt.dhtml.DatePicker = function(parent, config) {
this.className = "javaxt.dhtml.DatePicker";
var me = this;
var defaultConfig = {
/** Used to set the initial view. All we need is a month and a year.
*/
date: new Date(),
/** Used to set the selection mode. Options are "day" or "week".
*/
selectionMode: "day",
/** Day names or abbreviations to use in the column headers
*/
daysOfWeek: ["S","M","T","W","T","F","S"],
/** Month names or abbreviations used in the header
*/
months : ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
],
/** If true, allows users to deselect the current selection via mouse
* click
*/
allowDeselect: true,
/** Style for individual elements within the component. Note that you can
* provide CSS class names instead of individual style definitions.
*/
style: {
//Panel Style
panel: {
fontFamily: "helvetica,arial,verdana,sans-serif",
backgroundColor: "#ffffff",
border: "1px solid #b4cbdd",
display: "inline-block"
},
header: {
backgroundColor: "#d9e7f8",
height: "25px",
lineHeight: "25px"
},
//Title area
title: {
position: "absolute",
width: "100%",
whiteSpace: "nowrap",
//fontFamily: "helvetica,arial,verdana,sans-serif",
fontSize: "14px",
fontWeight: "bold",
color: "#555555",
textAlign: "center",
cursor: "default"
},
next: {
float: "right",
borderRight: "2px solid #5e8be0",
borderBottom: "2px solid #5e8be0",
width: "7px",
height: "7px",
transform: "rotate(-45deg)",
margin: "7px 10px 0 0",
cursor: "pointer"
},
back: {
float: "left",
borderRight: "2px solid #5e8be0",
borderBottom: "2px solid #5e8be0",
width: "7px",
height: "7px",
transform: "rotate(135deg)",
margin: "7px 0 0 10px",
cursor: "pointer"
},
cell: {
width: "18px",
height: "18px",
lineHeight: "18px",
textAlign: "right",
padding: "2px 4px 1px 0px",
fontSize: "11px",
color: "#000000",
cursor: "pointer",
border: "1px solid #ffffff",
margin: "1px"
},
cellHeader: { //overrides cell style for header cells
color: "#233d6d",
fontSize: "10px",
lineHeight: "10px",
paddingBottom: "0px",
paddingTop: "0px",
border: "0px",
borderTop: "1px solid #bbccff",
borderBottom: "1px solid #bbccff"
},
previousMonth: { //overrides cell style for previous month
color: "#aaaaaa"
},
nextMonth: { //overrides cell style for next month
color: "#aaaaaa"
},
today: {
width: "22px",
height: "21px",
border: "1px solid #FF7373",
top: "-1px",
left: "-1px"
},
selectedRow: {
},
selectedCell: {
color: "#000000",
fontWeight: "bold",
backgroundColor: "#fff4bf",
border: "1px solid #bfa52f"
}
}
};
var currDate;
var startDate;
var mainDiv;
var todayHighlightDiv;
var cells = [];
var selectionMode;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class. */
var init = function(){
if (typeof parent === "string"){
parent = document.getElementById(parent);
}
if (!parent) return;
//Clone the config so we don't modify the original config object
var clone = {};
merge(clone, config);
//Merge clone with default config
merge(clone, defaultConfig);
config = clone;
//Update style config keyword for legacy apps
if (config.style.selected && !config.style.selectedCell){
config.style.selectedCell = config.style.selected;
}
//Get selection mode
selectionMode = config.selectionMode;
//Create container
mainDiv = createElement('div', parent, config.style.panel);
mainDiv.style.display = "table";
me.el = mainDiv;
//Disable text selection
mainDiv.unselectable="on";
mainDiv.onselectstart=function(){return false;};
mainDiv.onmousedown=function(){return false;};
//Create highlight div
todayHighlightDiv = createElement('div', config.style.today);
todayHighlightDiv.style.position = "absolute";
//Render month
me.setDate(config.date);
};
//**************************************************************************
//** setDate
//**************************************************************************
/** Used to update the calendar and select the given date.
*/
this.setDate = function(date){
date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
//Render date
me.render(date);
//Deselect current selection
me.deselect();
//Select date
for (var i=0; i<cells.length; i++){
var d = cells[i].date;
if (d.getTime()===date.getTime()){
select(cells[i]);
return;
}
}
};
//**************************************************************************
//** render
//**************************************************************************
/** Used to update the calendar and render a given date.
*/
this.render = function(date){
date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
if (currDate && date.getTime()===currDate.getTime()) return;
//Compute date range
var range = computeRange(date);
//Check whether we should render a new calendar
if (startDate){
if (startDate.getTime()===range.startDate.getTime()){
//No need to re-render the calendar. Fire the onUpdate event
//as needed and exit.
if (currDate.getTime()!==date.getTime()){
currDate = date;
me.onUpdate(new Date(currDate));
}
return;
}
}
//Set variables
currDate = date;
startDate = range.startDate;
var currMonth = currDate.getMonth();
var numWeeks = range.numWeeks;
//Clear the current content
mainDiv.innerHTML = "";
//Create header
var headerDiv = createElement('div', mainDiv, config.style.header);
headerDiv.style.position = "relative";
var titleDiv = createElement('div', headerDiv, config.style.title);
titleDiv.innerHTML = config.months[currMonth] + " " + currDate.getFullYear();
titleDiv.onclick = function(e){
me.onHeaderClick(headerDiv, e);
};
var backDiv = createElement('div', headerDiv, config.style.back);
backDiv.onclick = function(e){
e.preventDefault();
e.stopPropagation();
me.back();
};
var nextDiv = createElement('div', headerDiv, config.style.next);
nextDiv.onclick = function(e){
e.preventDefault();
e.stopPropagation();
me.next();
};
//Create main table
var table = createTable(mainDiv);
table.style.fontFamily = "inherit";
table.style.textAlign = "inherit";
table.style.color = "inherit";
//Add cell headers
var tr = table.addRow();
for (var i=0; i<7; i++){
var td = tr.addColumn();
td.style.fontFamily = "inherit";
td.innerHTML = config.daysOfWeek[i];
addStyle(td, "cell");
addStyle(td, "cellHeader");
}
//Add cells
cells = [];
var d = new Date(startDate);
for (var i=0; i<numWeeks; i++){
tr = table.addRow();
for (var j=0; j<7; j++){
//Create new column
var td = tr.addColumn();
td.style.fontFamily = "inherit";
//Create cell
var cell = createElement('div', td);
cell.style.position = "relative";
cell.style.fontFamily = "inherit";
cell.date = new Date(d);
cell.selected = false;
cells.push(cell);
//Create div inside the cell to render the date
var div1 = createElement('div', cell);
div1.style.fontFamily = "inherit";
div1.innerHTML = d.getDate();
//Set style
addStyle(cell, "cell");
//Update style if prev or next month
var month = d.getMonth();
var monthOffset = 0;
if (month!==currMonth){
if (i===0){
monthOffset = -1;
addStyle(cell, "previousMonth");
}
else{
monthOffset = 1;
addStyle(cell, "nextMonth");
}
}
cell.monthOffset = monthOffset;
//On click event
cell.onclick = function(e){
var cell = this;
//Select cell(s)
var dateRange = select(cell);
//Fire onClick event
me.onClick(new Date(cell.date), dateRange);
};
d.setDate(d.getDate()+1);
}
}
//Highlight today's date
highlightTodaysDate();
//Create a task to highlight today's date at midnight
function scheduledTask() {
var now = new Date();
var night = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1, // the next day, ...
0, 0, 0 // ...at 00:00:00 hours
);
var msToMidnight = night.getTime() - now.getTime();
setTimeout(function() {
//Highlight Today's Date
highlightTodaysDate();
//Re-run the scheduled task for the next day
scheduledTask();
}, msToMidnight);
}
scheduledTask();
me.onUpdate(new Date(currDate));
};
//**************************************************************************
//** highlightTodaysDate
//**************************************************************************
/** Used to highlight today's date.
*/
var highlightTodaysDate = function(){
if (todayHighlightDiv.parentNode){
var cell = todayHighlightDiv.parentNode;
cell.removeChild(todayHighlightDiv);
}
var now = new Date();
for (var x=0; x<cells.length; x++){
var cell = cells[x];
var d = cell.date;
if (d.getFullYear()===now.getFullYear() &&
d.getMonth()===now.getMonth() &&
d.getDate()===now.getDate())
cell.appendChild(todayHighlightDiv);
}
};
//**************************************************************************
//** select
//**************************************************************************
/** Used to select a cell that corresponds to the given date. If the
* selectionMode is set to week, an entire row will be selected.
*/
this.select = function(date){
if (!date) return;
//Find cell that corresponds with the given date
var cell;
for (var x=0; x<cells.length; x++){
var _cell = cells[x];
var _date = _cell.date;
if (_date.getDate()===date.getDate() &&
_date.getMonth()===date.getMonth() &&
_date.getFullYear()===date.getFullYear()){
cell = _cell;
break;
}
}
//Select the cell
if (cell) select(cell);
};
//**************************************************************************
//** select
//**************************************************************************
/** Used to select a given cell. If the selectionMode is set to week, the
* entire row will be selected. Returns an array of dates that correspond
* to the selected cell(s).
*/
var select = function(cell){
if (selectionMode==="day"){
//Get date
var d = new Date(cell.date);
//Deselect previous selection
for (var x=0; x<cells.length; x++){
var _cell = cells[x];
if (_cell.selected){
//If a user clicked on a selected cell..
if (cell===_cell){
if (config.allowDeselect){
deselect(_cell);
return [];
}
else{
return [d];
}
}
else{
//Deselect previously selected cell
deselect(_cell);
}
break;
}
}
//Highlight cell
cell.selected=true;
addStyle(cell, "selectedCell");
//Return selected date
return [d];
}
else if (selectionMode==="week"){
//Update row style
var parentRow = cell.parentNode.parentNode;
addStyle(parentRow, "selectedRow");
//Get start/end dates for the week
var d1 = new Date(parentRow.childNodes[0].childNodes[0].date);
var d2 = new Date(parentRow.childNodes[parentRow.childNodes.length-1].childNodes[0].date);
//Select row
for (var x=0; x<cells.length; x++){
var _cell = cells[x];
if (_cell.parentNode.parentNode===parentRow){
//If a user clicked on a selected row...
if (_cell.selected){
if (config.allowDeselect){
//Deselect row
for (var y=0; y<cells.length; y++){
if (cells[y].selected) deselect(cells[y]);
}
return [];
}
else{
//Do nothing, the row is already selected
return [d1,d2];
}
}
//Highlight cell
_cell.selected=true;
addStyle(_cell, "selectedCell");
}
else{
//Deselect previously selected row
var row = _cell.parentNode.parentNode;
row.style = '';
row.className = '';
row.removeAttribute("class");
if (_cell.selected) deselect(_cell);
}
}
//Return selected date range
return [d1,d2];
}
else{
return [];
}
};
//**************************************************************************
//** deselect
//**************************************************************************
/** Used to deselect any selected cells in the calendar.
*/
this.deselect = function(){
for (var x=0; x<cells.length; x++){
var _cell = cells[x];
if (_cell.selected){
if (config.allowDeselect){
}
deselect(_cell);
}
}
};
//**************************************************************************
//** deselect
//**************************************************************************
/** Used to deselect a given cell.
*/
var deselect = function(_cell){
_cell.selected=false;
_cell.style = '';
_cell.className = '';
_cell.removeAttribute("class");
_cell.style.position = "relative";
addStyle(_cell, "cell");
switch(_cell.monthOffset){
case -1:
addStyle(_cell, "previousMonth");
break;
case 1:
addStyle(_cell, "nextMonth");
break;
}
};
//**************************************************************************
//** getSelectionMode
//**************************************************************************
this.getSelectionMode = function(){
return selectionMode;
};
//**************************************************************************
//** setSelectionMode
//**************************************************************************
this.setSelectionMode = function(str){
selectionMode = str;
};
//**************************************************************************
//** getSelectedCells
//**************************************************************************
this.getSelectedCells = function(){
var selectedCells = [];
for (var x=0; x<cells.length; x++){
var cell = cells[x];
if (cell.selected) selectedCells.push(cell);
}
return selectedCells;
};
//**************************************************************************
//** onClick
//**************************************************************************
/** Called when a cell in the calendar is clicked.
* @param date Date associated with the cell.
* @param dateRange An array representing the selected date range. If the
* selection mode is set to "week", the array will contain two entries: one
* for the start date and one for the end date. Otherwise, the array will
* contain only one entry.
*/
this.onClick = function(date, dateRange){};
//**************************************************************************
//** onHeaderClick
//**************************************************************************
/** Called when the calendar title is clicked. Typically used to render
* a month and year selection menu.
*/
this.onHeaderClick = function(headerDiv, e){};
//**************************************************************************
//** onUpdate
//**************************************************************************
/** Called when the date picker is first rendered or whenever the month is
* changed.
*/
this.onUpdate = function(date){};
//**************************************************************************
//** back
//**************************************************************************
/** Used to render the previous month.
*/
this.back = function(){
var m = currDate.getMonth()-1;
var d = currDate.getDate();
var y = currDate.getFullYear();
var x = new Date(y, m+1, 0).getDate();
if (d>x) console.log(d + " vs " + x);
if (d>x) d = x;
me.render(new Date(y, m, d));
};
//**************************************************************************
//** next
//**************************************************************************
/** Used to render the next month.
*/
this.next = function(){
var m = currDate.getMonth()+1;
var d = currDate.getDate();
var y = currDate.getFullYear();
var x = new Date(y, m+1, 0).getDate();
if (d>x) console.log(d + " vs " + x);
if (d>x) d = x;
me.render(new Date(y, m, d));
};
//**************************************************************************
//** getMonth
//**************************************************************************
/** Returns the month rendered in the date picker (0-11)
*/
this.getMonth = function(){
return currDate.getMonth();
};
//**************************************************************************
//** getYear
//**************************************************************************
/** Returns the year rendered in the date picker.
*/
this.getYear = function(){
return currDate.getFullYear();
};
//**************************************************************************
//** computeRange
//**************************************************************************
/** Computes several key variables used to render the calndar including
* start/end date and the total number of rows to render. Credit:
* http://stackoverflow.com/a/2485172
*/
var computeRange = function(d){
var year = d.getFullYear();
var month = d.getMonth()+1;
var firstOfMonth = new Date(year, month-1, 1);
var lastOfMonth = new Date(year, month, 0);
var numWeeks = Math.ceil( (firstOfMonth.getDay() + lastOfMonth.getDate()) / 7);
var startDate = new Date(firstOfMonth);
startDate.setDate(startDate.getDate()-firstOfMonth.getDay());
var endDate = new Date(lastOfMonth);
endDate.setDate(endDate.getDate()+(6-lastOfMonth.getDay()));
return {
numWeeks: numWeeks,
startDate: startDate,
endDate: endDate
};
};
//**************************************************************************
//** Utils
//**************************************************************************
var merge = javaxt.dhtml.utils.merge;
var createElement = javaxt.dhtml.utils.createElement;
var createTable = javaxt.dhtml.utils.createTable;
var addStyle = function(el, style){
javaxt.dhtml.utils.addStyle(el, config.style[style]);
};
init();
};