Skip to content

Commit 39a8856

Browse files
committed
MAGETWO-53299: "Go Today" button is not working for dates range component
1 parent ca0feac commit 39a8856

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

app/code/Magento/Backend/view/adminhtml/templates/page/js/calendar.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require([
5151
showHour: false,
5252
showMinute: false,
5353
serverTimezoneSeconds: <?php echo (int) $block->getStoreTimestamp(); ?>,
54+
serverTimezoneOffset: <?php echo (int) $block->getTimezoneOffsetSeconds(); ?>,
5455
yearRange: '<?php /* @escapeNotVerified */ echo $block->getYearRange() ?>'
5556
}
5657
});

dev/tests/js/JsTestDriver/testsuite/mage/calendar/calendar-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ CalendarTest.prototype.testWithServerTimezoneOffset = function() {
5858
assertEquals(true, currentDate.toString() === calendar.calendar('getTimezoneDate').toString());
5959
calendar.calendar('destroy');
6060
};
61+
CalendarTest.prototype.testWithServerTimezoneShift = function() {
62+
/*:DOC += <input type="text" id="calendar" /> */
63+
var serverTimezoneOffset = 43200,
64+
calendar = $('#calendar').calendar({serverTimezoneOffset: serverTimezoneOffset}),
65+
currentDate = new Date();
66+
67+
setTimeout(function () {
68+
currentDate.setTime(currentDate.getTime() + (serverTimezoneOffset + currentDate.getTimezoneOffset() * 60) * 1000);
69+
assertEquals(true, currentDate.toString() === calendar.calendar('getTimezoneDate').toString());
70+
calendar.calendar('destroy');
71+
}, 61000);
72+
};
6173
CalendarTest.prototype.testWithoutServerTimezoneOffset = function() {
6274
/*:DOC += <input type="text" id="calendar" /> */
6375
var calendar = $('#calendar').calendar(),

lib/web/mage/calendar.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,46 @@
2121
}(function ($) {
2222
'use strict';
2323

24-
var calendarBasePrototype;
24+
var calendarBasePrototype,
25+
datepickerPrototype = $.datepicker.constructor.prototype;
2526

2627
/**
27-
* Get date/time according to store settings
28-
* @returns {Date}
28+
* Extend JQuery date picker prototype with store local time methods
2929
*/
30-
function getTimezoneDate() {
31-
// Get local time in ms
32-
var ms = Date.now(),
33-
options = $.calendarConfig ? $.calendarConfig : {};
34-
35-
// Use store timestamp based on store timezone settings
36-
if (typeof options.serverTimezoneSeconds !== 'undefined') {
37-
//Adjust milliseconds according to client local timezone offset
38-
ms = (options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000;
39-
}
30+
$.extend(datepickerPrototype, {
4031

41-
return new Date(ms);
42-
}
32+
/**
33+
* Get date/time according to store settings.
34+
* We use serverTimezoneOffset (in seconds) instead of serverTimezoneSeconds
35+
* in order to have ability to know actual store time even if page hadn't been reloaded
36+
* @returns {Date}
37+
*/
38+
_getTimezoneDate: function () {
39+
// local time in ms
40+
var ms = Date.now(),
41+
options = $.calendarConfig ? $.calendarConfig : {};
42+
43+
// Adjust milliseconds according to store timezone offset,
44+
// mind the GMT zero offset
45+
if (typeof options.serverTimezoneOffset !== 'undefined') {
46+
// Make UTC time and add store timezone offset in seconds
47+
ms += new Date().getTimezoneOffset() * 60 * 1000 + options.serverTimezoneOffset * 1000;
48+
} else if (typeof this.options.serverTimezoneSeconds !== 'undefined') {
49+
//Set milliseconds according to client local timezone offset
50+
ms = (this.options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000;
51+
}
52+
53+
return new Date(ms);
54+
},
55+
56+
/**
57+
* Set date/time according to store settings.
58+
* @param {String|Object} target - the target input field or division or span
59+
*/
60+
_setTimezoneDateDatepicker: function (target) {
61+
this._setDateDatepicker(target, this._getTimezoneDate());
62+
}
63+
});
4364

4465
$.datepicker.markerClassName = '_has-datepicker';
4566

@@ -99,7 +120,7 @@
99120
* @return {String} html template
100121
*/
101122
$.datepicker.constructor.prototype._generateHTML = function (inst) {
102-
var today = getTimezoneDate(),
123+
var today = this._getTimezoneDate(),
103124
isRTL = this._get(inst, 'isRTL'),
104125
showButtonPanel = this._get(inst, 'showButtonPanel'),
105126
hideIfNoPrevNext = this._get(inst, 'hideIfNoPrevNext'),
@@ -339,8 +360,7 @@
339360
*/
340361
_setCurrentDate: function (element) {
341362
if (!element.val()) {
342-
element[this._picker()]('setDate', getTimezoneDate())
343-
.val('');
363+
element[this._picker()]('setTimezoneDate').val('');
344364
}
345365
},
346366

@@ -523,7 +543,7 @@
523543
var pickerObject = $(el);
524544

525545
//Set date/time according to timezone offset
526-
pickerObject.datepicker('setDate', getTimezoneDate())
546+
pickerObject.datepicker('setTimezoneDate')
527547
// To ensure that user can re-select date field without clicking outside it first.
528548
.blur();
529549
};

0 commit comments

Comments
 (0)