/*! * bootstrap-material-datetimepicker.js * * Copyright (c) 2015 Romain America * * Released under the MIT license * https://github.com/T00rk/bootstrap-material-datetimepicker/blob/gh-pages/LICENSE.md */ (function ($, moment) { var pluginName = "bootstrapMaterialDatePicker"; var pluginDataName = "plugin_" + pluginName; moment.locale('en'); function Plugin(element, options) { this.currentView = 0; this.minDate; this.maxDate; this._attachedEvents = []; this.element = element; this.$element = $(element); this.params = { date : true, time : true, format : 'YYYY-MM-DD', minDate : null, maxDate : null, currentDate : null, lang : 'en', weekStart : 0, shortTime : false, 'cancelText' : 'Cancel', 'okText' : 'OK','animation' : false}; this.params = $.fn.extend(this.params, options); this.name = "dtp_" + this.setName(); this.$element.attr("data-dtp", this.name); this.init(); } $.fn[pluginName] = function(options, p) { this.each(function() { if(!$.data(this, pluginDataName)) { $.data(this, pluginDataName, new Plugin(this, options)); } else { if(typeof($.data(this, pluginDataName)[options]) === 'function') { $.data(this, pluginDataName)[options](p); } if(options === 'destroy') { delete $.data(this, pluginDataName); } } }); return this; }; Plugin.prototype = { init: function() { this.initDays(); this.initDates(); this.initTemplate(); this.initButtons(); this._attachEvent($(window), 'resize', this._centerBox(this)); this._attachEvent(this.$dtpElement.find('.dtp-content'), 'click', this._onElementClick.bind(this)); this._attachEvent(this.$dtpElement, 'click', this._onBackgroundClick.bind(this)); this._attachEvent(this.$dtpElement.find('.dtp-close > a'), 'click', this._onCloseClick.bind(this)); this._attachEvent(this.$element, 'focus', this._onFocus.bind(this)); }, initDays: function() { this.days = []; for(var i = this.params.weekStart; this.days.length < 7; i++) { if(i > 6) { i = 0; } this.days.push(i.toString()); } }, initDates: function() { if(this.$element.val().length > 0) { if(typeof(this.params.format) !== 'undefined' && this.params.format !== null) { this.currentDate = moment(this.$element.val(), this.params.format).locale(this.params.lang); } else { this.currentDate = moment(this.$element.val()).locale(this.params.lang); } } else { if(typeof(this.$element.attr('value')) !== 'undefined' && this.$element.attr('value') !== null && this.$element.attr('value') !== "") { if(typeof(this.$element.attr('value')) === 'string') { if(typeof(this.params.format) !== 'undefined' && this.params.format !== null) { this.currentDate = moment(this.$element.attr('value'), this.params.format).locale(this.params.lang); } else { this.currentDate = moment(this.$element.attr('value')).locale(this.params.lang); } } } else { if(typeof(this.params.currentDate) !== 'undefined' && this.params.currentDate !== null) { if(typeof(this.params.currentDate) === 'string') { if(typeof(this.params.format) !== 'undefined' && this.params.format !== null) { this.currentDate = moment(this.params.currentDate, this.params.format).locale(this.params.lang); } else { this.currentDate = moment(this.params.currentDate).locale(this.params.lang); } } else { if(typeof(this.params.currentDate.isValid) === 'undefined' || typeof(this.params.currentDate.isValid) !== 'function') { var x = this.params.currentDate.getTime(); this.currentDate = moment(x, "x").locale(this.params.lang); } else { this.currentDate = this.params.currentDate; } } this.$element.val(this.currentDate.format(this.params.format)); } else this.currentDate = moment(); } } if(typeof(this.params.minDate) !== 'undefined' && this.params.minDate !== null) { if(typeof(this.params.minDate) === 'string') { if(typeof(this.params.format) !== 'undefined' && this.params.format !== null) { this.minDate = moment(this.params.minDate, this.params.format).locale(this.params.lang); } else { this.minDate = moment(this.params.minDate).locale(this.params.lang); } } else { if(typeof(this.params.minDate.isValid) === 'undefined' || typeof(this.params.minDate.isValid) !== 'function') { var x = this.params.minDate.getTime(); this.minDate = moment(x, "x").locale(this.params.lang); } else { this.minDate = this.params.minDate; } } } if(typeof(this.params.maxDate) !== 'undefined' && this.params.maxDate !== null) { if(typeof(this.params.maxDate) === 'string') { if(typeof(this.params.format) !== 'undefined' && this.params.format !== null) { this.maxDate = moment(this.params.maxDate, this.params.format).locale(this.params.lang); } else { this.maxDate = moment(this.params.maxDate).locale(this.params.lang); } } else { if(typeof(this.params.maxDate.isValid) === 'undefined' || typeof(this.params.maxDate.isValid) !== 'function') { var x = this.params.maxDate.getTime(); this.maxDate = moment(x, "x").locale(this.params.lang); } else { this.maxDate = this.params.maxDate; } } } if(!this.isAfterMinDate(this.currentDate)) { this.currentDate = moment(this.minDate); } if(!this.isBeforeMaxDate(this.currentDate)) { this.currentDate = moment(this.maxDate); } }, initTemplate: function() { if(this.params.animation===false) { this.dtpButtons = '
'; }else{this.dtpButtons = ' ';} this.template = ''; if($('body').find("#" + this.name).length <= 0) { $('body').append(this.template); this.dtpElement = $('body').find("#" + this.name); this.$dtpElement = $(this.dtpElement); } }, initButtons: function() { this._attachEvent(this.$dtpElement.find('.dtp-btn-cancel'), 'click', this._onCancelClick.bind(this)); this._attachEvent(this.$dtpElement.find('.dtp-btn-ok'), 'click', this._onOKClick.bind(this)); this._attachEvent(this.$dtpElement.find('a.dtp-select-month-before'), 'click', this._onMonthBeforeClick.bind(this)); this._attachEvent(this.$dtpElement.find('a.dtp-select-month-after'), 'click', this._onMonthAfterClick.bind(this)); this._attachEvent(this.$dtpElement.find('a.dtp-select-year-before'), 'click', this._onYearBeforeClick.bind(this)); this._attachEvent(this.$dtpElement.find('a.dtp-select-year-after'), 'click', this._onYearAfterClick.bind(this)); }, initMeridienButtons: function() { this.$dtpElement.find('a.dtp-meridien-am').off('click').on('click', this._onSelectAM.bind(this)); this.$dtpElement.find('a.dtp-meridien-pm').off('click').on('click', this._onSelectPM.bind(this)); }, initDate: function(d) { this.currentView = 0; this.$dtpElement.find('.dtp-picker-calendar').removeClass('hidden').addClass("animated fadeIn"); this.$dtpElement.find('.dtp-picker-datetime').addClass('hidden animated fadeIn'); var _date = ((typeof(this.currentDate) !== 'undefined' && this.currentDate !== null) ? this.currentDate : null); var _calendar = this.generateCalendar(this.currentDate); if(typeof(_calendar.week) !== 'undefined' && typeof(_calendar.days) !== 'undefined') { var _template = this.constructHTMLCalendar(_date, _calendar); this.$dtpElement.find('a.dtp-select-day').off('click'); this.$dtpElement.find('.dtp-picker-calendar').html(_template); this.$dtpElement.find('a.dtp-select-day').on('click', this._onSelectDate.bind(this)); this.toggleButtons(_date); } this._centerBox(); this.showDate(_date); }, initHours: function() { this.currentView = 1; if(!this.params.date) { var w = this.$dtpElement.find('.dtp-content').width(); var ml = this.$dtpElement.find('.dtp-picker-clock').css('marginLeft').replace('px', ''); var mr = this.$dtpElement.find('.dtp-picker-clock').css('marginRight').replace('px', ''); var pl = this.$dtpElement.find('.dtp-picker').css('paddingLeft').replace('px', ''); var pr = this.$dtpElement.find('.dtp-picker').css('paddingRight').replace('px', ''); this.$dtpElement.find('.dtp-picker-clock').innerWidth(w - (parseInt(ml) + parseInt(mr) + parseInt(pl) + parseInt(pr))); } this.showTime(this.currentDate); this.initMeridienButtons(); this.$dtpElement.find('.dtp-picker-datetime').removeClass('hidden'); this.$dtpElement.find('.dtp-picker-calendar').addClass('hidden'); if(this.currentDate.hour() < 12) { this.$dtpElement.find('a.dtp-meridien-am').click(); } else { this.$dtpElement.find('a.dtp-meridien-pm').click(); } var pL = this.$dtpElement.find('.dtp-picker-clock').parent().parent().css('paddingLeft').replace('px', ''); var pT = this.$dtpElement.find('.dtp-picker-clock').parent().parent().css('paddingTop').replace('px', ''); var mL = this.$dtpElement.find('.dtp-picker-clock').css('marginLeft').replace('px', ''); var mT = this.$dtpElement.find('.dtp-picker-clock').css('marginTop').replace('px', ''); var r = (this.$dtpElement.find('.dtp-picker-clock').innerWidth() / 2); var j = r / 1.2; var hours = []; for(var h = 0; h < 12; ++h) { var x = j * Math.sin(Math.PI * 2 * (h / 12)); var y = j * Math.cos(Math.PI * 2 * (h / 12)); var hour = $('| ' + moment(parseInt(calendar.week[i]), "d").locale(this.params.lang).format("dd").substring(0, 1) + ' | '; } _template += ''; _template += '
|---|
| '; if(calendar.days[i] != 0) { if(this.isBeforeMaxDate(moment(calendar.days[i]), false, false) === false || this.isAfterMinDate(moment(calendar.days[i]), false, false) === false) { _template += '' + moment(calendar.days[i]).locale(this.params.lang).format("DD") + ''; } else { if(moment(calendar.days[i]).locale(this.params.lang).format("DD") === moment(this.currentDate).locale(this.params.lang).format("DD")) { _template += '' + moment(calendar.days[i]).locale(this.params.lang).format("DD") + ''; } else { _template += '' + moment(calendar.days[i]).locale(this.params.lang).format("DD") + ''; } } _template += ' | '; } } _template += '