From d33c2de502b124bfe5067c677878b96f906cd1bd Mon Sep 17 00:00:00 2001 From: Keith Wood Date: Tue, 1 Dec 2009 01:27:25 +0000 Subject: [PATCH] Datepicker - Fixed #4870 - yearRange doesn't work as expected --- tests/unit/datepicker/datepicker_options.js | 11 +++++++-- ui/jquery.ui.datepicker.js | 26 ++++++++++----------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/tests/unit/datepicker/datepicker_options.js b/tests/unit/datepicker/datepicker_options.js index 8a716144b..5bdf415e0 100644 --- a/tests/unit/datepicker/datepicker_options.js +++ b/tests/unit/datepicker/datepicker_options.js @@ -267,14 +267,21 @@ test('miscellaneous', function() { } return range; }; + var curYear = new Date().getFullYear(); inp.val('02/04/2008').datepicker('show'); equals(dp.find('.ui-datepicker-year').text(), '2008', 'Year range - read-only default'); inp.datepicker('hide').datepicker('option', {changeYear: true}).datepicker('show'); 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'); - equals(dp.find('.ui-datepicker-year').text(), genRange(2008 - 6, 9), 'Year range - -6:+2'); + 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 - c-6:c+2'); 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'); + 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 inp.datepicker('option', {showButtonPanel: true}); diff --git a/ui/jquery.ui.datepicker.js b/ui/jquery.ui.datepicker.js index 757692cd4..92585547f 100644 --- a/ui/jquery.ui.datepicker.js +++ b/ui/jquery.ui.datepicker.js @@ -74,8 +74,9 @@ function Datepicker() { 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 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, - // either relative to current year (-nn:+nn) or absolute (nnnn:nnnn) + yearRange: 'c-10:c+10', // Range of years to display in drop-down, + // 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 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 @@ -1511,18 +1512,15 @@ $.extend(Datepicker.prototype, { else { // determine range of years to display var years = this._get(inst, 'yearRange').split(':'); - var year = 0; - var endYear = 0; - if (years.length != 2) { - year = drawYear - 10; - endYear = drawYear + 10; - } else if (years[0].charAt(0) == '+' || years[0].charAt(0) == '-') { - year = drawYear + parseInt(years[0], 10); - endYear = drawYear + parseInt(years[1], 10); - } else { - year = parseInt(years[0], 10); - endYear = parseInt(years[1], 10); - } + var thisYear = new Date().getFullYear(); + var determineYear = function(value) { + var year = (value.match(/c[+-].*/) ? drawYear + parseInt(value.substring(1), 10) : + (value.match(/[+-].*/) ? thisYear + parseInt(value, 10) : + parseInt(value, 10))); + return (isNaN(year) ? thisYear : year); + }; + var year = determineYear(years[0]); + var endYear = Math.max(year, determineYear(years[1] || '')); year = (minDate ? Math.max(year, minDate.getFullYear()) : year); endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear); html += '