]> source.dussan.org Git - jquery-ui.git/commitdiff
Datepicker - Fixed #4870 - yearRange doesn't work as expected
authorKeith Wood <kbwood.au@gmail.com>
Tue, 1 Dec 2009 01:27:25 +0000 (01:27 +0000)
committerKeith Wood <kbwood.au@gmail.com>
Tue, 1 Dec 2009 01:27:25 +0000 (01:27 +0000)
tests/unit/datepicker/datepicker_options.js
ui/jquery.ui.datepicker.js

index 8a716144b1b3ed6777bdd4387cd947fc6b0df292..5bdf415e06ba06fa13914548401fabbe27d0096a 100644 (file)
@@ -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});
index 757692cd4868e5cab4de988d4844d7d9c86338c3..92585547f64109f37a206465364e971109b67022 100644 (file)
@@ -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 += '<select class="ui-datepicker-year" ' +