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

suggested fix for #124 #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 40 additions & 3 deletions paper-calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@
type: Date,
value: null
},
/**
*Array of selectable dates
* (whitelist)
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add Format must be String YYYY-MM-DD here as well as on invalidDates so it's documented for people using this element stand-alone.

validDates:{
type: Array,
value: []
},
/**
* Array of not selectable dates
* (blacklist)
*/
invalidDates:{
type: Array,
value: []
},
/**
* The currently selected month (1-12)
*/
Expand Down Expand Up @@ -431,7 +447,7 @@
return cssClass;
},
_isDisabled: function(day, date) {
return !day || !this._withinValidRange(date);
return !day || !this._withinValidRange(date) || ! this._confirmsToWhiteAndBlacklist(date);
},
_getMonthClass: function(name, month) {
return name + ' month-' + month.year + '-' + month.month;
Expand Down Expand Up @@ -628,6 +644,7 @@
console.warn('Date outside of valid range: ' + date);
this.date = date = oldValue;
}

this.currentYear = date.getFullYear();
this.currentMonth = date.getMonth() + 1;
// Only trigger a notification if there actually is a difference.
Expand All @@ -637,7 +654,7 @@
this._updateSelection();
},
_tapDay: function(event) {
if (!this._withinValidRange(event.model.item.date)) {
if (!this._withinValidRange(event.model.item.date) || !this._confirmsToWhiteAndBlacklist(event.model.item.date)) {
return false;
}
var item = event.model.item;
Expand All @@ -652,7 +669,27 @@
},
_withinValidRange: function(date) {
if (this._isValidDate(date)) {
return (!this.minDate || date >= this.minDate) && (!this.maxDate || date <= this.maxDate);
return (!this.minDate || date >= this.minDate) && (!this.maxDate || date <= this.maxDate);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove double space

}
return false;
},

_confirmsToWhiteAndBlacklist: function(date){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Collapse these 3 into:

/**
 * Ensures that our date is valid and passes both white and black list checks.
 * @protected
 *
 * @param {Date} date
 * @return {Boolean}
 */
_checkWhiteBlackLists: function(date) {
  return this._isValidDate(date) &&
    (this.validDates || []).indexOf(this.dateFormat(date,'YYYY-MM-DD')) < 0 &&
    (this.invalidDates || []).indexOf(this.dateFormat(date,'YYYY-MM-DD')) < 0;
}

So we aren't biting the cost of always checking both lists, and checking for a valid date 3 times when we don't have to.

if(this._isValidDate(date)) {
var result = !this._isInBlacklist(date) && this._isInWhitelist(date);
return result;
}
return false;
},
_isInWhitelist: function(date){
if(this.validDates && this.validDates.length > 0 && this._isValidDate(date)) {
return this.validDates.indexOf(this.dateFormat(date,'YYYY-MM-DD')) >= 0;
}
return true;
},
_isInBlacklist: function(date){
if(this.invalidDates && this.invalidDates.length > 0 && this._isValidDate(date)) {
return this.invalidDates.indexOf(this.dateFormat(date,'YYYY-MM-DD')) >= 0;
}
return false;
},
Expand Down
20 changes: 19 additions & 1 deletion paper-date-picker.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
on-iron-select="_pageSelected">
<neon-animatable id="chooseDate">
<paper-calendar id="calendar" locale="{{locale}}" date="{{date}}"
min-date="{{minDate}}" max-date="{{maxDate}}">
min-date="{{minDate}}" max-date="{{maxDate}}" invalid-dates="{{invalidDates}}" valid-dates="{{validDates}}">
</paper-calendar>
</neon-animatable>
<neon-animatable id="chooseYear">
Expand Down Expand Up @@ -220,6 +220,24 @@
type: Date,
value: null
},
/**
* Array of selectable dates
* (whitelist)
* Format must be String YYYY-MM-DD
*/
validDates:{
type: Array,
value: []
},
/**
* Array of not selectable dates
* (blacklist)
* Format must be String YYYY-MM-DD
*/
invalidDates:{
type: Array,
value: []
},
/**
* Force narrow layout
*/
Expand Down