Datepicker - Fixed #4870 - yearRange doesn't work as expected

This commit is contained in:
Keith Wood 2009-12-01 01:27:25 +00:00
parent fe217c4745
commit d33c2de502
2 changed files with 21 additions and 16 deletions

View File

@ -267,14 +267,21 @@ test('miscellaneous', function() {
} }
return range; return range;
}; };
var curYear = new Date().getFullYear();
inp.val('02/04/2008').datepicker('show'); inp.val('02/04/2008').datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), '2008', 'Year range - read-only default'); equals(dp.find('.ui-datepicker-year').text(), '2008', 'Year range - read-only default');
inp.datepicker('hide').datepicker('option', {changeYear: true}).datepicker('show'); inp.datepicker('hide').datepicker('option', {changeYear: true}).datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), genRange(2008 - 10, 21), 'Year range - changeable default'); equals(dp.find('.ui-datepicker-year').text(), genRange(2008 - 10, 21), 'Year range - changeable default');
inp.datepicker('hide').datepicker('option', {yearRange: '-6:+2', changeYear: true}).datepicker('show'); inp.datepicker('hide').datepicker('option', {yearRange: 'c-6:c+2', changeYear: true}).datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), genRange(2008 - 6, 9), 'Year range - -6:+2'); equals(dp.find('.ui-datepicker-year').text(), genRange(2008 - 6, 9), 'Year range - c-6:c+2');
inp.datepicker('hide').datepicker('option', {yearRange: '2000:2010', changeYear: true}).datepicker('show'); inp.datepicker('hide').datepicker('option', {yearRange: '2000:2010', changeYear: true}).datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), genRange(2000, 11), 'Year range - 2000:2010'); equals(dp.find('.ui-datepicker-year').text(), genRange(2000, 11), 'Year range - 2000:2010');
inp.datepicker('hide').datepicker('option', {yearRange: '-5:+3', changeYear: true}).datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), genRange(curYear - 5, 9), 'Year range - -5:+3');
inp.datepicker('hide').datepicker('option', {yearRange: '2000:-5', changeYear: true}).datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), genRange(2000, curYear - 2004), 'Year range - 2000:-5');
inp.datepicker('hide').datepicker('option', {yearRange: '', changeYear: true}).datepicker('show');
equals(dp.find('.ui-datepicker-year').text(), genRange(curYear, 1), 'Year range - -6:+2');
// Navigation as date format // Navigation as date format
inp.datepicker('option', {showButtonPanel: true}); inp.datepicker('option', {showButtonPanel: true});

View File

@ -74,8 +74,9 @@ function Datepicker() {
gotoCurrent: false, // True if today link goes back to current selection instead gotoCurrent: false, // True if today link goes back to current selection instead
changeMonth: false, // True if month can be selected directly, false if only prev/next changeMonth: false, // True if month can be selected directly, false if only prev/next
changeYear: false, // True if year can be selected directly, false if only prev/next changeYear: false, // True if year can be selected directly, false if only prev/next
yearRange: '-10:+10', // Range of years to display in drop-down, yearRange: 'c-10:c+10', // Range of years to display in drop-down,
// either relative to current year (-nn:+nn) or absolute (nnnn:nnnn) // either relative to today's year (-nn:+nn), relative to currently displayed year
// (c-nn:c+nn), absolute (nnnn:nnnn), or a combination of the above (nnnn:-n)
showOtherMonths: false, // True to show dates in other months, false to leave blank showOtherMonths: false, // True to show dates in other months, false to leave blank
selectOtherMonths: false, // True to allow selection of dates in other months, false for unselectable selectOtherMonths: false, // True to allow selection of dates in other months, false for unselectable
showWeek: false, // True to show week of the year, false to not show it showWeek: false, // True to show week of the year, false to not show it
@ -1511,18 +1512,15 @@ $.extend(Datepicker.prototype, {
else { else {
// determine range of years to display // determine range of years to display
var years = this._get(inst, 'yearRange').split(':'); var years = this._get(inst, 'yearRange').split(':');
var year = 0; var thisYear = new Date().getFullYear();
var endYear = 0; var determineYear = function(value) {
if (years.length != 2) { var year = (value.match(/c[+-].*/) ? drawYear + parseInt(value.substring(1), 10) :
year = drawYear - 10; (value.match(/[+-].*/) ? thisYear + parseInt(value, 10) :
endYear = drawYear + 10; parseInt(value, 10)));
} else if (years[0].charAt(0) == '+' || years[0].charAt(0) == '-') { return (isNaN(year) ? thisYear : year);
year = drawYear + parseInt(years[0], 10); };
endYear = drawYear + parseInt(years[1], 10); var year = determineYear(years[0]);
} else { var endYear = Math.max(year, determineYear(years[1] || ''));
year = parseInt(years[0], 10);
endYear = parseInt(years[1], 10);
}
year = (minDate ? Math.max(year, minDate.getFullYear()) : year); year = (minDate ? Math.max(year, minDate.getFullYear()) : year);
endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear); endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);
html += '<select class="ui-datepicker-year" ' + html += '<select class="ui-datepicker-year" ' +