-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate-picker.min.js
2 lines (2 loc) · 7.71 KB
/
date-picker.min.js
1
2
define("model",[],function(){return Backbone.Model.extend({initialize:function(attr){this.date=attr.val?moment(attr.val):moment().startOf("year"),this.set("original_val",attr.val),this.updateAttrs()},dateValue:function(){return this.date.format("YYYY-MM-DD")},updateAttrs:function(){var val=this.get("val");this.set({val:val?this.dateValue():"",year:val?this.date.year():"",month:val?this.date.month():"",date:val?this.date.date():""})},changeDate:function(key,val){"date"==key&&(key="day"),this.date.add(val,key),this.save()},setDate:function(key,val){"month"==key&&_.isString(val)&&(val=parseInt(val)-1),this.date.set(key,val),this.save()},save:function(){this.set("val",this.dateValue()),this.updateAttrs()},reset:function(){this.set("val",this.get("original_val")),this.get("val")&&(this.date=moment(this.get("val"))),this.updateAttrs()}})}),define("unit",[],function(){return Backbone.View.extend({className:"unit",events:{mousewheel:"onMouseWheel",mouseenter:"activate",mouseleave:"deactivate",click:"activate"},val:function(){return this.model.get(this.key)},format:function(val){return""===val?"__":val},validate:function(val){return!1},initialize:function(opts){this.el.classList.add("unit-"+this.key),this.options.divider&&(this.el.dataset.divider=this.options.divider),this._onType=this.onType.bind(this),this.delta=0,this.typedVal="",this.listenTo(this.model,"change:"+this.key,this.render)},render:function(){return this.typedVal?this.$el.html(this.typedVal):this.$el.html(this.format(this.val())),this},onMouseWheel:function(e){clearTimeout(this.onMouseWheelTimeout),e.preventDefault(),e.stopPropagation(),e=e.originalEvent;var delta=e.wheelDelta;return(this.delta<0&&delta>0||this.delta>0&&0>delta)&&(this.delta=0),this.delta+=delta,Math.abs(this.delta)>=this.options.deltaThreshold&&(this.delta<0?this.decrease():this.increase(),this.delta=0),this.onMouseWheelTimeout=setTimeout(function(){this.delta=0}.bind(this),100),!1},decrease:function(){this.model.changeDate(this.key,-1)},increase:function(){this.model.changeDate(this.key,1)},activate:function(){window.__activeDatePickerUnit&&window.__activeDatePickerUnit.deactivate(),window.__activeDatePickerUnit=this,this.el.classList.add("active"),window.addEventListener("keydown",this._onType)},deactivate:function(){window.__activeDatePickerUnit==this&&(window.__activeDatePickerUnit=null),this.el.classList.remove("active"),window.removeEventListener("keydown",this._onType),this.useTypedValue()},isActive:function(){return this.el.classList.contains("active")},onType:function(e){return 8==e.which?(e.preventDefault(),e.stopPropagation(),this.typedVal=this.typedVal.substr(0,this.typedVal.length-1),this.render(),!1):27==e.which?(e.preventDefault(),e.stopPropagation(),this.typedVal="",this.parentView.reset(),!1):9==e.which||37==e.which||39==e.which||13==e.which?(e.preventDefault(),e.stopPropagation(),13==e.which?this.deactivate():e.shiftKey||37==e.which?this.parentView.activatePrev():this.parentView.activateNext(),!1):void(e.which<48||e.which>57||(this.typedVal+=String.fromCharCode(e.which),this.render(),this.maxLength&&this.typedVal.length>=this.maxLength?this.useTypedValue():(!this.minLength||this.typedVal.length>=this.minLength)&&(this._onTypeTimeout=setTimeout(this.useTypedValue.bind(this),this.options.typeDelay))))},typedValueIsValid:function(){return this.validate(this.typedVal)},useTypedValue:function(){this.typedValueIsValid()&&(this.model.setDate(this.key,this.typedVal),this.parentView.activateNext(!1)),this.typedVal="",this.render()}})}),define("unit-year",["unit"],function(UnitView){return UnitView.extend({key:"year",minLength:4,maxLength:4,format:function(val){return""===val?this.options.placeholder:val},validate:function(val){return val&&4===val.length?!0:!1}})}),define("unit-month",["unit"],function(UnitView){return UnitView.extend({key:"month",maxLength:2,format:function(val){return _.isFunction(this.options.format)?this.options.format(val):""===val?this.options.placeholder:"long"==this.options.format?moment.months()[val]:"short"==this.options.format?moment.monthsShort()[val]:_.lpad(val+1,2,"0")},validate:function(val){return!val||val.length>2?!1:!0}})}),define("unit-date",["unit"],function(UnitView){return UnitView.extend({key:"date",maxLength:2,format:function(val){return _.isFunction(this.options.format)?this.options.format(val):""===val?this.options.placeholder:_.lpad(val,2,"0")},validate:function(val){return!val||val.length>2?!1:!0}})}),define("date-picker",["model","unit-year","unit-month","unit-date"],function(Model,YearView,MonthView,DateView){return Backbone.View.extend({className:"datepicker",events:{mouseenter:"onHover",mouseleave:"blur"},appendTo:function(el){_.isString(el)&&(el=document.querySelector(el)),this.render().$el.appendTo(el)},defaultOpts:{appendTo:null,val:"",format:"y-m-d",valFormat:"YYYY-MM-DD",className:"",deltaThreshold:70,typeDelay:340,navLink:!0,saveOnBlur:!0,onSave:null,onBlur:null,y:{placeholder:"yyyy"},m:{format:"",placeholder:"mm"},d:{format:"",placeholder:"dd"}},initialize:function(){window.moment&&(this.el.datepicker=this,this.options=_.extend({},this.defaultOpts,this.options),this.format=this.options.format.toLowerCase().split(""),this.unitViews=[],this.model=new Model({val:this.options.val}),this.options.className&&this.el.classList.add(this.options.className),this.options.appendTo&&this.appendTo(this.options.appendTo))},render:function(){return window.moment?(window.dp=this,0==this.unitViews.length&&this.format.forEach(this.renderUnit.bind(this)),this):(this.$el.html("missing moment.js"),this)},renderUnit:function(key){var View;switch(key){case"y":View=YearView;break;case"m":View=MonthView;break;case"d":View=DateView}if(!View)return void(this.divider=key);var opts=_.extend({divider:this.divider,deltaThreshold:this.options.deltaThreshold,typeDelay:this.options.typeDelay,model:this.model},this.defaultOpts[key],this.options[key]),view=new View(opts);view.parentView=this,this.$el.append(view.render().el),this.unitViews.push(view),this.divider=""},activeView:function(){var view;return this.unitViews.forEach(function(v){v.isActive()&&(view=v)}),view},_activate:function(jump,loop){var view=this.activeView(),indx=this.unitViews.indexOf(view);if(view?(view.deactivate(),indx+=jump):indx=0,indx>this.unitViews.length-1){if(loop===!1)return;if(this.options.navLink&&this.el.nextElementSibling&&this.el.nextElementSibling.datepicker)return this.blur(),void this.el.nextElementSibling.datepicker.focus();indx=0}else if(0>indx){if(loop===!1)return;if(this.options.navLink&&this.el.previousElementSibling&&this.el.previousElementSibling.datepicker)return this.blur(),void this.el.previousElementSibling.datepicker.focusLast();indx=this.unitViews.length-1}view=this.unitViews[indx],view&&view.activate()},activateNext:function(loop){this._activate(1,loop)},activatePrev:function(loop){this._activate(-1,loop)},focus:function(num){this.blur(),this.unitViews[num||0].activate()},focusLast:function(){this.focus(this.unitViews.length-1)},blur:function(){if(window.moment){var view=this.activeView();view&&view.deactivate(),this.el.classList.remove("editing"),this.model.get("val")!=this.options.val&&(this.model.get("val")===this.model.get("original_val")?this.el.classList.remove("unsaved"):this.el.classList.add("unsaved"),this.options.val=this.model.dateValue(),this.options.saveOnBlur&&this.save(),this.options.onSave&&this.options.onSave(this,this.value())),this.options.onBlur&&this.options.onBlur(this)}},value:function(){return this.model.date.format(this.options.valFormat)},reset:function(){this.model.reset(),this.model.get("val")===this.model.get("original_val")&&this.el.classList.remove("unsaved")},save:function(){this.model.set("original_val",this.model.get("val")),this.el.classList.remove("unsaved")},onHover:function(){this.el.classList.add("editing")}})});
//# sourceMappingURL=date-picker.min.js.map