/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ /*eslint max-depth: 0*/ (function (factory) { 'use strict'; if (typeof define === 'function' && define.amd) { define([ 'jquery', 'jquery/ui', 'jquery/jquery-ui-timepicker-addon' ], factory); } else { factory(window.jQuery); } }(function ($) { 'use strict'; var calendarBasePrototype, datepickerPrototype = $.datepicker.constructor.prototype; $.datepicker.markerClassName = '_has-datepicker'; /** * Extend JQuery date picker prototype with store local time methods */ $.extend(datepickerPrototype, { /** * Get date/time according to store settings. * We use serverTimezoneOffset (in seconds) instead of serverTimezoneSeconds * in order to have ability to know actual store time even if page hadn't been reloaded * @returns {Date} */ _getTimezoneDate: function (options) { // local time in ms var ms = Date.now(); options = options || $.calendarConfig || {}; // Adjust milliseconds according to store timezone offset, // mind the GMT zero offset if (typeof options.serverTimezoneOffset !== 'undefined') { // Make UTC time and add store timezone offset in seconds ms += new Date().getTimezoneOffset() * 60 * 1000 + options.serverTimezoneOffset * 1000; } else if (typeof options.serverTimezoneSeconds !== 'undefined') { //Set milliseconds according to client local timezone offset ms = (options.serverTimezoneSeconds + new Date().getTimezoneOffset() * 60) * 1000; } return new Date(ms); }, /** * Set date/time according to store settings. * @param {String|Object} target - the target input field or division or span */ _setTimezoneDateDatepicker: function (target) { this._setDateDatepicker(target, this._getTimezoneDate()); } }); /** * Widget calendar */ $.widget('mage.calendar', { options: { autoComplete: true }, /** * Merge global options with options passed to widget invoke * @protected */ _create: function () { this._enableAMPM(); this.options = $.extend( {}, $.calendarConfig ? $.calendarConfig : {}, this.options.showsTime ? { showTime: true, showHour: true, showMinute: true } : {}, this.options ); this._initPicker(this.element); this._overwriteGenerateHtml(); }, /** * Get picker name * @protected */ _picker: function () { return this.options.showsTime ? 'datetimepicker' : 'datepicker'; }, /** * Fix for Timepicker - Set ampm option for Timepicker if timeformat contains string 'tt' * @protected */ _enableAMPM: function () { if (this.options.timeFormat && this.options.timeFormat.indexOf('tt') >= 0) { this.options.ampm = true; } }, /** * Wrapper for overwrite jQuery UI datepicker function. */ _overwriteGenerateHtml: function () { /** * Overwrite jQuery UI datepicker function. * Reason: magento date could be set before calendar show * but local date will be styled as current in original _generateHTML * * @param {Object} inst - instance datepicker. * @return {String} html template */ $.datepicker.constructor.prototype._generateHTML = function (inst) { var today = this._getTimezoneDate(), isRTL = this._get(inst, 'isRTL'), showButtonPanel = this._get(inst, 'showButtonPanel'), hideIfNoPrevNext = this._get(inst, 'hideIfNoPrevNext'), navigationAsDateFormat = this._get(inst, 'navigationAsDateFormat'), numMonths = this._getNumberOfMonths(inst), showCurrentAtPos = this._get(inst, 'showCurrentAtPos'), stepMonths = this._get(inst, 'stepMonths'), isMultiMonth = parseInt(numMonths[0], 10) !== 1 || parseInt(numMonths[1], 10) !== 1, currentDate = this._daylightSavingAdjust(!inst.currentDay ? new Date(9999, 9, 9) : new Date(inst.currentYear, inst.currentMonth, inst.currentDay)), minDate = this._getMinMaxDate(inst, 'min'), maxDate = this._getMinMaxDate(inst, 'max'), drawMonth = inst.drawMonth - showCurrentAtPos, drawYear = inst.drawYear, maxDraw, prevText = this._get(inst, 'prevText'), prev, nextText = this._get(inst, 'nextText'), next, currentText = this._get(inst, 'currentText'), gotoDate, controls, buttonPanel, firstDay, showWeek = this._get(inst, 'showWeek'), dayNames = this._get(inst, 'dayNames'), dayNamesMin = this._get(inst, 'dayNamesMin'), monthNames = this._get(inst, 'monthNames'), monthNamesShort = this._get(inst, 'monthNamesShort'), beforeShowDay = this._get(inst, 'beforeShowDay'), showOtherMonths = this._get(inst, 'showOtherMonths'), selectOtherMonths = this._get(inst, 'selectOtherMonths'), defaultDate = this._getDefaultDate(inst), html = '', row = 0, col = 0, selectedDate, cornerClass = ' ui-corner-all', group = '', calender = '', dow = 0, thead, day, daysInMonth, leadDays, curRows, numRows, printDate, dRow = 0, tbody, daySettings, otherMonth, unselectable; if (drawMonth < 0) { drawMonth += 12; drawYear--; } if (maxDate) { maxDraw = this._daylightSavingAdjust(new Date(maxDate.getFullYear(), maxDate.getMonth() - numMonths[0] * numMonths[1] + 1, maxDate.getDate())); maxDraw = minDate && maxDraw < minDate ? minDate : maxDraw; while (this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1)) > maxDraw) { drawMonth--; if (drawMonth < 0) { drawMonth = 11; drawYear--; } } } inst.drawMonth = drawMonth; inst.drawYear = drawYear; prevText = !navigationAsDateFormat ? prevText : this.formatDate(prevText, this._daylightSavingAdjust(new Date(drawYear, drawMonth - stepMonths, 1)), this._getFormatConfig(inst)); prev = this._canAdjustMonth(inst, -1, drawYear, drawMonth) ? '' + '' : hideIfNoPrevNext ? '' : ''; nextText = !navigationAsDateFormat ? nextText : this.formatDate(nextText, this._daylightSavingAdjust(new Date(drawYear, drawMonth + stepMonths, 1)), this._getFormatConfig(inst)); next = this._canAdjustMonth(inst, +1, drawYear, drawMonth) ? '' : hideIfNoPrevNext ? '' : '' + ''; gotoDate = this._get(inst, 'gotoCurrent') && inst.currentDay ? currentDate : today; currentText = !navigationAsDateFormat ? currentText : this.formatDate(currentText, gotoDate, this._getFormatConfig(inst)); controls = !inst.inline ? '' : ''; buttonPanel = showButtonPanel ? '
' : ''; firstDay = parseInt(this._get(inst, 'firstDay'), 10); firstDay = isNaN(firstDay) ? 0 : firstDay; for (row = 0; row < numMonths[0]; row++) { this.maxRows = 4; for (col = 0; col < numMonths[1]; col++) { selectedDate = this._daylightSavingAdjust(new Date(drawYear, drawMonth, inst.selectedDay)); calender = ''; if (isMultiMonth) { calender += '' + this._get(inst, 'weekHeader') + ' | ' : ''; for (dow = 0; dow < 7; dow++) { // days of the week day = (dow + firstDay) % 7; thead += '= 5 ? ' class="ui-datepicker-week-end"' : '') + '>' + '' + dayNamesMin[day] + ' | '; } calender += thead + '
---|---|
' + this._get(inst, 'calculateWeek')(printDate) + ' | '; for (dow = 0; dow < 7; dow++) { // create date picker days daySettings = beforeShowDay ? beforeShowDay.apply(inst.input ? inst.input[0] : null, [printDate]) : [true, '']; otherMonth = printDate.getMonth() !== drawMonth; unselectable = otherMonth && !selectOtherMonths || !daySettings[0] || minDate && printDate < minDate || maxDate && printDate > maxDate; tbody += '' + (otherMonth && !showOtherMonths ? ' ' : // display for other months unselectable ? '' + printDate.getDate() + '' : '' + printDate.getDate() + '') + ' | '; printDate.setDate(printDate.getDate() + 1); printDate = this._daylightSavingAdjust(printDate); } calender += tbody + '