wmdeit_kaform/addons/slider/kube.slider.min.js

1 line
3.4 KiB
JavaScript
Raw Permalink Normal View History

2020-02-20 13:45:47 +00:00
!function(t){t.add("module","slider",{init:function(t,i){this.app=t,this.$win=t.$win,this.$doc=t.$doc;var s={min:0,max:100,step:1,value:0,target:!1};this.context=i,this.params=i.getParams(s),this.$element=i.getElement(),this.$target=i.getTarget(),this.isTicks=!1},start:function(){this._buildTrack(),this._buildFill(),this._buildHandle(),this._buildTicks(),this.update(),this.$win.on("resize.kube.slider",this._resize.bind(this)),this.$element.on("mousedown.kube.slider touchstart.kube.slider",this._handleDown.bind(this))},stop:function(){this.$win.off(".kube.slider"),this.$doc.off(".kube.slider"),this.$element.off(".kube.slider")},update:function(t){this.value=t?t:this.params.value,this.value=this.value<this.params.min?this.params.min:this.value,this.handleWidth=this.$handle.width(),this.trackWidth=this.$track.width(),this.maxHandlePosition=this.trackWidth-this.handleWidth,this.fixPosition=this.handleWidth/2,this.position=this._getPositionFromValue(this.value),this._setPosition(this.position),this._setTarget()},_resize:function(){this._buildTicks(),this.update(this.value)},_isDisabled:function(){return this.$element.hasClass("is-disabled")||this.$element.attr("disabled")},_buildTrack:function(){this.$track=t.dom("<div />"),this.$track.addClass("slider-track"),this.$element.prepend(this.$track)},_buildFill:function(){this.$fill=t.dom("<div />"),this.$fill.addClass("slider-fill"),this.$track.append(this.$fill)},_buildHandle:function(){this.$handle=t.dom("<div />"),this.$handle.addClass("slider-handle"),this.$track.append(this.$handle)},_buildTicks:function(){this.$ticks=this.$element.find(".slider-ticks span");var i=this.$ticks.length;if(this.isTicks=0!==i,this.isTicks){var s=this.$handle.width(),e=this.$element.width()-s,h=s/2,a=e/(i-1),n=h;this.$ticks.each(function(i,s){var e=t.dom(i),l=n+a*s;e.css({left:l+"px",width:a+"px","text-indent":"-"+(a-h)+"px"})})}},_handleDown:function(t){if(t.preventDefault(),!this._isDisabled()){this.$doc.on("mousemove.kube.slider touchmove.kube.slider",this._handleMove.bind(this)),this.$doc.on("mouseup.kube.slider touchend.kube.slider",this._handleEnd.bind(this));var i=t.touches&&t.touches.length>0?t.changedTouches[0].clientX:t.clientX,s=this.$track.offset().left,e=i-s-this.fixPosition;this._setPosition(e),this._setTarget()}},_handleMove:function(t){t.preventDefault();var i=t.touches&&t.touches.length>0?t.changedTouches[0].clientX:t.clientX,s=this.$track.offset().left,e=i-s-this.fixPosition;this._setPosition(e),this._setTarget()},_handleEnd:function(t){t.preventDefault(),this.$doc.off(".kube.slider")},_setPosition:function(t){t=this._getEdge(t,0,this.maxHandlePosition);var i=this._getValueFromPosition(t),s=this._getPositionFromValue(i);this.$fill.css("width",s+this.fixPosition+"px"),this.$handle.css("left",s+"px"),this.position=s,this.value=i},_setTarget:function(){if(this.app.broadcast("slider.set",this,this.value),0!==this.$target.length){var t=this.$target.get().tagName;"INPUT"===t||"SELECT"===t?this.$target.val(this.value):this.$target.text(this.value)}},_getPositionFromValue:function(t){var i=(t-this.params.min)/(this.params.max-this.params.min);return pos=Number.isNaN(i)?0:i*this.maxHandlePosition},_getValueFromPosition:function(t){var i=t/(this.maxHandlePosition||1),s=this.params.step*Math.round(i*(this.params.max-this.params.min)/this.params.step)+this.params.min;return Number(s.toFixed((this.params.step+"").replace(".","").length-1))},_getEdge:function(t,i,s){return i>t?i:t>s?s:t}})}(Kube);