From 080640ab15afd6588f52082d90af86a3922cca65 Mon Sep 17 00:00:00 2001 From: colinf Date: Wed, 14 Nov 2012 14:21:07 +0000 Subject: [PATCH 1/3] Constrain view to specified from/to years --- lib/calendar.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/calendar.js b/lib/calendar.js index 6551f4f..5c133cc 100644 --- a/lib/calendar.js +++ b/lib/calendar.js @@ -43,6 +43,7 @@ function Calendar(date) { this.days.on('month', this.menuChange.bind(this, 'month')); this.show(date || new Date); this.days.on('change', function(date){ + if (self.invalidDate(date)) { return }; self.emit('change', date); }); } @@ -92,6 +93,7 @@ Calendar.prototype.select = function(date){ */ Calendar.prototype.show = function(date){ + if (this.invalidDate(date)) { return this }; this._date = date; this.days.show(date); return this; @@ -109,6 +111,8 @@ Calendar.prototype.show = function(date){ Calendar.prototype.showYearSelect = function(from, to){ from = from || this._date.getFullYear() - 10; to = to || this._date.getFullYear() + 10; + this._from = from; + this._to = to; this.days.yearMenu(from, to); this.show(this._date); return this; @@ -194,3 +198,18 @@ Calendar.prototype.menuChange = function(action){ this.emit('view change', date, action); return this; }; + +/** + * Check if date is outside the from/to years specified. + * + * @return {Boolean} + * @api private + */ + +Calendar.prototype.invalidDate = function(date) { + var year = date.getFullYear(); + if ((this._from && year < this._from) || (this._to && year > this._to)) { + return true; + } + return false; +} From 3a19c009d169426539916e3729d40e4205d0c027 Mon Sep 17 00:00:00 2001 From: colinf Date: Wed, 14 Nov 2012 15:46:32 +0000 Subject: [PATCH 2/3] Allow for reverse range when checking date --- lib/calendar.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/calendar.js b/lib/calendar.js index 5c133cc..01aef27 100644 --- a/lib/calendar.js +++ b/lib/calendar.js @@ -208,7 +208,12 @@ Calendar.prototype.menuChange = function(action){ Calendar.prototype.invalidDate = function(date) { var year = date.getFullYear(); - if ((this._from && year < this._from) || (this._to && year > this._to)) { + + // allow for reversed range with from > to + var from = this._to < this._from ? this._to : this._from; + var to = this._to < this._from ? this._from : this._to; + + if ((from && year < from) || (to && year > to)) { return true; } return false; From 528c40c8b3891d01819d3758ca74bc6a8b02ce69 Mon Sep 17 00:00:00 2001 From: colinf Date: Wed, 14 Nov 2012 17:41:27 +0000 Subject: [PATCH 3/3] improve return condition for invalidDate --- lib/calendar.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/calendar.js b/lib/calendar.js index 01aef27..475e06e 100644 --- a/lib/calendar.js +++ b/lib/calendar.js @@ -213,8 +213,5 @@ Calendar.prototype.invalidDate = function(date) { var from = this._to < this._from ? this._to : this._from; var to = this._to < this._from ? this._from : this._to; - if ((from && year < from) || (to && year > to)) { - return true; - } - return false; + return year < from || year > to; }