This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pep.edited.min.js
1 lines (1 loc) · 18.5 KB
/
pep.edited.min.js
1
(function(e,t,n){"use strict";function s(t,n){this.name=r;this.el=t;this.$el=e(t);this.options=e.extend({},i,n);this.$document=e(this.$el[0].ownerDocument);this.$body=this.$document.find("body");this.moveTrigger="MSPointerMove touchmove mousemove";this.startTrigger="MSPointerDown touchstart mousedown";this.stopTrigger="MSPointerUp touchend mouseup";this.startTriggerArray=this.startTrigger.split(" ");this.moveTriggerArray=this.moveTrigger.split(" ");this.stopTriggerArray=this.stopTrigger.split(" ");this.stopEvents=[this.stopTrigger,this.options.stopEvents].join(" ");if(this.options.constrainTo==="window")this.$container=this.$document;else if(this.options.constrainTo&&this.options.constrainTo!=="parent")this.$container=e(this.options.constrainTo);else this.$container=this.$el.parent();if(this.isPointerEventCompatible())this.applyMSDefaults();this.CSSEaseHash=this.getCSSEaseHash();this.scale=1;this.started=false;this.disabled=false;this.activeDropRegions=[];this.resetVelocityQueue();this.init()}var r="pep";var i={initiate:function(){},start:function(){},drag:function(){},stop:function(){},rest:function(){},moveTo:false,callIfNotStarted:["stop","rest"],startThreshold:[0,0],grid:[1,1],debug:false,activeClass:"pep-active",multiplier:1.2,velocityMultiplier:2.5,shouldPreventDefault:false,allowDragEventPropagation:true,stopEvents:"",hardwareAccelerate:true,useCSSTranslation:true,disableSelect:true,cssEaseString:"cubic-bezier(0.190, 1.000, 0.220, 1.000)",cssEaseDuration:1e3,shouldEase:true,droppable:false,droppableActiveClass:"pep-dpa",overlapFunction:false,constrainTo:false,removeMargins:true,place:true,deferPlacement:false,axis:null,forceNonCSS3Movement:false,elementsWithInteraction:"input",revert:false,revertAfter:"stop",revertIf:function(){return true},ignoreRightClick:true};s.prototype.init=function(){if(this.options.debug)this.buildDebugDiv();if(this.options.disableSelect)this.disableSelect();if(this.options.place&&!this.options.deferPlacement){this.positionParent();this.placeObject()}this.ev={};this.pos={};this.subscribe()};s.prototype.subscribe=function(){var e=this;this.onStartEvent=function(t){e.handleStart(t)};this.$el.on(this.startTrigger,this.onStartEvent);this.onStartEventOnElementsWithInteraction=function(e){e.stopPropagation()};this.$el.on(this.startTrigger,this.options.elementsWithInteraction,this.onStartEventOnElementsWithInteraction);this.onStopEvents=function(t){e.handleStop(t)};this.$document.on(this.stopEvents,this.onStopEvents);this.onMoveEvents=function(t){e.moveEvent=t};this.$document.on(this.moveTrigger,this.onMoveEvents)};s.prototype.unsubscribe=function(){this.$el.off(this.startTrigger,this.onStartEvent);this.$el.off(this.startTrigger,this.options.elementsWithInteraction,this.onStartEventOnElementsWithInteraction);this.$document.off(this.stopEvents,this.onStopEvents);this.$document.off(this.moveTrigger,this.onMoveEvents)};s.prototype.handleStart=function(t){var n=this;if(this.isValidMoveEvent(t)&&!this.disabled){if(!(this.options.ignoreRightClick&&t.which===3)){if(this.isPointerEventCompatible()&&t.preventManipulation)t.preventManipulation();t=this.normalizeEvent(t);if(this.options.place&&this.options.deferPlacement){this.positionParent();this.placeObject()}this.log({type:"event",event:t.type});if(this.options.hardwareAccelerate&&!this.hardwareAccelerated){this.hardwareAccelerate();this.hardwareAccelerated=true}var r=this.options.initiate.call(this,t,this);if(r===false)return;clearTimeout(this.restTimeout);this.$el.addClass(this.options.activeClass);this.removeCSSEasing();this.startX=this.ev.x=t.pep.x;this.startY=this.ev.y=t.pep.y;this.initialPosition=this.initialPosition||this.$el.position();this.startEvent=this.moveEvent=t;this.active=true;if(this.options.shouldPreventDefault)t.preventDefault();if(!this.options.allowDragEventPropagation)t.stopPropagation();(function i(){if(!n.active)return;n.handleMove();n.requestAnimationFrame(i)})(e,n)}}};s.prototype.handleMove=function(){if(typeof this.moveEvent==="undefined")return;var n=this.normalizeEvent(this.moveEvent);var r=t.parseInt(n.pep.x/this.options.grid[0])*this.options.grid[0];var i=t.parseInt(n.pep.y/this.options.grid[1])*this.options.grid[1];this.addToLIFO({time:n.timeStamp,x:r,y:i});var s,o;if(e.inArray(n.type,this.startTriggerArray)>-1){s=0;o=0}else{s=r-this.ev.x;o=i-this.ev.y}this.dx=s;this.dy=o;this.ev.x=r;this.ev.y=i;if(s===0&&o===0){this.log({type:"event",event:"** stopped **"});return}var u=Math.abs(this.startX-r);var a=Math.abs(this.startY-i);if(!this.started&&(u>this.options.startThreshold[0]||a>this.options.startThreshold[1])){this.started=true;this.$el.addClass("pep-start");this.options.start.call(this,this.startEvent,this)}if(this.options.droppable){this.calculateActiveDropRegions()}var f=this.options.drag.call(this,n,this);if(f===false){this.resetVelocityQueue();return}this.log({type:"event",event:n.type});this.log({type:"event-coords",x:this.ev.x,y:this.ev.y});this.log({type:"velocity"});this.doMoveTo(s,o)};s.prototype.doMoveTo=function(e,t){var n=this.handleConstraint(e,t);var r,i;if(typeof this.options.moveTo==="function"){r=e>=0?"+="+Math.abs(e/this.scale)*this.options.multiplier:"-="+Math.abs(e/this.scale)*this.options.multiplier;i=t>=0?"+="+Math.abs(t/this.scale)*this.options.multiplier:"-="+Math.abs(t/this.scale)*this.options.multiplier;if(this.options.constrainTo){r=n.x!==false?n.x:r;i=n.y!==false?n.y:i}if(this.options.axis==="x")i=n.y;if(this.options.axis==="y")r=n.x;this.options.moveTo.call(this,r,i)}else if(!this.shouldUseCSSTranslation()){r=e>=0?"+="+Math.abs(e/this.scale)*this.options.multiplier:"-="+Math.abs(e/this.scale)*this.options.multiplier;i=t>=0?"+="+Math.abs(t/this.scale)*this.options.multiplier:"-="+Math.abs(t/this.scale)*this.options.multiplier;if(this.options.constrainTo){r=n.x!==false?n.x:r;i=n.y!==false?n.y:i}if(this.options.axis==="x")i=n.y;if(this.options.axis==="y")r=n.x;this.moveTo(r,i)}else{e=e/this.scale*this.options.multiplier;t=t/this.scale*this.options.multiplier;if(this.options.constrainTo){e=n.x===false?e:0;t=n.y===false?t:0}if(this.options.axis==="x")t=0;if(this.options.axis==="y")e=0;this.moveToUsingTransforms(e,t)}};s.prototype.handleStop=function(t){if(!this.active)return;this.log({type:"event",event:t.type});this.active=false;this.$el.removeClass("pep-start").addClass("pep-ease");if(this.options.droppable){this.calculateActiveDropRegions()}if(this.started||!this.started&&e.inArray("stop",this.options.callIfNotStarted)>-1){this.options.stop.call(this,t,this)}if(this.options.shouldEase){this.ease(t,this.started)}else{this.removeActiveClass()}if(this.options.revert&&(this.options.revertAfter==="stop"||!this.options.shouldEase)&&this.options.revertIf&&this.options.revertIf.call(this)){this.revert()}this.started=false;this.resetVelocityQueue()};s.prototype.ease=function(t,n){var r=this.$el.position();var i=this.velocity();var s=this.dt;var o=i.x/this.scale*this.options.multiplier;var u=i.y/this.scale*this.options.multiplier;var a=this.handleConstraint(o,u,true);if(this.cssAnimationsSupported())this.$el.css(this.getCSSEaseHash());var f=i.x>0?"+="+o:"-="+Math.abs(o);var l=i.y>0?"+="+u:"-="+Math.abs(u);if(this.options.constrainTo){f=a.x!==false?a.x:f;l=a.y!==false?a.y:l}if(this.options.axis==="x")l="+=0";if(this.options.axis==="y")f="+=0";var c=!this.cssAnimationsSupported()||this.options.forceNonCSS3Movement;if(typeof this.options.moveTo==="function"){this.options.moveTo.call(this,f,l)}else{this.moveTo(f,l,c)}var h=this;this.restTimeout=setTimeout(function(){if(h.options.droppable){h.calculateActiveDropRegions()}if(n||!n&&e.inArray("rest",h.options.callIfNotStarted)>-1){h.options.rest.call(h,t,h)}if(h.options.revert&&h.options.revertAfter==="ease"&&h.options.shouldEase&&h.options.revertIf&&h.options.revertIf.call(h)){h.revert()}h.removeActiveClass()},this.options.cssEaseDuration)};s.prototype.normalizeEvent=function(e){e.pep={};if(this.isPointerEventCompatible()||!this.isTouch(e)){if(e.pageX){e.pep.x=e.pageX;e.pep.y=e.pageY}else{e.pep.x=e.originalEvent.pageX;e.pep.y=e.originalEvent.pageY}e.pep.type=e.type}else{e.pep.x=e.originalEvent.touches[0].pageX;e.pep.y=e.originalEvent.touches[0].pageY;e.pep.type=e.type}return e};s.prototype.resetVelocityQueue=function(){this.velocityQueue=new Array(5)};s.prototype.moveTo=function(e,t,n){this.log({type:"delta",x:e,y:t});if(n){this.$el.animate({top:t,left:e},0,"easeOutQuad",{queue:false})}else{this.$el.stop(true,false).css({top:t,left:e})}};s.prototype.moveToUsingTransforms=function(e,t){var n=this.matrixToArray(this.matrixString());if(!this.cssX)this.cssX=this.xTranslation(n);if(!this.cssY)this.cssY=this.yTranslation(n);if(dragreset=="no"){this.cssX=this.cssX+e;this.cssY=this.cssY+t;this.log({type:"delta",x:e,y:t})}else{this.cssX=0;this.cssY=0;}dragreset="no";n[4]=this.cssX;n[5]=this.cssY;this.translation=this.arrayToMatrix(n);this.transform(this.translation)};s.prototype.transform=function(e){this.$el.css({"-webkit-transform":e,"-moz-transform":e,"-ms-transform":e,"-o-transform":e,transform:e})};s.prototype.xTranslation=function(e){e=e||this.matrixToArray(this.matrixString());return parseInt(e[4],10)};s.prototype.yTranslation=function(e){e=e||this.matrixToArray(this.matrixString());return parseInt(e[5],10)};s.prototype.matrixString=function(){var e=function(e){return!(!e||e==="none"||e.indexOf("matrix")<0)};var t="matrix(1, 0, 0, 1, 0, 0)";if(e(this.$el.css("-webkit-transform")))t=this.$el.css("-webkit-transform");if(e(this.$el.css("-moz-transform")))t=this.$el.css("-moz-transform");if(e(this.$el.css("-ms-transform")))t=this.$el.css("-ms-transform");if(e(this.$el.css("-o-transform")))t=this.$el.css("-o-transform");if(e(this.$el.css("transform")))t=this.$el.css("transform");return t};s.prototype.matrixToArray=function(e){return e.split("(")[1].split(")")[0].split(",")};s.prototype.arrayToMatrix=function(e){return"matrix("+e.join(",")+")"};s.prototype.addToLIFO=function(e){var t=this.velocityQueue;t=t.slice(1,t.length);t.push(e);this.velocityQueue=t};s.prototype.velocity=function(){var e=0;var t=0;for(var n=0;n<this.velocityQueue.length-1;n++){if(this.velocityQueue[n]){e+=this.velocityQueue[n+1].x-this.velocityQueue[n].x;t+=this.velocityQueue[n+1].y-this.velocityQueue[n].y;this.dt=this.velocityQueue[n+1].time-this.velocityQueue[n].time}}return{x:e*this.options.velocityMultiplier,y:t*this.options.velocityMultiplier}};s.prototype.revert=function(){if(this.shouldUseCSSTranslation()){this.moveToUsingTransforms(-this.xTranslation(),-this.yTranslation())}this.moveTo(this.initialPosition.left,this.initialPosition.top)};s.prototype.requestAnimationFrame=function(e){return t.requestAnimationFrame&&t.requestAnimationFrame(e)||t.webkitRequestAnimationFrame&&t.webkitRequestAnimationFrame(e)||t.mozRequestAnimationFrame&&t.mozRequestAnimationFrame(e)||t.oRequestAnimationFrame&&t.mozRequestAnimationFrame(e)||t.msRequestAnimationFrame&&t.msRequestAnimationFrame(e)||t.setTimeout(e,1e3/60)};s.prototype.positionParent=function(){if(!this.options.constrainTo||this.parentPositioned)return;this.parentPositioned=true;if(this.options.constrainTo==="parent"){this.$container.css({position:"relative"})}else if(this.options.constrainTo==="window"&&this.$container.get(0).nodeName!=="#document"&&this.$container.css("position")!=="static"){this.$container.css({position:"static"})}};s.prototype.placeObject=function(){if(this.objectPlaced)return;this.objectPlaced=true;this.offset=this.options.constrainTo==="parent"||this.hasNonBodyRelative()?this.$el.position():this.$el.offset();if(parseInt(this.$el.css("left"),10))this.offset.left=this.$el.css("left");if(parseInt(this.$el.css("top"),10))this.offset.top=this.$el.css("top");if(this.options.removeMargins)this.$el.css({margin:0});this.$el.css({position:"absolute",top:this.offset.top,left:this.offset.left})};s.prototype.hasNonBodyRelative=function(){return this.$el.parents().filter(function(){var t=e(this);return t.is("body")||t.css("position")==="relative"}).length>1};s.prototype.setScale=function(e){this.scale=e};s.prototype.setMultiplier=function(e){this.options.multiplier=e};s.prototype.removeCSSEasing=function(){if(this.cssAnimationsSupported())this.$el.css(this.getCSSEaseHash(true))};s.prototype.disableSelect=function(){this.$el.css({"-webkit-touch-callout":"none","-webkit-user-select":"none","-khtml-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","user-select":"none"})};s.prototype.removeActiveClass=function(){this.$el.removeClass([this.options.activeClass,"pep-ease"].join(" "))};s.prototype.handleConstraint=function(t,r,i){var s=this.$el.position();this.pos.x=s.left;this.pos.y=s.top;var o={x:false,y:false};var u,a,f,l;this.log({type:"pos-coords",x:this.pos.x,y:this.pos.y});if(e.isArray(this.options.constrainTo)){if(this.options.constrainTo[3]!==n&&this.options.constrainTo[1]!==n){a=this.options.constrainTo[1]===false?Infinity:this.options.constrainTo[1];f=this.options.constrainTo[3]===false?-Infinity:this.options.constrainTo[3]}if(this.options.constrainTo[0]!==false&&this.options.constrainTo[2]!==false){u=this.options.constrainTo[2]===false?Infinity:this.options.constrainTo[2];l=this.options.constrainTo[0]===false?-Infinity:this.options.constrainTo[0]}if(this.pos.x+t<f)o.x=f;if(this.pos.y+r<l)o.y=l}else if(typeof this.options.constrainTo==="string"){f=0;l=0;a=this.$container.width()-this.$el.outerWidth();u=this.$container.height()-this.$el.outerHeight();if(this.pos.x+t<0)o.x=0;if(this.pos.y+r<0)o.y=0}if(this.pos.x+t>a)o.x=a;if(this.pos.y+r>u)o.y=u;if(this.shouldUseCSSTranslation()&&i){if(o.x===f&&this.xTranslation())o.x=f-this.xTranslation();if(o.x===a&&this.xTranslation())o.x=a-this.xTranslation();if(o.y===l&&this.yTranslation())o.y=l-this.yTranslation();if(o.y===u&&this.yTranslation())o.y=u-this.yTranslation()}return o};s.prototype.getCSSEaseHash=function(e){if(typeof e==="undefined")e=false;var t;if(e){t=""}else if(this.CSSEaseHash){return this.CSSEaseHash}else{t=["all",this.options.cssEaseDuration+"ms",this.options.cssEaseString].join(" ")}return{"-webkit-transition":t,"-moz-transition":t,"-ms-transition":t,"-o-transition":t,transition:t}};s.prototype.calculateActiveDropRegions=function(){var t=this;this.activeDropRegions.length=0;e.each(e(this.options.droppable),function(n,r){var i=e(r);if(t.isOverlapping(i,t.$el)){i.addClass(t.options.droppableActiveClass);t.activeDropRegions.push(i)}else{i.removeClass(t.options.droppableActiveClass)}})};s.prototype.isOverlapping=function(e,t){if(this.options.overlapFunction){return this.options.overlapFunction(e,t)}var n=e[0].getBoundingClientRect();var r=t[0].getBoundingClientRect();return!(n.right<r.left||n.left>r.right||n.bottom<r.top||n.top>r.bottom)};s.prototype.isTouch=function(e){return e.type.search("touch")>-1};s.prototype.isPointerEventCompatible=function(){return"MSPointerEvent"in t};s.prototype.applyMSDefaults=function(e){this.$el.css({"-ms-touch-action":"none","touch-action":"none","-ms-scroll-chaining":"none","-ms-scroll-limit":"0 0 0 0"})};s.prototype.isValidMoveEvent=function(e){return!this.isTouch(e)||this.isTouch(e)&&e.originalEvent&&e.originalEvent.touches&&e.originalEvent.touches.length===1};s.prototype.shouldUseCSSTranslation=function(){if(this.options.forceNonCSS3Movement)return false;if(typeof this.useCSSTranslation!=="undefined")return this.useCSSTranslation;var e=false;if(!this.options.useCSSTranslation||typeof Modernizr!=="undefined"&&!Modernizr.csstransforms){e=false}else{e=true}this.useCSSTranslation=e;return e};s.prototype.cssAnimationsSupported=function(){if(typeof this.cssAnimationsSupport!=="undefined"){return this.cssAnimationsSupport}if(typeof Modernizr!=="undefined"&&Modernizr.cssanimations){this.cssAnimationsSupport=true;return true}var e=false,t=document.createElement("div"),r="animation",i="",s="Webkit Moz O ms Khtml".split(" "),o="";if(t.style.animationName){e=true}if(e===false){for(var u=0;u<s.length;u++){if(t.style[s[u]+"AnimationName"]!==n){o=s[u];r=o+"Animation";i="-"+o.toLowerCase()+"-";e=true;break}}}this.cssAnimationsSupport=e;return e};s.prototype.hardwareAccelerate=function(){this.$el.css({"-webkit-perspective":1e3,perspective:1e3,"-webkit-backface-visibility":"hidden","backface-visibility":"hidden"})};s.prototype.getMovementValues=function(){return{ev:this.ev,pos:this.pos,velocity:this.velocity()}};s.prototype.buildDebugDiv=function(){var t;if(e("#pep-debug").length===0){t=e("<div></div>");t.attr("id","pep-debug").append("<div style='font-weight:bold; background: red; color: white;'>DEBUG MODE</div>").append("<div id='pep-debug-event'>no event</div>").append("<div id='pep-debug-ev-coords'>event coords: <span class='pep-x'>-</span>, <span class='pep-y'>-</span></div>").append("<div id='pep-debug-pos-coords'>position coords: <span class='pep-x'>-</span>, <span class='pep-y'>-</span></div>").append("<div id='pep-debug-velocity'>velocity: <span class='pep-x'>-</span>, <span class='pep-y'>-</span></div>").append("<div id='pep-debug-delta'>Δ movement: <span class='pep-x'>-</span>, <span class='pep-y'>-</span></div>").css({position:"fixed",bottom:5,right:5,zIndex:99999,textAlign:"right",fontFamily:"Arial, sans",fontSize:10,border:"1px solid #DDD",padding:"3px",background:"white",color:"#333"})}var n=this;setTimeout(function(){n.debugElements={$event:e("#pep-debug-event"),$velocityX:e("#pep-debug-velocity .pep-x"),$velocityY:e("#pep-debug-velocity .pep-y"),$dX:e("#pep-debug-delta .pep-x"),$dY:e("#pep-debug-delta .pep-y"),$evCoordsX:e("#pep-debug-ev-coords .pep-x"),$evCoordsY:e("#pep-debug-ev-coords .pep-y"),$posCoordsX:e("#pep-debug-pos-coords .pep-x"),$posCoordsY:e("#pep-debug-pos-coords .pep-y")}},0);e("body").append(t)};s.prototype.log=function(e){if(!this.options.debug)return;switch(e.type){case"event":this.debugElements.$event.text(e.event);break;case"pos-coords":this.debugElements.$posCoordsX.text(e.x);this.debugElements.$posCoordsY.text(e.y);break;case"event-coords":this.debugElements.$evCoordsX.text(e.x);this.debugElements.$evCoordsY.text(e.y);break;case"delta":this.debugElements.$dX.text(e.x);this.debugElements.$dY.text(e.y);break;case"velocity":var t=this.velocity();this.debugElements.$velocityX.text(Math.round(t.x));this.debugElements.$velocityY.text(Math.round(t.y));break}};s.prototype.toggle=function(e){if(typeof e==="undefined"){this.disabled=!this.disabled}else{this.disabled=!e}};e.extend(e.easing,{easeOutQuad:function(e,t,n,r,i){return-r*(t/=i)*(t-2)+n},easeOutCirc:function(e,t,n,r,i){return r*Math.sqrt(1-(t=t/i-1)*t)+n},easeOutExpo:function(e,t,n,r,i){return t===i?n+r:r*(-Math.pow(2,-10*t/i)+1)+n}});e.fn[r]=function(t){return this.each(function(){if(!e.data(this,"plugin_"+r)){var n=new s(this,t);e.data(this,"plugin_"+r,n);e.pep.peps.push(n)}})};e.pep={};e.pep.peps=[];e.pep.toggleAll=function(t){e.each(this.peps,function(e,n){n.toggle(t)})};e.pep.unbind=function(e){var t=e.data("plugin_"+r);if(typeof t==="undefined")return;t.toggle(false);t.unsubscribe();e.removeData("plugin_"+r)}})(jQuery,window)