/* ========================================================= * bootstrap-datepicker-select.js * http://github.com/sompylasar/bootstrap-datepicker-select * ========================================================= * Copyright 2012 sompylasar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ========================================================= */ (function( window, $, undefined ) {'use strict'; var DPGlobal = $.fn.datepicker.DPGlobal; var dates = $.fn.datepicker.dates; // Copied from Bootstrap Datepicker by eternicode. function UTCDate() { return new Date(Date.UTC.apply(Date, arguments)); } function UTCToday() { var today = new Date(); return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate()); } // Copied from Bootstrap Dropdown's private scope to be able to close other Dropdowns on the page. var Dropdown_toggle = '[data-toggle=dropdown]'; function Dropdown_getParent($this) { var selector = $this.attr('data-target') , $parent; if (!selector) { selector = $this.attr('href'); selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, ''); //strip for ie7 } $parent = selector && $(selector); if (!$parent || !$parent.length) { $parent = $this.parent(); } return $parent; } function Dropdown_clearMenus() { $(Dropdown_toggle).each(function () { Dropdown_getParent($(this)).removeClass('open'); }); } var DatepickerSelect = function (element, options) { var _this = this; var nowDate = UTCToday(); var year, month, day; _this.element = $(element); _this.language = options.language||_this.element.data('date-language')||"en"; _this.language = _this.language in dates ? _this.language : _this.language.split('-')[0]; //Check if "de-DE" style date is available, if not language should fallback to 2 letter code eg "de" _this.language = _this.language in dates ? _this.language : "en"; //_this.isRTL = dates[_this.language].rtl||false; _this.format = DPGlobal.parseFormat(options.format||_this.element.data('date-format')||dates[_this.language].format||'mm/dd/yyyy'); _this.monthNames = (options.monthNames === undefined ? _this.element.data('date-monthnames') : options.monthNames); _this.monthNames = ($.isArray(_this.monthNames) ? _this.monthNames : (_this.monthNames === true || _this.monthNames === 'true' ? dates[_this.language].months : undefined ) ); _this.measureWidth = !!options.measureWidth; _this.$input = _this.element.find('.datepicker-select-input'); var selectedDate = options.date || _this.$input.val(); if (selectedDate) { selectedDate = DPGlobal.parseDate(selectedDate, _this.format, _this.language); } else { selectedDate = null; } _this.$year = _this.element.find('.datepicker-select-year'); _this.$month = _this.element.find('.datepicker-select-month'); _this.$day = _this.element.find('.datepicker-select-day'); _this._populateSelect(_this.$year, 1900, nowDate.getFullYear(), !!options.yearsReverse); _this._populateSelect(_this.$month, 1, 12, false, _this.monthNames); if (selectedDate) { year = selectedDate.getUTCFullYear(); month = (selectedDate.getUTCMonth() + 1); day = selectedDate.getUTCDate(); if (!isNaN(year)) { _this.$year.val(year); } if (!isNaN(month)) { _this.$month.val(month); } } _this._populateSelect(_this.$day, 1, (selectedDate ? DPGlobal.getDaysInMonth(year, month - 1) : 31)); if (selectedDate && !isNaN(day)) { _this.$day.val(day); } _this.$yearDropdown = _this._makeDropdown(_this.$year, true); _this.$monthDropdown = _this._makeDropdown(_this.$month, false, !!_this.monthNames); _this.$dayDropdown = _this._makeDropdown(_this.$day, true); _this._populateDropdown(_this.$year, _this.$yearDropdown); _this._populateDropdown(_this.$month, _this.$monthDropdown); _this._populateDropdown(_this.$day, _this.$dayDropdown); _this.$day .on('change.datepicker-select', function () { _this.$input.val(_this.getFormattedDate() || '').trigger('change'); }); _this.$year.add(_this.$month) .on('change.datepicker-select', function () { var year = parseInt(_this.$year.val(), 10); var month = parseInt(_this.$month.val(), 10); var day = parseInt(_this.$day.val(), 10); if (!isNaN(year) && !isNaN(month)) { _this._populateSelect(_this.$day, 1, DPGlobal.getDaysInMonth(year, month - 1)); _this._populateDropdown(_this.$day, _this.$dayDropdown); } else { _this._populateSelect(_this.$day, 1, 31); _this._populateDropdown(_this.$day, _this.$dayDropdown); } if (!isNaN(day)) { _this.$day.val(day).trigger('change'); } else { _this.$day.val('').trigger('change'); } }); _this.$input.val(selectedDate ? _this.getFormattedDate() || '' : '').trigger('change'); }; DatepickerSelect.prototype = { constructor: DatepickerSelect, /** * Returns the currently selected date. * * @returns {Date|undefined} The selected date (local timezone), or undefined if something went wrong. */ getDate: function() { var d = this.getUTCDate(); if (d) { return new Date(d.getTime() + (d.getTimezoneOffset()*60000)); } }, /** * Returns the currently selected date in UTC. * * @returns {Date|undefined} The selected date (UTC), or undefined if something went wrong. */ getUTCDate: function() { var _this = this; var year = parseInt(_this.$year.val(), 10); var month = parseInt(_this.$month.val(), 10); var day = parseInt(_this.$day.val(), 10); if (!isNaN(year) && !isNaN(month) && !isNaN(day)) { return UTCDate(year, month - 1, day, 0, 0, 0); } }, setDate: function (date, format) { var _this = this; if (format === undefined) { format = _this.format; } if (date) { date = DPGlobal.parseDate(date, format, _this.language); } this.setUTCDate(date ? new Date(date.getTime() - (date.getTimezoneOffset()*60000)) : null); }, /** * Sets the currently selected date. * Only year (getUTCFullYear), month (getUTCMonth) and date (getUTCDate) components are used. * * @param {Date|String} date The date object or a string in the current or the specified format. * @param {Object} [format] Parsed format returned from DPGlobal.parseFormat(format:String) */ setUTCDate: function (date, format) { var _this = this, year, month, day; if (format === undefined) { format = _this.format; } if (date) { date = DPGlobal.parseDate(date, format, _this.language); year = date.getUTCFullYear(); month = (date.getUTCMonth() + 1); day = date.getUTCDate(); } if (!isNaN(year) && !isNaN(month) && !isNaN(day)) { _this.$year.val(year).trigger('change'); _this.$month.val(month).trigger('change'); _this.$day.val(day).trigger('change'); } else { _this.$year.val('').trigger('change'); _this.$month.val('').trigger('change'); _this.$day.val('').trigger('change'); } }, /** * Returns the formatted date string. * * @param {Object} [format] Parsed format returned from DPGlobal.parseFormat(format:String) * @param {String} The date formatted according to the current or the specified format. */ getFormattedDate: function (format) { var _this = this, date; if (format === undefined) { format = _this.format; } date = _this.getUTCDate(); return (date ? DPGlobal.formatDate(date, format, _this.language) : ''); }, update: function () { var _this = this; if (_this.$input.length) { _this.setUTCDate(_this.$input.val() || ''); } }, _makeDropdown: function ($select, isLong, isWide) { var _this = this; var $wrapper = $('