diff options
Diffstat (limited to 'template-common/lib/ui/ui.datepicker.js')
-rw-r--r-- | template-common/lib/ui/ui.datepicker.js | 100 |
1 files changed, 59 insertions, 41 deletions
diff --git a/template-common/lib/ui/ui.datepicker.js b/template-common/lib/ui/ui.datepicker.js index 2ddca2889..877e19b52 100644 --- a/template-common/lib/ui/ui.datepicker.js +++ b/template-common/lib/ui/ui.datepicker.js @@ -233,12 +233,6 @@ $.extend(Datepicker.prototype, { this._updateDatepicker(inst); }, - /* Tidy up after displaying the date picker. */ - _inlineShow: function(inst) { - var numMonths = this._getNumberOfMonths(inst); // fix width for dynamic number of date pickers - inst.dpDiv.width(numMonths[1] * $('.ui-datepicker', inst.dpDiv[0]).width()); - }, - /* Pop-up the date picker in a "dialog" box. @param input element - ignored @param dateText string - the initial date to display (in the current format) @@ -688,7 +682,8 @@ $.extend(Datepicker.prototype, { this._selectDate(id, this._formatDate(inst, inst.currentDay, inst.currentMonth, inst.currentYear)); if (this._stayOpen) { - inst.rangeStart = new Date(inst.currentYear, inst.currentMonth, inst.currentDay); + inst.rangeStart = this._daylightSavingAdjust( + new Date(inst.currentYear, inst.currentMonth, inst.currentDay)); this._updateDatepicker(inst); } else if (rangeSelect) { @@ -766,7 +761,7 @@ $.extend(Datepicker.prototype, { @param date Date - the date to get the week for @return number - the number of the week within the year that contains this date */ iso8601Week: function(date) { - var checkDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), (date.getTimezoneOffset() / -60)); + var checkDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()); var firstMon = new Date(checkDate.getFullYear(), 1 - 1, 4); // First week always contains 4 Jan var firstDay = firstMon.getDay() || 7; // Day of week: Mon = 1, ..., Sun = 7 firstMon.setDate(firstMon.getDate() + 1 - firstDay); // Preceding Monday @@ -907,7 +902,7 @@ $.extend(Datepicker.prototype, { if (year < 100) year += new Date().getFullYear() - new Date().getFullYear() % 100 + (year <= shortYearCutoff ? 0 : -100); - var date = new Date(year, month - 1, day); + var date = this._daylightSavingAdjust(new Date(year, month - 1, day)); if (date.getFullYear() != year || date.getMonth() + 1 != month || date.getDate() != day) throw 'Invalid date'; // E.g. 31/02/* return date; @@ -1094,7 +1089,7 @@ $.extend(Datepicker.prototype, { _determineDate: function(date, defaultDate) { var offsetNumeric = function(offset) { var date = new Date(); - date.setUTCDate(date.getUTCDate() + offset); + date.setDate(date.getDate() + offset); return date; }; var offsetString = function(offset, getDaysInMonth) { @@ -1123,9 +1118,29 @@ $.extend(Datepicker.prototype, { } return new Date(year, month, day); }; - return (date == null ? defaultDate : + date = (date == null ? defaultDate : (typeof date == 'string' ? offsetString(date, this._getDaysInMonth) : - (typeof date == 'number' ? offsetNumeric(date) : date))); + (typeof date == 'number' ? (isNaN(date) ? defaultDate : offsetNumeric(date)) : date))); + date = (date && date.toString() == 'Invalid Date' ? defaultDate : date); + if (date) { + date.setHours(0); + date.setMinutes(0); + date.setSeconds(0); + date.setMilliseconds(0); + } + return this._daylightSavingAdjust(date); + }, + + /* Handle switch to/from daylight saving. + Hours may be non-zero on daylight saving cut-over: + > 12 when midnight changeover, but then cannot generate + midnight datetime, so jump to 1AM, otherwise reset. + @param date (Date) the date to check + @return (Date) the corrected date */ + _daylightSavingAdjust: function(date) { + if (!date) return null; + date.setHours(date.getHours() > 12 ? date.getHours() + 2 : 0); + return date; }, /* Set the date(s) directly. */ @@ -1157,10 +1172,12 @@ $.extend(Datepicker.prototype, { /* Retrieve the date(s) directly. */ _getDate: function(inst) { var startDate = (!inst.currentYear || (inst.input && inst.input.val() == '') ? null : - new Date(inst.currentYear, inst.currentMonth, inst.currentDay)); + this._daylightSavingAdjust(new Date( + inst.currentYear, inst.currentMonth, inst.currentDay))); if (this._get(inst, 'rangeSelect')) { - return [inst.rangeStart || startDate, (!inst.endYear ? null : - new Date(inst.endYear, inst.endMonth, inst.endDay))]; + return [inst.rangeStart || startDate, + (!inst.endYear ? inst.rangeStart || startDate : + this._daylightSavingAdjust(new Date(inst.endYear, inst.endMonth, inst.endDay)))]; } else return startDate; }, @@ -1168,7 +1185,8 @@ $.extend(Datepicker.prototype, { /* Generate the HTML for the current state of the date picker. */ _generateDatepicker: function(inst) { var today = new Date(); - today = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // clear time + today = this._daylightSavingAdjust( + new Date(today.getFullYear(), today.getMonth(), today.getDate())); // clear time var showStatus = this._get(inst, 'showStatus'); var isRTL = this._get(inst, 'isRTL'); // build the date picker HTML @@ -1187,17 +1205,17 @@ $.extend(Datepicker.prototype, { var numMonths = this._getNumberOfMonths(inst); var stepMonths = this._get(inst, 'stepMonths'); var isMultiMonth = (numMonths[0] != 1 || numMonths[1] != 1); - var currentDate = (!inst.currentDay ? new Date(9999, 9, 9) : - new Date(inst.currentYear, inst.currentMonth, inst.currentDay)); + var currentDate = this._daylightSavingAdjust((!inst.currentDay ? new Date(9999, 9, 9) : + new Date(inst.currentYear, inst.currentMonth, inst.currentDay))); var minDate = this._getMinMaxDate(inst, 'min', true); var maxDate = this._getMinMaxDate(inst, 'max'); var drawMonth = inst.drawMonth; var drawYear = inst.drawYear; if (maxDate) { - var maxDraw = new Date(maxDate.getFullYear(), - maxDate.getMonth() - numMonths[1] + 1, maxDate.getDate()); + var maxDraw = this._daylightSavingAdjust(new Date(maxDate.getFullYear(), + maxDate.getMonth() - numMonths[1] + 1, maxDate.getDate())); maxDraw = (minDate && maxDraw < minDate ? minDate : maxDraw); - while (new Date(drawYear, drawMonth, 1) > maxDraw) { + while (this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1)) > maxDraw) { drawMonth--; if (drawMonth < 0) { drawMonth = 11; @@ -1207,15 +1225,17 @@ $.extend(Datepicker.prototype, { } // controls and links var prevText = this._get(inst, 'prevText'); - prevText = (!navigationAsDateFormat ? prevText : this.formatDate( - prevText, new Date(drawYear, drawMonth - stepMonths, 1), this._getFormatConfig(inst))); + prevText = (!navigationAsDateFormat ? prevText : this.formatDate(prevText, + this._daylightSavingAdjust(new Date(drawYear, drawMonth - stepMonths, 1)), + this._getFormatConfig(inst))); var prev = '<div class="ui-datepicker-prev">' + (this._canAdjustMonth(inst, -1, drawYear, drawMonth) ? '<a onclick="jQuery.datepicker._adjustDate(\'#' + inst.id + '\', -' + stepMonths + ', \'M\');"' + (showStatus ? this._addStatus(inst, this._get(inst, 'prevStatus') || ' ') : '') + '>' + prevText + '</a>' : (hideIfNoPrevNext ? '' : '<label>' + prevText + '</label>')) + '</div>'; var nextText = this._get(inst, 'nextText'); - nextText = (!navigationAsDateFormat ? nextText : this.formatDate( - nextText, new Date(drawYear, drawMonth + stepMonths, 1), this._getFormatConfig(inst))); + nextText = (!navigationAsDateFormat ? nextText : this.formatDate(nextText, + this._daylightSavingAdjust(new Date(drawYear, drawMonth + stepMonths, 1)), + this._getFormatConfig(inst))); var next = '<div class="ui-datepicker-next">' + (this._canAdjustMonth(inst, +1, drawYear, drawMonth) ? '<a onclick="jQuery.datepicker._adjustDate(\'#' + inst.id + '\', +' + stepMonths + ', \'M\');"' + (showStatus ? this._addStatus(inst, this._get(inst, 'nextStatus') || ' ') : '') + '>' + nextText + '</a>' : @@ -1244,10 +1264,11 @@ $.extend(Datepicker.prototype, { var calculateWeek = this._get(inst, 'calculateWeek') || this.iso8601Week; var status = (showStatus ? this._get(inst, 'dayStatus') || ' ' : ''); var dateStatus = this._get(inst, 'statusForDate') || this.dateStatus; - var endDate = inst.endDay ? new Date(inst.endYear, inst.endMonth, inst.endDay) : currentDate; + var endDate = inst.endDay ? this._daylightSavingAdjust( + new Date(inst.endYear, inst.endMonth, inst.endDay)) : currentDate; for (var row = 0; row < numMonths[0]; row++) for (var col = 0; col < numMonths[1]; col++) { - var selectedDate = new Date(drawYear, drawMonth, inst.selectedDay); + var selectedDate = this._daylightSavingAdjust(new Date(drawYear, drawMonth, inst.selectedDay)); html += '<div class="ui-datepicker-one-month' + (col == 0 ? ' ui-datepicker-new-row' : '') + '">' + this._generateMonthYearHeader(inst, drawMonth, drawYear, minDate, maxDate, selectedDate, row > 0 || col > 0, showStatus, monthNames) + // draw month headers @@ -1269,8 +1290,8 @@ $.extend(Datepicker.prototype, { if (drawYear == inst.selectedYear && drawMonth == inst.selectedMonth) inst.selectedDay = Math.min(inst.selectedDay, daysInMonth); var leadDays = (this._getFirstDayOfMonth(drawYear, drawMonth) - firstDay + 7) % 7; - var printDate = new Date(drawYear, drawMonth, 1 - leadDays); var numRows = (isMultiMonth ? 6 : Math.ceil((leadDays + daysInMonth) / 7)); // calculate the number of rows to generate + var printDate = this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1 - leadDays)); for (var dRow = 0; dRow < numRows; dRow++) { // create date picker rows html += '<tr class="ui-datepicker-days-row">' + (showWeeks ? '<td class="ui-datepicker-week-col">' + calculateWeek(printDate) + '</td>' : ''); @@ -1305,7 +1326,8 @@ $.extend(Datepicker.prototype, { inst.id + '\',' + drawMonth + ',' + drawYear + ', this);"') + '>' + // actions (otherMonth ? (showOtherMonths ? printDate.getDate() : ' ') : // display for other months (unselectable ? printDate.getDate() : '<a>' + printDate.getDate() + '</a>')) + '</td>'; // display for this month - printDate.setUTCDate(printDate.getUTCDate() + 1); + printDate.setDate(printDate.getDate() + 1); + printDate = this._daylightSavingAdjust(printDate); } html += '</tr>'; } @@ -1397,7 +1419,7 @@ $.extend(Datepicker.prototype, { var month = inst.drawMonth + (period == 'M' ? offset : 0); var day = Math.min(inst.selectedDay, this._getDaysInMonth(year, month)) + (period == 'D' ? offset : 0); - var date = new Date(year, month, day); + var date = this._daylightSavingAdjust(new Date(year, month, day)); // ensure it is within the bounds set var minDate = this._getMinMaxDate(inst, 'min', true); var maxDate = this._getMinMaxDate(inst, 'max'); @@ -1427,12 +1449,6 @@ $.extend(Datepicker.prototype, { /* Determine the current maximum date - ensure no time components are set - may be overridden for a range. */ _getMinMaxDate: function(inst, minMax, checkRange) { var date = this._determineDate(this._get(inst, minMax + 'Date'), null); - if (date) { - date.setHours(0); - date.setMinutes(0); - date.setSeconds(0); - date.setMilliseconds(0); - } return (!checkRange || !inst.rangeStart ? date : (!date || inst.rangeStart > date ? inst.rangeStart : date)); }, @@ -1450,7 +1466,8 @@ $.extend(Datepicker.prototype, { /* Determines if we should allow a "next/prev" month display change. */ _canAdjustMonth: function(inst, offset, curYear, curMonth) { var numMonths = this._getNumberOfMonths(inst); - var date = new Date(curYear, curMonth + (offset < 0 ? offset : numMonths[1]), 1); + var date = this._daylightSavingAdjust(new Date( + curYear, curMonth + (offset < 0 ? offset : numMonths[1]), 1)); if (offset < 0) date.setDate(this._getDaysInMonth(date.getFullYear(), date.getMonth())); return this._isInRange(inst, date); @@ -1459,8 +1476,8 @@ $.extend(Datepicker.prototype, { /* Is the given date in the accepted range? */ _isInRange: function(inst, date) { // during range selection, use minimum of selected date and range start - var newMinDate = (!inst.rangeStart ? null : - new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay)); + var newMinDate = (!inst.rangeStart ? null : this._daylightSavingAdjust( + new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay))); newMinDate = (newMinDate && inst.rangeStart < newMinDate ? inst.rangeStart : newMinDate); var minDate = newMinDate || this._getMinMaxDate(inst, 'min'); var maxDate = this._getMinMaxDate(inst, 'max'); @@ -1484,8 +1501,9 @@ $.extend(Datepicker.prototype, { inst.currentMonth = inst.selectedMonth; inst.currentYear = inst.selectedYear; } - var date = (day ? (typeof day == 'object' ? day : new Date(year, month, day)) : - new Date(inst.currentYear, inst.currentMonth, inst.currentDay)); + var date = (day ? (typeof day == 'object' ? day : + this._daylightSavingAdjust(new Date(year, month, day))) : + this._daylightSavingAdjust(new Date(inst.currentYear, inst.currentMonth, inst.currentDay))); return this.formatDate(this._get(inst, 'dateFormat'), date, this._getFormatConfig(inst)); } }); |