1 | 'use strict'; |
---|
2 | |
---|
3 | function datePicker(target) { |
---|
4 | if (!document.getElementById) { |
---|
5 | return; |
---|
6 | } |
---|
7 | |
---|
8 | if (!target || target.nodeName.toLowerCase() != 'input') { |
---|
9 | return; |
---|
10 | } |
---|
11 | |
---|
12 | this.target = target; |
---|
13 | this.oTable = document.createElement('table'); |
---|
14 | this.oBody = document.createElement('tbody'); |
---|
15 | this.oDates = []; |
---|
16 | this.oMonth = document.createElement('span'); |
---|
17 | this.oYear = document.createElement('span'); |
---|
18 | this.oHour = document.createElement('input'); |
---|
19 | this.oMinute = document.createElement('input'); |
---|
20 | this.oTable.id = 'dc_datepicker_' + target.id; |
---|
21 | this.oTable.className = 'date-picker'; |
---|
22 | |
---|
23 | var cur = 1; |
---|
24 | var oRow, oHeading, oSpan; |
---|
25 | |
---|
26 | // Set title |
---|
27 | oRow = document.createElement('tr'); |
---|
28 | |
---|
29 | // Month block |
---|
30 | oHeading = document.createElement('th'); |
---|
31 | oHeading.colSpan = 4; |
---|
32 | oHeading.className = 'date-picker-month'; |
---|
33 | |
---|
34 | var nav = document.createElement('span'); |
---|
35 | nav.appendChild(document.createTextNode(String.fromCharCode(171))); |
---|
36 | nav.fn = this.changeMonth; |
---|
37 | nav.obj = this; |
---|
38 | nav.onclick = function() { |
---|
39 | this.fn.call(this.obj, -1); |
---|
40 | }; |
---|
41 | nav.className = 'date-picker-control'; |
---|
42 | oHeading.appendChild(nav); |
---|
43 | |
---|
44 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
45 | |
---|
46 | nav = document.createElement('span'); |
---|
47 | nav.appendChild(document.createTextNode(String.fromCharCode(187))); |
---|
48 | nav.fn = this.changeMonth; |
---|
49 | nav.obj = this; |
---|
50 | nav.onclick = function() { |
---|
51 | this.fn.call(this.obj, +1); |
---|
52 | }; |
---|
53 | nav.className = 'date-picker-control'; |
---|
54 | oHeading.appendChild(nav); |
---|
55 | |
---|
56 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
57 | |
---|
58 | oHeading.appendChild(this.oMonth); |
---|
59 | |
---|
60 | oRow.appendChild(oHeading); |
---|
61 | |
---|
62 | // Year block |
---|
63 | oHeading = document.createElement('th'); |
---|
64 | oHeading.colSpan = 3; |
---|
65 | oHeading.className = 'date-picker-year'; |
---|
66 | |
---|
67 | oHeading.appendChild(this.oYear); |
---|
68 | |
---|
69 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
70 | |
---|
71 | nav = document.createElement('span'); |
---|
72 | nav.appendChild(document.createTextNode(String.fromCharCode(171))); |
---|
73 | nav.fn = this.changeYear; |
---|
74 | nav.obj = this; |
---|
75 | nav.onclick = function() { |
---|
76 | this.fn.call(this.obj, -1); |
---|
77 | }; |
---|
78 | nav.className = 'date-picker-control'; |
---|
79 | oHeading.appendChild(nav); |
---|
80 | |
---|
81 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
82 | |
---|
83 | nav = document.createElement('span'); |
---|
84 | nav.appendChild(document.createTextNode(String.fromCharCode(187))); |
---|
85 | nav.fn = this.changeYear; |
---|
86 | nav.obj = this; |
---|
87 | nav.onclick = function() { |
---|
88 | this.fn.call(this.obj, +1); |
---|
89 | }; |
---|
90 | nav.className = 'date-picker-control'; |
---|
91 | oHeading.appendChild(nav); |
---|
92 | |
---|
93 | oRow.appendChild(oHeading); |
---|
94 | |
---|
95 | this.oBody.appendChild(oRow); |
---|
96 | |
---|
97 | // Create legend |
---|
98 | oRow = document.createElement('tr'); |
---|
99 | var cday; |
---|
100 | for (i = 0; i < this.days.length; i++) { |
---|
101 | cday = this.days[i].substring(0, 1).toUpperCase(); |
---|
102 | oHeading = document.createElement('th'); |
---|
103 | oHeading.appendChild(document.createTextNode(cday)); |
---|
104 | oHeading.setAttribute('title', this.days[i]); |
---|
105 | oRow.appendChild(oHeading); |
---|
106 | } |
---|
107 | this.oBody.appendChild(oRow); |
---|
108 | |
---|
109 | // Create 6 rows of 7 cols for days |
---|
110 | for (var i = 0; i < 6; i++) { |
---|
111 | oRow = document.createElement('tr'); |
---|
112 | |
---|
113 | for (var j = 0; j < 7; j++) { |
---|
114 | this.oDates[cur] = document.createElement('td'); |
---|
115 | this.oDates[cur].appendChild(document.createTextNode( |
---|
116 | String.fromCharCode(160))); |
---|
117 | oRow.appendChild(this.oDates[cur]); |
---|
118 | cur++; |
---|
119 | } |
---|
120 | |
---|
121 | this.oBody.appendChild(oRow); |
---|
122 | } |
---|
123 | |
---|
124 | // Time controls |
---|
125 | oRow = document.createElement('tr'); |
---|
126 | |
---|
127 | oHeading = document.createElement('th'); |
---|
128 | oHeading.className = 'date-picker-control'; |
---|
129 | oHeading.appendChild(document.createTextNode('!')); |
---|
130 | oHeading.setAttribute('title', this.now_msg); |
---|
131 | oHeading.fn = this.sendNow; |
---|
132 | oHeading.obj = this; |
---|
133 | oHeading.onclick = function() { |
---|
134 | this.fn.call(this.obj); |
---|
135 | }; |
---|
136 | |
---|
137 | oRow.appendChild(oHeading); |
---|
138 | |
---|
139 | oHeading = document.createElement('th'); |
---|
140 | oHeading.colSpan = 5; |
---|
141 | |
---|
142 | oSpan = document.createElement('span'); |
---|
143 | oSpan.className = 'date-picker-control'; |
---|
144 | oSpan.appendChild(document.createTextNode('-')); |
---|
145 | oSpan.fn = this.changeHour; |
---|
146 | oSpan.obj = this; |
---|
147 | oSpan.onclick = function() { |
---|
148 | this.fn.call(this.obj, -1); |
---|
149 | }; |
---|
150 | oHeading.appendChild(oSpan); |
---|
151 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
152 | oSpan = document.createElement('span'); |
---|
153 | oSpan.className = 'date-picker-control'; |
---|
154 | oSpan.appendChild(document.createTextNode('+')); |
---|
155 | oSpan.fn = this.changeHour; |
---|
156 | oSpan.obj = this; |
---|
157 | oSpan.onclick = function() { |
---|
158 | this.fn.call(this.obj, +1); |
---|
159 | }; |
---|
160 | oHeading.appendChild(oSpan); |
---|
161 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
162 | |
---|
163 | this.oHour.size = 2; |
---|
164 | oHeading.appendChild(this.oHour); |
---|
165 | |
---|
166 | oHeading.appendChild(document.createTextNode(' : ')); |
---|
167 | |
---|
168 | this.oMinute.size = 2; |
---|
169 | oHeading.appendChild(this.oMinute); |
---|
170 | |
---|
171 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
172 | oSpan = document.createElement('span'); |
---|
173 | oSpan.className = 'date-picker-control'; |
---|
174 | oSpan.appendChild(document.createTextNode('-')); |
---|
175 | oSpan.fn = this.changeMinute; |
---|
176 | oSpan.obj = this; |
---|
177 | oSpan.onclick = function() { |
---|
178 | this.fn.call(this.obj, -1); |
---|
179 | }; |
---|
180 | oHeading.appendChild(oSpan); |
---|
181 | oHeading.appendChild(document.createTextNode(String.fromCharCode(160))); |
---|
182 | oSpan = document.createElement('span'); |
---|
183 | oSpan.className = 'date-picker-control'; |
---|
184 | oSpan.appendChild(document.createTextNode('+')); |
---|
185 | oSpan.fn = this.changeMinute; |
---|
186 | oSpan.obj = this; |
---|
187 | oSpan.onclick = function() { |
---|
188 | this.fn.call(this.obj, +1); |
---|
189 | }; |
---|
190 | |
---|
191 | oHeading.appendChild(oSpan); |
---|
192 | |
---|
193 | oRow.appendChild(oHeading); |
---|
194 | |
---|
195 | // Close control |
---|
196 | oHeading = document.createElement('th'); |
---|
197 | oHeading.className = 'date-picker-control'; |
---|
198 | oHeading.appendChild(document.createTextNode('x')); |
---|
199 | oHeading.setAttribute('title', this.close_msg); |
---|
200 | oHeading.fn = this.close; |
---|
201 | oHeading.obj = this; |
---|
202 | oHeading.onclick = function() { |
---|
203 | this.fn.call(this.obj); |
---|
204 | }; |
---|
205 | |
---|
206 | oRow.appendChild(oHeading); |
---|
207 | |
---|
208 | this.oBody.appendChild(oRow); |
---|
209 | } |
---|
210 | |
---|
211 | datePicker.prototype = { |
---|
212 | year: 0, |
---|
213 | month: 0, |
---|
214 | day: 0, |
---|
215 | hour: 0, |
---|
216 | minute: 0, |
---|
217 | |
---|
218 | img_src: '', |
---|
219 | img_top: '0.2em', |
---|
220 | now_msg: 'now', |
---|
221 | close_msg: 'close', |
---|
222 | |
---|
223 | days: new Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', |
---|
224 | 'Saturday', 'Sunday'), |
---|
225 | |
---|
226 | months: new Array('January', 'February', 'March', 'April', 'May', 'June', |
---|
227 | 'July', 'August', 'September', 'October', 'November', 'December'), |
---|
228 | |
---|
229 | setDate: function() { |
---|
230 | if (this.numberOfDays() < this.day) { |
---|
231 | this.day = this.numberOfDays(); |
---|
232 | } |
---|
233 | |
---|
234 | while (this.oYear.hasChildNodes()) { |
---|
235 | this.oYear.removeChild(this.oYear.firstChild); |
---|
236 | } |
---|
237 | this.oYear.appendChild(document.createTextNode(this.year)); |
---|
238 | |
---|
239 | while (this.oMonth.hasChildNodes()) { |
---|
240 | this.oMonth.removeChild(this.oMonth.firstChild); |
---|
241 | } |
---|
242 | this.oMonth.appendChild(document.createTextNode( |
---|
243 | this.months[this.month - 1])); |
---|
244 | |
---|
245 | var firstDay = this.firstDay(); |
---|
246 | var nbDays = this.numberOfDays(); |
---|
247 | |
---|
248 | // Empty days |
---|
249 | for (var i = 1; i <= 42; i++) { |
---|
250 | while (this.oDates[i].hasChildNodes()) { |
---|
251 | this.oDates[i].removeChild(this.oDates[i].firstChild); |
---|
252 | } |
---|
253 | this.oDates[i].appendChild(document.createTextNode('-')); |
---|
254 | this.oDates[i].className = ''; |
---|
255 | this.oDates[i].onclick = function() { |
---|
256 | return; |
---|
257 | }; |
---|
258 | } |
---|
259 | |
---|
260 | // Insert days from the first day to the last |
---|
261 | for (i = 1; i <= nbDays; i++) { |
---|
262 | var j = firstDay + i - 1; |
---|
263 | |
---|
264 | while (this.oDates[j].hasChildNodes()) { |
---|
265 | this.oDates[j].removeChild(this.oDates[j].firstChild); |
---|
266 | } |
---|
267 | |
---|
268 | this.oDates[j].appendChild(document.createTextNode(i)); |
---|
269 | this.oDates[j].index = i; |
---|
270 | this.oDates[j].fn = this.sendDate; |
---|
271 | this.oDates[j].obj = this; |
---|
272 | this.oDates[j].onclick = function() { |
---|
273 | this.fn.call(this.obj, this.index); |
---|
274 | }; |
---|
275 | if (i == this.day) { |
---|
276 | this.oDates[j].className = 'date-picker-today'; |
---|
277 | } else { |
---|
278 | this.oDates[j].className = 'date-picker-day'; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | // Set time |
---|
283 | this.setHour(this.hour); |
---|
284 | this.setMinute(this.minute); |
---|
285 | }, |
---|
286 | |
---|
287 | setHour: function(h) { |
---|
288 | if (h < 0) { |
---|
289 | h = 23; |
---|
290 | } |
---|
291 | if (h > 23) { |
---|
292 | h = 0; |
---|
293 | } |
---|
294 | if (h < 10) { |
---|
295 | h = '0' + h; |
---|
296 | } |
---|
297 | |
---|
298 | this.hour = h * 1; |
---|
299 | this.oHour.value = h; |
---|
300 | }, |
---|
301 | |
---|
302 | setMinute: function(m) { |
---|
303 | if (m < 0) { |
---|
304 | m = 59; |
---|
305 | } |
---|
306 | if (m > 59) { |
---|
307 | m = 0; |
---|
308 | } |
---|
309 | if (m < 10) { |
---|
310 | m = '0' + m; |
---|
311 | } |
---|
312 | |
---|
313 | this.minute = m * 1; |
---|
314 | this.oMinute.value = m; |
---|
315 | }, |
---|
316 | |
---|
317 | changeMonth: function(dir) { |
---|
318 | var m = this.month; |
---|
319 | m = m + dir; |
---|
320 | |
---|
321 | if (m > 12) { |
---|
322 | this.month = 1; |
---|
323 | this.year++; |
---|
324 | } else if (m < 1) { |
---|
325 | this.month = 12; |
---|
326 | this.year--; |
---|
327 | } else { |
---|
328 | this.month = m; |
---|
329 | } |
---|
330 | |
---|
331 | this.setDate(); |
---|
332 | }, |
---|
333 | |
---|
334 | changeYear: function(dir) { |
---|
335 | this.year = this.year + dir; |
---|
336 | this.setDate(); |
---|
337 | }, |
---|
338 | |
---|
339 | changeHour: function(dir) { |
---|
340 | this.setHour(this.hour * 1 + dir); |
---|
341 | }, |
---|
342 | |
---|
343 | changeMinute: function(dir) { |
---|
344 | this.setMinute(this.minute * 1 + dir); |
---|
345 | }, |
---|
346 | |
---|
347 | sendDate: function(d) { |
---|
348 | var m = this.month; |
---|
349 | var hour = this.oHour.value * 1; |
---|
350 | var minute = this.oMinute.value * 1; |
---|
351 | |
---|
352 | if (hour < 0 || hour > 23 || isNaN(hour)) { |
---|
353 | hour = 0; |
---|
354 | } |
---|
355 | if (minute < 0 || minute > 59 || isNaN(minute)) { |
---|
356 | minute = 0; |
---|
357 | } |
---|
358 | |
---|
359 | if (m < 10) { |
---|
360 | m = '0' + m; |
---|
361 | } |
---|
362 | if (d < 10) { |
---|
363 | d = '0' + d; |
---|
364 | } |
---|
365 | if (hour < 10) { |
---|
366 | hour = '0' + hour; |
---|
367 | } |
---|
368 | if (minute < 10) { |
---|
369 | minute = '0' + minute; |
---|
370 | } |
---|
371 | |
---|
372 | this.target.value = this.year + '-' + m + '-' + d + ' ' + hour + ':' + minute; |
---|
373 | this.close(); |
---|
374 | }, |
---|
375 | |
---|
376 | sendNow: function() { |
---|
377 | var dt = new Date(); |
---|
378 | var y = dt.getFullYear(); |
---|
379 | var m = dt.getMonth() + 1; |
---|
380 | var d = dt.getDate(); |
---|
381 | var h = dt.getHours(); |
---|
382 | var i = dt.getMinutes(); |
---|
383 | |
---|
384 | if (m < 10) { |
---|
385 | m = '0' + m; |
---|
386 | } |
---|
387 | if (d < 10) { |
---|
388 | d = '0' + d; |
---|
389 | } |
---|
390 | if (h < 10) { |
---|
391 | h = '0' + h; |
---|
392 | } |
---|
393 | if (i < 10) { |
---|
394 | i = '0' + i; |
---|
395 | } |
---|
396 | |
---|
397 | this.target.value = y + '-' + m + '-' + d + ' ' + h + ':' + i; |
---|
398 | this.close(); |
---|
399 | }, |
---|
400 | |
---|
401 | close: function() { |
---|
402 | document.body.removeChild(this.oTable); |
---|
403 | }, |
---|
404 | |
---|
405 | numberOfDays: function() { |
---|
406 | var res = 31; |
---|
407 | if (this.month == 4 || this.month == 6 || this.month == 9 || |
---|
408 | this.month == 11) { |
---|
409 | res = 30; |
---|
410 | } else if (this.month == 2) { |
---|
411 | res = 28; |
---|
412 | if (this.year % 4 == 0 && (this.year % 100 != 0 || |
---|
413 | this.year % 400 == 0)) { |
---|
414 | res = 29; |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | return res; |
---|
419 | }, |
---|
420 | |
---|
421 | firstDay: function() { |
---|
422 | var dt = new Date(this.year, this.month - 1, 1); |
---|
423 | var res = dt.getDay(); |
---|
424 | |
---|
425 | if (res == 0) { |
---|
426 | res = 7; |
---|
427 | } |
---|
428 | |
---|
429 | return res; |
---|
430 | }, |
---|
431 | |
---|
432 | show: function() { |
---|
433 | // Parsing target value |
---|
434 | var re = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/; |
---|
435 | var match = re.exec(this.target.value); |
---|
436 | if (match) { |
---|
437 | this.year = match[1] * 1; |
---|
438 | this.month = match[2] * 1; |
---|
439 | this.day = match[3] * 1; |
---|
440 | this.hour = match[4] * 1; |
---|
441 | this.minute = match[5] * 1; |
---|
442 | } else { |
---|
443 | var dt = new Date(); |
---|
444 | this.year = dt.getFullYear(); |
---|
445 | this.month = dt.getMonth() + 1; |
---|
446 | this.day = dt.getDate(); |
---|
447 | this.hour = dt.getHours(); |
---|
448 | this.minute = dt.getMinutes(); |
---|
449 | } |
---|
450 | |
---|
451 | this.oTable.appendChild(this.oBody); |
---|
452 | this.setDate(); |
---|
453 | this.setPosition(); |
---|
454 | document.body.appendChild(this.oTable); |
---|
455 | this.oHour.focus(); |
---|
456 | }, |
---|
457 | |
---|
458 | setPosition: function() { |
---|
459 | var t_x = this.findPosX(this.target); |
---|
460 | var t_y = this.findPosY(this.target); |
---|
461 | |
---|
462 | this.oTable.style.position = 'absolute'; |
---|
463 | this.oTable.style.zIndex = '100'; |
---|
464 | this.oTable.style.top = t_y + 'px'; |
---|
465 | this.oTable.style.left = t_x + 'px'; |
---|
466 | }, |
---|
467 | |
---|
468 | findPosX: function(obj) { |
---|
469 | var curleft = 0; |
---|
470 | if (obj.offsetParent) { |
---|
471 | while (1) { |
---|
472 | curleft += obj.offsetLeft; |
---|
473 | if (!obj.offsetParent) { |
---|
474 | break; |
---|
475 | } |
---|
476 | obj = obj.offsetParent; |
---|
477 | } |
---|
478 | } else if (obj.x) { |
---|
479 | curleft += obj.x; |
---|
480 | } |
---|
481 | return curleft; |
---|
482 | }, |
---|
483 | |
---|
484 | findPosY: function(obj) { |
---|
485 | var curtop = 0; |
---|
486 | if (obj.offsetParent) { |
---|
487 | while (1) { |
---|
488 | curtop += obj.offsetTop; |
---|
489 | if (!obj.offsetParent) { |
---|
490 | break; |
---|
491 | } |
---|
492 | obj = obj.offsetParent; |
---|
493 | } |
---|
494 | } else if (obj.y) { |
---|
495 | curtop += obj.y; |
---|
496 | } |
---|
497 | return curtop; |
---|
498 | }, |
---|
499 | |
---|
500 | draw: function() { |
---|
501 | var imgE = document.createElement('img'); |
---|
502 | imgE.src = this.img_src; |
---|
503 | imgE.alt = this.img_alt; |
---|
504 | imgE.style.position = 'absolute'; |
---|
505 | imgE.style.top = this.img_top; |
---|
506 | imgE.style.left = (this.target.clientWidth + 4) + 'px'; |
---|
507 | imgE.obj = this; |
---|
508 | imgE.fn = this.show; |
---|
509 | imgE.onclick = function() { |
---|
510 | this.fn.apply(this.obj); |
---|
511 | }; |
---|
512 | |
---|
513 | this.target.parentNode.style.position = 'relative'; |
---|
514 | this.target.parentNode.insertBefore(imgE, this.target.nextSibling); |
---|
515 | } |
---|
516 | }; |
---|