Advertisement
Guest User

bootstrap-datetimepicker for TB3

a guest
Oct 12th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* =========================================================
  2.  * bootstrap-datetimepicker.js
  3.  * =========================================================
  4.  * Copyright 2012 Stefan Petre
  5.  * Improvements by Andrew Rowls
  6.  * Improvements by Sébastien Malot
  7.  * Project URL : http://www.malot.fr/bootstrap-datetimepicker
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License");
  10.  * you may not use this file except in compliance with the License.
  11.  * You may obtain a copy of the License at
  12.  *
  13.  * http://www.apache.org/licenses/LICENSE-2.0
  14.  *
  15.  * Unless required by applicable law or agreed to in writing, software
  16.  * distributed under the License is distributed on an "AS IS" BASIS,
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18.  * See the License for the specific language governing permissions and
  19.  * limitations under the License.
  20.  * ========================================================= */
  21.  
  22. !function( $ ) {
  23.  
  24.     function UTCDate(){
  25.         return new Date(Date.UTC.apply(Date, arguments));
  26.     }
  27.     function UTCToday(){
  28.         var today = new Date();
  29.         return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), today.getUTCHours(), today.getUTCMinutes(), today.getUTCSeconds(), 0);
  30.     }
  31.  
  32.     // Picker object
  33.  
  34.     var Datetimepicker = function(element, options) {
  35.         var that = this;
  36.  
  37.         this.element = $(element);
  38.         this.language = options.language || this.element.data('date-language') || "en";
  39.         this.language = this.language in dates ? this.language : "en";
  40.         this.isRTL = dates[this.language].rtl || false;
  41.         this.formatType = options.formatType || this.element.data('format-type') || 'standard';
  42.         this.format = DPGlobal.parseFormat(options.format || this.element.data('date-format') || DPGlobal.getDefaultFormat(this.formatType, 'input'), this.formatType);
  43.         this.isInline = false;
  44.         this.isVisible = false;
  45.         this.isInput = this.element.is('input');
  46.         this.component = this.element.is('.date') ? this.element.find('.input-group-addon .glyphicon .glyphicon-th, .input-group-addon .glyphicon .glyphicon-time, .input-group-addon .glyphicon .glyphicon-calendar').parent() : false;
  47.         this.componentReset = this.element.is('.date') ? this.element.find('.input-group-addon .glyphicon .glyphicon-remove').parent() : false;
  48.         this.hasInput = this.component && this.element.find('input').length;
  49.         if (this.component && this.component.length === 0) {
  50.             this.component = false;
  51.         }
  52.         this.linkField = options.linkField || this.element.data('link-field') || false;
  53.         this.linkFormat = DPGlobal.parseFormat(options.linkFormat || this.element.data('link-format') || DPGlobal.getDefaultFormat(this.formatType, 'link'), this.formatType);
  54.         this.minuteStep = options.minuteStep || this.element.data('minute-step') || 5;
  55.         this.pickerPosition = options.pickerPosition || this.element.data('picker-position') || 'bottom-right';
  56.                 this.showMeridian = options.showMeridian || this.element.data('show-meridian') || false;
  57.                 this.initialDate = options.initialDate || new Date();
  58.  
  59.         this._attachEvents();
  60.        
  61.             this.formatViewType = "datetime";
  62.             if ('formatViewType' in options) {
  63.                     this.formatViewType = options.formatViewType;
  64.             } else if ('formatViewType' in this.element.data()) {
  65.                     this.formatViewType = this.element.data('formatViewType');
  66.             }
  67.  
  68.         this.minView = 0;
  69.         if ('minView' in options) {
  70.             this.minView = options.minView;
  71.         } else if ('minView' in this.element.data()) {
  72.             this.minView = this.element.data('min-view');
  73.         }
  74.         this.minView = DPGlobal.convertViewMode(this.minView);
  75.  
  76.         this.maxView = DPGlobal.modes.length-1;
  77.         if ('maxView' in options) {
  78.             this.maxView = options.maxView;
  79.         } else if ('maxView' in this.element.data()) {
  80.             this.maxView = this.element.data('max-view');
  81.         }
  82.         this.maxView = DPGlobal.convertViewMode(this.maxView);
  83.  
  84.         this.startViewMode = 2;
  85.         if ('startView' in options) {
  86.             this.startViewMode = options.startView;
  87.         } else if ('startView' in this.element.data()) {
  88.             this.startViewMode = this.element.data('start-view');
  89.         }
  90.         this.startViewMode = DPGlobal.convertViewMode(this.startViewMode);
  91.         this.viewMode = this.startViewMode;
  92.  
  93.                 this.viewSelect = this.minView;
  94.                 if ('viewSelect' in options) {
  95.                         this.viewSelect = options.viewSelect;
  96.                 } else if ('viewSelect' in this.element.data()) {
  97.                         this.viewSelect = this.element.data('view-select');
  98.                 }
  99.                 this.viewSelect = DPGlobal.convertViewMode(this.viewSelect);
  100.  
  101.         this.forceParse = true;
  102.         if ('forceParse' in options) {
  103.             this.forceParse = options.forceParse;
  104.         } else if ('dateForceParse' in this.element.data()) {
  105.             this.forceParse = this.element.data('date-force-parse');
  106.         }
  107.  
  108.         this.picker = $(DPGlobal.template)
  109.                             .appendTo(this.isInline ? this.element : 'body')
  110.                             .on({
  111.                                 click: $.proxy(this.click, this),
  112.                                 mousedown: $.proxy(this.mousedown, this)
  113.                             });
  114.  
  115.         if (this.isInline) {
  116.             this.picker.addClass('datetimepicker-inline');
  117.         } else {
  118.             this.picker.addClass('datetimepicker-dropdown-' + this.pickerPosition + ' dropdown-menu');
  119.         }
  120.         if (this.isRTL){
  121.             this.picker.addClass('datetimepicker-rtl');
  122.             this.picker.find('.prev i, .next i')
  123.                         .toggleClass('glyphicon-arrow-left glyphicon-arrow-right');;
  124.         }
  125.         $(document).on('mousedown', function (e) {
  126.             // Clicked outside the datetimepicker, hide it
  127.             if ($(e.target).closest('.datetimepicker').length === 0) {
  128.                 that.hide();
  129.             }
  130.         });
  131.  
  132.         this.autoclose = false;
  133.         if ('autoclose' in options) {
  134.             this.autoclose = options.autoclose;
  135.         } else if ('dateAutoclose' in this.element.data()) {
  136.             this.autoclose = this.element.data('date-autoclose');
  137.         }
  138.  
  139.         this.keyboardNavigation = true;
  140.         if ('keyboardNavigation' in options) {
  141.             this.keyboardNavigation = options.keyboardNavigation;
  142.         } else if ('dateKeyboardNavigation' in this.element.data()) {
  143.             this.keyboardNavigation = this.element.data('date-keyboard-navigation');
  144.         }
  145.  
  146.         this.todayBtn = (options.todayBtn || this.element.data('date-today-btn') || false);
  147.         this.todayHighlight = (options.todayHighlight || this.element.data('date-today-highlight') || false);
  148.  
  149.         this.weekStart = ((options.weekStart || this.element.data('date-weekstart') || dates[this.language].weekStart || 0) % 7);
  150.         this.weekEnd = ((this.weekStart + 6) % 7);
  151.         this.startDate = -Infinity;
  152.         this.endDate = Infinity;
  153.         this.daysOfWeekDisabled = [];
  154.         this.setStartDate(options.startDate || this.element.data('date-startdate'));
  155.         this.setEndDate(options.endDate || this.element.data('date-enddate'));
  156.         this.setDaysOfWeekDisabled(options.daysOfWeekDisabled || this.element.data('date-days-of-week-disabled'));
  157.         this.fillDow();
  158.         this.fillMonths();
  159.         this.update();
  160.         this.showMode();
  161.  
  162.         if(this.isInline) {
  163.             this.show();
  164.         }
  165.     };
  166.  
  167.     Datetimepicker.prototype = {
  168.         constructor: Datetimepicker,
  169.  
  170.         _events: [],
  171.         _attachEvents: function(){
  172.             this._detachEvents();
  173.             if (this.isInput) { // single input
  174.                 this._events = [
  175.                     [this.element, {
  176.                         focus: $.proxy(this.show, this),
  177.                         keyup: $.proxy(this.update, this),
  178.                         keydown: $.proxy(this.keydown, this)
  179.                     }]
  180.                 ];
  181.             }
  182.             else if (this.component && this.hasInput){ // component: input + button
  183.                 this._events = [
  184.                     // For components that are not readonly, allow keyboard nav
  185.                     [this.element.find('input'), {
  186.                         focus: $.proxy(this.show, this),
  187.                         keyup: $.proxy(this.update, this),
  188.                         keydown: $.proxy(this.keydown, this)
  189.                     }],
  190.                     [this.component, {
  191.                         click: $.proxy(this.show, this)
  192.                     }]
  193.                 ];
  194.                 if (this.componentReset) {
  195.                     this._events.push([
  196.                         this.componentReset,
  197.                         {click: $.proxy(this.reset, this)}
  198.                     ]);
  199.                 }
  200.             }
  201.             else if (this.element.is('div')) {  // inline datetimepicker
  202.                 this.isInline = true;
  203.             }
  204.             else {
  205.                 this._events = [
  206.                     [this.element, {
  207.                         click: $.proxy(this.show, this)
  208.                     }]
  209.                 ];
  210.             }
  211.             for (var i=0, el, ev; i<this._events.length; i++){
  212.                 el = this._events[i][0];
  213.                 ev = this._events[i][1];
  214.                 el.on(ev);
  215.             }
  216.         },
  217.        
  218.         _detachEvents: function(){
  219.             for (var i=0, el, ev; i<this._events.length; i++){
  220.                 el = this._events[i][0];
  221.                 ev = this._events[i][1];
  222.                 el.off(ev);
  223.             }
  224.             this._events = [];
  225.         },
  226.  
  227.         show: function(e) {
  228.             this.picker.show();
  229.             this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
  230.             if (this.forceParse) {
  231.                 this.update();
  232.             }
  233.             this.place();
  234.             $(window).on('resize', $.proxy(this.place, this));
  235.             if (e) {
  236.                 e.stopPropagation();
  237.                 e.preventDefault();
  238.             }
  239.             this.isVisible = true;
  240.             this.element.trigger({
  241.                 type: 'show',
  242.                 date: this.date
  243.             });
  244.         },
  245.  
  246.         hide: function(e){
  247.             if(!this.isVisible) return;
  248.             if(this.isInline) return;
  249.             this.picker.hide();
  250.             $(window).off('resize', this.place);
  251.             this.viewMode = this.startViewMode;
  252.             this.showMode();
  253.             if (!this.isInput) {
  254.                 $(document).off('mousedown', this.hide);
  255.             }
  256.  
  257.             if (
  258.                 this.forceParse &&
  259.                 (
  260.                     this.isInput && this.element.val()  ||
  261.                     this.hasInput && this.element.find('input').val()
  262.                 )
  263.             )
  264.                 this.setValue();
  265.             this.isVisible = false;
  266.             this.element.trigger({
  267.                 type: 'hide',
  268.                 date: this.date
  269.             });
  270.         },
  271.  
  272.         remove: function() {
  273.             this._detachEvents();
  274.             this.picker.remove();
  275.             delete this.element.data().datetimepicker;
  276.         },
  277.  
  278.         getDate: function() {
  279.             var d = this.getUTCDate();
  280.             return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
  281.         },
  282.  
  283.         getUTCDate: function() {
  284.             return this.date;
  285.         },
  286.  
  287.         setDate: function(d) {
  288.             this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
  289.         },
  290.  
  291.         setUTCDate: function(d) {
  292.             if (d >= this.startDate && d <= this.endDate) {
  293.                 this.date = d;
  294.                 this.setValue();
  295.                 this.viewDate = this.date;
  296.                 this.fill();
  297.             } else {
  298.                 this.element.trigger({
  299.                     type: 'outOfRange',
  300.                     date: d,
  301.                     startDate: this.startDate,
  302.                     endDate: this.endDate
  303.                 });
  304.             }
  305.         },
  306.  
  307.         setFormat: function(format) {
  308.             this.format = DPGlobal.parseFormat(format, this.formatType);
  309.             var element;
  310.             if (this.isInput) {
  311.                 element = this.element;
  312.             } else if (this.component){
  313.                 element = this.element.find('input');
  314.             }
  315.             if (element && element.val()) {
  316.                 this.setValue();
  317.             }
  318.         },
  319.  
  320.         setValue: function() {
  321.             var formatted = this.getFormattedDate();
  322.             if (!this.isInput) {
  323.                 if (this.component){
  324.                     this.element.find('input').val(formatted);
  325.                 }
  326.                 this.element.data('date', formatted);
  327.             } else {
  328.                 this.element.val(formatted);
  329.             }
  330.             if (this.linkField) {
  331.                 $('#' + this.linkField).val(this.getFormattedDate(this.linkFormat));
  332.             }
  333.         },
  334.  
  335.         getFormattedDate: function(format) {
  336.             if(format == undefined) format = this.format;
  337.             return DPGlobal.formatDate(this.date, format, this.language, this.formatType);
  338.         },
  339.  
  340.         setStartDate: function(startDate){
  341.             this.startDate = startDate || -Infinity;
  342.             if (this.startDate !== -Infinity) {
  343.                 this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language, this.formatType);
  344.             }
  345.             this.update();
  346.             this.updateNavArrows();
  347.         },
  348.  
  349.         setEndDate: function(endDate){
  350.             this.endDate = endDate || Infinity;
  351.             if (this.endDate !== Infinity) {
  352.                 this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language, this.formatType);
  353.             }
  354.             this.update();
  355.             this.updateNavArrows();
  356.         },
  357.  
  358.         setDaysOfWeekDisabled: function(daysOfWeekDisabled){
  359.             this.daysOfWeekDisabled = daysOfWeekDisabled || [];
  360.             if (!$.isArray(this.daysOfWeekDisabled)) {
  361.                 this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
  362.             }
  363.             this.daysOfWeekDisabled = $.map(this.daysOfWeekDisabled, function (d) {
  364.                 return parseInt(d, 10);
  365.             });
  366.             this.update();
  367.             this.updateNavArrows();
  368.         },
  369.  
  370.         place: function(){
  371.             if(this.isInline) return;
  372.             var zIndex = parseInt(this.element.parents().filter(function() {
  373.                 return $(this).css('z-index') != 'auto';
  374.             }).first().css('z-index'))+10;
  375.             var offset, top, left;
  376.             if (this.component) {
  377.                 offset = this.component.offset();
  378.                 left = offset.left;
  379.                 if (this.pickerPosition == 'bottom-left' || this.pickerPosition == 'top-left') {
  380.                     left += this.component.outerWidth() - this.picker.outerWidth();
  381.                 }
  382.             } else {
  383.                 offset = this.element.offset();
  384.                 left = offset.left;
  385.             }
  386.             if (this.pickerPosition == 'top-left' || this.pickerPosition == 'top-right') {
  387.                 top = offset.top - this.picker.outerHeight();
  388.             } else {
  389.                 top = offset.top + this.height;
  390.             }
  391.             this.picker.css({
  392.                 top: top,
  393.                 left: left,
  394.                 zIndex: zIndex
  395.             });
  396.         },
  397.  
  398.         update: function(){
  399.             var date, fromArgs = false;
  400.             if(arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof Date)) {
  401.                 date = arguments[0];
  402.                 fromArgs = true;
  403.             } else {
  404.                 date = this.element.data('date') || (this.isInput ? this.element.val() : this.element.find('input').val()) || this.initialDate;
  405.             }
  406.  
  407.             if (!date) {
  408.                 date = new Date();
  409.                 fromArgs = false;
  410.             }
  411.  
  412.             this.date = DPGlobal.parseDate(date, this.format, this.language, this.formatType);
  413.  
  414.             if (fromArgs) this.setValue();
  415.  
  416.             if (this.date < this.startDate) {
  417.                 this.viewDate = new Date(this.startDate);
  418.             } else if (this.date > this.endDate) {
  419.                 this.viewDate = new Date(this.endDate);
  420.             } else {
  421.                 this.viewDate = new Date(this.date);
  422.             }
  423.             this.fill();
  424.         },
  425.  
  426.         fillDow: function(){
  427.             var dowCnt = this.weekStart,
  428.             html = '<tr>';
  429.             while (dowCnt < this.weekStart + 7) {
  430.                 html += '<th class="dow">'+dates[this.language].daysMin[(dowCnt++)%7]+'</th>';
  431.             }
  432.             html += '</tr>';
  433.             this.picker.find('.datetimepicker-days thead').append(html);
  434.         },
  435.  
  436.         fillMonths: function(){
  437.             var html = '',
  438.             i = 0;
  439.             while (i < 12) {
  440.                 html += '<span class="month">'+dates[this.language].monthsShort[i++]+'</span>';
  441.             }
  442.             this.picker.find('.datetimepicker-months td').html(html);
  443.         },
  444.  
  445.         fill: function() {
  446.             if (this.date == null || this.viewDate == null) {
  447.                 return;
  448.             }
  449.             var d = new Date(this.viewDate),
  450.                 year = d.getUTCFullYear(),
  451.                 month = d.getUTCMonth(),
  452.                 dayMonth = d.getUTCDate(),
  453.                 hours = d.getUTCHours(),
  454.                 minutes = d.getUTCMinutes(),
  455.                 startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
  456.                 startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
  457.                 endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
  458.                 endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
  459.                 currentDate = (new UTCDate(this.date.getUTCFullYear(), this.date.getUTCMonth(), this.date.getUTCDate())).valueOf(),
  460.                 today = new Date();
  461.             this.picker.find('.datetimepicker-days thead th:eq(1)')
  462.                         .text(dates[this.language].months[month]+' '+year);
  463.                 if (this.formatViewType == "time") {
  464.                         var hourConverted = hours % 12 ? hours % 12 : 12;
  465.                         var hoursDisplay = (hourConverted < 10 ? '0' : '') + hourConverted;
  466.                         var minutesDisplay = (minutes < 10 ? '0' : '') + minutes;
  467.                         var meridianDisplay = dates[this.language].meridiem[hours < 12 ? 0 : 1];
  468.                         this.picker.find('.datetimepicker-hours thead th:eq(1)')
  469.                                 .text(hoursDisplay + ':' + minutesDisplay + ' ' + meridianDisplay.toUpperCase());
  470.                         this.picker.find('.datetimepicker-minutes thead th:eq(1)')
  471.                                 .text(hoursDisplay + ':' + minutesDisplay + ' ' + meridianDisplay.toUpperCase());
  472.                 } else {
  473.                         this.picker.find('.datetimepicker-hours thead th:eq(1)')
  474.                                 .text(dayMonth + ' ' + dates[this.language].months[month] + ' ' + year);
  475.                         this.picker.find('.datetimepicker-minutes thead th:eq(1)')
  476.                                 .text(dayMonth + ' ' + dates[this.language].months[month] + ' ' + year);               
  477.                 }
  478.                 this.picker.find('tfoot th.today')
  479.                         .text(dates[this.language].today)
  480.                         .toggle(this.todayBtn !== false);
  481.             this.updateNavArrows();
  482.             this.fillMonths();
  483.             /*var prevMonth = UTCDate(year, month, 0,0,0,0,0);
  484.             prevMonth.setUTCDate(prevMonth.getDate() - (prevMonth.getUTCDay() - this.weekStart + 7)%7);*/
  485.             var prevMonth = UTCDate(year, month-1, 28,0,0,0,0),
  486.                                 day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
  487.                 prevMonth.setUTCDate(day);
  488.                     prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
  489.             var nextMonth = new Date(prevMonth);
  490.             nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
  491.             nextMonth = nextMonth.valueOf();
  492.             var html = [];
  493.             var clsName;
  494.             while(prevMonth.valueOf() < nextMonth) {
  495.                 if (prevMonth.getUTCDay() == this.weekStart) {
  496.                     html.push('<tr>');
  497.                 }
  498.                 clsName = '';
  499.                 if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
  500.                     clsName += ' old';
  501.                 } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
  502.                     clsName += ' new';
  503.                 }
  504.                 // Compare internal UTC date with local today, not UTC today
  505.                 if (this.todayHighlight &&
  506.                     prevMonth.getUTCFullYear() == today.getFullYear() &&
  507.                     prevMonth.getUTCMonth() == today.getMonth() &&
  508.                     prevMonth.getUTCDate() == today.getDate()) {
  509.                     clsName += ' today';
  510.                 }
  511.                 if (prevMonth.valueOf() == currentDate) {
  512.                     clsName += ' active';
  513.                 }
  514.                 if ((prevMonth.valueOf() + 86400000) <= this.startDate || prevMonth.valueOf() > this.endDate ||
  515.                     $.inArray(prevMonth.getUTCDay(), this.daysOfWeekDisabled) !== -1) {
  516.                     clsName += ' disabled';
  517.                 }
  518.                 html.push('<td class="day'+clsName+'">'+prevMonth.getUTCDate() + '</td>');
  519.                 if (prevMonth.getUTCDay() == this.weekEnd) {
  520.                     html.push('</tr>');
  521.                 }
  522.                 prevMonth.setUTCDate(prevMonth.getUTCDate()+1);
  523.             }
  524.             this.picker.find('.datetimepicker-days tbody').empty().append(html.join(''));
  525.  
  526.             html = [];
  527.                         var txt = '', meridian = '', meridianOld = '';
  528.             for (var i=0;i<24;i++) {
  529.                 var actual = UTCDate(year, month, dayMonth, i);
  530.                 clsName = '';
  531.                 // We want the previous hour for the startDate
  532.                 if ((actual.valueOf() + 3600000) <= this.startDate || actual.valueOf() > this.endDate) {
  533.                     clsName += ' disabled';
  534.                 } else if (hours == i) {
  535.                     clsName += ' active';
  536.                 }
  537.                                 if (this.showMeridian && dates[this.language].meridiem.length == 2) {
  538.                                         meridian = (i<12?dates[this.language].meridiem[0]:dates[this.language].meridiem[1]);
  539.                                         if (meridian != meridianOld) {
  540.                                                 if (meridianOld != '') {
  541.                                                         html.push('</fieldset>');
  542.                                                 }
  543.                                                 html.push('<fieldset class="hour"><legend>'+meridian.toUpperCase()+'</legend>');
  544.                                         }
  545.                                         meridianOld = meridian;
  546.                                         txt = (i%12?i%12:12);
  547.                                         html.push('<span class="hour'+clsName+' hour_'+(i<12?'am':'pm')+'">'+txt+'</span>');
  548.                                         if (i == 23) {
  549.                                                 html.push('</fieldset>');
  550.                                         }
  551.                                 } else {
  552.                                         txt = i+':00';
  553.                                         html.push('<span class="hour'+clsName+'">'+txt+'</span>');
  554.                                 }
  555.             }
  556.             this.picker.find('.datetimepicker-hours td').html(html.join(''));
  557.  
  558.             html = [];
  559.                         txt = '', meridian = '', meridianOld = '';
  560.             for(var i=0;i<60;i+=this.minuteStep) {
  561.                 var actual = UTCDate(year, month, dayMonth, hours, i, 0);
  562.                 clsName = '';
  563.                 if (actual.valueOf() < this.startDate || actual.valueOf() > this.endDate) {
  564.                     clsName += ' disabled';
  565.                 } else if (Math.floor(minutes/this.minuteStep) == Math.floor(i/this.minuteStep)) {
  566.                     clsName += ' active';
  567.                 }
  568.                                 if (this.showMeridian && dates[this.language].meridiem.length == 2) {
  569.                                         meridian = (hours<12?dates[this.language].meridiem[0]:dates[this.language].meridiem[1]);
  570.                                         if (meridian != meridianOld) {
  571.                                                 if (meridianOld != '') {
  572.                                                         html.push('</fieldset>');
  573.                                                 }
  574.                                                 html.push('<fieldset class="minute"><legend>'+meridian.toUpperCase()+'</legend>');
  575.                                         }
  576.                                         meridianOld = meridian;
  577.                                         txt = (hours%12?hours%12:12);
  578.                                         //html.push('<span class="minute'+clsName+' minute_'+(hours<12?'am':'pm')+'">'+txt+'</span>');
  579.                                         html.push('<span class="minute'+clsName+'">'+txt+':'+(i<10?'0'+i:i)+'</span>');
  580.                                         if (i == 59) {
  581.                                                 html.push('</fieldset>');
  582.                                         }
  583.                                 } else {
  584.                                         txt = i+':00';
  585.                                         //html.push('<span class="hour'+clsName+'">'+txt+'</span>');
  586.                                         html.push('<span class="minute'+clsName+'">'+hours+':'+(i<10?'0'+i:i)+'</span>');
  587.                                 }
  588.             }
  589.             this.picker.find('.datetimepicker-minutes td').html(html.join(''));
  590.  
  591.             var currentYear = this.date.getUTCFullYear();
  592.             var months = this.picker.find('.datetimepicker-months')
  593.                         .find('th:eq(1)')
  594.                         .text(year)
  595.                         .end()
  596.                         .find('span').removeClass('active');
  597.             if (currentYear == year) {
  598.                 months.eq(this.date.getUTCMonth()).addClass('active');
  599.             }
  600.             if (year < startYear || year > endYear) {
  601.                 months.addClass('disabled');
  602.             }
  603.             if (year == startYear) {
  604.                 months.slice(0, startMonth).addClass('disabled');
  605.             }
  606.             if (year == endYear) {
  607.                 months.slice(endMonth+1).addClass('disabled');
  608.             }
  609.  
  610.             html = '';
  611.             year = parseInt(year/10, 10) * 10;
  612.             var yearCont = this.picker.find('.datetimepicker-years')
  613.                                 .find('th:eq(1)')
  614.                                 .text(year + '-' + (year + 9))
  615.                                 .end()
  616.                                 .find('td');
  617.             year -= 1;
  618.             for (var i = -1; i < 11; i++) {
  619.                 html += '<span class="year'+(i == -1 || i == 10 ? ' old' : '')+(currentYear == year ? ' active' : '')+(year < startYear || year > endYear ? ' disabled' : '')+'">'+year+'</span>';
  620.                 year += 1;
  621.             }
  622.             yearCont.html(html);
  623.             this.place();
  624.         },
  625.  
  626.         updateNavArrows: function() {
  627.             var d = new Date(this.viewDate),
  628.                 year = d.getUTCFullYear(),
  629.                 month = d.getUTCMonth(),
  630.                 day = d.getUTCDate(),
  631.                 hour = d.getUTCHours();
  632.             switch (this.viewMode) {
  633.                 case 0:
  634.                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  635.                                                      && month <= this.startDate.getUTCMonth()
  636.                                                      && day <= this.startDate.getUTCDate()
  637.                                                      && hour <= this.startDate.getUTCHours()) {
  638.                         this.picker.find('.prev').css({visibility: 'hidden'});
  639.                     } else {
  640.                         this.picker.find('.prev').css({visibility: 'visible'});
  641.                     }
  642.                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  643.                                                     && month >= this.endDate.getUTCMonth()
  644.                                                     && day >= this.endDate.getUTCDate()
  645.                                                     && hour >= this.endDate.getUTCHours()) {
  646.                         this.picker.find('.next').css({visibility: 'hidden'});
  647.                     } else {
  648.                         this.picker.find('.next').css({visibility: 'visible'});
  649.                     }
  650.                     break;
  651.                 case 1:
  652.                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  653.                                                      && month <= this.startDate.getUTCMonth()
  654.                                                      && day <= this.startDate.getUTCDate()) {
  655.                         this.picker.find('.prev').css({visibility: 'hidden'});
  656.                     } else {
  657.                         this.picker.find('.prev').css({visibility: 'visible'});
  658.                     }
  659.                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  660.                                                     && month >= this.endDate.getUTCMonth()
  661.                                                     && day >= this.endDate.getUTCDate()) {
  662.                         this.picker.find('.next').css({visibility: 'hidden'});
  663.                     } else {
  664.                         this.picker.find('.next').css({visibility: 'visible'});
  665.                     }
  666.                     break;
  667.                 case 2:
  668.                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  669.                                                      && month <= this.startDate.getUTCMonth()) {
  670.                         this.picker.find('.prev').css({visibility: 'hidden'});
  671.                     } else {
  672.                         this.picker.find('.prev').css({visibility: 'visible'});
  673.                     }
  674.                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  675.                                                     && month >= this.endDate.getUTCMonth()) {
  676.                         this.picker.find('.next').css({visibility: 'hidden'});
  677.                     } else {
  678.                         this.picker.find('.next').css({visibility: 'visible'});
  679.                     }
  680.                     break;
  681.                 case 3:
  682.                 case 4:
  683.                     if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
  684.                         this.picker.find('.prev').css({visibility: 'hidden'});
  685.                     } else {
  686.                         this.picker.find('.prev').css({visibility: 'visible'});
  687.                     }
  688.                     if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
  689.                         this.picker.find('.next').css({visibility: 'hidden'});
  690.                     } else {
  691.                         this.picker.find('.next').css({visibility: 'visible'});
  692.                     }
  693.                     break;
  694.             }
  695.         },
  696.  
  697.         click: function(e) {
  698.             e.stopPropagation();
  699.             e.preventDefault();
  700.             var target = $(e.target).closest('span, td, th, legend');
  701.             if (target.length == 1) {
  702.                 if (target.is('.disabled')) {
  703.                     this.element.trigger({
  704.                         type: 'outOfRange',
  705.                         date: this.viewDate,
  706.                         startDate: this.startDate,
  707.                         endDate: this.endDate
  708.                     });
  709.                     return;
  710.                 }
  711.                 switch(target[0].nodeName.toLowerCase()) {
  712.                     case 'th':
  713.                         switch(target[0].className) {
  714.                             case 'switch':
  715.                                 this.showMode(1);
  716.                                 break;
  717.                             case 'prev':
  718.                             case 'next':
  719.                                 var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
  720.                                 switch(this.viewMode){
  721.                                     case 0:
  722.                                         this.viewDate = this.moveHour(this.viewDate, dir);
  723.                                         break;
  724.                                     case 1:
  725.                                         this.viewDate = this.moveDate(this.viewDate, dir);
  726.                                         break;
  727.                                     case 2:
  728.                                         this.viewDate = this.moveMonth(this.viewDate, dir);
  729.                                         break;
  730.                                     case 3:
  731.                                     case 4:
  732.                                         this.viewDate = this.moveYear(this.viewDate, dir);
  733.                                         break;
  734.                                 }
  735.                                 this.fill();
  736.                                 break;
  737.                             case 'today':
  738.                                 var date = new Date();
  739.                                 date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0);
  740.  
  741.                                 this.viewMode = this.startViewMode;
  742.                                 this.showMode(0);
  743.                                 this._setDate(date);
  744.                                 this.fill();
  745.                                 if (this.autoclose) {
  746.                                     this.hide();
  747.                                 }
  748.                                 break;
  749.                         }
  750.                         break;
  751.                     case 'span':
  752.                         if (!target.is('.disabled')) {
  753.                                                         var year    = this.viewDate.getUTCFullYear(),
  754.                                                                 month   = this.viewDate.getUTCMonth(),
  755.                                                                 day     = this.viewDate.getUTCDate(),
  756.                                                                 hours   = this.viewDate.getUTCHours(),
  757.                                                                 minutes = this.viewDate.getUTCMinutes(),
  758.                                                                 seconds = this.viewDate.getUTCSeconds();
  759.  
  760.                             if (target.is('.month')) {
  761.                                 this.viewDate.setUTCDate(1);
  762.                                 month = target.parent().find('span').index(target);
  763.                                                                 day   = this.viewDate.getUTCDate();
  764.                                 this.viewDate.setUTCMonth(month);
  765.                                 this.element.trigger({
  766.                                     type: 'changeMonth',
  767.                                     date: this.viewDate
  768.                                 });
  769.                                                                 if (this.viewSelect >= 3) {
  770.                                         this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  771.                                                                 }
  772.                             } else if (target.is('.year')) {
  773.                                 this.viewDate.setUTCDate(1);
  774.                                 year = parseInt(target.text(), 10) || 0;
  775.                                 this.viewDate.setUTCFullYear(year);
  776.                                 this.element.trigger({
  777.                                     type: 'changeYear',
  778.                                     date: this.viewDate
  779.                                 });
  780.                                                                 if (this.viewSelect >= 4) {
  781.                                                                         this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  782.                                                                 }
  783.                             } else if (target.is('.hour')){
  784.                                 hours = parseInt(target.text(), 10) || 0;
  785.                                                                 if (target.hasClass('hour_am') || target.hasClass('hour_pm')) {
  786.                                                                         if (hours == 12 && target.hasClass('hour_am')) {
  787.                                                                                 hours = 0;
  788.                                                                         } else if (hours != 12 && target.hasClass('hour_pm')) {
  789.                                                                                 hours += 12;
  790.                                                                         }
  791.                                                                 }
  792.                                                                 this.viewDate.setUTCHours(hours);
  793.                                 this.element.trigger({
  794.                                     type: 'changeHour',
  795.                                     date: this.viewDate
  796.                                 });
  797.                                                                 if (this.viewSelect >= 1) {
  798.                                         this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  799.                                                                 }
  800.                             } else if (target.is('.minute')){
  801.                                 minutes = parseInt(target.text().substr(target.text().indexOf(':')+1), 10) || 0;
  802.                                                                 this.viewDate.setUTCMinutes(minutes);
  803.                                 this.element.trigger({
  804.                                     type: 'changeMinute',
  805.                                     date: this.viewDate
  806.                                 });
  807.                                                                 if (this.viewSelect >= 0) {
  808.                                         this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  809.                                                                 }
  810.                             }
  811.                             if (this.viewMode != 0) {
  812.                                 var oldViewMode = this.viewMode;
  813.                                 this.showMode(-1);
  814.                                 this.fill();
  815.                                 if (oldViewMode == this.viewMode && this.autoclose) {
  816.                                     this.hide();
  817.                                 }
  818.                             } else {
  819.                                 this.fill();
  820.                                 if (this.autoclose) {
  821.                                     this.hide();
  822.                                 }
  823.                             }
  824.                         }
  825.                         break;
  826.                     case 'td':
  827.                         if (target.is('.day') && !target.is('.disabled')){
  828.                             var day = parseInt(target.text(), 10) || 1;
  829.                             var year = this.viewDate.getUTCFullYear(),
  830.                                 month = this.viewDate.getUTCMonth(),
  831.                                 hours = this.viewDate.getUTCHours(),
  832.                                 minutes = this.viewDate.getUTCMinutes(),
  833.                                 seconds = this.viewDate.getUTCSeconds();
  834.                             if (target.is('.old')) {
  835.                                 if (month === 0) {
  836.                                     month = 11;
  837.                                     year -= 1;
  838.                                 } else {
  839.                                     month -= 1;
  840.                                 }
  841.                             } else if (target.is('.new')) {
  842.                                 if (month == 11) {
  843.                                     month = 0;
  844.                                     year += 1;
  845.                                 } else {
  846.                                     month += 1;
  847.                                 }
  848.                             }
  849.                                                         this.viewDate.setUTCFullYear(year);
  850.                                                         this.viewDate.setUTCMonth(month);
  851.                                                         this.viewDate.setUTCDate(day);
  852.                                                         this.element.trigger({
  853.                                                                 type: 'changeDay',
  854.                                                                 date: this.viewDate
  855.                                                         });
  856.                                                         if (this.viewSelect >= 2) {
  857.                                     this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  858.                                                         }
  859.                         }
  860.                         var oldViewMode = this.viewMode;
  861.                         this.showMode(-1);
  862.                         this.fill();
  863.                         if (oldViewMode == this.viewMode && this.autoclose) {
  864.                             this.hide();
  865.                         }
  866.                         break;
  867.                 }
  868.             }
  869.         },
  870.  
  871.         _setDate: function(date, which){
  872.             if (!which || which == 'date')
  873.                 this.date = date;
  874.             if (!which || which  == 'view')
  875.                 this.viewDate = date;
  876.             this.fill();
  877.             this.setValue();
  878.             var element;
  879.             if (this.isInput) {
  880.                 element = this.element;
  881.             } else if (this.component){
  882.                 element = this.element.find('input');
  883.             }
  884.             if (element) {
  885.                 element.change();
  886.                 if (this.autoclose && (!which || which == 'date')) {
  887.                     //this.hide();
  888.                 }
  889.             }
  890.             this.element.trigger({
  891.                 type: 'changeDate',
  892.                 date: this.date
  893.             });
  894.         },
  895.  
  896.         moveMinute: function(date, dir){
  897.             if (!dir) return date;
  898.             var new_date = new Date(date.valueOf());
  899.             //dir = dir > 0 ? 1 : -1;
  900.             new_date.setUTCMinutes(new_date.getUTCMinutes() + (dir * this.minuteStep));
  901.             return new_date;
  902.         },
  903.  
  904.         moveHour: function(date, dir){
  905.             if (!dir) return date;
  906.             var new_date = new Date(date.valueOf());
  907.             //dir = dir > 0 ? 1 : -1;
  908.             new_date.setUTCHours(new_date.getUTCHours() + dir);
  909.             return new_date;
  910.         },
  911.  
  912.         moveDate: function(date, dir){
  913.             if (!dir) return date;
  914.             var new_date = new Date(date.valueOf());
  915.             //dir = dir > 0 ? 1 : -1;
  916.             new_date.setUTCDate(new_date.getUTCDate() + dir);
  917.             return new_date;
  918.         },
  919.  
  920.         moveMonth: function(date, dir){
  921.             if (!dir) return date;
  922.             var new_date = new Date(date.valueOf()),
  923.                 day = new_date.getUTCDate(),
  924.                 month = new_date.getUTCMonth(),
  925.                 mag = Math.abs(dir),
  926.                 new_month, test;
  927.             dir = dir > 0 ? 1 : -1;
  928.             if (mag == 1){
  929.                 test = dir == -1
  930.                     // If going back one month, make sure month is not current month
  931.                     // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
  932.                     ? function(){ return new_date.getUTCMonth() == month; }
  933.                     // If going forward one month, make sure month is as expected
  934.                     // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
  935.                     : function(){ return new_date.getUTCMonth() != new_month; };
  936.                 new_month = month + dir;
  937.                 new_date.setUTCMonth(new_month);
  938.                 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
  939.                 if (new_month < 0 || new_month > 11)
  940.                     new_month = (new_month + 12) % 12;
  941.             } else {
  942.                 // For magnitudes >1, move one month at a time...
  943.                 for (var i=0; i<mag; i++)
  944.                     // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
  945.                     new_date = this.moveMonth(new_date, dir);
  946.                 // ...then reset the day, keeping it in the new month
  947.                 new_month = new_date.getUTCMonth();
  948.                 new_date.setUTCDate(day);
  949.                 test = function(){ return new_month != new_date.getUTCMonth(); };
  950.             }
  951.             // Common date-resetting loop -- if date is beyond end of month, make it
  952.             // end of month
  953.             while (test()){
  954.                 new_date.setUTCDate(--day);
  955.                 new_date.setUTCMonth(new_month);
  956.             }
  957.             return new_date;
  958.         },
  959.  
  960.         moveYear: function(date, dir){
  961.             return this.moveMonth(date, dir*12);
  962.         },
  963.  
  964.         dateWithinRange: function(date){
  965.             return date >= this.startDate && date <= this.endDate;
  966.         },
  967.  
  968.         keydown: function(e){
  969.             if (this.picker.is(':not(:visible)')){
  970.                 if (e.keyCode == 27) // allow escape to hide and re-show picker
  971.                     this.show();
  972.                 return;
  973.             }
  974.             var dateChanged = false,
  975.                 dir, day, month,
  976.                 newDate, newViewDate;
  977.             switch(e.keyCode){
  978.                 case 27: // escape
  979.                     this.hide();
  980.                     e.preventDefault();
  981.                     break;
  982.                 case 37: // left
  983.                 case 39: // right
  984.                     if (!this.keyboardNavigation) break;
  985.                     dir = e.keyCode == 37 ? -1 : 1;
  986.                                         viewMode = this.viewMode;
  987.                                         if (e.ctrlKey) {
  988.                                                 viewMode += 2;
  989.                                         } else if (e.shiftKey) {
  990.                                                 viewMode += 1;
  991.                                         }
  992.                                         if (viewMode == 4) {
  993.                         newDate = this.moveYear(this.date, dir);
  994.                         newViewDate = this.moveYear(this.viewDate, dir);
  995.                                         } else if (viewMode == 3) {
  996.                         newDate = this.moveMonth(this.date, dir);
  997.                         newViewDate = this.moveMonth(this.viewDate, dir);
  998.                                         } else if (viewMode == 2) {
  999.                         newDate = this.moveDate(this.date, dir);
  1000.                         newViewDate = this.moveDate(this.viewDate, dir);
  1001.                                         } else if (viewMode == 1) {
  1002.                         newDate = this.moveHour(this.date, dir);
  1003.                         newViewDate = this.moveHour(this.viewDate, dir);
  1004.                                         } else if (viewMode == 0) {
  1005.                         newDate = this.moveMinute(this.date, dir);
  1006.                         newViewDate = this.moveMinute(this.viewDate, dir);
  1007.                                         }
  1008.                     if (this.dateWithinRange(newDate)){
  1009.                         this.date = newDate;
  1010.                         this.viewDate = newViewDate;
  1011.                         this.setValue();
  1012.                         this.update();
  1013.                         e.preventDefault();
  1014.                         dateChanged = true;
  1015.                     }
  1016.                     break;
  1017.                 case 38: // up
  1018.                 case 40: // down
  1019.                     if (!this.keyboardNavigation) break;
  1020.                     dir = e.keyCode == 38 ? -1 : 1;
  1021.                                         viewMode = this.viewMode;
  1022.                                         if (e.ctrlKey) {
  1023.                                                 viewMode += 2;
  1024.                                         } else if (e.shiftKey) {
  1025.                                                 viewMode += 1;
  1026.                                         }
  1027.                                         if (viewMode == 4) {
  1028.                         newDate = this.moveYear(this.date, dir);
  1029.                         newViewDate = this.moveYear(this.viewDate, dir);
  1030.                                         } else if (viewMode == 3) {
  1031.                         newDate = this.moveMonth(this.date, dir);
  1032.                         newViewDate = this.moveMonth(this.viewDate, dir);
  1033.                                         } else if (viewMode == 2) {
  1034.                         newDate = this.moveDate(this.date, dir * 7);
  1035.                         newViewDate = this.moveDate(this.viewDate, dir * 7);
  1036.                                         } else if (viewMode == 1) {
  1037.                                                 if (this.showMeridian) {
  1038.                                                         newDate = this.moveHour(this.date, dir * 6);
  1039.                                                         newViewDate = this.moveHour(this.viewDate, dir * 6);
  1040.                                                 } else {
  1041.                                                         newDate = this.moveHour(this.date, dir * 4);
  1042.                                                         newViewDate = this.moveHour(this.viewDate, dir * 4);
  1043.                                                 }
  1044.                                         } else if (viewMode == 0) {
  1045.                         newDate = this.moveMinute(this.date, dir * 4);
  1046.                         newViewDate = this.moveMinute(this.viewDate, dir * 4);
  1047.                                         }
  1048.                     if (this.dateWithinRange(newDate)){
  1049.                         this.date = newDate;
  1050.                         this.viewDate = newViewDate;
  1051.                         this.setValue();
  1052.                         this.update();
  1053.                         e.preventDefault();
  1054.                         dateChanged = true;
  1055.                     }
  1056.                     break;
  1057.                 case 13: // enter
  1058.                                         if (this.viewMode != 0) {
  1059.                                                 var oldViewMode = this.viewMode;
  1060.                                                 this.showMode(-1);
  1061.                                                 this.fill();
  1062.                                                 if (oldViewMode == this.viewMode && this.autoclose) {
  1063.                                                         this.hide();
  1064.                                                 }
  1065.                                         } else {
  1066.                                                 this.fill();
  1067.                                                 if (this.autoclose) {
  1068.                                     this.hide();
  1069.                                                 }
  1070.                                         }
  1071.                     e.preventDefault();
  1072.                     break;
  1073.                 case 9: // tab
  1074.                     this.hide();
  1075.                     break;
  1076.             }
  1077.             if (dateChanged){
  1078.                 var element;
  1079.                 if (this.isInput) {
  1080.                     element = this.element;
  1081.                 } else if (this.component){
  1082.                     element = this.element.find('input');
  1083.                 }
  1084.                 if (element) {
  1085.                     element.change();
  1086.                 }
  1087.                 this.element.trigger({
  1088.                     type: 'changeDate',
  1089.                     date: this.date
  1090.                 });
  1091.             }
  1092.         },
  1093.  
  1094.         showMode: function(dir) {
  1095.             if (dir) {
  1096.                 var newViewMode = Math.max(0, Math.min(DPGlobal.modes.length - 1, this.viewMode + dir));
  1097.                 if (newViewMode >= this.minView && newViewMode <= this.maxView) {
  1098.                     this.element.trigger({
  1099.                         type: 'changeMode',
  1100.                         date: this.viewDate,
  1101.                         oldViewMode: this.viewMode,
  1102.                         newViewMode: newViewMode
  1103.                     });
  1104.  
  1105.                     this.viewMode = newViewMode;
  1106.                 }
  1107.             }
  1108.             /*
  1109.                 vitalets: fixing bug of very special conditions:
  1110.                 jquery 1.7.1 + webkit + show inline datetimepicker in bootstrap popover.
  1111.                 Method show() does not set display css correctly and datetimepicker is not shown.
  1112.                 Changed to .css('display', 'block') solve the problem.
  1113.                 See https://github.com/vitalets/x-editable/issues/37
  1114.  
  1115.                 In jquery 1.7.2+ everything works fine.
  1116.             */
  1117.             //this.picker.find('>div').hide().filter('.datetimepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
  1118.             this.picker.find('>div').hide().filter('.datetimepicker-'+DPGlobal.modes[this.viewMode].clsName).css('display', 'block');
  1119.             this.updateNavArrows();
  1120.         },
  1121.        
  1122.         reset: function(e) {
  1123.             this._setDate(null, 'date');
  1124.         }
  1125.     };
  1126.  
  1127.     $.fn.datetimepicker = function ( option ) {
  1128.         var args = Array.apply(null, arguments);
  1129.         args.shift();
  1130.         return this.each(function () {
  1131.             var $this = $(this),
  1132.                 data = $this.data('datetimepicker'),
  1133.                 options = typeof option == 'object' && option;
  1134.             if (!data) {
  1135.                 $this.data('datetimepicker', (data = new Datetimepicker(this, $.extend({}, $.fn.datetimepicker.defaults,options))));
  1136.             }
  1137.             if (typeof option == 'string' && typeof data[option] == 'function') {
  1138.                 data[option].apply(data, args);
  1139.             }
  1140.         });
  1141.     };
  1142.  
  1143.     $.fn.datetimepicker.defaults = {
  1144.     };
  1145.     $.fn.datetimepicker.Constructor = Datetimepicker;
  1146.     var dates = $.fn.datetimepicker.dates = {
  1147.         en: {
  1148.             days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  1149.             daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  1150.             daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  1151.             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  1152.             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  1153.             meridiem: ["am", "pm"],
  1154.             suffix: ["st", "nd", "rd", "th"],
  1155.             today: "Today"
  1156.         }
  1157.     };
  1158.  
  1159.     var DPGlobal = {
  1160.         modes: [
  1161.             {
  1162.                 clsName: 'minutes',
  1163.                 navFnc: 'Hours',
  1164.                 navStep: 1
  1165.             },
  1166.             {
  1167.                 clsName: 'hours',
  1168.                 navFnc: 'Date',
  1169.                 navStep: 1
  1170.             },
  1171.             {
  1172.                 clsName: 'days',
  1173.                 navFnc: 'Month',
  1174.                 navStep: 1
  1175.             },
  1176.             {
  1177.                 clsName: 'months',
  1178.                 navFnc: 'FullYear',
  1179.                 navStep: 1
  1180.             },
  1181.             {
  1182.                 clsName: 'years',
  1183.                 navFnc: 'FullYear',
  1184.                 navStep: 10
  1185.         }],
  1186.         isLeapYear: function (year) {
  1187.             return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
  1188.         },
  1189.         getDaysInMonth: function (year, month) {
  1190.             return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
  1191.         },
  1192.         getDefaultFormat: function (type, field) {
  1193.             if (type == "standard") {
  1194.                 if (field == 'input')
  1195.                     return 'yyyy-mm-dd hh:ii';
  1196.                 else
  1197.                     return 'yyyy-mm-dd hh:ii:ss';
  1198.             } else if (type == "php") {
  1199.                 if (field == 'input')
  1200.                     return 'Y-m-d H:i';
  1201.                 else
  1202.                     return 'Y-m-d H:i:s';
  1203.             } else {
  1204.                 throw new Error("Invalid format type.");
  1205.             }
  1206.         },
  1207.         validParts: function (type) {
  1208.             if (type == "standard") {
  1209.                 return /hh?|HH?|p|P|ii?|ss?|dd?|DD?|mm?|MM?|yy(?:yy)?/g;
  1210.             } else if (type == "php") {
  1211.                 return /[dDjlNwzFmMnStyYaABgGhHis]/g;
  1212.             } else {
  1213.                 throw new Error("Invalid format type.");
  1214.             }
  1215.         },
  1216.         nonpunctuation: /[^ -\/:-@\[-`{-~\t\n\rTZ]+/g,
  1217.         parseFormat: function(format, type){
  1218.             // IE treats \0 as a string end in inputs (truncating the value),
  1219.             // so it's a bad format delimiter, anyway
  1220.             var separators = format.replace(this.validParts(type), '\0').split('\0'),
  1221.                 parts = format.match(this.validParts(type));
  1222.             if (!separators || !separators.length || !parts || parts.length == 0){
  1223.                 throw new Error("Invalid date format.");
  1224.             }
  1225.             return {separators: separators, parts: parts};
  1226.         },
  1227.         parseDate: function(date, format, language, type) {
  1228.             if (date instanceof Date) {
  1229.                 var dateUTC = new Date(date.valueOf() - date.getTimezoneOffset() * 60000);
  1230.                                 dateUTC.setMilliseconds(0);
  1231.                 return dateUTC;
  1232.             }
  1233.             if (/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(date)) {
  1234.                 format = this.parseFormat('yyyy-mm-dd', type);
  1235.             }
  1236.             if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}$/.test(date)) {
  1237.                 format = this.parseFormat('yyyy-mm-dd hh:ii', type);
  1238.             }
  1239.             if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}\:\d{1,2}[Z]{0,1}$/.test(date)) {
  1240.                 format = this.parseFormat('yyyy-mm-dd hh:ii:ss', type);
  1241.             }
  1242.             if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
  1243.                 var part_re = /([-+]\d+)([dmwy])/,
  1244.                     parts = date.match(/([-+]\d+)([dmwy])/g),
  1245.                     part, dir;
  1246.                 date = new Date();
  1247.                 for (var i=0; i<parts.length; i++) {
  1248.                     part = part_re.exec(parts[i]);
  1249.                     dir = parseInt(part[1]);
  1250.                     switch(part[2]){
  1251.                         case 'd':
  1252.                             date.setUTCDate(date.getUTCDate() + dir);
  1253.                             break;
  1254.                         case 'm':
  1255.                             date = Datetimepicker.prototype.moveMonth.call(Datetimepicker.prototype, date, dir);
  1256.                             break;
  1257.                         case 'w':
  1258.                             date.setUTCDate(date.getUTCDate() + dir * 7);
  1259.                             break;
  1260.                         case 'y':
  1261.                             date = Datetimepicker.prototype.moveYear.call(Datetimepicker.prototype, date, dir);
  1262.                             break;
  1263.                     }
  1264.                 }
  1265.                 return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), 0);
  1266.             }
  1267.             var parts = date && date.match(this.nonpunctuation) || [],
  1268.                 date = new Date(0, 0, 0, 0, 0, 0, 0),
  1269.                 parsed = {},
  1270.                 setters_order = ['hh', 'h', 'ii', 'i', 'ss', 's', 'yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'D', 'DD', 'd', 'dd', 'H', 'HH', 'p', 'P'],
  1271.                 setters_map = {
  1272.                     hh: function(d,v){ return d.setUTCHours(v); },
  1273.                     h:  function(d,v){ return d.setUTCHours(v); },
  1274.                     HH: function(d,v){ return d.setUTCHours(v==12?0:v); },
  1275.                     H:  function(d,v){ return d.setUTCHours(v==12?0:v); },
  1276.                     ii: function(d,v){ return d.setUTCMinutes(v); },
  1277.                     i:  function(d,v){ return d.setUTCMinutes(v); },
  1278.                     ss: function(d,v){ return d.setUTCSeconds(v); },
  1279.                     s:  function(d,v){ return d.setUTCSeconds(v); },
  1280.                     yyyy: function(d,v){ return d.setUTCFullYear(v); },
  1281.                     yy: function(d,v){ return d.setUTCFullYear(2000+v); },
  1282.                     m: function(d,v){
  1283.                         v -= 1;
  1284.                         while (v<0) v += 12;
  1285.                         v %= 12;
  1286.                         d.setUTCMonth(v);
  1287.                         while (d.getUTCMonth() != v)
  1288.                             d.setUTCDate(d.getUTCDate()-1);
  1289.                         return d;
  1290.                     },
  1291.                     d: function(d,v){ return d.setUTCDate(v); },
  1292.                     p: function(d,v){ return d.setUTCHours(v==1?d.getUTCHours()+12:d.getUTCHours()); }
  1293.                 },
  1294.                 val, filtered, part;
  1295.             setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  1296.             setters_map['dd'] = setters_map['d'];
  1297.                     setters_map['P'] = setters_map['p'];
  1298.             date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
  1299.             if (parts.length == format.parts.length) {
  1300.                 for (var i=0, cnt = format.parts.length; i < cnt; i++) {
  1301.                     val = parseInt(parts[i], 10);
  1302.                     part = format.parts[i];
  1303.                     if (isNaN(val)) {
  1304.                         switch(part) {
  1305.                             case 'MM':
  1306.                                 filtered = $(dates[language].months).filter(function(){
  1307.                                     var m = this.slice(0, parts[i].length),
  1308.                                         p = parts[i].slice(0, m.length);
  1309.                                     return m == p;
  1310.                                 });
  1311.                                 val = $.inArray(filtered[0], dates[language].months) + 1;
  1312.                                 break;
  1313.                             case 'M':
  1314.                                 filtered = $(dates[language].monthsShort).filter(function(){
  1315.                                     var m = this.slice(0, parts[i].length),
  1316.                                         p = parts[i].slice(0, m.length);
  1317.                                     return m == p;
  1318.                                 });
  1319.                                 val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  1320.                                 break;
  1321.                                 case 'p':
  1322.                                 case 'P':
  1323.                                         val = $.inArray(parts[i].toLowerCase(), dates[language].meridiem);
  1324.                                         break;
  1325.                         }
  1326.                     }
  1327.                     parsed[part] = val;
  1328.                 }
  1329.                 for (var i=0, s; i<setters_order.length; i++){
  1330.                     s = setters_order[i];
  1331.                     if (s in parsed && !isNaN(parsed[s]))
  1332.                         setters_map[s](date, parsed[s])
  1333.                 }
  1334.             }
  1335.             return date;
  1336.         },
  1337.         formatDate: function(date, format, language, type){
  1338.             if (date == null) {
  1339.                 return '';
  1340.             }
  1341.             var val;
  1342.             if (type == 'standard') {
  1343.                 val = {
  1344.                     // year
  1345.                     yy: date.getUTCFullYear().toString().substring(2),
  1346.                     yyyy: date.getUTCFullYear(),
  1347.                     // month
  1348.                     m: date.getUTCMonth() + 1,
  1349.                     M: dates[language].monthsShort[date.getUTCMonth()],
  1350.                     MM: dates[language].months[date.getUTCMonth()],
  1351.                     // day
  1352.                     d: date.getUTCDate(),
  1353.                     D: dates[language].daysShort[date.getUTCDay()],
  1354.                     DD: dates[language].days[date.getUTCDay()],
  1355.                     p: (dates[language].meridiem.length==2?dates[language].meridiem[date.getUTCHours()<12?0:1]:''),
  1356.                     // hour
  1357.                     h: date.getUTCHours(),
  1358.                     // minute
  1359.                     i: date.getUTCMinutes(),
  1360.                     // second
  1361.                     s: date.getUTCSeconds()
  1362.                 };
  1363.                                 val.H  = (val.h%12==0? 12 : val.h%12);
  1364.                                 val.HH = (val.H < 10 ? '0' : '') + val.H;
  1365.                                 val.P  = val.p.toUpperCase();
  1366.                 val.hh = (val.h < 10 ? '0' : '') + val.h;
  1367.                 val.ii = (val.i < 10 ? '0' : '') + val.i;
  1368.                 val.ss = (val.s < 10 ? '0' : '') + val.s;
  1369.                 val.dd = (val.d < 10 ? '0' : '') + val.d;
  1370.                 val.mm = (val.m < 10 ? '0' : '') + val.m;
  1371.             } else if (type == 'php') {
  1372.                 // php format
  1373.                 val = {
  1374.                     // year
  1375.                     y: date.getUTCFullYear().toString().substring(2),
  1376.                     Y: date.getUTCFullYear(),
  1377.                     // month
  1378.                     F: dates[language].months[date.getUTCMonth()],
  1379.                     M: dates[language].monthsShort[date.getUTCMonth()],
  1380.                     n: date.getUTCMonth() + 1,
  1381.                     t: DPGlobal.getDaysInMonth(date.getUTCFullYear(), date.getUTCMonth()),
  1382.                     // day
  1383.                     j: date.getUTCDate(),
  1384.                     l: dates[language].days[date.getUTCDay()],
  1385.                     D: dates[language].daysShort[date.getUTCDay()],
  1386.                     w: date.getUTCDay(), // 0 -> 6
  1387.                     N: (date.getUTCDay()==0?7:date.getUTCDay()),       // 1 -> 7
  1388.                     S: (date.getUTCDate()%10<=dates[language].suffix.length?dates[language].suffix[date.getUTCDate()%10-1]:''),
  1389.                     // hour
  1390.                     a: (dates[language].meridiem.length==2?dates[language].meridiem[date.getUTCHours()<12?0:1]:''),
  1391.                     g: (date.getUTCHours()%12==0?12:date.getUTCHours()%12),
  1392.                     G: date.getUTCHours(),
  1393.                     // minute
  1394.                     i: date.getUTCMinutes(),
  1395.                     // second
  1396.                     s: date.getUTCSeconds()
  1397.                 };
  1398.                 val.m = (val.n < 10 ? '0' : '') + val.n;
  1399.                 val.d = (val.j < 10 ? '0' : '') + val.j;
  1400.                 val.A = val.a.toString().toUpperCase();
  1401.                 val.h = (val.g < 10 ? '0' : '') + val.g;
  1402.                 val.H = (val.G < 10 ? '0' : '') + val.G;
  1403.                 val.i = (val.i < 10 ? '0' : '') + val.i;
  1404.                 val.s = (val.s < 10 ? '0' : '') + val.s;
  1405.             } else {
  1406.                 throw new Error("Invalid format type.");
  1407.             }
  1408.             var date = [],
  1409.                 seps = $.extend([], format.separators);
  1410.             for (var i=0, cnt = format.parts.length; i < cnt; i++) {
  1411.                 if (seps.length)
  1412.                     date.push(seps.shift())
  1413.                 date.push(val[format.parts[i]]);
  1414.             }
  1415.             return date.join('');
  1416.         },
  1417.         convertViewMode: function(viewMode){
  1418.             switch (viewMode) {
  1419.                 case 4:
  1420.                 case 'decade':
  1421.                     viewMode = 4;
  1422.                     break;
  1423.                 case 3:
  1424.                 case 'year':
  1425.                     viewMode = 3;
  1426.                     break;
  1427.                 case 2:
  1428.                 case 'month':
  1429.                     viewMode = 2;
  1430.                     break;
  1431.                 case 1:
  1432.                 case 'day':
  1433.                     viewMode = 1;
  1434.                     break;
  1435.                 case 0:
  1436.                 case 'hour':
  1437.                     viewMode = 0;
  1438.                     break;
  1439.             }
  1440.  
  1441.             return viewMode;
  1442.         },
  1443.         headTemplate: '<thead>'+
  1444.                             '<tr>'+
  1445.                                 '<th class="prev"><i class="glyphicon glyphicon-arrow-left"/></th>'+
  1446.                                 '<th colspan="5" class="switch"></th>'+
  1447.                                 '<th class="next"><i class="glyphicon glyphicon-arrow-right"/></th>'+
  1448.                             '</tr>'+
  1449.                         '</thead>',
  1450.         contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
  1451.         footTemplate: '<tfoot><tr><th colspan="7" class="today"></th></tr></tfoot>'
  1452.     };
  1453.     DPGlobal.template = '<div class="datetimepicker">'+
  1454.                             '<div class="datetimepicker-minutes">'+
  1455.                                 '<table class=" table-condensed">'+
  1456.                                     DPGlobal.headTemplate+
  1457.                                     DPGlobal.contTemplate+
  1458.                                     DPGlobal.footTemplate+
  1459.                                 '</table>'+
  1460.                             '</div>'+
  1461.                             '<div class="datetimepicker-hours">'+
  1462.                                 '<table class=" table-condensed">'+
  1463.                                     DPGlobal.headTemplate+
  1464.                                     DPGlobal.contTemplate+
  1465.                                     DPGlobal.footTemplate+
  1466.                                 '</table>'+
  1467.                             '</div>'+
  1468.                             '<div class="datetimepicker-days">'+
  1469.                                 '<table class=" table-condensed">'+
  1470.                                     DPGlobal.headTemplate+
  1471.                                     '<tbody></tbody>'+
  1472.                                     DPGlobal.footTemplate+
  1473.                                 '</table>'+
  1474.                             '</div>'+
  1475.                             '<div class="datetimepicker-months">'+
  1476.                                 '<table class="table-condensed">'+
  1477.                                     DPGlobal.headTemplate+
  1478.                                     DPGlobal.contTemplate+
  1479.                                     DPGlobal.footTemplate+
  1480.                                 '</table>'+
  1481.                             '</div>'+
  1482.                             '<div class="datetimepicker-years">'+
  1483.                                 '<table class="table-condensed">'+
  1484.                                     DPGlobal.headTemplate+
  1485.                                     DPGlobal.contTemplate+
  1486.                                     DPGlobal.footTemplate+
  1487.                                 '</table>'+
  1488.                             '</div>'+
  1489.                         '</div>';
  1490.  
  1491.     $.fn.datetimepicker.DPGlobal = DPGlobal;
  1492.  
  1493.  
  1494.     /* DATETIMEPICKER NO CONFLICT
  1495.     * =================== */
  1496.  
  1497.     $.fn.datetimepicker.noConflict = function () {
  1498.         $.fn.datetimepicker = old;
  1499.         return this;
  1500.     };
  1501.  
  1502.  
  1503.     /* DATETIMEPICKER DATA-API
  1504.     * ================== */
  1505.  
  1506.     $(document).on(
  1507.         'focus.datetimepicker.data-api click.datetimepicker.data-api',
  1508.         '[data-provide="datetimepicker"]',
  1509.         function (e) {
  1510.             var $this = $(this);
  1511.             if ($this.data('datetimepicker')) return;
  1512.             e.preventDefault();
  1513.             // component click requires us to explicitly show it
  1514.             $this.datetimepicker('show');
  1515.         }
  1516.     );
  1517.     $(function () {
  1518.         $('[data-provide="datetimepicker-inline"]').datetimepicker();
  1519.     });
  1520.  
  1521. }( window.jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement