|
9 | 9 | * Since: Mar 2010 |
10 | 10 | * Date: @DATE |
11 | 11 | */ |
12 | | -(function($) { |
| 12 | +(function($, undefined) { |
13 | 13 |
|
14 | 14 | /* TODO: |
15 | 15 | preserve today highlighted |
|
91 | 91 |
|
92 | 92 | // @return amount of days in certain month |
93 | 93 | function dayAm(year, month) { |
94 | | - return 32 - new Date(year, month, 32).getDate(); |
| 94 | + return new Date(year, month + 1, 0).getDate(); |
95 | 95 | } |
96 | 96 |
|
97 | 97 | function zeropad(val, len) { |
|
339 | 339 |
|
340 | 340 |
|
341 | 341 | if (index > 41) { |
342 | | - self.addMonth(); |
| 342 | + showNextMonth(); |
343 | 343 | el = $("#" + css.weeks + " a:eq(" + (index-42) + ")"); |
344 | 344 | } else if (index < 0) { |
345 | | - self.addMonth(-1); |
| 345 | + showPreviousMonth(); |
346 | 346 | el = $("#" + css.weeks + " a:eq(" + (index+42) + ")"); |
347 | 347 | } else { |
348 | 348 | el = days.eq(index); |
|
354 | 354 | } |
355 | 355 |
|
356 | 356 | // pageUp / pageDown |
357 | | - if (key == 34) { return self.addMonth(); } |
358 | | - if (key == 33) { return self.addMonth(-1); } |
| 357 | + if (key == 34) { return showNextMonth(); } |
| 358 | + if (key == 33) { return showPreviousMonth(); } |
359 | 359 |
|
360 | 360 | // home |
361 | | - if (key == 36) { return self.today(); } |
| 361 | + if (key == 36) { return self.today(); } |
362 | 362 |
|
363 | 363 | // enter |
364 | 364 | if (key == 13) { |
365 | 365 | if (!$(e.target).is("select")) { |
366 | | - $("." + css.focus).click(); |
367 | | - } |
| 366 | + $("." + css.focus).click(); |
| 367 | + } |
368 | 368 | } |
369 | 369 |
|
370 | 370 | return $([16, 17, 18, 9]).index(key) >= 0; |
|
382 | 382 | }); |
383 | 383 | } |
384 | 384 | //}}} |
385 | | - |
| 385 | + |
| 386 | + |
| 387 | + /** |
| 388 | + * @private |
| 389 | + * |
| 390 | + * Calculates the days in the next month to properly switch months |
| 391 | + * |
| 392 | + */ |
| 393 | + function showNextMonth() { |
| 394 | + |
| 395 | + var daysNextMonth = dayAm(currYear, currMonth + 1); |
| 396 | + |
| 397 | + /* |
| 398 | + * If next month has less days than the current date |
| 399 | + * add number of days in the next month, otherwise add |
| 400 | + * number of days in the current month |
| 401 | + */ |
| 402 | + return self.addDay(currDay > daysNextMonth ? daysNextMonth : dayAm(currYear, currMonth)); |
| 403 | + } |
| 404 | + |
| 405 | + /** |
| 406 | + * @private |
| 407 | + * |
| 408 | + * Return to previous month |
| 409 | + */ |
| 410 | + function showPreviousMonth() { |
| 411 | + return self.addDay(-dayAm(currYear, currMonth)); |
| 412 | + } |
386 | 413 |
|
387 | 414 | $.extend(self, { |
388 | 415 |
|
389 | | -//{{{ show |
390 | | - |
| 416 | + |
| 417 | + /** |
| 418 | + * @public |
| 419 | + * Show the calendar |
| 420 | + */ |
391 | 421 | show: function(e) { |
392 | 422 |
|
393 | 423 | if (input.attr("readonly") || input.attr("disabled") || opened) { return; } |
|
417 | 447 | // prev / next month |
418 | 448 | pm = root.find("#" + css.prev).unbind("click").click(function(e) { |
419 | 449 | if (!pm.hasClass(css.disabled)) { |
420 | | - self.addMonth(-1); |
| 450 | + showPreviousMonth(); |
421 | 451 | } |
422 | 452 | return false; |
423 | 453 | }); |
424 | 454 |
|
425 | 455 | nm = root.find("#" + css.next).unbind("click").click(function(e) { |
426 | 456 | if (!nm.hasClass(css.disabled)) { |
427 | | - self.addMonth(); |
| 457 | + showNextMonth(); |
428 | 458 | } |
429 | 459 | return false; |
430 | 460 | }); |
|
456 | 486 |
|
457 | 487 | return self; |
458 | 488 | }, |
459 | | -//}}} |
460 | | - |
461 | | - |
462 | | -//{{{ setValue |
463 | 489 |
|
| 490 | + /** |
| 491 | + * @public |
| 492 | + * |
| 493 | + * Set the value of the dateinput |
| 494 | + */ |
464 | 495 | setValue: function(year, month, day) { |
465 | 496 |
|
466 | | - |
467 | | - |
468 | | - var date = integer(month) >= -1 ? new Date(integer(year), integer(month), integer(day || 1)) : |
| 497 | + var date = integer(month) >= -1 ? new Date(integer(year), integer(month), integer(day == undefined || isNaN(day) ? 1 : day)) : |
469 | 498 | year || value; |
470 | 499 |
|
471 | 500 | if (date < min) { date = min; } |
|
495 | 524 |
|
496 | 525 | currMonth = month; |
497 | 526 | currYear = year; |
| 527 | + currDay = day; |
498 | 528 |
|
499 | 529 | // variables |
500 | 530 | var tmp = new Date(year, month, 1 - conf.firstDay), begin = tmp.getDay(), |
|
508 | 538 | // month selector |
509 | 539 | monthSelector.empty(); |
510 | 540 | $.each(labels.months, function(i, m) { |
511 | | - if (min < new Date(year, i + 1, -1) && max > new Date(year, i, 0)) { |
| 541 | + if (min < new Date(year, i + 1, 1) && max > new Date(year, i, 0)) { |
512 | 542 | monthSelector.append($("<option/>").html(m).attr("value", i)); |
513 | 543 | } |
514 | 544 | }); |
|
518 | 548 | var yearNow = now.getFullYear(); |
519 | 549 |
|
520 | 550 | for (var i = yearNow + conf.yearRange[0]; i < yearNow + conf.yearRange[1]; i++) { |
521 | | - if (min <= new Date(i + 1, -1, 1) && max > new Date(i, 0, 0)) { |
| 551 | + if (min < new Date(i + 1, 0, 1) && max > new Date(i, 0, 0)) { |
522 | 552 | yearSelector.append($("<option/>").text(i)); |
523 | 553 | } |
524 | 554 | } |
|
0 commit comments