UNPKG

apex4x

Version:

The Comprehensive ARIA Development Suite

8 lines 13.9 kB
/*@license * Dragdealer.js 0.10.0 * http://github.com/skidding/dragdealer * * (c) 2010+ Ovidiu Cherecheș * http://skidding.mit-license.org */ !function(){function t(t,i){this.options=this.applyDefaults(i||{}),this.bindMethods(),this.wrapper=this.getWrapperElement(t),this.wrapper&&(this.handle=this.getHandleElement(this.wrapper,this.options.handleClass),this.handle)&&(this.init(),this.bindEventListeners())}t.prototype={defaults:{disabled:!1,horizontal:!0,vertical:!1,slide:!1,steps:0,snap:!1,loose:!1,speed:.1,xPrecision:0,yPrecision:0,handleClass:"handle",css3:!0,activeClass:"active",tapping:!0},init:function(){var t;this.options.css3&&(t=this.handle,r.backfaceVisibility)&&r.perspective&&(t.style[r.perspective]="1000px",t.style[r.backfaceVisibility]="hidden"),this.value={prev:[-1,-1],current:[this.options.x||0,this.options.y||0],target:[this.options.x||0,this.options.y||0]},this.offset={wrapper:[0,0],mouse:[0,0],prev:[-999999,-999999],current:[0,0],target:[0,0]},this.dragStartPosition={x:0,y:0},this.change=[0,0],this.stepRatios=this.calculateStepRatios(),this.activity=!1,this.dragging=!1,this.tapping=!1,this.reflow(),this.options.disabled&&this.disable()},applyDefaults:function(t){for(var i in this.defaults)t.hasOwnProperty(i)||(t[i]=this.defaults[i]);return t},getWrapperElement:function(t){return"string"==typeof t?document.getElementById(t):t},getHandleElement:function(t,i){var e,s,n;if(t.getElementsByClassName){if(0<(e=t.getElementsByClassName(i)).length)return e[0]}else for(s=new RegExp("(^|\\s)"+i+"(\\s|$)"),e=t.getElementsByTagName("*"),n=0;n<e.length;n++)if(s.test(e[n].className))return e[n]},calculateStepRatios:function(){var t=[];if(1<=this.options.steps)for(var i=0;i<=this.options.steps-1;i++)1<this.options.steps?t[i]=i/(this.options.steps-1):t[i]=0;return t},setWrapperOffset:function(){this.offset.wrapper=h.get(this.wrapper)},calculateBounds:function(){var t={top:this.options.top||0,bottom:-(this.options.bottom||0)+this.wrapper.offsetHeight,left:this.options.left||0,right:-(this.options.right||0)+this.wrapper.offsetWidth};return t.availWidth=t.right-t.left-this.handle.offsetWidth,t.availHeight=t.bottom-t.top-this.handle.offsetHeight,t},calculateValuePrecision:function(){var t=this.options.xPrecision||Math.abs(this.bounds.availWidth),i=this.options.yPrecision||Math.abs(this.bounds.availHeight);return[t?1/t:0,i?1/i:0]},bindMethods:function(){"function"==typeof this.options.customRequestAnimationFrame?this.requestAnimationFrame=i(this.options.customRequestAnimationFrame,window):this.requestAnimationFrame=i(p,window),"function"==typeof this.options.customCancelAnimationFrame?this.cancelAnimationFrame=i(this.options.customCancelAnimationFrame,window):this.cancelAnimationFrame=i(c,window),this.animateWithRequestAnimationFrame=i(this.animateWithRequestAnimationFrame,this),this.animate=i(this.animate,this),this.onHandleMouseDown=i(this.onHandleMouseDown,this),this.onHandleTouchStart=i(this.onHandleTouchStart,this),this.onDocumentMouseMove=i(this.onDocumentMouseMove,this),this.onWrapperTouchMove=i(this.onWrapperTouchMove,this),this.onWrapperMouseDown=i(this.onWrapperMouseDown,this),this.onWrapperTouchStart=i(this.onWrapperTouchStart,this),this.onDocumentMouseUp=i(this.onDocumentMouseUp,this),this.onDocumentTouchEnd=i(this.onDocumentTouchEnd,this),this.onHandleClick=i(this.onHandleClick,this),this.onWindowResize=i(this.onWindowResize,this)},bindEventListeners:function(){e(this.handle,"mousedown",this.onHandleMouseDown),e(this.handle,"touchstart",this.onHandleTouchStart),e(document,"mousemove",this.onDocumentMouseMove),e(this.wrapper,"touchmove",this.onWrapperTouchMove),e(this.wrapper,"mousedown",this.onWrapperMouseDown),e(this.wrapper,"touchstart",this.onWrapperTouchStart),e(document,"mouseup",this.onDocumentMouseUp),e(document,"touchend",this.onDocumentTouchEnd),e(this.handle,"click",this.onHandleClick),e(window,"resize",this.onWindowResize),this.animate(!1,!0),this.interval=this.requestAnimationFrame(this.animateWithRequestAnimationFrame)},unbindEventListeners:function(){s(this.handle,"mousedown",this.onHandleMouseDown),s(this.handle,"touchstart",this.onHandleTouchStart),s(document,"mousemove",this.onDocumentMouseMove),s(this.wrapper,"touchmove",this.onWrapperTouchMove),s(this.wrapper,"mousedown",this.onWrapperMouseDown),s(this.wrapper,"touchstart",this.onWrapperTouchStart),s(document,"mouseup",this.onDocumentMouseUp),s(document,"touchend",this.onDocumentTouchEnd),s(this.handle,"click",this.onHandleClick),s(window,"resize",this.onWindowResize),this.cancelAnimationFrame(this.interval)},onHandleMouseDown:function(t){a.refresh(t),n(t),o(t),this.activity=!1,this.startDrag()},onHandleTouchStart:function(t){a.refresh(t),o(t),this.activity=!1,this.startDrag()},onDocumentMouseMove:function(t){t.clientX-this.dragStartPosition.x==0&&t.clientY-this.dragStartPosition.y==0||(a.refresh(t),this.dragging&&(this.activity=!0,n(t)))},onWrapperTouchMove:function(t){a.refresh(t),!this.activity&&this.draggingOnDisabledAxis()?this.dragging&&this.stopDrag():(n(t),this.activity=!0)},onWrapperMouseDown:function(t){a.refresh(t),n(t),this.startTap()},onWrapperTouchStart:function(t){a.refresh(t),n(t),this.startTap()},onDocumentMouseUp:function(t){this.stopDrag(),this.stopTap()},onDocumentTouchEnd:function(t){this.stopDrag(),this.stopTap()},onHandleClick:function(t){this.activity&&(n(t),o(t))},onWindowResize:function(t){this.reflow()},enable:function(){this.disabled=!1,this.handle.className=this.handle.className.replace(/\s?disabled/g,"")},disable:function(){this.disabled=!0,this.handle.className+=" disabled"},reflow:function(){this.setWrapperOffset(),this.bounds=this.calculateBounds(),this.valuePrecision=this.calculateValuePrecision(),this.updateOffsetFromValue()},getStep:function(){return[this.getStepNumber(this.value.target[0]),this.getStepNumber(this.value.target[1])]},getStepWidth:function(){return Math.abs(this.bounds.availWidth/this.options.steps)},getValue:function(){return this.value.target},setStep:function(t,i,e){this.setValue(this.options.steps&&1<t?(t-1)/(this.options.steps-1):0,this.options.steps&&1<i?(i-1)/(this.options.steps-1):0,e)},setValue:function(t,i,e){this.setTargetValue([t,i||0]),e&&(this.groupCopy(this.value.current,this.value.target),this.updateOffsetFromValue(),this.callAnimationCallback())},startTap:function(){var t,i;!this.disabled&&this.options.tapping&&(this.tapping=!0,this.setWrapperOffset(),this.options.snap&&this.options.steps?(t=(a.x-this.offset.wrapper[0])/this.bounds.availWidth,i=(a.y-this.offset.wrapper[1])/this.bounds.availHeight,this.setValue(this.getClosestStep(t),this.getClosestStep(i),!0)):this.setTargetValueByOffset([a.x-this.offset.wrapper[0]-this.handle.offsetWidth/2,a.y-this.offset.wrapper[1]-this.handle.offsetHeight/2]))},stopTap:function(){!this.disabled&&this.tapping&&(this.tapping=!1,this.setTargetValue(this.value.current))},startDrag:function(){this.disabled||(this.dragging=!0,this.setWrapperOffset(),this.dragStartPosition={x:a.x,y:a.y},this.offset.mouse=[a.x-h.get(this.handle)[0],a.y-h.get(this.handle)[1]],this.wrapper.className.match(this.options.activeClass)||(this.wrapper.className+=" "+this.options.activeClass),this.callDragStartCallback())},stopDrag:function(){var t,i,e;!this.disabled&&this.dragging&&(this.dragging=!1,t=[0===this.bounds.availWidth?0:(a.x-this.dragStartPosition.x)/this.bounds.availWidth,0===this.bounds.availHeight?0:(a.y-this.dragStartPosition.y)/this.bounds.availHeight],i=this.groupClone(this.value.current),this.options.slide&&(e=this.change,i[0]+=4*e[0],i[1]+=4*e[1]),this.setTargetValue(i),this.wrapper.className=this.wrapper.className.replace(" "+this.options.activeClass,""),this.callDragStopCallback(t))},callAnimationCallback:function(){var t=this.value.current;this.options.snap&&1<this.options.steps&&(t=this.getClosestSteps(t)),this.groupCompare(t,this.value.prev)||("function"==typeof this.options.animationCallback&&this.options.animationCallback.call(this,t[0],t[1]),this.groupCopy(this.value.prev,t))},callTargetCallback:function(){"function"==typeof this.options.callback&&this.options.callback.call(this,this.value.target[0],this.value.target[1])},callDragStartCallback:function(){"function"==typeof this.options.dragStartCallback&&this.options.dragStartCallback.call(this,this.value.target[0],this.value.target[1])},callDragStopCallback:function(t){"function"==typeof this.options.dragStopCallback&&this.options.dragStopCallback.call(this,this.value.target[0],this.value.target[1],t)},animateWithRequestAnimationFrame:function(t){t?(this.timeOffset=this.timeStamp?t-this.timeStamp:0,this.timeStamp=t):this.timeOffset=25,this.animate(),this.interval=this.requestAnimationFrame(this.animateWithRequestAnimationFrame)},animate:function(t,i){var e;(!t||this.dragging)&&(this.dragging&&(t=this.groupClone(this.value.target),e=[a.x-this.offset.wrapper[0]-this.offset.mouse[0],a.y-this.offset.wrapper[1]-this.offset.mouse[1]],this.setTargetValueByOffset(e,this.options.loose),this.change=[this.value.target[0]-t[0],this.value.target[1]-t[1]]),(this.dragging||i)&&this.groupCopy(this.value.current,this.value.target),this.dragging||this.glide()||i)&&(this.updateOffsetFromValue(),this.callAnimationCallback())},glide:function(){var t=[this.value.target[0]-this.value.current[0],this.value.target[1]-this.value.current[1]];return!(!t[0]&&!t[1]||(Math.abs(t[0])>this.valuePrecision[0]||Math.abs(t[1])>this.valuePrecision[1]?(this.value.current[0]+=t[0]*Math.min(this.options.speed*this.timeOffset/25,1),this.value.current[1]+=t[1]*Math.min(this.options.speed*this.timeOffset/25,1)):this.groupCopy(this.value.current,this.value.target),0))},updateOffsetFromValue:function(){this.options.snap?this.offset.current=this.getOffsetsByRatios(this.getClosestSteps(this.value.current)):this.offset.current=this.getOffsetsByRatios(this.value.current),this.groupCompare(this.offset.current,this.offset.prev)||(this.renderHandlePosition(),this.groupCopy(this.offset.prev,this.offset.current))},renderHandlePosition:function(){var t="";this.options.css3&&r.transform?(this.options.horizontal&&(t+="translateX("+this.offset.current[0]+"px)"),this.options.vertical&&(t+=" translateY("+this.offset.current[1]+"px)"),this.handle.style[r.transform]=t):(this.options.horizontal&&(this.handle.style.left=this.offset.current[0]+"px"),this.options.vertical&&(this.handle.style.top=this.offset.current[1]+"px"))},setTargetValue:function(t,i){i=i?this.getLooseValue(t):this.getProperValue(t);this.groupCopy(this.value.target,i),this.offset.target=this.getOffsetsByRatios(i),this.callTargetCallback()},setTargetValueByOffset:function(t,i){t=this.getRatiosByOffsets(t),i=i?this.getLooseValue(t):this.getProperValue(t);this.groupCopy(this.value.target,i),this.offset.target=this.getOffsetsByRatios(i)},getLooseValue:function(t){var i=this.getProperValue(t);return[i[0]+(t[0]-i[0])/4,i[1]+(t[1]-i[1])/4]},getProperValue:function(t){t=this.groupClone(t);return t[0]=Math.max(t[0],0),t[1]=Math.max(t[1],0),t[0]=Math.min(t[0],1),t[1]=Math.min(t[1],1),t=(!this.dragging&&!this.tapping||this.options.snap)&&1<this.options.steps?this.getClosestSteps(t):t},getRatiosByOffsets:function(t){return[this.getRatioByOffset(t[0],this.bounds.availWidth,this.bounds.left),this.getRatioByOffset(t[1],this.bounds.availHeight,this.bounds.top)]},getRatioByOffset:function(t,i,e){return i?(t-e)/i:0},getOffsetsByRatios:function(t){return[this.getOffsetByRatio(t[0],this.bounds.availWidth,this.bounds.left),this.getOffsetByRatio(t[1],this.bounds.availHeight,this.bounds.top)]},getOffsetByRatio:function(t,i,e){return Math.round(t*i)+e},getStepNumber:function(t){return this.getClosestStep(t)*(this.options.steps-1)+1},getClosestSteps:function(t){return[this.getClosestStep(t[0]),this.getClosestStep(t[1])]},getClosestStep:function(t){for(var i=0,e=1,s=0;s<=this.options.steps-1;s++)Math.abs(this.stepRatios[s]-t)<e&&(e=Math.abs(this.stepRatios[s]-t),i=s);return this.stepRatios[i]},groupCompare:function(t,i){return t[0]===i[0]&&t[1]===i[1]},groupCopy:function(t,i){t[0]=i[0],t[1]=i[1]},groupClone:function(t){return[t[0],t[1]]},draggingOnDisabledAxis:function(){return!this.options.horizontal&&a.xDiff>a.yDiff||!this.options.vertical&&a.yDiff>a.xDiff}};var i=function(t,i){return function(){return t.apply(i,arguments)}},e=function(t,i,e){t.addEventListener?t.addEventListener(i,e,!1):t.attachEvent&&t.attachEvent("on"+i,e)},s=function(t,i,e){t.removeEventListener?t.removeEventListener(i,e,!1):t.detachEvent&&t.detachEvent("on"+i,e)},n=function(t){(t=t||window.event).preventDefault&&t.preventDefault(),t.returnValue=!1},o=function(t){(t=t||window.event).stopPropagation&&t.stopPropagation(),t.cancelBubble=!0},a={x:0,y:0,xDiff:0,yDiff:0,refresh:function(t){"mousemove"===(t=t||window.event).type?this.set(t):t.touches&&this.set(t.touches[0])},set:function(t){var i=this.x,e=this.y;t.clientX||t.clientY?(this.x=t.clientX,this.y=t.clientY):(t.pageX||t.pageY)&&(this.x=t.pageX-document.body.scrollLeft-document.documentElement.scrollLeft,this.y=t.pageY-document.body.scrollTop-document.documentElement.scrollTop),this.xDiff=Math.abs(this.x-i),this.yDiff=Math.abs(this.y-e)}},h={get:function(t){var i={left:0,top:0};return[(i=void 0!==t.getBoundingClientRect?t.getBoundingClientRect():i).left,i.top]}},r={transform:u("transform"),perspective:u("perspective"),backfaceVisibility:u("backfaceVisibility")};function u(t){var i="Webkit Moz ms O".split(" "),e=document.documentElement.style;if(void 0!==e[t])return t;t=t.charAt(0).toUpperCase()+t.substr(1);for(var s=0;s<i.length;s++)if(void 0!==e[i[s]+t])return i[s]+t}for(var l=["webkit","moz"],p=window.requestAnimationFrame,c=window.cancelAnimationFrame,f=0;f<l.length&&!p;++f)p=window[l[f]+"RequestAnimationFrame"],c=window[l[f]+"CancelAnimationFrame"]||window[l[f]+"CancelRequestAnimationFrame"];p||(p=function(t){return setTimeout(t,25)},c=clearTimeout),$A.Dragdealer=t}();