Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change month/year correctly when clicking days before start

or after end if current month
  • Loading branch information...
commit 285b0043c57834dc727d3a076acae847ab446bfb 1 parent 4baa202
@colinf colinf authored
Showing with 23 additions and 18 deletions.
  1. +20 −15 lib/days.js
  2. +3 −3 test/index.html
View
35 lib/days.js
@@ -78,8 +78,11 @@ function Days() {
this.body.on('click', 'a', function(e){
var el = o(e.target);
var day = parseInt(el.text(), 10);
- var month = el.data('month');
+ var data = el.data('month').split('-');
+ var year = data[0];
+ var month = data[1];
var date = new Date;
+ date.setYear(year);
date.setMonth(month);
date.setDate(day);
self.select(date);
@@ -178,11 +181,12 @@ Days.prototype.renderDays = function(date){
* @api private
*/
-Days.prototype.cellsBefore = function(n, month){
+Days.prototype.cellsBefore = function(n, month, year){
var cells = [];
- var prev = clamp(month);
+ if (month == 0) --year;
+ var prev = clamp(month - 1);
var before = daysInMonth(prev);
- while (n--) cells.push(prevMonthDay(prev, before--));
+ while (n--) cells.push(prevMonthDay(year, prev, before--));
return cells.reverse();
};
@@ -195,11 +199,12 @@ Days.prototype.cellsBefore = function(n, month){
* @api private
*/
-Days.prototype.cellsAfter = function(n, month){
+Days.prototype.cellsAfter = function(n, month, year){
var cells = [];
var day = 0;
- var next = clamp(month);
- while (n--) cells.push(nextMonthDay(next, ++day));
+ if (month == 11) ++year;
+ var next = clamp(month + 1);
+ while (n--) cells.push(nextMonthDay(year, next, ++day));
return cells;
};
@@ -234,20 +239,20 @@ Days.prototype.rowsFor = function(date){
var cells = [];
// cells before
- cells = cells.concat(this.cellsBefore(before, month));
+ cells = cells.concat(this.cellsBefore(before, month, year));
// current cells
for (var i = 0; i < total; ++i) {
var day = i + 1;
if (day == selectedDay && month == selectedMonth && year == selectedYear) {
- cells.push('<td class=selected><a href="#" data-month=' + month + '>' + day + '</a></td>');
+ cells.push('<td class=selected><a href="#" data-month=' + year+'-'+month + '>' + day + '</a></td>');
} else {
- cells.push('<td><a href="#" data-month=' + month + '>' + day + '</a></td>');
+ cells.push('<td><a href="#" data-month=' + year+'-'+month + '>' + day + '</a></td>');
}
}
// after cells
- cells = cells.concat(this.cellsAfter(after, month));
+ cells = cells.concat(this.cellsAfter(after, month, year));
return inGroupsOf(cells, 7);
};
@@ -256,15 +261,15 @@ Days.prototype.rowsFor = function(date){
* Prev month day template.
*/
-function prevMonthDay(prev, day) {
- return '<td><a href="#" data-month=' + prev + ' class=prev-day>' + day + '</a></td>';
+function prevMonthDay(year, prev, day) {
+ return '<td><a href="#" data-month=' + year+'-'+prev + ' class=prev-day>' + day + '</a></td>';
}
/**
* Next month day template.
*/
-function nextMonthDay(next, day) {
- return '<td><a href="#" data-month=' + next + ' class=next-day>' + day + '</a></td>';
+function nextMonthDay(year, next, day) {
+ return '<td><a href="#" data-month=' + year+'-'+next + ' class=next-day>' + day + '</a></td>';
}
View
6 test/index.html
@@ -31,9 +31,9 @@
date.getDate(),
date.getMonth(),
date.getFullYear());
-
- date.setMonth(date.getMonth() + 1);
- two.select(date);
+ var newDate = new Date(date);
+ newDate.setMonth(date.getMonth() + 1);
+ two.select(newDate);
});
one.el.appendTo('body');
Please sign in to comment.
Something went wrong with that request. Please try again.