Skip to content
Browse files

Ticket #7362: refactored the calculation of the year range into it's …

…own function so it can be reused in _isInRange(), also fixed unit test, as well as fixed a few issues with the options: miscellaneous unit test
  • Loading branch information...
1 parent 5ef3cdf commit 26827fa4afaad51f682376313c61e2313ac6392b Jay Merrifield committed Jun 13, 2011
View
4 tests/unit/datepicker/datepicker_options.js
@@ -252,7 +252,6 @@ test('defaultDate', function() {
});
test('miscellaneous', function() {
- var dp = $('#ui-datepicker-div');
var inp = init('#inp');
// Year range
var genRange = function(start, offset) {
@@ -264,6 +263,7 @@ test('miscellaneous', function() {
};
var curYear = new Date().getFullYear();
inp.val('02/04/2008').datepicker('show');
+ var dp = $('#ui-datepicker-div');
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');
@@ -300,7 +300,7 @@ test('miscellaneous', function() {
longNames[date.getMonth()], 'Navigation current - as date format + pgdn');
equals(dp.find('.ui-datepicker-next').text(),
shortNames[3] + ' >', 'Navigation next - as date format + pgdn');
- inp.datepicker('hide').datepicker('option', {gotoCurrent: true}).
+ inp.datepicker('hide').datepicker('option', {gotoCurrent: true, yearRange: '2007:2009'}).
val('02/04/2008').datepicker('show');
equals(dp.find('.ui-datepicker-prev').text(),
'< ' + shortNames[0], 'Navigation prev - as date format + goto current');
View
5 tests/unit/datepicker/datepicker_tickets.js
@@ -29,11 +29,10 @@ test('Ticket 7362: Able to navigate past the year range which causes weird behav
changeYear: true,
changeMonth: true,
yearRange: '-0:+1',
- defaultDate: '01/01/' + new Date().getFullYear(),
- altField: '#disp'
+ defaultDate: new Date(new Date().getFullYear(), 0, 1)
});
ok(d.find(".ui-datepicker-prev").hasClass("ui-state-disabled"), "previous button disabled");
- d.datepicker("setDate", "12/30/" + new Date().getFullYear());
+ d.datepicker("setDate", new Date(new Date().getFullYear() + 1, 11, 30));
ok(d.find(".ui-datepicker-next").hasClass("ui-state-disabled"), "next button disabled");
d.remove();
});
View
51 ui/jquery.ui.datepicker.js
@@ -1548,6 +1548,27 @@ $.extend(Datepicker.prototype, {
return html;
},
+ _calculateYearRange: function(minDate, maxDate, drawYear, yearRange){
+ var rangeNotation = yearRange.split(':');
+ var thisYear = new Date().getFullYear();
+ var calculateFn = function(notation) {
+ var year;
+ if (notation.match(/c[+-].*/)){
+ year = drawYear + parseInt(notation.substring(1), 10);
+ }else if (notation.match(/[+-].*/)){
+ year = thisYear + parseInt(notation, 10);
+ }else{
+ year = parseInt(notation, 10);
+ }
+ return (isNaN(year) ? thisYear : year);
+ };
+ var minYear = calculateFn(rangeNotation[0]);
+ minYear = (minDate ? Math.max(minYear, minDate.getFullYear()) : minYear);
+ var maxYear = Math.max(minYear, calculateFn(rangeNotation[1] || ''));
+ maxYear = (maxDate ? Math.min(maxYear, maxDate.getFullYear()) : maxYear);
+ return {minYear: minYear, maxYear: maxYear};
+ },
+
/* Generate the month and year header. */
_generateMonthYearHeader: function(inst, drawMonth, drawYear, minDate, maxDate,
secondary, monthNames, monthNamesShort) {
@@ -1584,23 +1605,13 @@ $.extend(Datepicker.prototype, {
html += '<span class="ui-datepicker-year">' + drawYear + '</span>';
else {
// determine range of years to display
- var years = this._get(inst, 'yearRange').split(':');
- 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);
+ var yearRange = this._calculateYearRange(minDate, maxDate, drawYear, this._get(inst, 'yearRange'));
inst.yearshtml += '<select class="ui-datepicker-year" ' +
'onchange="DP_jQuery_' + dpuuid + '.datepicker._selectMonthYear(\'#' + inst.id + '\', this, \'Y\');" ' +
'onclick="DP_jQuery_' + dpuuid + '.datepicker._clickMonthYear(\'#' + inst.id + '\');"' +
'>';
- for (; year <= endYear; year++) {
+ var year = yearRange.minYear;
+ for (; year <= yearRange.maxYear; year++) {
inst.yearshtml += '<option value="' + year + '"' +
(year == drawYear ? ' selected="selected"' : '') +
'>' + year + '</option>';
@@ -1685,20 +1696,12 @@ $.extend(Datepicker.prototype, {
_isInRange: function(inst, date) {
var minDate = this._getMinMaxDate(inst, 'min');
var maxDate = this._getMinMaxDate(inst, 'max');
- var minYear = null;
- var maxYear = null;
- var years = this._get(inst, 'yearRange');
- if (years){
- var yearSplit = years.split(':');
- var currentYear = new Date().getFullYear();
- minYear = parseInt(yearSplit[0]) + currentYear;
- maxYear = parseInt(yearSplit[1]) + currentYear;
- }
+ var yearRange = this._calculateYearRange(minDate, maxDate, inst.drawYear, this._get(inst, 'yearRange'));
return ((!minDate || date.getTime() >= minDate.getTime()) &&
(!maxDate || date.getTime() <= maxDate.getTime()) &&
- (!minYear || date.getFullYear() >= minYear) &&
- (!maxYear || date.getFullYear() <= maxYear));
+ (date.getFullYear() >= yearRange.minYear) &&
+ (date.getFullYear() <= yearRange.maxYear));
},
/* Provide the configuration settings for formatting/parsing. */

0 comments on commit 26827fa

Please sign in to comment.
Something went wrong with that request. Please try again.