Datepicker: handling yearRange when generating the HTML (fixes #7782) #521

Closed
wants to merge 2 commits into from
View
41 ui/jquery.ui.datepicker.js
@@ -1424,12 +1424,17 @@ $.extend(Datepicker.prototype, {
new Date(inst.currentYear, inst.currentMonth, inst.currentDay)));
var minDate = this._getMinMaxDate(inst, 'min');
var maxDate = this._getMinMaxDate(inst, 'max');
+ var yearRange = this._get(inst, 'yearRange');
var drawMonth = inst.drawMonth - showCurrentAtPos;
var drawYear = inst.drawYear;
if (drawMonth < 0) {
drawMonth += 12;
drawYear--;
}
+ if (yearRange) {
+ var years = this._getYearRange(yearRange, minDate, maxDate);
+ drawYear = years[1];
+ }
if (maxDate) {
var maxDraw = this._daylightSavingAdjust(new Date(maxDate.getFullYear(),
maxDate.getMonth() - (numMonths[0] * numMonths[1]) + 1, maxDate.getDate()));
@@ -1614,22 +1619,12 @@ $.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._get(inst, 'yearRange');
+ var years = this._getYearRange(yearRange, minDate, maxDate);
inst.yearshtml += '<select class="ui-datepicker-year" ' +
'onchange="DP_jQuery_' + dpuuid + '.datepicker._selectMonthYear(\'#' + inst.id + '\', this, \'Y\');" ' +
'>';
- for (; year <= endYear; year++) {
+ for (var year = years[0]; year <= years[1]; year++) {
inst.yearshtml += '<option value="' + year + '"' +
(year == drawYear ? ' selected="selected"' : '') +
'>' + year + '</option>';
@@ -1647,6 +1642,26 @@ $.extend(Datepicker.prototype, {
return html;
},
+ /*
+ * Computes the correct year range according to yearRange, minDate and maxDate
+ * Returns [startYear, endYear]
+ */
+ _getYearRange: function(yearRange, minDate, maxDate) {
+ var years = 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 startYear = determineYear(years[0]);
+ var endYear = Math.max(startYear, determineYear(years[1] || ''));
+ startYear = (minDate ? Math.max(startYear, minDate.getFullYear()) : startYear);
+ endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);
+ return [startYear, endYear];
+ },
+
/* Adjust one of the date sub-fields. */
_adjustInstDate: function(inst, offset, period) {
var year = inst.drawYear + (period == 'Y' ? offset : 0);