1
0
forked from sent/waves
waves/public/assets/g/littlealchemy/js/dragNdrop.580.js
2025-04-09 17:11:14 -05:00

1 line
12 KiB
JavaScript

!function(){function t(t,e){for(var i in e)t[i]=e[i];return t}function e(e,i){this.element="string"==typeof e?document.querySelector(e):e,this.$element=$(this.element),this.options=t({},this.options),t(this.options,i),this._create()}function i(t,e){t.x=void 0!==e.pageX?e.pageX:e.clientX,t.y=void 0!==e.pageY?e.pageY:e.clientY}function n(e,i){this.element="string"==typeof e?document.querySelector(e):e,this.$element=$(this.element),this.options=t({},this.options),t(this.options,i),this._create()}for(var o,s=0,r="webkit moz ms o".split(" "),h=window.requestAnimationFrame,p=window.cancelAnimationFrame,a=0;a<r.length&&(!h||!p);a++)o=r[a],window.requestAnimationFrame=h||window[o+"RequestAnimationFrame"],window.cancelAnimationFrame=p||window[o+"CancelAnimationFrame"]||window[o+"CancelRequestAnimationFrame"];h&&p&&void 0!==h||(window.requestAnimationFrame=function(t){var e=(new Date).getTime(),i=Math.max(0,16-(e-s)),n=window.setTimeout((function(){t(e+i)}),i);return s=e+i,n},window.cancelAnimationFrame=function(t){window.clearTimeout(t)});var l=getComputedStyle?function(t){return getComputedStyle(t,null)}:function(t){return t.currentStyle},d=(r="Webkit Moz ms Ms O".split(" "),document.documentElement.style);window.getStyleProperty=function(t){if(t){if("string"==typeof d[t])return t;t=t.charAt(0).toUpperCase()+t.slice(1);for(var e,i=0,n=r.length;i<n;i++)if(e=r[i]+t,"string"==typeof d[e])return e}};var g=window.getStyleProperty("transform"),u=!!window.getStyleProperty("perspective");window.Draggables={isDragging:null},e.prototype.options={helperShowDistance:8},e.prototype._create=function(){this.position={},this.size={},this._getPosition(),this.startPoint={x:0,y:0},this.dragPoint={x:0,y:0},this.startPosition=t({},this.position),this.enable(),this.setHandles(),this.visible=!1},e.prototype.setHandles=function(){this.handles=this.options.handle?this.element.querySelectorAll(this.options.handle):[this.element],this.addListenersToHandles()},e.prototype.changeHandles=function(t){this.removeHandlerListeners(),this._unbindEvents(),this.handles=t?this.element.querySelectorAll(t):[this.element],this.addListenersToHandles()},e.prototype.addListenersToHandles=function(){for(var t,e=0,i=this.handles.length;e<i;e++)(t=this.handles[e]).addEventListener("mousedown",this),t.addEventListener("touchstart",this),window.navigator.pointerEnabled?(t.addEventListener("pointerdown",this),t.style.touchAction="none"):navigator.msMaxTouchPoints&&(t.addEventListener("MSPointerDown",this),t.style.msTouchAction="none")},e.prototype.removeHandlerListeners=function(){for(var t=0,e=this.handles.length;t<e;t++){var i=this.handles[t];i.removeEventListener("mousedown",this),i.removeEventListener("touchstart",this),i.removeEventListener("pointerdown",this),i.removeEventListener("MSPointerDown",this)}},e.prototype._getPosition=function(){var t=l(this.element);if("absolute"===t.position)this.position={x:parseInt(t.left,10),y:parseInt(t.top,10)};else{var e=this.element.getBoundingClientRect();this.position={x:e.left,y:e.top}}},e.prototype._getHelperSize=function(){var t=l(this.helper),e=parseInt(t.width,10),i=parseInt(t.height,10);this.size.width=isNaN(e)?0:e,this.size.height=isNaN(i)?0:i},e.prototype.handleEvent=function(t){var e="on"+t.type;this[e]&&this[e](t)},e.prototype.onmousedown=function(t){var e=t.button;e&&0!==e&&1!==e||this.dragStart(t,t)},e.prototype.ontouchstart=function(t){this.isDragging||this.dragStart(t,t.changedTouches[0])},e.prototype.onMSPointerDown=e.prototype.onpointerdown=function(t){this.isDragging||this.dragStart(t,t)};var c={mousedown:["mousemove","mouseup"],touchstart:["touchmove","touchend","touchcancel"],pointerdown:["pointermove","pointerup","pointercancel"],MSPointerDown:["MSPointerMove","MSPointerUp","MSPointerCancel"]};e.prototype.dragStart=function(t,e){if(null!==Draggables.isDragging){if(!Draggables.isDragging.parentNode||Draggables.isDragging.parentNode.contains(Draggables.isDragging))return;Draggables.isDragging=null}this.isEnabled&&(t.preventDefault(),this.options.helper&&(this.helper=null),this.isDragging=!0,Draggables.isDragging=this,this.isOver=!1,this.pointerIdentifier=void 0!==e.pointerId?e.pointerId:e.identifier,this._getPosition(),i(this.startPoint,e),this.startPosition.x=this.position.x,this.startPosition.y=this.position.y,!this.options.helper&&this.options.helperShowDistance&&(this.helper=this.element,this.helper.style.position="absolute",this._getHelperSize(),this.helper.style.zIndex=this.options.zIndex?this.options.zIndex.toString():this.helper.style.zIndex,this.setLeftTop()),this.dragPoint.x=0,this.dragPoint.y=0,this._bindEvents({events:c[t.type],node:t.preventDefault?window:document}),!this.options.helper&&this.options.helperShowDistance&&(this.$element.trigger("dragStart",[this]),$(".droppable").trigger("drag",[this]),this.animate()))},e.prototype._bindEvents=function(t){for(var e=0,i=t.events.length;e<i;e++)t.node.addEventListener(t.events[e],this);this._boundEvents=t},e.prototype._unbindEvents=function(){var t=this._boundEvents;if(t&&t.events){for(var e=0,i=t.events.length;e<i;e++)t.node.removeEventListener(t.events[e],this,!1);this._boundEvents=null}},e.prototype._getTouch=function(t){for(var e=0,i=t.changedTouches.length;e<i;e++)if(this.pointerIdentifier===t.changedTouches[e].identifier)return t.changedTouches[e]},e.prototype.onmousemove=function(t){this.dragMove(t,t)},e.prototype.ontouchmove=function(t){var e=this._getTouch(t);e&&this.dragMove(t,e)},e.prototype.onMSPointerMove=e.prototype.onpointermove=function(t){this.pointerIdentifier===t.pointerId&&this.dragMove(t,t)},e.prototype.dragMove=function(t,e){if(!this.isDragging)return!1;i(this.dragPoint,e);var n=this.dragPoint.x-this.startPoint.x,o=this.dragPoint.y-this.startPoint.y;this.options.helper&&!this.visible&&Math.sqrt(n*n+o*o)>this.options.helperShowDistance&&(this.helper=this.options.helper?this.options.helper():this.element,this.helper.style.position="absolute",this._getHelperSize(),this.position.x=this.startPosition.x,this.position.y=this.startPosition.y,this.setLeftTop(),this.helper.style.zIndex=this.options.zIndex?this.options.zIndex.toString():this.helper.style.zIndex,this.helper.style.display="block",this.visible=!0,this.$element.trigger("dragStart",[this]),$(".droppable").trigger("drag",[this]),this.animate()),this.position.x=this.startPosition.x+n,this.position.y=this.startPosition.y+o,this.dragPoint.x=n,this.dragPoint.y=o,this.$element.trigger("dragMove",[this])},e.prototype.onmouseup=function(t){this.dragEnd(t,t)},e.prototype.ontouchend=function(t){var e=this._getTouch(t);e&&this.dragEnd(t,e)},e.prototype.onMSPointerUp=e.prototype.onpointerup=function(t){this.pointerIdentifier===t.pointerId&&this.dragEnd(t,t)},e.prototype.dragEnd=function(t,e){return Draggables.isDragging=null,this.isDragging=!1,this.visible=!1,this.pointerIdentifier=null,this._unbindEvents(),!(!this.helper||"none"===this.helper.style.display)&&(g&&(this.helper.style[g]="",this.setLeftTop()),this.options.zIndex&&this.options.initialZIndex&&this.helper&&"none"!==this.helper.style.display&&(this.helper.style.zIndex=this.options.initialZIndex.toString()),this.options.removeHelper&&this.helper.parentNode.removeChild(this.helper),void this.$element.trigger("dragEnd",[this]))},e.prototype.onMSPointerCancel=e.prototype.onpointercancel=function(t){t.pointerId===this.pointerIdentifier&&this.dragEnd(t,t)},e.prototype.ontouchcancel=function(t){var e=this._getTouch(t);e&&this.dragEnd(t,e)},e.prototype.animate=function(){if(this.isDragging){this.positionDrag();var t=this;window.requestAnimationFrame((function(){t.animate()}))}};var f=u?function(t,e){return"translate3d( "+t+"px, "+e+"px, 0)"}:function(t,e){return"translate( "+t+"px, "+e+"px)"};e.prototype.setLeftTop=function(){this.helper.style.left=this.position.x+"px",this.helper.style.top=this.position.y+"px"},e.prototype.positionDrag=g?function(){this.helper.style[g]=f(this.dragPoint.x,this.dragPoint.y)}:e.prototype.setLeftTop,e.prototype.enable=function(){this.isEnabled=!0},e.prototype.disable=function(){this.isEnabled=!1,this.isDragging&&this.dragEnd()},e.prototype.destroy=function(){this.removeHandlerListeners(),this.$element.off()},window.Draggable=e,window.Droppables={isOverPtr:null},n.prototype.options={tolerance:"intersect"},n.prototype._create=function(){this.position={},this.size={},this._getPosition(),this._getSize(),this.isOver=!1,this.blocker=null,this._listen(),this.element.className+=" droppable",this.enabled=!0;var t=this;this.$element.on("resized",(function(e){t._getPosition(),t._getSize()}))},n.prototype._getPosition=function(){var t=l(this.element);if("absolute"===t.position)this.position={x:parseInt(t.left,10),y:parseInt(t.top,10)};else{var e=this.element.getBoundingClientRect();this.position={x:e.left,y:e.top}}},n.prototype._getSize=function(){var t=l(this.element),e=parseInt(t.width,10),i=parseInt(t.height,10);this.size.width=isNaN(e)?0:e,this.size.height=isNaN(i)?0:i},n.prototype._listen=function(){var t=this;this.dragEnd=function(e,i){n.prototype._onDragEnd.apply(t,[e,i])};var e=function(e,i){n.prototype._onDragMove.apply(t,[i,e])};this.$element.on("drag",(function(i,n){return void 0!==n&&void 0!==n.element&&void(t.enabled&&(null!==t.blocker&&t.blocker===n.helper&&(t.blocker=null),t._accept(n.element)&&(n.$element.on("dragMove",e),n.$element.one("dragEnd",t.dragEnd))))})),this.$element.on("dragEnd",(function(e,i){t.element===i.element&&t._getPosition()}))},n.prototype.startListeningToDrag=function(t){var e=this;t.$element.on("dragMove",(function(t,i){n.prototype._onDragMove.apply(e,[i])})),t.$element.one("dragEnd",this.dragEnd)},n.prototype._onDragMove=function(t){if(this.enabled){if(null!==Droppables.isOverPtr&&Droppables.isOverPtr!==this)return;this._checkIntersection(t)?this._over(t):this.isOver&&this._out(t)}else this.destroy()},n.prototype._onDragEnd=function(t,e){if(e.$element.off("dragMove"),null!==this.dragEnd&&e.$element.off("dragEnd",this.dragEnd),this.enabled){if(0==e.dragPoint.x&&0==e.dragPoint.y)return;this.isOver&&null===this.blocker&&this._drop(e)}else this.destroy()},n.prototype._accept=function(t){return!(this.$element.data("ptr")&&this.$element.data("ptr").draggable.isDragging||null===this.$element||this.element===t||this.options.acceptOne&&null!==this.blocker&&(this.element.parentNode.contains(this.blocker)&&this._checkIntersection($(this.blocker).data("ptr").draggable)||(this.blocker=null,0)))},n.prototype._over=function(t){this.isOver=!0,t.isOver=!0,Droppables.isOverPtr=this,this._trigger("droppableOver",t)},n.prototype._out=function(t){this.isOver=!1,t.isOver=!1,Droppables.isOverPtr=null,this.options.acceptOne&&(this.blocker=null),this._trigger("droppableOut",t)},n.prototype._drop=function(t){this.options.acceptOne&&(this.blocker=t.helper),Droppables.isOverPtr=null,this._trigger("droppableDrop",t)},n.prototype._trigger=function(t,e){null!==this.$element?this.$element.trigger(t,[e]):($(document).trigger("draggableDroppedFix",[e]),this.destroy())},n.prototype._checkIntersection=function(t){var e=this.position.x,i=this.position.y,n=e+this.size.width,o=i+this.size.height,s=t.size.width,r=t.size.height,h=t.position.x,p=h+s,a=t.position.y,l=a+r;return"touch"===this.options.tolerance?(a>=i&&a<=o||l>=i&&l<=o||a<i&&l>o)&&(h>=e&&h<=n||p>=e&&p<=n||h<e&&p>n):e<h+s/2&&p-s/2<n&&i<a+r/2&&l-r/2<o},n.prototype.destroy=function(){Droppables.isOverPtr===this&&(Droppables.isOverPtr=null),this.enabled=!1,null!==this.$element&&(this.$element.off("drag"),this.$element.off("dragMove"),this.$element.off("dragEnd"),this.$element.off("resized"),this.$element.off()),this.$element=null,this.dragEnd=null,this.blocker=null,this.element&&this.element.parentNode.removeChild(this.element),this.element=null},window.Droppable=n}(window),function(){this.templateEngine=function(t,e){var i="var p=[];with(obj){p.push('"+t.replace(/[\r\t\n]/g," ").replace(/'(?=[^%]*%>)/g,"\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g,"',$1,'").split("<%").join("');").split("%>").join("p.push('")+"');}return p.join('');",n=new Function("obj",i);return e?n(e):n}}();