|
21 | 21 | }(function ($) {
|
22 | 22 | 'use strict';
|
23 | 23 |
|
24 |
| - var calendarBasePrototype; |
| 24 | + var calendarBasePrototype, |
| 25 | + datepickerPrototype = $.datepicker.constructor.prototype; |
25 | 26 |
|
26 | 27 | $.datepicker.markerClassName = '_has-datepicker';
|
27 | 28 |
|
| 29 | + /** |
| 30 | + * Extend JQuery date picker prototype with store local time methods |
| 31 | + */ |
| 32 | + $.extend(datepickerPrototype, { |
| 33 | + /** |
| 34 | + * Get date/time according to store settings. |
| 35 | + * We use serverTimezoneOffset (in seconds) instead of serverTimezoneSeconds |
| 36 | + * in order to have ability to know actual store time even if page hadn't been reloaded |
| 37 | + * @returns {Date} |
| 38 | + */ |
| 39 | + _getTimezoneDate: function (options) { |
| 40 | + // local time in ms |
| 41 | + var ms = Date.now(); |
| 42 | + |
| 43 | + options = options || $.calendarConfig || {}; |
| 44 | + |
| 45 | + // Adjust milliseconds according to store timezone offset, |
| 46 | + // mind the GMT zero offset |
| 47 | + if (typeof options.serverTimezoneOffset !== 'undefined') { |
| 48 | + // Make UTC time and add store timezone offset in seconds |
| 49 | + ms += new Date().getTimezoneOffset() * 60 * 1000 + options.serverTimezoneOffset * 1000; |
| 50 | + } else if (typeof options.serverTimezoneSeconds !== 'undefined') { |
| 51 | + //Set milliseconds according to client local timezone offset |
| 52 | + ms = (options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000; |
| 53 | + } |
| 54 | + |
| 55 | + return new Date(ms); |
| 56 | + }, |
| 57 | + |
| 58 | + /** |
| 59 | + * Set date/time according to store settings. |
| 60 | + * @param {String|Object} target - the target input field or division or span |
| 61 | + */ |
| 62 | + _setTimezoneDateDatepicker: function (target) { |
| 63 | + this._setDateDatepicker(target, this._getTimezoneDate()); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
28 | 67 | /**
|
29 | 68 | * Widget calendar
|
30 | 69 | */
|
|
72 | 111 | * Wrapper for overwrite jQuery UI datepicker function.
|
73 | 112 | */
|
74 | 113 | _overwriteGenerateHtml: function () {
|
75 |
| - var self = this; |
76 |
| - |
77 | 114 | /**
|
78 | 115 | * Overwrite jQuery UI datepicker function.
|
79 | 116 | * Reason: magento date could be set before calendar show
|
|
83 | 120 | * @return {String} html template
|
84 | 121 | */
|
85 | 122 | $.datepicker.constructor.prototype._generateHTML = function (inst) {
|
86 |
| - var today = self.getTimezoneDate(), |
| 123 | + var today = this._getTimezoneDate(), |
87 | 124 | isRTL = this._get(inst, 'isRTL'),
|
88 | 125 | showButtonPanel = this._get(inst, 'showButtonPanel'),
|
89 | 126 | hideIfNoPrevNext = this._get(inst, 'hideIfNoPrevNext'),
|
|
316 | 353 | };
|
317 | 354 | },
|
318 | 355 |
|
319 |
| - /** |
320 |
| - * If server timezone is defined then take to account server timezone shift |
321 |
| - * @return {Object} date |
322 |
| - */ |
323 |
| - getTimezoneDate: function () { |
324 |
| - |
325 |
| - // Get local time in ms |
326 |
| - var ms = Date.now(); |
327 |
| - |
328 |
| - // Use store timestamp based on store timezone settings |
329 |
| - if (typeof this.options.serverTimezoneSeconds !== 'undefined') { |
330 |
| - //Adjust milliseconds according to client local timezone offset |
331 |
| - ms = (this.options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000; |
332 |
| - } |
333 |
| - |
334 |
| - return new Date(ms); |
335 |
| - }, |
336 |
| - |
337 | 356 | /**
|
338 | 357 | * Set current date if the date is not set
|
339 | 358 | * @protected
|
340 | 359 | * @param {Object} element
|
341 | 360 | */
|
342 | 361 | _setCurrentDate: function (element) {
|
343 | 362 | if (!element.val()) {
|
344 |
| - element[this._picker()]('setDate', this.getTimezoneDate()) |
345 |
| - .val(''); |
| 363 | + element[this._picker()]('setTimezoneDate').val(''); |
346 | 364 | }
|
347 | 365 | },
|
348 | 366 |
|
|
356 | 374 | pickerButtonText = picker.next('.ui-datepicker-trigger')
|
357 | 375 | .find('img')
|
358 | 376 | .attr('title');
|
| 377 | + |
359 | 378 | picker.next('.ui-datepicker-trigger')
|
360 | 379 | .addClass('v-middle')
|
361 | 380 | .text('') // Remove jQuery UI datepicker generated image
|
|
369 | 388 | _destroy: function () {
|
370 | 389 | this.element[this._picker()]('destroy');
|
371 | 390 | this._super();
|
| 391 | + }, |
| 392 | + |
| 393 | + /** |
| 394 | + * Method is kept for backward compatibility and unit-tests acceptance |
| 395 | + * see \mage\calendar\calendar-test.js |
| 396 | + * @return {Object} date |
| 397 | + */ |
| 398 | + getTimezoneDate: function () { |
| 399 | + return datepickerPrototype._getTimezoneDate.call(this, this.options); |
372 | 400 | }
|
373 | 401 | });
|
374 | 402 |
|
|
521 | 549 | * @param {Object} el
|
522 | 550 | */
|
523 | 551 | $.datepicker._gotoToday = function (el) {
|
524 |
| - var pickerObject = $(el); |
525 |
| - |
526 |
| - // Set date/time according to timezone offset |
527 |
| - pickerObject.datepicker( "setDate", pickerObject.calendar("getTimezoneDate") ) |
| 552 | + //Set date/time according to timezone offset |
| 553 | + $(el).datepicker('setTimezoneDate') |
528 | 554 | // To ensure that user can re-select date field without clicking outside it first.
|
529 | 555 | .blur();
|
530 | 556 | };
|
|
0 commit comments