/
datepicker.js
213 lines (200 loc) · 10 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
// Converts a date into '12-Oct-1984' format
function getDateString(dt) {
return dt.getDate() + '-' +
['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][dt.getMonth()] +
'-' + dt.getFullYear();
}
// Converts a date into 'July 2010' format
function getMonthYearString(dt) {
return ['January','February','March','April','May','June','July',
'August','September','October','November','December'][dt.getMonth()] +
' ' + dt.getFullYear();
}
// This is the function called when the user clicks any button
function chooseDate(e) {
var targ; // Crossbrowser way to find the target (http://www.quirksmode.org/js/events_properties.html)
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
var div = targ.parentNode.parentNode.parentNode.parentNode.parentNode; // Find the div
var idOfTextbox = div.getAttribute('datepickertextbox'); // Get the textbox id which was saved in the div
var textbox = document.getElementById(idOfTextbox); // Find the textbox now
if (targ.value=='<' || targ.value=='>' || targ.value=='<<' || targ.value=='>>') { // Do they want the change the month?
createCalendar(div, new Date(targ.getAttribute('date')));
return;
}
textbox.value = targ.getAttribute('date'); // Set the selected date
div.parentNode.removeChild(div); // Remove the dropdown box now
}
// Parse a date in d-MMM-yyyy format
function parseMyDate(d) {
if (d=="") return new Date('NotADate'); // For Safari
var a = d.split('-');
if (a.length!=3) return new Date(d); // Missing 2 dashes
var m = -1; // Now find the month
if (a[1]=='Jan') m=0;
if (a[1]=='Feb') m=1;
if (a[1]=='Mar') m=2;
if (a[1]=='Apr') m=3;
if (a[1]=='May') m=4;
if (a[1]=='Jun') m=5;
if (a[1]=='Jul') m=6;
if (a[1]=='Aug') m=7;
if (a[1]=='Sep') m=8;
if (a[1]=='Oct') m=9;
if (a[1]=='Nov') m=10;
if (a[1]=='Dec') m=11;
if (m<0) return new Date(d); // Couldn't find the month
return new Date(a[2],m,a[0],0,0,0,0);
}
// This creates the calendar for a given month
function createCalendar(div, month) {
var idOfTextbox = div.getAttribute('datepickertextbox'); // Get the textbox id which was saved in the div
var textbox = document.getElementById(idOfTextbox); // Find the textbox now
var tbl = document.createElement('table');
var topRow = tbl.insertRow(-1);
var td = topRow.insertCell(-1);
var lastYearBn = document.createElement('input');
lastYearBn.type='button'; // Have to immediately set the type due to IE
td.appendChild(lastYearBn);
lastYearBn.value='<<';
lastYearBn.onclick=chooseDate;
lastYearBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()-12,1,0,0,0,0).toString());
var td = topRow.insertCell(-1);
var lastMonthBn = document.createElement('input');
lastMonthBn.type='button'; // Have to immediately set the type due to IE
td.appendChild(lastMonthBn);
lastMonthBn.value='<';
lastMonthBn.onclick=chooseDate;
lastMonthBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()-1,1,0,0,0,0).toString());
var td = topRow.insertCell(-1);
td.colSpan=3;
var mon = document.createElement('input');
mon.type='text';
td.appendChild(mon);
mon.value = getMonthYearString(month);
mon.size=15;
mon.disabled='disabled';
mon.className='monthDsp';
var td = topRow.insertCell(-1);
var nextMonthBn = document.createElement('input');
nextMonthBn.type='button';
td.appendChild(nextMonthBn);
nextMonthBn.value = '>';
nextMonthBn.onclick=chooseDate;
nextMonthBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()+1,1,0,0,0,0).toString());
var td = topRow.insertCell(-1);
var nextYearBn = document.createElement('input');
nextYearBn.type='button'; // Have to immediately set the type due to IE
td.appendChild(nextYearBn);
nextYearBn.value='>>';
nextYearBn.onclick=chooseDate;
nextYearBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()+12,1,0,0,0,0).toString());
var daysRow = tbl.insertRow(-1);
daysRow.insertCell(-1).innerHTML="Mon";
daysRow.insertCell(-1).innerHTML="Tue";
daysRow.insertCell(-1).innerHTML="Wed";
daysRow.insertCell(-1).innerHTML="Thu";
daysRow.insertCell(-1).innerHTML="Fri";
daysRow.insertCell(-1).innerHTML="Sat";
daysRow.insertCell(-1).innerHTML="Sun";
daysRow.className='daysRow';
// Make the calendar
var selected = parseMyDate(textbox.value); // Try parsing the date
var today = new Date();
date = new Date(month.getFullYear(),month.getMonth(),1,0,0,0,0); // Starting at the 1st of the month
var extras = (date.getDay() + 6) % 7; // How many days of the last month do we need to include?
date.setDate(date.getDate()-extras); // Skip back to the previous monday
while (1) { // Loop for each week
var tr = tbl.insertRow(-1);
for (i=0;i<7;i++) { // Loop each day of this week
var td = tr.insertCell(-1);
var inp = document.createElement('input');
inp.type = 'button';
td.appendChild(inp);
inp.value = date.getDate();
inp.onclick = chooseDate;
inp.setAttribute('date',getDateString(date));
if (date.getMonth() != month.getMonth()) {
if (inp.className) inp.className += ' ';
inp.className+='othermonth';
}
if (date.getDate()==today.getDate() && date.getMonth()==today.getMonth() && date.getFullYear()==today.getFullYear()) {
if (inp.className) inp.className += ' ';
inp.className+='today';
}
if (!isNaN(selected) && date.getDate()==selected.getDate() && date.getMonth()==selected.getMonth() && date.getFullYear()==selected.getFullYear()) {
if (inp.className) inp.className += ' ';
inp.className+='selected';
}
date.setDate(date.getDate()+1); // Increment a day
}
// We are done if we've moved on to the next month
if (date.getMonth() != month.getMonth()) {
break;
}
}
// At the end, we do a quick insert of the newly made table, hopefully to remove any chance of screen flicker
if (div.hasChildNodes()) { // For flicking between months
div.replaceChild(tbl, div.childNodes[0]);
} else { // For creating the calendar when they first click the icon
div.appendChild(tbl);
}
}
// This is called when they click the icon next to the date inputbox
function showDatePicker(idOfTextbox) {
var textbox = document.getElementById(idOfTextbox);
// See if the date picker is already there, if so, remove it
x = textbox.parentNode.getElementsByTagName('div');
for (i=0;i<x.length;i++) {
if (x[i].getAttribute('class')=='datepickerdropdown') {
textbox.parentNode.removeChild(x[i]);
return false;
}
}
// Grab the date, or use the current date if not valid
var date = parseMyDate(textbox.value);
if (isNaN(date)) date = new Date();
// Create the box
var div = document.createElement('div');
div.className='datepickerdropdown';
div.setAttribute('datepickertextbox', idOfTextbox); // Remember the textbox id in the div
createCalendar(div, date); // Create the calendar
insertAfter(div, textbox); // Add the box to screen just after the textbox
return false;
}
// Adds an item after an existing one
function insertAfter(newItem, existingItem) {
if (existingItem.nextSibling) { // Find the next sibling, and add newItem before it
existingItem.parentNode.insertBefore(newItem, existingItem.nextSibling);
} else { // In case the existingItem has no sibling after itself, append it
existingItem.parentNode.appendChild(newItem);
}
}
/*
* onDOMReady
* Copyright (c) 2009 Ryan Morr (ryanmorr.com)
* Licensed under the MIT license.
*/
function onDOMReady(fn,ctx){var ready,timer;var onStateChange=function(e){if(e&&e.type=="DOMContentLoaded"){fireDOMReady()}else if(e&&e.type=="load"){fireDOMReady()}else if(document.readyState){if((/loaded|complete/).test(document.readyState)){fireDOMReady()}else if(!!document.documentElement.doScroll){try{ready||document.documentElement.doScroll('left')}catch(e){return}fireDOMReady()}}};var fireDOMReady=function(){if(!ready){ready=true;fn.call(ctx||window);if(document.removeEventListener)document.removeEventListener("DOMContentLoaded",onStateChange,false);document.onreadystatechange=null;window.onload=null;clearInterval(timer);timer=null}};if(document.addEventListener)document.addEventListener("DOMContentLoaded",onStateChange,false);document.onreadystatechange=onStateChange;timer=setInterval(onStateChange,5);window.onload=onStateChange};
// This is called when the page loads, it searches for inputs where the class is 'datepicker'
onDOMReady(function(){
// Search for elements by class
var allElements = document.getElementsByTagName("*");
for (i=0; i<allElements.length; i++) {
var className = allElements[i].className;
if (className=='datepicker' || className.indexOf('datepicker ') != -1 || className.indexOf(' datepicker') != -1) {
// Found one! Now lets add a datepicker next to it
var a = document.createElement('a');
a.href='#';
a.className="datepickershow";
a.setAttribute('onclick','return showDatePicker("' + allElements[i].id + '")');
var img = document.createElement('img');
img.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAAK/INwWK6QAAABh0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjM2qefiJQAAAdtJREFUOE+Vj+9PUnEUxvPvar3xja96Q1hGEKG0ubZqbfHCNqIVA4eYLAwFp0LYD4iIJEdeRGGZwDAEcUOn9oNIvPcGgjBQfHE69/YFihe1zs59du7d83nOuR0AcOq/CgEqWbaHDqaD+clF1rLAmija6MsZ5vb0s9nB1xm168s9x67y6Y7q2TaXjo8tVKjUTv7Zt61pAkwt/UA3zFwFuxysV2BKAuYeMAnBcBaGukDdCaozaLg5sUGAiQDLA3IIDIBfAfO34N118PaDRwYvRfBcCMrTaLg2liTAOEW3NjzpBZsMpqUwKQaLCMYvwGMhjArQIDfGCTDqy3EAX47lfVTnCo3qCnOzJ8IpW6pJR2IEGHn7/bBaR5MLO8y8CtPuKO2J0nMfGdKr+5uZ4kVdhAD6N99K1bo7ynB5vHpj3AZ0NxWBbs0KAbTur8VKfTbGeFcbkc1sfnBHuA1CzTIB7js/H5SPffFW3q9sau2PDdLhxkl3X+wiQCVYf4Jt3h1Itmb8iBvEusZJd2a2CuXjxXUWU5dSnAZ5/b0QkOobgMKWzh8eMcXaXr6aYSqfcuXtbAkdbS3RfSD/MGDfvGFO9ZuSfY/ilx/GLumi57Vhgfp9W597ECJA2/a/v/4ENLpYKsDo3kgAAAAASUVORK5CYII=';
img.title='Show calendar';
a.appendChild(img);
insertAfter(a, allElements[i]);
}
}
});