2 lines
8.8 KiB
JavaScript
2 lines
8.8 KiB
JavaScript
import e,{Component as t}from"react";import{DomHandler as n,classNames as r}from"primereact/utils";function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function s(e){if(Array.isArray(e))return i(e)}function o(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function a(e,t){if(e){if("string"==typeof e)return i(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(e,t):void 0}}function l(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function h(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function p(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function d(e,t){return d=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},d(e,t)}function c(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&d(e,t)}function f(e){return f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},f(e)}function g(e,t){if(t&&("object"===f(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return p(e)}function m(e){return m=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},m(e)}function v(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var y,b,L,E=function(i){c(w,t);var d,f,y,b,L,E=(d=w,f=v(),function(){var e,t=m(d);if(f){var n=m(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return g(this,e)});function w(e){var t;return u(this,w),(t=E.call(this,e)).onBarClick=t.onBarClick.bind(p(t)),t.onKeyDown=t.onKeyDown.bind(p(t)),t.handleIndex=0,t}return y=w,(b=[{key:"value",get:function(){return this.props.range?this.props.value||[0,100]:this.props.value||0}},{key:"spin",value:function(e,t){this.updateValue(e,(this.props.range?this.value[this.handleIndex]:this.value)+(this.props.step||1)*t),e.preventDefault()}},{key:"onDragStart",value:function(e,t){this.props.disabled||(this.dragging=!0,this.updateDomData(),this.sliderHandleClick=!0,this.handleIndex=t)}},{key:"onMouseDown",value:function(e,t){this.bindDragListeners(),this.onDragStart(e,t)}},{key:"onTouchStart",value:function(e,t){this.bindTouchListeners(),this.onDragStart(e,t)}},{key:"onKeyDown",value:function(e,t){if(!this.props.disabled){this.handleIndex=t;var n=e.key;"ArrowRight"===n||"ArrowUp"===n?this.spin(e,1):"ArrowLeft"!==n&&"ArrowDown"!==n||this.spin(e,-1)}}},{key:"onBarClick",value:function(e){if(!this.props.disabled){if(!this.sliderHandleClick){this.updateDomData();var t=this.setValue(e);this.props.onSlideEnd&&this.props.onSlideEnd({originalEvent:e,value:t})}this.sliderHandleClick=!1}}},{key:"onDrag",value:function(e){this.dragging&&(this.setValue(e),e.preventDefault())}},{key:"onDragEnd",value:function(e){this.dragging&&(this.dragging=!1,this.props.onSlideEnd&&this.props.onSlideEnd({originalEvent:e,value:this.props.value}),this.unbindDragListeners(),this.unbindTouchListeners())}},{key:"bindDragListeners",value:function(){this.dragListener||(this.dragListener=this.onDrag.bind(this),document.addEventListener("mousemove",this.dragListener)),this.dragEndListener||(this.dragEndListener=this.onDragEnd.bind(this),document.addEventListener("mouseup",this.dragEndListener))}},{key:"unbindDragListeners",value:function(){this.dragListener&&(document.removeEventListener("mousemove",this.dragListener),this.dragListener=null),this.dragEndListener&&(document.removeEventListener("mouseup",this.dragEndListener),this.dragEndListener=null)}},{key:"bindTouchListeners",value:function(){this.dragListener||(this.dragListener=this.onDrag.bind(this),document.addEventListener("touchmove",this.dragListener)),this.dragEndListener||(this.dragEndListener=this.onDragEnd.bind(this),document.addEventListener("touchend",this.dragEndListener))}},{key:"unbindTouchListeners",value:function(){this.dragListener&&(document.removeEventListener("touchmove",this.dragListener),this.dragListener=null),this.dragEndListener&&(document.removeEventListener("touchend",this.dragEndListener),this.dragEndListener=null)}},{key:"updateDomData",value:function(){var e=this.el.getBoundingClientRect();this.initX=e.left+n.getWindowScrollLeft(),this.initY=e.top+n.getWindowScrollTop(),this.barWidth=this.el.offsetWidth,this.barHeight=this.el.offsetHeight}},{key:"setValue",value:function(e){var t=("horizontal"===this.props.orientation?100*((e.touches?e.touches[0].pageX:e.pageX)-this.initX)/this.barWidth:100*(this.initY+this.barHeight-(e.touches?e.touches[0].pageY:e.pageY))/this.barHeight)/100*(this.props.max-this.props.min)+this.props.min;if(this.props.step){var n=this.props.range?this.value[this.handleIndex]:this.value,r=t-n;r<0?t=n+Math.ceil(t/this.props.step-n/this.props.step)*this.props.step:r>0&&(t=n+Math.floor(t/this.props.step-n/this.props.step)*this.props.step)}else t=Math.floor(t);return this.updateValue(e,t)}},{key:"updateValue",value:function(e,t){var n,r=parseFloat(t.toFixed(10)),i=r;return this.props.range?(0===this.handleIndex?r<this.props.min?r=this.props.min:r>this.value[1]&&(r=this.value[1]):r>this.props.max?r=this.props.max:r<this.value[0]&&(r=this.value[0]),(i=s(n=this.value)||o(n)||a(n)||l())[this.handleIndex]=r,this.props.onChange&&this.props.onChange({originalEvent:e,value:i})):(r<this.props.min?r=this.props.min:r>this.props.max&&(r=this.props.max),i=r,this.props.onChange&&this.props.onChange({originalEvent:e,value:i})),i}},{key:"componentWillUnmount",value:function(){this.unbindDragListeners(),this.unbindTouchListeners()}},{key:"renderHandle",value:function(t,n,i){var s=this,o=r("p-slider-handle",{"p-slider-handle-start":0===i,"p-slider-handle-end":1===i,"p-slider-handle-active":this.handleIndex===i});return e.createElement("span",{onMouseDown:function(e){return s.onMouseDown(e,i)},onTouchStart:function(e){return s.onTouchStart(e,i)},onKeyDown:function(e){return s.onKeyDown(e,i)},tabIndex:this.props.tabIndex,className:o,style:{transition:this.dragging?"none":null,left:null!==t&&t+"%",bottom:n&&n+"%"},role:"slider","aria-valuemin":this.props.min,"aria-valuemax":this.props.max,"aria-valuenow":t||n,"aria-labelledby":this.props.ariaLabelledBy})}},{key:"renderRangeSlider",value:function(){var t=this.value,n="horizontal"===this.props.orientation,r=100*(t[0]<this.props.min?0:t[0]-this.props.min)/(this.props.max-this.props.min),i=100*(t[1]>this.props.max?100:t[1]-this.props.min)/(this.props.max-this.props.min),s=n?this.renderHandle(r,null,0):this.renderHandle(null,r,0),o=n?this.renderHandle(i,null,1):this.renderHandle(null,i,1);return e.createElement(e.Fragment,null,e.createElement("span",{className:"p-slider-range",style:n?{left:r+"%",width:i-r+"%"}:{bottom:r+"%",height:i-r+"%"}}),s,o)}},{key:"renderSingleSlider",value:function(){var t,n=this.value;t=n<this.props.min?0:n>this.props.max?100:100*(n-this.props.min)/(this.props.max-this.props.min);var r="horizontal"===this.props.orientation?{width:t+"%"}:{height:t+"%"},i="horizontal"===this.props.orientation?this.renderHandle(t,null,null):this.renderHandle(null,t,null);return e.createElement(e.Fragment,null,e.createElement("span",{className:"p-slider-range",style:r}),i)}},{key:"render",value:function(){var t=this,n=r("p-slider p-component",this.props.className,{"p-disabled":this.props.disabled,"p-slider-horizontal":"horizontal"===this.props.orientation,"p-slider-vertical":"vertical"===this.props.orientation}),i=this.props.range?this.renderRangeSlider():this.renderSingleSlider();return e.createElement("div",{id:this.props.id,ref:function(e){return t.el=e},style:this.props.style,className:n,onClick:this.onBarClick},i)}}])&&h(y.prototype,b),L&&h(y,L),Object.defineProperty(y,"prototype",{writable:!1}),w}();L={id:null,value:null,min:0,max:100,orientation:"horizontal",step:null,range:!1,style:null,className:null,disabled:!1,tabIndex:0,ariaLabelledBy:null,onChange:null,onSlideEnd:null},(b="defaultProps")in(y=E)?Object.defineProperty(y,b,{value:L,enumerable:!0,configurable:!0,writable:!0}):y[b]=L;export{E as Slider};
|