UNPKG

@tarojs/components

Version:
1 lines 3.53 kB
import{r as registerInstance,c as createEvent,h,H as Host,g as getElement}from"./index-5bd7cbab.js";var Slider=function(){function e(e){var t=this;registerInstance(this,e);this.onChange=createEvent(this,"change",7);this.onChanging=createEvent(this,"changing",7);this.min=0;this.max=100;this.step=1;this.disabled=false;this.value=0;this.activeColor="#1aad19";this.backgroundColor="#e9e9e9";this.blockSize=28;this.blockColor="#ffffff";this.showValue=false;this.name="";this.totalWidth=0;this.touching=false;this.ogX=0;this.touchId=null;this.percent=0;this.isWillLoadCalled=false;this.handleTouchStart=function(e){if(t.touching||t.disabled)return;t.touching=true;t.touchId=e.targetTouches[0].identifier;t.totalWidth=t.sliderInsRef.clientWidth;t.ogX=e.targetTouches[0].pageX;t.ogPercent=t.percent};this.handleTouchMove=function(e){var i=t,a=i.disabled,n=i.touching,r=i.touchId,o=i.totalWidth,l=i.max,h=i.min,s=i.ogX,u=i.ogPercent;if(!n||a)return;if(e.targetTouches[0].identifier!==r)return;e.preventDefault();var c=e.targetTouches[0].pageX;var d=c-s;var f=d/o*100+u;f=Math.max(0,Math.min(f,100));var v=h+f*.01*(l-h);t.updateByStep(v);t.onChanging.emit({detail:e.detail,value:t.val})};this.handleTouchEnd=function(e){var i=t,a=i.disabled,n=i.touching;if(!n||a)return;if(t.percent!==t.ogPercent){t.onChange.emit({detail:e.detail,value:t.val})}t.touching=false;t.touchId=null;t.ogX=0;t.ogPercent=0}}e.prototype.function=function(e){if(!this.isWillLoadCalled)return;var t=this,i=t.max,a=t.min;if(e!==null&&e!==this.val){var n=Math.max(a,Math.min(e,i));this.updateByStep(n)}};e.prototype.componentDidLoad=function(){var e=this;Object.defineProperty(this.el,"value",{get:function(){return e.val},set:function(t){return e.value=t},configurable:true});this.handler.addEventListener("touchstart",this.handleTouchStart);this.handler.addEventListener("touchmove",this.handleTouchMove);this.handler.addEventListener("touchend",this.handleTouchEnd)};e.prototype.componentDidUpdate=function(){this.value=null};e.prototype.componentWillLoad=function(){this.isWillLoadCalled=true;var e=this,t=e.value,i=e.max,a=e.min;if(t===null)return;var n=Math.max(a,Math.min(t,i));this.updateByStep(n)};e.prototype.updateByStep=function(e){var t=this,i=t.max,a=t.min,n=t.step;var r=Math.floor((i-a)/n);for(var o=0;o<=r;o++){var l=a+n*o;var h=o===r?null:a+n*(o+1);if(e===l)break;if(!h&&e>l){e=l}if(h&&e>l&&e<h){if(e-l<n/2){e=l}else{e=h}break}}var s=(e-a)/(i-a)*100;this.val=e;this.percent=s};e.prototype.render=function(){var e=this;var t=this,i=t.showValue,a=t.backgroundColor,n=t.activeColor,r=t.blockColor,o=t.name,l=t.percent,s=t.val;var u=this.blockSize;var c={backgroundColor:a};var d=l>100?100:l;var f={width:d+"%",backgroundColor:n};if(u<12){u=12}if(u>28){u=28}var v={left:d+"%",width:u+"px",height:u+"px",backgroundColor:r,marginTop:"-"+Math.floor(u/2)+"px",marginLeft:"-"+Math.floor(u/2)+"px"};return h(Host,{class:"weui-slider-box"},h("div",{class:"weui-slider"},h("div",{class:"weui-slider__inner",style:c,ref:function(t){return e.sliderInsRef=t}},h("div",{style:f,class:"weui-slider__track"}),h("div",{class:"weui-slider__handler",ref:function(t){if(t)e.handler=t},style:v}),h("input",{type:"hidden",name:o,value:s}))),i&&h("div",{class:"weui-slider-box__value"},s))};Object.defineProperty(e.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});Object.defineProperty(e,"watchers",{get:function(){return{value:["function"]}},enumerable:false,configurable:true});return e}();export{Slider as taro_slider_core};