/* ========================================================= * bootstrap-datetimepicker.js * ========================================================= * copyright 2012 stefan petre * improvements by andrew rowls * improvements by sébastien malot * project url : http://www.malot.fr/bootstrap-datetimepicker * * 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( $ ) { function utcdate(){ return new date(date.utc.apply(date, arguments)); } function utctoday(){ var today = new date(); return utcdate(today.getutcfullyear(), today.getutcmonth(), today.getutcdate(), today.getutchours(), today.getutcminutes(), today.getutcseconds(), 0); } // picker object var datetimepicker = function(element, options) { var that = this; this.element = $(element); this.language = options.language || this.element.data('date-language') || "en"; this.language = this.language in dates ? this.language : "en"; this.isrtl = dates[this.language].rtl || false; this.formattype = options.formattype || this.element.data('format-type') || 'standard'; this.format = dpglobal.parseformat(options.format || this.element.data('date-format') || dpglobal.getdefaultformat(this.formattype, 'input'), this.formattype); this.isinline = false; this.isvisible = false; this.isinput = this.element.is('input'); this.component = this.element.is('.date') ? this.element.find('.add-on .icon-th, .add-on .icon-time, .add-on .icon-calendar').parent() : false; this.componentreset = this.element.is('.date') ? this.element.find('.add-on .icon-remove').parent() : false; this.hasinput = this.component && this.element.find('input').length; if (this.component && this.component.length === 0) { this.component = false; } this.linkfield = options.linkfield || this.element.data('link-field') || false; this.linkformat = dpglobal.parseformat(options.linkformat || this.element.data('link-format') || dpglobal.getdefaultformat(this.formattype, 'link'), this.formattype); this.minutestep = options.minutestep || this.element.data('minute-step') || 5; this.pickerposition = options.pickerposition || this.element.data('picker-position') || 'bottom-right'; this.showmeridian = options.showmeridian || this.element.data('show-meridian') || false; this.initialdate = options.initialdate || new date(); this._attachevents(); this.formatviewtype = "datetime"; if ('formatviewtype' in options) { this.formatviewtype = options.formatviewtype; } else if ('formatviewtype' in this.element.data()) { this.formatviewtype = this.element.data('formatviewtype'); } this.minview = 0; if ('minview' in options) { this.minview = options.minview; } else if ('minview' in this.element.data()) { this.minview = this.element.data('min-view'); } this.minview = dpglobal.convertviewmode(this.minview); this.maxview = dpglobal.modes.length-1; if ('maxview' in options) { this.maxview = options.maxview; } else if ('maxview' in this.element.data()) { this.maxview = this.element.data('max-view'); } this.maxview = dpglobal.convertviewmode(this.maxview); this.startviewmode = 2; if ('startview' in options) { this.startviewmode = options.startview; } else if ('startview' in this.element.data()) { this.startviewmode = this.element.data('start-view'); } this.startviewmode = dpglobal.convertviewmode(this.startviewmode); this.viewmode = this.startviewmode; this.viewselect = this.minview; if ('viewselect' in options) { this.viewselect = options.viewselect; } else if ('viewselect' in this.element.data()) { this.viewselect = this.element.data('view-select'); } this.viewselect = dpglobal.convertviewmode(this.viewselect); this.forceparse = true; if ('forceparse' in options) { this.forceparse = options.forceparse; } else if ('dateforceparse' in this.element.data()) { this.forceparse = this.element.data('date-force-parse'); } this.picker = $(dpglobal.template) .appendto(this.isinline ? this.element : 'body') .on({ click: $.proxy(this.click, this), mousedown: $.proxy(this.mousedown, this) }); if (this.isinline) { this.picker.addclass('datetimepicker-inline'); } else { this.picker.addclass('datetimepicker-dropdown-' + this.pickerposition + ' dropdown-menu'); } if (this.isrtl){ this.picker.addclass('datetimepicker-rtl'); this.picker.find('.prev i, .next i') .toggleclass('icon-arrow-left icon-arrow-right'); } $(document).on('mousedown', function (e) { // clicked outside the datetimepicker, hide it if ($(e.target).closest('.datetimepicker').length === 0) { that.hide(); } }); this.autoclose = false; if ('autoclose' in options) { this.autoclose = options.autoclose; } else if ('dateautoclose' in this.element.data()) { this.autoclose = this.element.data('date-autoclose'); } this.keyboardnavigation = true; if ('keyboardnavigation' in options) { this.keyboardnavigation = options.keyboardnavigation; } else if ('datekeyboardnavigation' in this.element.data()) { this.keyboardnavigation = this.element.data('date-keyboard-navigation'); } this.todaybtn = (options.todaybtn || this.element.data('date-today-btn') || false); this.todayhighlight = (options.todayhighlight || this.element.data('date-today-highlight') || false); this.weekstart = ((options.weekstart || this.element.data('date-weekstart') || dates[this.language].weekstart || 0) % 7); this.weekend = ((this.weekstart + 6) % 7); this.startdate = -infinity; this.enddate = infinity; this.daysofweekdisabled = []; this.setstartdate(options.startdate || this.element.data('date-startdate')); this.setenddate(options.enddate || this.element.data('date-enddate')); this.setdaysofweekdisabled(options.daysofweekdisabled || this.element.data('date-days-of-week-disabled')); this.filldow(); this.fillmonths(); this.update(); this.showmode(); if(this.isinline) { this.show(); } }; datetimepicker.prototype = { constructor: datetimepicker, _events: [], _attachevents: function(){ this._detachevents(); if (this.isinput) { // single input this._events = [ [this.element, { focus: $.proxy(this.show, this), keyup: $.proxy(this.update, this), keydown: $.proxy(this.keydown, this) }] ]; } else if (this.component && this.hasinput){ // component: input + button this._events = [ // for components that are not readonly, allow keyboard nav [this.element.find('input'), { focus: $.proxy(this.show, this), keyup: $.proxy(this.update, this), keydown: $.proxy(this.keydown, this) }], [this.component, { click: $.proxy(this.show, this) }] ]; if (this.componentreset) { this._events.push([ this.componentreset, {click: $.proxy(this.reset, this)} ]); } } else if (this.element.is('div')) { // inline datetimepicker this.isinline = true; } else { this._events = [ [this.element, { click: $.proxy(this.show, this) }] ]; } for (var i=0, el, ev; i= this.startdate && d <= this.enddate) { this.date = d; this.setvalue(); this.viewdate = this.date; this.fill(); } else { this.element.trigger({ type: 'outofrange', date: d, startdate: this.startdate, enddate: this.enddate }); } }, setformat: function(format) { this.format = dpglobal.parseformat(format, this.formattype); var element; if (this.isinput) { element = this.element; } else if (this.component){ element = this.element.find('input'); } if (element && element.val()) { this.setvalue(); } }, setvalue: function() { var formatted = this.getformatteddate(); if (!this.isinput) { if (this.component){ this.element.find('input').val(formatted); } this.element.data('date', formatted); } else { this.element.val(formatted); } if (this.linkfield) { $('#' + this.linkfield).val(this.getformatteddate(this.linkformat)); } }, getformatteddate: function(format) { if(format == undefined) format = this.format; return dpglobal.formatdate(this.date, format, this.language, this.formattype); }, setstartdate: function(startdate){ this.startdate = startdate || -infinity; if (this.startdate !== -infinity) { this.startdate = dpglobal.parsedate(this.startdate, this.format, this.language, this.formattype); } this.update(); this.updatenavarrows(); }, setenddate: function(enddate){ this.enddate = enddate || infinity; if (this.enddate !== infinity) { this.enddate = dpglobal.parsedate(this.enddate, this.format, this.language, this.formattype); } this.update(); this.updatenavarrows(); }, setdaysofweekdisabled: function(daysofweekdisabled){ this.daysofweekdisabled = daysofweekdisabled || []; if (!$.isarray(this.daysofweekdisabled)) { this.daysofweekdisabled = this.daysofweekdisabled.split(/,\s*/); } this.daysofweekdisabled = $.map(this.daysofweekdisabled, function (d) { return parseint(d, 10); }); this.update(); this.updatenavarrows(); }, place: function(){ if(this.isinline) return; var zindex = parseint(this.element.parents().filter(function() { return $(this).css('z-index') != 'auto'; }).first().css('z-index'))+10; var offset, top, left; if (this.component) { offset = this.component.offset(); left = offset.left; if (this.pickerposition == 'bottom-left' || this.pickerposition == 'top-left') { left += this.component.outerwidth() - this.picker.outerwidth(); } } else { offset = this.element.offset(); left = offset.left; } if (this.pickerposition == 'top-left' || this.pickerposition == 'top-right') { top = offset.top - this.picker.outerheight(); } else { top = offset.top + this.height; } this.picker.css({ top: top, left: left, zindex: zindex }); }, update: function(){ var date, fromargs = false; if(arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof date)) { date = arguments[0]; fromargs = true; } else { date = this.element.data('date') || (this.isinput ? this.element.val() : this.element.find('input').val()) || this.initialdate; } if (!date) { date = new date(); fromargs = false; } this.date = dpglobal.parsedate(date, this.format, this.language, this.formattype); if (fromargs) this.setvalue(); if (this.date < this.startdate) { this.viewdate = new date(this.startdate); } else if (this.date > this.enddate) { this.viewdate = new date(this.enddate); } else { this.viewdate = new date(this.date); } this.fill(); }, filldow: function(){ var dowcnt = this.weekstart, html = ''; while (dowcnt < this.weekstart + 7) { html += ''+dates[this.language].daysmin[(dowcnt++)%7]+''; } html += ''; this.picker.find('.datetimepicker-days thead').append(html); }, fillmonths: function(){ var html = '', i = 0; while (i < 12) { html += ''+dates[this.language].monthsshort[i++]+''; } this.picker.find('.datetimepicker-months td').html(html); }, fill: function() { if (this.date == null || this.viewdate == null) { return; } var d = new date(this.viewdate), year = d.getutcfullyear(), month = d.getutcmonth(), daymonth = d.getutcdate(), hours = d.getutchours(), minutes = d.getutcminutes(), startyear = this.startdate !== -infinity ? this.startdate.getutcfullyear() : -infinity, startmonth = this.startdate !== -infinity ? this.startdate.getutcmonth() : -infinity, endyear = this.enddate !== infinity ? this.enddate.getutcfullyear() : infinity, endmonth = this.enddate !== infinity ? this.enddate.getutcmonth() : infinity, currentdate = (new utcdate(this.date.getutcfullyear(), this.date.getutcmonth(), this.date.getutcdate())).valueof(), today = new date(); this.picker.find('.datetimepicker-days thead th:eq(1)') .text(dates[this.language].months[month]+' '+year); if (this.formatviewtype == "time") { var hourconverted = hours % 12 ? hours % 12 : 12; var hoursdisplay = (hourconverted < 10 ? '0' : '') + hourconverted; var minutesdisplay = (minutes < 10 ? '0' : '') + minutes; var meridiandisplay = dates[this.language].meridiem[hours < 12 ? 0 : 1]; this.picker.find('.datetimepicker-hours thead th:eq(1)') .text(hoursdisplay + ':' + minutesdisplay + ' ' + meridiandisplay.touppercase()); this.picker.find('.datetimepicker-minutes thead th:eq(1)') .text(hoursdisplay + ':' + minutesdisplay + ' ' + meridiandisplay.touppercase()); } else { this.picker.find('.datetimepicker-hours thead th:eq(1)') .text(daymonth + ' ' + dates[this.language].months[month] + ' ' + year); this.picker.find('.datetimepicker-minutes thead th:eq(1)') .text(daymonth + ' ' + dates[this.language].months[month] + ' ' + year); } this.picker.find('tfoot th.today') .text(dates[this.language].today) .toggle(this.todaybtn !== false); this.updatenavarrows(); this.fillmonths(); /*var prevmonth = utcdate(year, month, 0,0,0,0,0); prevmonth.setutcdate(prevmonth.getdate() - (prevmonth.getutcday() - this.weekstart + 7)%7);*/ var prevmonth = utcdate(year, month-1, 28,0,0,0,0), day = dpglobal.getdaysinmonth(prevmonth.getutcfullyear(), prevmonth.getutcmonth()); prevmonth.setutcdate(day); prevmonth.setutcdate(day - (prevmonth.getutcday() - this.weekstart + 7)%7); var nextmonth = new date(prevmonth); nextmonth.setutcdate(nextmonth.getutcdate() + 42); nextmonth = nextmonth.valueof(); var html = []; var clsname; while(prevmonth.valueof() < nextmonth) { if (prevmonth.getutcday() == this.weekstart) { html.push(''); } clsname = ''; if (prevmonth.getutcfullyear() < year || (prevmonth.getutcfullyear() == year && prevmonth.getutcmonth() < month)) { clsname += ' old'; } else if (prevmonth.getutcfullyear() > year || (prevmonth.getutcfullyear() == year && prevmonth.getutcmonth() > month)) { clsname += ' new'; } // compare internal utc date with local today, not utc today if (this.todayhighlight && prevmonth.getutcfullyear() == today.getfullyear() && prevmonth.getutcmonth() == today.getmonth() && prevmonth.getutcdate() == today.getdate()) { clsname += ' today'; } if (prevmonth.valueof() == currentdate) { clsname += ' active'; } if ((prevmonth.valueof() + 86400000) <= this.startdate || prevmonth.valueof() > this.enddate || $.inarray(prevmonth.getutcday(), this.daysofweekdisabled) !== -1) { clsname += ' disabled'; } html.push(''+prevmonth.getutcdate() + ''); if (prevmonth.getutcday() == this.weekend) { html.push(''); } prevmonth.setutcdate(prevmonth.getutcdate()+1); } this.picker.find('.datetimepicker-days tbody').empty().append(html.join('')); html = []; var txt = '', meridian = '', meridianold = ''; for (var i=0;i<24;i++) { var actual = utcdate(year, month, daymonth, i); clsname = ''; // we want the previous hour for the startdate if ((actual.valueof() + 3600000) <= this.startdate || actual.valueof() > this.enddate) { clsname += ' disabled'; } else if (hours == i) { clsname += ' active'; } if (this.showmeridian && dates[this.language].meridiem.length == 2) { meridian = (i<12?dates[this.language].meridiem[0]:dates[this.language].meridiem[1]); if (meridian != meridianold) { if (meridianold != '') { html.push(''); } html.push('
'+meridian.touppercase()+''); } meridianold = meridian; txt = (i%12?i%12:12); html.push(''+txt+''); if (i == 23) { html.push('
'); } } else { txt = i+':00'; html.push(''+txt+''); } } this.picker.find('.datetimepicker-hours td').html(html.join('')); html = []; txt = '', meridian = '', meridianold = ''; for(var i=0;i<60;i+=this.minutestep) { var actual = utcdate(year, month, daymonth, hours, i, 0); clsname = ''; if (actual.valueof() < this.startdate || actual.valueof() > this.enddate) { clsname += ' disabled'; } else if (math.floor(minutes/this.minutestep) == math.floor(i/this.minutestep)) { clsname += ' active'; } if (this.showmeridian && dates[this.language].meridiem.length == 2) { meridian = (hours<12?dates[this.language].meridiem[0]:dates[this.language].meridiem[1]); if (meridian != meridianold) { if (meridianold != '') { html.push(''); } html.push('
'+meridian.touppercase()+''); } meridianold = meridian; txt = (hours%12?hours%12:12); //html.push(''+txt+''); html.push(''+txt+':'+(i<10?'0'+i:i)+''); if (i == 59) { html.push('
'); } } else { txt = i+':00'; //html.push(''+txt+''); html.push(''+hours+':'+(i<10?'0'+i:i)+''); } } this.picker.find('.datetimepicker-minutes td').html(html.join('')); var currentyear = this.date.getutcfullyear(); var months = this.picker.find('.datetimepicker-months') .find('th:eq(1)') .text(year) .end() .find('span').removeclass('active'); if (currentyear == year) { months.eq(this.date.getutcmonth()).addclass('active'); } if (year < startyear || year > endyear) { months.addclass('disabled'); } if (year == startyear) { months.slice(0, startmonth).addclass('disabled'); } if (year == endyear) { months.slice(endmonth+1).addclass('disabled'); } html = ''; year = parseint(year/10, 10) * 10; var yearcont = this.picker.find('.datetimepicker-years') .find('th:eq(1)') .text(year + '-' + (year + 9)) .end() .find('td'); year -= 1; for (var i = -1; i < 11; i++) { html += ''+year+''; year += 1; } yearcont.html(html); this.place(); }, updatenavarrows: function() { var d = new date(this.viewdate), year = d.getutcfullyear(), month = d.getutcmonth(), day = d.getutcdate(), hour = d.getutchours(); switch (this.viewmode) { case 0: if (this.startdate !== -infinity && year <= this.startdate.getutcfullyear() && month <= this.startdate.getutcmonth() && day <= this.startdate.getutcdate() && hour <= this.startdate.getutchours()) { this.picker.find('.prev').css({visibility: 'hidden'}); } else { this.picker.find('.prev').css({visibility: 'visible'}); } if (this.enddate !== infinity && year >= this.enddate.getutcfullyear() && month >= this.enddate.getutcmonth() && day >= this.enddate.getutcdate() && hour >= this.enddate.getutchours()) { this.picker.find('.next').css({visibility: 'hidden'}); } else { this.picker.find('.next').css({visibility: 'visible'}); } break; case 1: if (this.startdate !== -infinity && year <= this.startdate.getutcfullyear() && month <= this.startdate.getutcmonth() && day <= this.startdate.getutcdate()) { this.picker.find('.prev').css({visibility: 'hidden'}); } else { this.picker.find('.prev').css({visibility: 'visible'}); } if (this.enddate !== infinity && year >= this.enddate.getutcfullyear() && month >= this.enddate.getutcmonth() && day >= this.enddate.getutcdate()) { this.picker.find('.next').css({visibility: 'hidden'}); } else { this.picker.find('.next').css({visibility: 'visible'}); } break; case 2: if (this.startdate !== -infinity && year <= this.startdate.getutcfullyear() && month <= this.startdate.getutcmonth()) { this.picker.find('.prev').css({visibility: 'hidden'}); } else { this.picker.find('.prev').css({visibility: 'visible'}); } if (this.enddate !== infinity && year >= this.enddate.getutcfullyear() && month >= this.enddate.getutcmonth()) { this.picker.find('.next').css({visibility: 'hidden'}); } else { this.picker.find('.next').css({visibility: 'visible'}); } break; case 3: case 4: if (this.startdate !== -infinity && year <= this.startdate.getutcfullyear()) { this.picker.find('.prev').css({visibility: 'hidden'}); } else { this.picker.find('.prev').css({visibility: 'visible'}); } if (this.enddate !== infinity && year >= this.enddate.getutcfullyear()) { this.picker.find('.next').css({visibility: 'hidden'}); } else { this.picker.find('.next').css({visibility: 'visible'}); } break; } }, click: function(e) { e.stoppropagation(); e.preventdefault(); var target = $(e.target).closest('span, td, th, legend'); if (target.length == 1) { if (target.is('.disabled')) { this.element.trigger({ type: 'outofrange', date: this.viewdate, startdate: this.startdate, enddate: this.enddate }); return; } switch(target[0].nodename.tolowercase()) { case 'th': switch(target[0].classname) { case 'switch': this.showmode(1); break; case 'prev': case 'next': var dir = dpglobal.modes[this.viewmode].navstep * (target[0].classname == 'prev' ? -1 : 1); switch(this.viewmode){ case 0: this.viewdate = this.movehour(this.viewdate, dir); break; case 1: this.viewdate = this.movedate(this.viewdate, dir); break; case 2: this.viewdate = this.movemonth(this.viewdate, dir); break; case 3: case 4: this.viewdate = this.moveyear(this.viewdate, dir); break; } this.fill(); break; case 'today': var date = new date(); date = utcdate(date.getfullyear(), date.getmonth(), date.getdate(), date.gethours(), date.getminutes(), date.getseconds(), 0); this.viewmode = this.startviewmode; this.showmode(0); this._setdate(date); this.fill(); if (this.autoclose) { this.hide(); } break; } break; case 'span': if (!target.is('.disabled')) { var year = this.viewdate.getutcfullyear(), month = this.viewdate.getutcmonth(), day = this.viewdate.getutcdate(), hours = this.viewdate.getutchours(), minutes = this.viewdate.getutcminutes(), seconds = this.viewdate.getutcseconds(); if (target.is('.month')) { this.viewdate.setutcdate(1); month = target.parent().find('span').index(target); day = this.viewdate.getutcdate(); this.viewdate.setutcmonth(month); this.element.trigger({ type: 'changemonth', date: this.viewdate }); if (this.viewselect >= 3) { this._setdate(utcdate(year, month, day, hours, minutes, seconds, 0)); } } else if (target.is('.year')) { this.viewdate.setutcdate(1); year = parseint(target.text(), 10) || 0; this.viewdate.setutcfullyear(year); this.element.trigger({ type: 'changeyear', date: this.viewdate }); if (this.viewselect >= 4) { this._setdate(utcdate(year, month, day, hours, minutes, seconds, 0)); } } else if (target.is('.hour')){ hours = parseint(target.text(), 10) || 0; if (target.hasclass('hour_am') || target.hasclass('hour_pm')) { if (hours == 12 && target.hasclass('hour_am')) { hours = 0; } else if (hours != 12 && target.hasclass('hour_pm')) { hours += 12; } } this.viewdate.setutchours(hours); this.element.trigger({ type: 'changehour', date: this.viewdate }); if (this.viewselect >= 1) { this._setdate(utcdate(year, month, day, hours, minutes, seconds, 0)); } } else if (target.is('.minute')){ minutes = parseint(target.text().substr(target.text().indexof(':')+1), 10) || 0; this.viewdate.setutcminutes(minutes); this.element.trigger({ type: 'changeminute', date: this.viewdate }); if (this.viewselect >= 0) { this._setdate(utcdate(year, month, day, hours, minutes, seconds, 0)); } } if (this.viewmode != 0) { var oldviewmode = this.viewmode; this.showmode(-1); this.fill(); if (oldviewmode == this.viewmode && this.autoclose) { this.hide(); } } else { this.fill(); if (this.autoclose) { this.hide(); } } } break; case 'td': if (target.is('.day') && !target.is('.disabled')){ var day = parseint(target.text(), 10) || 1; var year = this.viewdate.getutcfullyear(), month = this.viewdate.getutcmonth(), hours = this.viewdate.getutchours(), minutes = this.viewdate.getutcminutes(), seconds = this.viewdate.getutcseconds(); if (target.is('.old')) { if (month === 0) { month = 11; year -= 1; } else { month -= 1; } } else if (target.is('.new')) { if (month == 11) { month = 0; year += 1; } else { month += 1; } } this.viewdate.setutcdate(day); this.viewdate.setutcmonth(month); this.viewdate.setutcfullyear(year); this.element.trigger({ type: 'changeday', date: this.viewdate }); if (this.viewselect >= 2) { this._setdate(utcdate(year, month, day, hours, minutes, seconds, 0)); } } var oldviewmode = this.viewmode; this.showmode(-1); this.fill(); if (oldviewmode == this.viewmode && this.autoclose) { this.hide(); } break; } } }, _setdate: function(date, which){ if (!which || which == 'date') this.date = date; if (!which || which == 'view') this.viewdate = date; this.fill(); this.setvalue(); var element; if (this.isinput) { element = this.element; } else if (this.component){ element = this.element.find('input'); } if (element) { element.change(); if (this.autoclose && (!which || which == 'date')) { //this.hide(); } } this.element.trigger({ type: 'changedate', date: this.date }); }, moveminute: function(date, dir){ if (!dir) return date; var new_date = new date(date.valueof()); //dir = dir > 0 ? 1 : -1; new_date.setutcminutes(new_date.getutcminutes() + (dir * this.minutestep)); return new_date; }, movehour: function(date, dir){ if (!dir) return date; var new_date = new date(date.valueof()); //dir = dir > 0 ? 1 : -1; new_date.setutchours(new_date.getutchours() + dir); return new_date; }, movedate: function(date, dir){ if (!dir) return date; var new_date = new date(date.valueof()); //dir = dir > 0 ? 1 : -1; new_date.setutcdate(new_date.getutcdate() + dir); return new_date; }, movemonth: function(date, dir){ if (!dir) return date; var new_date = new date(date.valueof()), day = new_date.getutcdate(), month = new_date.getutcmonth(), mag = math.abs(dir), new_month, test; dir = dir > 0 ? 1 : -1; if (mag == 1){ test = dir == -1 // if going back one month, make sure month is not current month // (eg, mar 31 -> feb 31 == feb 28, not mar 02) ? function(){ return new_date.getutcmonth() == month; } // if going forward one month, make sure month is as expected // (eg, jan 31 -> feb 31 == feb 28, not mar 02) : function(){ return new_date.getutcmonth() != new_month; }; new_month = month + dir; new_date.setutcmonth(new_month); // dec -> jan (12) or jan -> dec (-1) -- limit expected date to 0-11 if (new_month < 0 || new_month > 11) new_month = (new_month + 12) % 12; } else { // for magnitudes >1, move one month at a time... for (var i=0; i= this.startdate && date <= this.enddate; }, keydown: function(e){ if (this.picker.is(':not(:visible)')){ if (e.keycode == 27) // allow escape to hide and re-show picker this.show(); return; } var datechanged = false, dir, day, month, newdate, newviewdate; switch(e.keycode){ case 27: // escape this.hide(); e.preventdefault(); break; case 37: // left case 39: // right if (!this.keyboardnavigation) break; dir = e.keycode == 37 ? -1 : 1; viewmode = this.viewmode; if (e.ctrlkey) { viewmode += 2; } else if (e.shiftkey) { viewmode += 1; } if (viewmode == 4) { newdate = this.moveyear(this.date, dir); newviewdate = this.moveyear(this.viewdate, dir); } else if (viewmode == 3) { newdate = this.movemonth(this.date, dir); newviewdate = this.movemonth(this.viewdate, dir); } else if (viewmode == 2) { newdate = this.movedate(this.date, dir); newviewdate = this.movedate(this.viewdate, dir); } else if (viewmode == 1) { newdate = this.movehour(this.date, dir); newviewdate = this.movehour(this.viewdate, dir); } else if (viewmode == 0) { newdate = this.moveminute(this.date, dir); newviewdate = this.moveminute(this.viewdate, dir); } if (this.datewithinrange(newdate)){ this.date = newdate; this.viewdate = newviewdate; this.setvalue(); this.update(); e.preventdefault(); datechanged = true; } break; case 38: // up case 40: // down if (!this.keyboardnavigation) break; dir = e.keycode == 38 ? -1 : 1; viewmode = this.viewmode; if (e.ctrlkey) { viewmode += 2; } else if (e.shiftkey) { viewmode += 1; } if (viewmode == 4) { newdate = this.moveyear(this.date, dir); newviewdate = this.moveyear(this.viewdate, dir); } else if (viewmode == 3) { newdate = this.movemonth(this.date, dir); newviewdate = this.movemonth(this.viewdate, dir); } else if (viewmode == 2) { newdate = this.movedate(this.date, dir * 7); newviewdate = this.movedate(this.viewdate, dir * 7); } else if (viewmode == 1) { if (this.showmeridian) { newdate = this.movehour(this.date, dir * 6); newviewdate = this.movehour(this.viewdate, dir * 6); } else { newdate = this.movehour(this.date, dir * 4); newviewdate = this.movehour(this.viewdate, dir * 4); } } else if (viewmode == 0) { newdate = this.moveminute(this.date, dir * 4); newviewdate = this.moveminute(this.viewdate, dir * 4); } if (this.datewithinrange(newdate)){ this.date = newdate; this.viewdate = newviewdate; this.setvalue(); this.update(); e.preventdefault(); datechanged = true; } break; case 13: // enter if (this.viewmode != 0) { var oldviewmode = this.viewmode; this.showmode(-1); this.fill(); if (oldviewmode == this.viewmode && this.autoclose) { this.hide(); } } else { this.fill(); if (this.autoclose) { this.hide(); } } e.preventdefault(); break; case 9: // tab this.hide(); break; } if (datechanged){ var element; if (this.isinput) { element = this.element; } else if (this.component){ element = this.element.find('input'); } if (element) { element.change(); } this.element.trigger({ type: 'changedate', date: this.date }); } }, showmode: function(dir) { if (dir) { var newviewmode = math.max(0, math.min(dpglobal.modes.length - 1, this.viewmode + dir)); if (newviewmode >= this.minview && newviewmode <= this.maxview) { this.element.trigger({ type: 'changemode', date: this.viewdate, oldviewmode: this.viewmode, newviewmode: newviewmode }); this.viewmode = newviewmode; } } /* vitalets: fixing bug of very special conditions: jquery 1.7.1 + webkit + show inline datetimepicker in bootstrap popover. method show() does not set display css correctly and datetimepicker is not shown. changed to .css('display', 'block') solve the problem. see https://github.com/vitalets/x-editable/issues/37 in jquery 1.7.2+ everything works fine. */ //this.picker.find('>div').hide().filter('.datetimepicker-'+dpglobal.modes[this.viewmode].clsname).show(); this.picker.find('>div').hide().filter('.datetimepicker-'+dpglobal.modes[this.viewmode].clsname).css('display', 'block'); this.updatenavarrows(); }, reset: function(e) { this._setdate(null, 'date'); } }; $.fn.datetimepicker = function ( option ) { var args = array.apply(null, arguments); args.shift(); return this.each(function () { var $this = $(this), data = $this.data('datetimepicker'), options = typeof option == 'object' && option; if (!data) { $this.data('datetimepicker', (data = new datetimepicker(this, $.extend({}, $.fn.datetimepicker.defaults,options)))); } if (typeof option == 'string' && typeof data[option] == 'function') { data[option].apply(data, args); } }); }; $.fn.datetimepicker.defaults = { }; $.fn.datetimepicker.constructor = datetimepicker; var dates = $.fn.datetimepicker.dates = { en: { days: ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"], daysshort: ["sun", "mon", "tue", "wed", "thu", "fri", "sat", "sun"], daysmin: ["su", "mo", "tu", "we", "th", "fr", "sa", "su"], months: ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"], monthsshort: ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"], meridiem: ["am", "pm"], suffix: ["st", "nd", "rd", "th"], today: "today" } }; var dpglobal = { modes: [ { clsname: 'minutes', navfnc: 'hours', navstep: 1 }, { clsname: 'hours', navfnc: 'date', navstep: 1 }, { clsname: 'days', navfnc: 'month', navstep: 1 }, { clsname: 'months', navfnc: 'fullyear', navstep: 1 }, { clsname: 'years', navfnc: 'fullyear', navstep: 10 }], isleapyear: function (year) { return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) }, getdaysinmonth: function (year, month) { return [31, (dpglobal.isleapyear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] }, getdefaultformat: function (type, field) { if (type == "standard") { if (field == 'input') return 'yyyy-mm-dd hh:ii'; else return 'yyyy-mm-dd hh:ii:ss'; } else if (type == "php") { if (field == 'input') return 'y-m-d h:i'; else return 'y-m-d h:i:s'; } else { throw new error("invalid format type."); } }, validparts: function (type) { if (type == "standard") { return /hh?|hh?|p|p|ii?|ss?|dd?|dd?|mm?|mm?|yy(?:yy)?/g; } else if (type == "php") { return /[ddjlnwzfmmnstyyaabgghhis]/g; } else { throw new error("invalid format type."); } }, nonpunctuation: /[^ -\/:-@\[-`{-~\t\n\rtz]+/g, parseformat: function(format, type){ // ie treats \0 as a string end in inputs (truncating the value), // so it's a bad format delimiter, anyway var separators = format.replace(this.validparts(type), '\0').split('\0'), parts = format.match(this.validparts(type)); if (!separators || !separators.length || !parts || parts.length == 0){ throw new error("invalid date format."); } return {separators: separators, parts: parts}; }, parsedate: function(date, format, language, type) { if (date instanceof date) { var dateutc = new date(date.valueof() - date.gettimezoneoffset() * 60000); dateutc.setmilliseconds(0); return dateutc; } if (/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(date)) { format = this.parseformat('yyyy-mm-dd', type); } if (/^\d{4}\-\d{1,2}\-\d{1,2}[t ]\d{1,2}\:\d{1,2}$/.test(date)) { format = this.parseformat('yyyy-mm-dd hh:ii', type); } if (/^\d{4}\-\d{1,2}\-\d{1,2}[t ]\d{1,2}\:\d{1,2}\:\d{1,2}[z]{0,1}$/.test(date)) { format = this.parseformat('yyyy-mm-dd hh:ii:ss', type); } if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) { var part_re = /([-+]\d+)([dmwy])/, parts = date.match(/([-+]\d+)([dmwy])/g), part, dir; date = new date(); for (var i=0; i 6 n: (date.getutcday()==0?7:date.getutcday()), // 1 -> 7 s: (date.getutcdate()%10<=dates[language].suffix.length?dates[language].suffix[date.getutcdate()%10-1]:''), // hour a: (dates[language].meridiem.length==2?dates[language].meridiem[date.getutchours()<12?0:1]:''), g: (date.getutchours()%12==0?12:date.getutchours()%12), g: date.getutchours(), // minute i: date.getutcminutes(), // second s: date.getutcseconds() }; val.m = (val.n < 10 ? '0' : '') + val.n; val.d = (val.j < 10 ? '0' : '') + val.j; val.a = val.a.tostring().touppercase(); val.h = (val.g < 10 ? '0' : '') + val.g; val.h = (val.g < 10 ? '0' : '') + val.g; val.i = (val.i < 10 ? '0' : '') + val.i; val.s = (val.s < 10 ? '0' : '') + val.s; } else { throw new error("invalid format type."); } var date = [], seps = $.extend([], format.separators); for (var i=0, cnt = format.parts.length; i < cnt; i++) { if (seps.length) date.push(seps.shift()) date.push(val[format.parts[i]]); } return date.join(''); }, convertviewmode: function(viewmode){ switch (viewmode) { case 4: case 'decade': viewmode = 4; break; case 3: case 'year': viewmode = 3; break; case 2: case 'month': viewmode = 2; break; case 1: case 'day': viewmode = 1; break; case 0: case 'hour': viewmode = 0; break; } return viewmode; }, headtemplate: ''+ ''+ ''+ ''+ ''+ ''+ '', conttemplate: '', foottemplate: '' }; dpglobal.template = '
'+ '
'+ ''+ dpglobal.headtemplate+ dpglobal.conttemplate+ dpglobal.foottemplate+ '
'+ '
'+ '
'+ ''+ dpglobal.headtemplate+ dpglobal.conttemplate+ dpglobal.foottemplate+ '
'+ '
'+ '
'+ ''+ dpglobal.headtemplate+ ''+ dpglobal.foottemplate+ '
'+ '
'+ '
'+ ''+ dpglobal.headtemplate+ dpglobal.conttemplate+ dpglobal.foottemplate+ '
'+ '
'+ '
'+ ''+ dpglobal.headtemplate+ dpglobal.conttemplate+ dpglobal.foottemplate+ '
'+ '
'+ '
'; $.fn.datetimepicker.dpglobal = dpglobal; }( window.jquery );