Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constrain view to specified from/to years #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -194,3 +198,23 @@ 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();

// 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think we need reversing really, it would be pretty weird to specify them in reverse IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using calendar in a colinf/datepicker. And for example when selecting date of birth, it is a common requirement especially when dealing with children to list years in reverse starting from current year.

BTW reversing can only be invoked if you accept my pull request (or an improved version of it!) to component/range.

Cheers, Colin

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see what you mean, that makes sense


if ((from && year < from) || (to && year > to)) {
return true;
}
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be return (from && year < from) || (to && year > to) or just year < from || year > to since we're defaulting from/to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better - thanks. Will fix shortly.

}