Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sortable-dnd

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sortable-dnd - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

143

dist/sortable.js
/*!
* sortable-dnd v0.2.1
* sortable-dnd v0.2.2
* open source under the MIT license

@@ -202,30 +202,2 @@ * https://github.com/mfuu/sortable-dnd#readme

}
/**
* get scroll element
* @param {HTMLElement} el
* @param {Boolean} includeSelf whether to include the passed element
* @returns {HTMLElement} scroll element
*/
function getParentAutoScrollElement(el, includeSelf) {
// skip to window
if (!el || !el.getBoundingClientRect) return getWindowScrollingElement();
var elem = el;
var gotSelf = false;
do {
// we don't need to get elem css if it isn't even overflowing in the first place (performance)
if (elem.clientWidth < elem.scrollWidth || elem.clientHeight < elem.scrollHeight) {
var elemCSS = css(elem);
if (elem.clientWidth < elem.scrollWidth && (elemCSS.overflowX == 'auto' || elemCSS.overflowX == 'scroll') || elem.clientHeight < elem.scrollHeight && (elemCSS.overflowY == 'auto' || elemCSS.overflowY == 'scroll')) {
if (!elem.getBoundingClientRect || elem === document.body) return getWindowScrollingElement();
if (gotSelf || includeSelf) return elem;
gotSelf = true;
}
}
} while (elem = elem.parentNode);
return getWindowScrollingElement();
}
function getWindowScrollingElement() {

@@ -460,4 +432,10 @@ var scrollingElement = document.scrollingElement;

this.options = options;
this.x = 0;
this.y = 0;
this.diff = {
x: 0,
y: 0
};
this.position = {
x: 0,
y: 0
};
this.exist = false;

@@ -469,2 +447,3 @@ }

value: function init(el, rect) {
if (this.$el) this.$el.remove();
if (!el) return;

@@ -493,13 +472,10 @@ this.$el = el;

}, {
key: "get",
value: function get(key) {
return this[key];
key: "setPosition",
value: function setPosition(x, y) {
this.position = {
x: x - this.diff.x,
y: y - this.diff.y
};
}
}, {
key: "set",
value: function set(key, value) {
this[key] = value;
this[key] = value;
}
}, {
key: "setStyle",

@@ -519,2 +495,3 @@ value: function setStyle(style) {

value: function move(smooth) {
if (!this.$el) return;
var ghostAnimation = this.options.ghostAnimation;

@@ -528,3 +505,3 @@ if (smooth) setTransition(this.$el, "".concat(ghostAnimation, "ms"));else setTransition(this.$el, 'none'); // 将初始化放在 move 事件中,避免与鼠标点击事件冲突

setTransform(this.$el, "translate3d(".concat(this.x, "px, ").concat(this.y, "px, 0)"));
setTransform(this.$el, "translate3d(".concat(this.position.x, "px, ").concat(this.position.y, "px, 0)"));
if (this.$el.style.cursor !== 'move') this.$el.style.cursor = 'move';

@@ -538,4 +515,6 @@ }

if (rect) {
this.x = rect.left;
this.y = rect.top;
this.position = {
x: rect.left,
y: rect.top
};
this.move(true);

@@ -554,4 +533,10 @@ }

this.$el = null;
this.x = 0;
this.y = 0;
this.diff = {
x: 0,
y: 0
};
this.position = {
x: 0,
y: 0
};
this.exist = false;

@@ -645,7 +630,2 @@ }

}
if (this.nativeDraggable) {
on(this.$el, 'dragover', this);
on(this.$el, 'dragenter', this);
}
},

@@ -657,9 +637,4 @@ _unbindEventListener: function _unbindEventListener() {

off(this.$el, 'mousedown', this._onStart, supportPassive);
if (this.nativeDraggable) {
off(this.$el, 'dragover', this);
off(this.$el, 'dragenter', this);
}
},
_onMoveEvents: function _onMoveEvents(touch) {
_bindMoveEvents: function _bindMoveEvents(touch) {
var _this$options2 = this.options,

@@ -678,3 +653,3 @@ supportPointer = _this$options2.supportPointer,

},
_onUpEvents: function _onUpEvents() {
_bindUpEvents: function _bindUpEvents() {
var _this$options3 = this.options,

@@ -689,3 +664,3 @@ ownerDocument = _this$options3.ownerDocument,

},
_offMoveEvents: function _offMoveEvents() {
_unbindMoveEvents: function _unbindMoveEvents() {
var _this$options4 = this.options,

@@ -698,3 +673,3 @@ ownerDocument = _this$options4.ownerDocument,

},
_offUpEvents: function _offUpEvents() {
_unbindUpEvents: function _unbindUpEvents() {
var _this$options5 = this.options,

@@ -728,4 +703,2 @@ ownerDocument = _this$options5.ownerDocument,

this.options = options = Object.assign({}, options);
this.scrollEl = getParentAutoScrollElement(this.$el, true); // 获取页面滚动元素
this.dragEl = null; // 拖拽元素

@@ -739,7 +712,2 @@

this.calcXY = {
x: 0,
y: 0
}; // 记录拖拽移动时坐标
var defaults = {

@@ -872,4 +840,7 @@ delay: 0,

window.sortableDndOnDownState = true;
this.ghost.set('x', rect.left);
this.ghost.set('y', rect.top);
this.ghost.setPosition(rect.left, rect.top);
this.ghost.diff = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
this.differ.from = {

@@ -880,10 +851,6 @@ node: this.dragEl,

};
this.calcXY = {
x: e.clientX,
y: e.clientY
};
this._onMoveEvents(touch);
this._bindMoveEvents(touch);
this._onUpEvents(touch);
this._bindUpEvents(touch);
},

@@ -930,8 +897,3 @@ _onMove: function _onMove(

window.sortableDndOnMoveState = true;
this.ghost.set('x', this.ghost.x + clientX - this.calcXY.x);
this.ghost.set('y', this.ghost.y + clientY - this.calcXY.y);
this.calcXY = {
x: clientX,
y: clientY
};
this.ghost.setPosition(clientX, clientY);
this.ghost.move(); // 判断边界值

@@ -958,14 +920,3 @@

bottom = rect.bottom;
if (!el || index < 0 || top < 0) return; // 加上当前滚动距离
var _this$scrollEl = this.scrollEl,
scrollTop = _this$scrollEl.scrollTop,
scrollLeft = _this$scrollEl.scrollLeft;
var boundaryL = rc.left + scrollLeft;
var boundaryT = rc.top + scrollTop; // 如果目标元素超出当前可视区,不允许拖动
if (this.scrollEl !== this.$el && (rc.left < 0 || rc.top < 0)) {
if (rc.top < 0 && top < boundaryT || rc.left < 0 && left < boundaryL) return;
} else if (top < rc.top || left < rc.left) return;
if (!el || index < 0) return;
this.dropEl = el;

@@ -1007,5 +958,5 @@

evt) {
this._offMoveEvents();
this._unbindMoveEvents();
this._offUpEvents();
this._unbindUpEvents();

@@ -1048,6 +999,2 @@ clearTimeout(this.dragStartTimer);

this.differ.destroy();
this.calcXY = {
x: 0,
y: 0
};

@@ -1054,0 +1001,0 @@ this._removeWindowState();

@@ -1,1 +0,1 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Sortable=e()}(this,function(){"use strict";function p(t){return(p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function i(t,e,o){e&&n(t.prototype,e),o&&n(t,o),Object.defineProperty(t,"prototype",{writable:!1})}function r(t){return function(t){if(Array.isArray(t))return s(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return s(t,e);var o=Object.prototype.toString.call(t).slice(8,-1);return"Map"===(o="Object"===o&&t.constructor?t.constructor.name:o)||"Set"===o?Array.from(t):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?s(t,e):void 0}}(t)||function(){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 s(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,n=new Array(e);o<e;o++)n[o]=t[o];return n}function t(t){if("undefined"!=typeof window&&window.navigator)return!!navigator.userAgent.match(t)}var a=t(/(?:Trident.*rv[ :]?11\.|msie|iemobile|Windows Phone)/i),l=t(/Edge/i),h=t(/safari/i)&&!t(/chrome/i)&&!t(/android/i),e=t(/iP(ad|od|hone)/i),f=t(/chrome/i)&&t(/android/i),c={capture:!1,passive:!1},u=/\s+/g,d=["-webkit-transition","-moz-transition","-ms-transition","-o-transition","transition"],v=["-webkit-transform","-moz-transform","-ms-transform","-o-transform","transform"];function g(e,o){o?"none"===o?d.forEach(function(t){return x(e,t,"none")}):d.forEach(function(t){return x(e,t,"".concat(t.split("transition")[0],"transform ").concat(o))}):d.forEach(function(t){return x(e,t,"")})}function m(e,o){o?v.forEach(function(t){return x(e,t,"".concat(t.split("transform")[0]).concat(o))}):v.forEach(function(t){return x(e,t,"")})}function w(t,e,o,n){window.addEventListener?t.addEventListener(e,o,!(!n&&a)&&c):window.attachEvent&&t.attachEvent("on"+e,o)}function y(t,e,o,n){window.removeEventListener?t.removeEventListener(e,o,!(!n&&a)&&c):window.detachEvent&&t.detachEvent("on"+e,o)}function b(t){for(var e={top:0,left:0,height:0,width:0},o=(e.height=t.offsetHeight,e.width=t.offsetWidth,e.top=t.offsetTop,e.left=t.offsetLeft,t.offsetParent);null!==o;)e.top+=o.offsetTop,e.left+=o.offsetLeft,o=o.offsetParent;return e}function E(){var t=document.scrollingElement;return!t||t.contains(document.body)?document:t}function S(t){var e;if(t.getBoundingClientRect||t===window)return e={top:0,left:0,bottom:0,right:0,height:0,width:0},t!==window&&t.parentNode&&t!==E()?(t=t.getBoundingClientRect(),e.top=t.top,e.left=t.left,e.bottom=t.bottom,e.right=t.right,e.height=t.height,e.width=t.width):(e.top=0,e.left=0,e.bottom=window.innerHeight,e.right=window.innerWidth,e.height=window.innerHeight,e.width=window.innerWidth),e}function D(t,e,o){var n=r(Array.from(t.children)),t=n.indexOf(e);if(-1<t)return o?n[t]:{index:t,el:n[t],rect:S(n[t]),offset:b(n[t])};for(var i=0;i<n.length;i++)if(function(t,e){var o;if(t&&e)for(o=t.parentNode;o;){if(e===o)return 1;o=o.parentNode}return}(e,n[i]))return o?n[i]:{index:i,el:n[i],rect:S(n[i]),offset:b(n[i])};return o?null:{index:-1,el:null,rect:{},offset:{}}}function _(t,e,o){var n;t&&e&&(t.classList?t.classList[o?"add":"remove"](e):(n=(" "+t.className+" ").replace(u," ").replace(" "+e+" "," "),t.className=(n+(o?" "+e:"")).replace(u," ")))}function x(t,e,o){var n=t&&t.style;if(n){if(void 0===o)return document.defaultView&&document.defaultView.getComputedStyle?o=document.defaultView.getComputedStyle(t,""):t.currentStyle&&(o=t.currentStyle),void 0===e?o:o[e];n[e=e in n||-1!==e.indexOf("webkit")?e:"-webkit-"+e]=o+("string"==typeof o?"":"px")}}var $=function(){function t(){o(this,t),this.from={node:null,rect:{},offset:{}},this.to={node:null,rect:{},offset:{}}}return i(t,[{key:"get",value:function(t){return this[t]}},{key:"set",value:function(t,e){this[t]=e}},{key:"destroy",value:function(){this.from={node:null,rect:{},offset:{}},this.to={node:null,rect:{},offset:{}}}}]),t}(),M=function(){function e(t){o(this,e),this.options=t,this.x=0,this.y=0,this.exist=!1}return i(e,[{key:"init",value:function(t,e){var o,n;t&&(this.$el=t,o=(t=this.options).ghostClass,t=void 0===(t=t.ghostStyle)?{}:t,n=e.width,e=e.height,this.$el.class=o,this.$el.style.width=n+"px",this.$el.style.height=e+"px",this.$el.style.position="fixed",this.$el.style.left=0,this.$el.style.top=0,this.$el.style.zIndex=1e5,this.$el.style.opacity=.8,this.$el.style.pointerEvents="none",this.$el.style.cursor="move",g(this.$el,"none"),m(this.$el,"translate3d(0px, 0px, 0px)"),this.setStyle(t))}},{key:"get",value:function(t){return this[t]}},{key:"set",value:function(t,e){this[t]=e,this[t]=e}},{key:"setStyle",value:function(t){for(var e in t)x(this.$el,e,t[e])}},{key:"rect",value:function(){return S(this.$el)}},{key:"move",value:function(t){var e=this.options.ghostAnimation;g(this.$el,t?"".concat(e,"ms"):"none"),this.exist||(document.body.appendChild(this.$el),this.exist=!0),m(this.$el,"translate3d(".concat(this.x,"px, ").concat(this.y,"px, 0)")),"move"!==this.$el.style.cursor&&(this.$el.style.cursor="move")}},{key:"destroy",value:function(t){var e=this,t=(t&&(this.x=t.left,this.y=t.top,this.move(!0)),this.options.ghostAnimation);t?setTimeout(function(){return e.clear()},t):this.clear()}},{key:"clear",value:function(){this.$el&&this.$el.remove(),this.$el=null,this.x=0,this.y=0,this.exist=!1}}]),e}();function O(){var i=[];return{captureAnimationState:function(){var t=r(Array.from(this.$el.children)),e=(o=t,n=this.dragEl,e=this.dropEl,n=o.indexOf(n),o=o.indexOf(e),n<o?{start:n,end:o}:{start:o,end:n}),o=e.start,n=e.end;i.length=0,t.slice(o,n+1).forEach(function(t){i.push({target:t,rect:S(t)})})},animateRange:function(){var o=this;i.forEach(function(t){var e=t.target,t=t.rect;o.animate(e,t,o.animation)})},animate:function(t,e){var o=2<arguments.length&&void 0!==arguments[2]?arguments[2]:150,n=S(t),i=e.left-n.left,e=e.top-n.top;g(t,"none"),m(t,"translate3d(".concat(i,"px, ").concat(e,"px, 0)")),t.offsetLeft,g(t,"".concat(o,"ms")),m(t,"translate3d(0px, 0px, 0px)"),clearTimeout(t.animated),t.animated=setTimeout(function(){g(t,""),m(t,""),t.animated=null},o)}}}var P="undefined"!=typeof document&&!f&&!e&&"draggable"in document.createElement("div");function T(t,e){if(!t||!t.nodeType||1!==t.nodeType)throw"Sortable: `el` must be an HTMLElement, not ".concat({}.toString.call(t));this.$el=t,this.options=e=Object.assign({},e),this.scrollEl=function(t,e){if(!t||!t.getBoundingClientRect)return E();var o=t,n=!1;do{if(o.clientWidth<o.scrollWidth||o.clientHeight<o.scrollHeight){var i=x(o);if(o.clientWidth<o.scrollWidth&&("auto"==i.overflowX||"scroll"==i.overflowX)||o.clientHeight<o.scrollHeight&&("auto"==i.overflowY||"scroll"==i.overflowY)){if(!o.getBoundingClientRect||o===document.body)return E();if(n||e)return o;n=!0}}}while(o=o.parentNode);return E()}(this.$el,!0),this.dragEl=null,this.dropEl=null,this.differ=null,this.ghost=null;var o,n,i={delay:0,delayOnTouchOnly:!(this.calcXY={x:0,y:0}),disabled:!1,animation:150,ghostAnimation:0,ghostClass:"",ghostStyle:{},chosenClass:"",draggable:void 0,dragging:void 0,onDrag:void 0,onMove:void 0,onDrop:void 0,onChange:void 0,forceFallback:!1,stopPropagation:!1,supportPassive:(o=!1,document.addEventListener("checkIfSupportPassive",null,{get passive(){return o=!0}}),o),supportPointer:"PointerEvent"in window&&!h,supportTouch:"ontouchstart"in window,ownerDocument:this.$el.ownerDocument};for(n in i)n in this.options||(this.options[n]=i[n]);this.nativeDraggable=!this.options.forceFallback&&P,this.differ=new $,this.ghost=new M(this.options),Object.assign(this,O(),{_bindEventListener:function(){this._onStart=this._onStart.bind(this),this._onMove=this._onMove.bind(this),this._onDrop=this._onDrop.bind(this);var t=this.options,e=t.supportPointer,o=t.supportTouch,t=t.supportPassive;w(this.$el,e?"pointerdown":o?"touchstart":"mousedown",this._onStart,t),this.nativeDraggable&&(w(this.$el,"dragover",this),w(this.$el,"dragenter",this))},_unbindEventListener:function(){var t=this.options.supportPassive;y(this.$el,"pointerdown",this._onStart,t),y(this.$el,"touchstart",this._onStart,t),y(this.$el,"mousedown",this._onStart,t),this.nativeDraggable&&(y(this.$el,"dragover",this),y(this.$el,"dragenter",this))},_onMoveEvents:function(t){var e=this.options,o=e.supportPointer,n=e.ownerDocument,e=e.supportPassive;w(n,o?"pointermove":t?"touchmove":"mousemove",this._onMove,e)},_onUpEvents:function(){var t=this.options,e=t.ownerDocument,t=t.supportPassive;w(e,"pointerup",this._onDrop,t),w(e,"pointercancel",this._onDrop,t),w(e,"touchend",this._onDrop,t),w(e,"touchcancel",this._onDrop,t),w(e,"mouseup",this._onDrop,t)},_offMoveEvents:function(){var t=this.options,e=t.ownerDocument,t=t.supportPassive;y(e,"pointermove",this._onMove,t),y(e,"touchmove",this._onMove,t),y(e,"mousemove",this._onMove,t)},_offUpEvents:function(){var t=this.options,e=t.ownerDocument,t=t.supportPassive;y(e,"pointerup",this._onDrop,t),y(e,"pointercancel",this._onDrop,t),y(e,"touchend",this._onDrop,t),y(e,"touchcancel",this._onDrop,t),y(e,"mouseup",this._onDrop,t)}}),this._bindEventListener(),this._handleDestroy()}return T.prototype={constructor:T,destroy:function(){this._unbindEventListener(),this._resetState()},_onStart:function(t){var e=this.options,o=e.delay,n=e.disabled,i=e.stopPropagation,e=e.delayOnTouchOnly;if(!(/mousedown|pointerdown/.test(t.type)&&0!==t.button||n)){var n=t.touches&&t.touches[0]||t.pointerType&&"touch"===t.pointerType&&t,r=n||t;if(this.nativeDraggable||!h||!r.target||"SELECT"!==r.target.tagName.toUpperCase()){if(r.target===this.$el)return!0;i&&t.stopPropagation(),!o||e&&!n||this.nativeDraggable&&(l||a)?this._onDrag(r,n):this.dragStartTimer=setTimeout(this._onDrag(r,n),o)}}},_onDrag:function(t,e){var o=this.options,n=o.draggable,o=o.dragging;if("function"==typeof n){if(!n(t))return!0}else if("string"==typeof n){if(!function(t,e){if(e&&(">"===e[0]&&(e=e.substring(1)),t))try{if(t.matches)return t.matches(e);if(t.msMatchesSelector)return t.msMatchesSelector(e);if(t.webkitMatchesSelector)return t.webkitMatchesSelector(e)}catch(t){return}}(t.target,n))return!0}else if(void 0!==n)throw new Error('draggable expected "function" or "string" but received "'.concat(p(n),'"'));try{document.selection?setTimeout(function(){document.selection.empty()},0):window.getSelection().removeAllRanges()}catch(t){throw new Error(t)}if(o){if("function"!=typeof o)throw new Error('dragging expected "function" or "string" but received "'.concat(p(o),'"'));this.dragEl=o(t)}else this.dragEl=D(this.$el,t.target,!0);if(!this.dragEl||this.dragEl.animated)return!0;n=D(this.$el,this.dragEl),o=n.rect,n=n.offset;window.sortableDndOnDownState=!0,this.ghost.set("x",o.left),this.ghost.set("y",o.top),this.differ.from={node:this.dragEl,rect:o,offset:n},this.calcXY={x:t.clientX,y:t.clientY},this._onMoveEvents(e),this._onUpEvents(e)},_onMove:function(t){void 0!==t.preventDefault&&t.preventDefault();var e=this.options,o=e.chosenClass,n=e.stopPropagation,i=e.onMove,e=e.onDrag,n=(n&&t.stopPropagation(),t.touches&&t.touches[0]),r=n||t,s=r.clientX,a=r.clientY,n=n?document.elementFromPoint(s,a):r.target;if(!this.ghost.$el&&(this.ghost.init(this.dragEl.cloneNode(!0),this.differ.from.rect),void 0!==e)){if("function"!=typeof e)throw new Error('onDrag expected "function" but received "'.concat(p(e),'"'));e(this.dragEl,r,t)}if(void 0!==i){if("function"!=typeof i)throw new Error('onMove expected "function" but received "'.concat(p(i),'"'));i(this.differ.from,this.ghost.$el,r,t)}if(_(this.dragEl,o,!0),this.ghost.move(),window.sortableDndOnDownState&&!(s<0||a<0)){window.sortableDndOnMoveState=!0,this.ghost.set("x",this.ghost.x+s-this.calcXY.x),this.ghost.set("y",this.ghost.y+a-this.calcXY.y),this.calcXY={x:s,y:a},this.ghost.move();e=S(this.$el);if(s<e.left||s>e.right||a<e.top||a>e.bottom)this.ghost.setStyle({cursor:"not-allowed"});else{var i=D(this.$el,n),o=i.index,n=i.el,l=i.rect,i=i.offset,h=l.left,f=l.right,c=l.top,u=l.bottom;if(!(!n||o<0||c<0)){var o=this.scrollEl,d=o.scrollTop,o=o.scrollLeft,o=e.left+o,d=e.top+d;if(this.scrollEl!==this.$el&&(e.left<0||e.top<0)){if(e.top<0&&c<d||e.left<0&&h<o)return}else if(c<e.top||h<e.left)return;if(this.dropEl=n,h<s&&s<f&&c<a&&a<u&&n!==this.dragEl&&(this.differ.to={node:this.dropEl,rect:l,offset:i},!n.animated)){this.captureAnimationState();d=this.options.onChange,o=b(this.dragEl);if(void 0!==d){if("function"!=typeof d)throw new Error('onChange expected "function" but received "'.concat(p(d),'"'));d(this.differ.from,this.differ.to,r,t)}o.top<i.top||o.left<i.left?this.$el.insertBefore(this.dragEl,n.nextElementSibling):this.$el.insertBefore(this.dragEl,n),this.animateRange()}}}}},_onDrop:function(t){this._offMoveEvents(),this._offUpEvents(),clearTimeout(this.dragStartTimer);var e=this.options,o=e.onDrop,n=e.chosenClass;if(e.stopPropagation&&t.stopPropagation(),_(this.dragEl,n,!1),window.sortableDndOnDownState&&window.sortableDndOnMoveState){this.differ.to.offset=b(this.dragEl),this.differ.to.rect=S(this.dragEl);e=this.differ,n=e.from,e=e.to,n=n.offset.top!==e.offset.top||n.offset.left!==e.offset.left;if(void 0!==o){if("function"!=typeof o)throw new Error('onDrop expected "function" but received "'.concat(p(o),'"'));o(n,t)}this.ghost.destroy(S(this.dragEl))}this.differ.destroy(),this._removeWindowState()},_resetState:function(){this.dragEl=null,this.dropEl=null,this.ghost.destroy(),this.differ.destroy(),this.calcXY={x:0,y:0},this._removeWindowState()},_removeWindowState:function(){window.sortableDndOnDownState=null,window.sortableDndOnMoveState=null,window.sortableDndAnimationEnd=null,delete window.sortableDndOnDownState,delete window.sortableDndOnMoveState,delete window.sortableDndAnimationEnd},_handleDestroy:function(){var t=this,e=null,o=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver;if(o){var n=this.options.ownerDocument;if(!n)return;(e=new o(function(){n.body.contains(t.$el)||(e.disconnect(),e=null,t._unbindEventListener(),t._resetState())})).observe(this.$el.parentNode,{childList:!0,attributes:!1,subtree:!1})}window.onbeforeunload=function(){e&&e.disconnect(),e=null,t._unbindEventListener(),t._resetState()}}},T});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Sortable=e()}(this,function(){"use strict";function d(t){return(d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function i(t,e,o){e&&n(t.prototype,e),o&&n(t,o),Object.defineProperty(t,"prototype",{writable:!1})}function r(t){return function(t){if(Array.isArray(t))return s(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return s(t,e);var o=Object.prototype.toString.call(t).slice(8,-1);return"Map"===(o="Object"===o&&t.constructor?t.constructor.name:o)||"Set"===o?Array.from(t):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?s(t,e):void 0}}(t)||function(){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 s(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,n=new Array(e);o<e;o++)n[o]=t[o];return n}function t(t){if("undefined"!=typeof window&&window.navigator)return!!navigator.userAgent.match(t)}var a=t(/(?:Trident.*rv[ :]?11\.|msie|iemobile|Windows Phone)/i),l=t(/Edge/i),f=t(/safari/i)&&!t(/chrome/i)&&!t(/android/i),e=t(/iP(ad|od|hone)/i),h=t(/chrome/i)&&t(/android/i),c={capture:!1,passive:!1},u=/\s+/g,p=["-webkit-transition","-moz-transition","-ms-transition","-o-transition","transition"],v=["-webkit-transform","-moz-transform","-ms-transform","-o-transform","transform"];function m(e,o){o?"none"===o?p.forEach(function(t){return D(e,t,"none")}):p.forEach(function(t){return D(e,t,"".concat(t.split("transition")[0],"transform ").concat(o))}):p.forEach(function(t){return D(e,t,"")})}function g(e,o){o?v.forEach(function(t){return D(e,t,"".concat(t.split("transform")[0]).concat(o))}):v.forEach(function(t){return D(e,t,"")})}function w(t,e,o,n){window.addEventListener?t.addEventListener(e,o,!(!n&&a)&&c):window.attachEvent&&t.attachEvent("on"+e,o)}function y(t,e,o,n){window.removeEventListener?t.removeEventListener(e,o,!(!n&&a)&&c):window.detachEvent&&t.detachEvent("on"+e,o)}function b(t){for(var e={top:0,left:0,height:0,width:0},o=(e.height=t.offsetHeight,e.width=t.offsetWidth,e.top=t.offsetTop,e.left=t.offsetLeft,t.offsetParent);null!==o;)e.top+=o.offsetTop,e.left+=o.offsetLeft,o=o.offsetParent;return e}function E(t){var e,o;if(t.getBoundingClientRect||t===window)return e={top:0,left:0,bottom:0,right:0,height:0,width:0},t!==window&&t.parentNode&&t!==(!(o=document.scrollingElement)||o.contains(document.body)?document:o)?(o=t.getBoundingClientRect(),e.top=o.top,e.left=o.left,e.bottom=o.bottom,e.right=o.right,e.height=o.height,e.width=o.width):(e.top=0,e.left=0,e.bottom=window.innerHeight,e.right=window.innerWidth,e.height=window.innerHeight,e.width=window.innerWidth),e}function S(t,e,o){var n=r(Array.from(t.children)),t=n.indexOf(e);if(-1<t)return o?n[t]:{index:t,el:n[t],rect:E(n[t]),offset:b(n[t])};for(var i=0;i<n.length;i++)if(function(t,e){var o;if(t&&e)for(o=t.parentNode;o;){if(e===o)return 1;o=o.parentNode}return}(e,n[i]))return o?n[i]:{index:i,el:n[i],rect:E(n[i]),offset:b(n[i])};return o?null:{index:-1,el:null,rect:{},offset:{}}}function _(t,e,o){var n;t&&e&&(t.classList?t.classList[o?"add":"remove"](e):(n=(" "+t.className+" ").replace(u," ").replace(" "+e+" "," "),t.className=(n+(o?" "+e:"")).replace(u," ")))}function D(t,e,o){var n=t&&t.style;if(n){if(void 0===o)return document.defaultView&&document.defaultView.getComputedStyle?o=document.defaultView.getComputedStyle(t,""):t.currentStyle&&(o=t.currentStyle),void 0===e?o:o[e];n[e=e in n||-1!==e.indexOf("webkit")?e:"-webkit-"+e]=o+("string"==typeof o?"":"px")}}var x=function(){function t(){o(this,t),this.from={node:null,rect:{},offset:{}},this.to={node:null,rect:{},offset:{}}}return i(t,[{key:"get",value:function(t){return this[t]}},{key:"set",value:function(t,e){this[t]=e}},{key:"destroy",value:function(){this.from={node:null,rect:{},offset:{}},this.to={node:null,rect:{},offset:{}}}}]),t}(),$=function(){function e(t){o(this,e),this.options=t,this.diff={x:0,y:0},this.position={x:0,y:0},this.exist=!1}return i(e,[{key:"init",value:function(t,e){var o,n;this.$el&&this.$el.remove(),t&&(this.$el=t,o=(t=this.options).ghostClass,t=void 0===(t=t.ghostStyle)?{}:t,n=e.width,e=e.height,this.$el.class=o,this.$el.style.width=n+"px",this.$el.style.height=e+"px",this.$el.style.position="fixed",this.$el.style.left=0,this.$el.style.top=0,this.$el.style.zIndex=1e5,this.$el.style.opacity=.8,this.$el.style.pointerEvents="none",this.$el.style.cursor="move",m(this.$el,"none"),g(this.$el,"translate3d(0px, 0px, 0px)"),this.setStyle(t))}},{key:"setPosition",value:function(t,e){this.position={x:t-this.diff.x,y:e-this.diff.y}}},{key:"setStyle",value:function(t){for(var e in t)D(this.$el,e,t[e])}},{key:"rect",value:function(){return E(this.$el)}},{key:"move",value:function(t){var e;this.$el&&(e=this.options.ghostAnimation,m(this.$el,t?"".concat(e,"ms"):"none"),this.exist||(document.body.appendChild(this.$el),this.exist=!0),g(this.$el,"translate3d(".concat(this.position.x,"px, ").concat(this.position.y,"px, 0)")),"move"!==this.$el.style.cursor&&(this.$el.style.cursor="move"))}},{key:"destroy",value:function(t){var e=this,t=(t&&(this.position={x:t.left,y:t.top},this.move(!0)),this.options.ghostAnimation);t?setTimeout(function(){return e.clear()},t):this.clear()}},{key:"clear",value:function(){this.$el&&this.$el.remove(),this.$el=null,this.diff={x:0,y:0},this.position={x:0,y:0},this.exist=!1}}]),e}();function P(){var i=[];return{captureAnimationState:function(){var t=r(Array.from(this.$el.children)),e=(o=t,n=this.dragEl,e=this.dropEl,n=o.indexOf(n),o=o.indexOf(e),n<o?{start:n,end:o}:{start:o,end:n}),o=e.start,n=e.end;i.length=0,t.slice(o,n+1).forEach(function(t){i.push({target:t,rect:E(t)})})},animateRange:function(){var o=this;i.forEach(function(t){var e=t.target,t=t.rect;o.animate(e,t,o.animation)})},animate:function(t,e){var o=2<arguments.length&&void 0!==arguments[2]?arguments[2]:150,n=E(t),i=e.left-n.left,e=e.top-n.top;m(t,"none"),g(t,"translate3d(".concat(i,"px, ").concat(e,"px, 0)")),t.offsetLeft,m(t,"".concat(o,"ms")),g(t,"translate3d(0px, 0px, 0px)"),clearTimeout(t.animated),t.animated=setTimeout(function(){m(t,""),g(t,""),t.animated=null},o)}}}var M="undefined"!=typeof document&&!h&&!e&&"draggable"in document.createElement("div");function O(t,e){if(!t||!t.nodeType||1!==t.nodeType)throw"Sortable: `el` must be an HTMLElement, not ".concat({}.toString.call(t));this.$el=t,this.options=e=Object.assign({},e),this.dragEl=null,this.dropEl=null,this.differ=null,this.ghost=null;var o,n,i={delay:0,delayOnTouchOnly:!1,disabled:!1,animation:150,ghostAnimation:0,ghostClass:"",ghostStyle:{},chosenClass:"",draggable:void 0,dragging:void 0,onDrag:void 0,onMove:void 0,onDrop:void 0,onChange:void 0,forceFallback:!1,stopPropagation:!1,supportPassive:(o=!1,document.addEventListener("checkIfSupportPassive",null,{get passive(){return o=!0}}),o),supportPointer:"PointerEvent"in window&&!f,supportTouch:"ontouchstart"in window,ownerDocument:this.$el.ownerDocument};for(n in i)n in this.options||(this.options[n]=i[n]);this.nativeDraggable=!this.options.forceFallback&&M,this.differ=new x,this.ghost=new $(this.options),Object.assign(this,P(),{_bindEventListener:function(){this._onStart=this._onStart.bind(this),this._onMove=this._onMove.bind(this),this._onDrop=this._onDrop.bind(this);var t=this.options,e=t.supportPointer,o=t.supportTouch,t=t.supportPassive;w(this.$el,e?"pointerdown":o?"touchstart":"mousedown",this._onStart,t)},_unbindEventListener:function(){var t=this.options.supportPassive;y(this.$el,"pointerdown",this._onStart,t),y(this.$el,"touchstart",this._onStart,t),y(this.$el,"mousedown",this._onStart,t)},_bindMoveEvents:function(t){var e=this.options,o=e.supportPointer,n=e.ownerDocument,e=e.supportPassive;w(n,o?"pointermove":t?"touchmove":"mousemove",this._onMove,e)},_bindUpEvents:function(){var t=this.options,e=t.ownerDocument,t=t.supportPassive;w(e,"pointerup",this._onDrop,t),w(e,"pointercancel",this._onDrop,t),w(e,"touchend",this._onDrop,t),w(e,"touchcancel",this._onDrop,t),w(e,"mouseup",this._onDrop,t)},_unbindMoveEvents:function(){var t=this.options,e=t.ownerDocument,t=t.supportPassive;y(e,"pointermove",this._onMove,t),y(e,"touchmove",this._onMove,t),y(e,"mousemove",this._onMove,t)},_unbindUpEvents:function(){var t=this.options,e=t.ownerDocument,t=t.supportPassive;y(e,"pointerup",this._onDrop,t),y(e,"pointercancel",this._onDrop,t),y(e,"touchend",this._onDrop,t),y(e,"touchcancel",this._onDrop,t),y(e,"mouseup",this._onDrop,t)}}),this._bindEventListener(),this._handleDestroy()}return O.prototype={constructor:O,destroy:function(){this._unbindEventListener(),this._resetState()},_onStart:function(t){var e=this.options,o=e.delay,n=e.disabled,i=e.stopPropagation,e=e.delayOnTouchOnly;if(!(/mousedown|pointerdown/.test(t.type)&&0!==t.button||n)){var n=t.touches&&t.touches[0]||t.pointerType&&"touch"===t.pointerType&&t,r=n||t;if(this.nativeDraggable||!f||!r.target||"SELECT"!==r.target.tagName.toUpperCase()){if(r.target===this.$el)return!0;i&&t.stopPropagation(),!o||e&&!n||this.nativeDraggable&&(l||a)?this._onDrag(r,n):this.dragStartTimer=setTimeout(this._onDrag(r,n),o)}}},_onDrag:function(t,e){var o=this.options,n=o.draggable,o=o.dragging;if("function"==typeof n){if(!n(t))return!0}else if("string"==typeof n){if(!function(t,e){if(e&&(">"===e[0]&&(e=e.substring(1)),t))try{if(t.matches)return t.matches(e);if(t.msMatchesSelector)return t.msMatchesSelector(e);if(t.webkitMatchesSelector)return t.webkitMatchesSelector(e)}catch(t){return}}(t.target,n))return!0}else if(void 0!==n)throw new Error('draggable expected "function" or "string" but received "'.concat(d(n),'"'));try{document.selection?setTimeout(function(){document.selection.empty()},0):window.getSelection().removeAllRanges()}catch(t){throw new Error(t)}if(o){if("function"!=typeof o)throw new Error('dragging expected "function" or "string" but received "'.concat(d(o),'"'));this.dragEl=o(t)}else this.dragEl=S(this.$el,t.target,!0);if(!this.dragEl||this.dragEl.animated)return!0;n=S(this.$el,this.dragEl),o=n.rect,n=n.offset;window.sortableDndOnDownState=!0,this.ghost.setPosition(o.left,o.top),this.ghost.diff={x:t.clientX-o.left,y:t.clientY-o.top},this.differ.from={node:this.dragEl,rect:o,offset:n},this._bindMoveEvents(e),this._bindUpEvents(e)},_onMove:function(t){void 0!==t.preventDefault&&t.preventDefault();var e=this.options,o=e.chosenClass,n=e.stopPropagation,i=e.onMove,e=e.onDrag,n=(n&&t.stopPropagation(),t.touches&&t.touches[0]),r=n||t,s=r.clientX,a=r.clientY,n=n?document.elementFromPoint(s,a):r.target;if(!this.ghost.$el&&(this.ghost.init(this.dragEl.cloneNode(!0),this.differ.from.rect),void 0!==e)){if("function"!=typeof e)throw new Error('onDrag expected "function" but received "'.concat(d(e),'"'));e(this.dragEl,r,t)}if(void 0!==i){if("function"!=typeof i)throw new Error('onMove expected "function" but received "'.concat(d(i),'"'));i(this.differ.from,this.ghost.$el,r,t)}if(_(this.dragEl,o,!0),this.ghost.move(),window.sortableDndOnDownState&&!(s<0||a<0)){window.sortableDndOnMoveState=!0,this.ghost.setPosition(s,a),this.ghost.move();e=E(this.$el);if(s<e.left||s>e.right||a<e.top||a>e.bottom)this.ghost.setStyle({cursor:"not-allowed"});else{var i=S(this.$el,n),o=i.index,e=i.el,n=i.rect,i=i.offset,l=n.left,f=n.right,h=n.top,c=n.bottom;if(e&&!(o<0)&&(this.dropEl=e,l<s&&s<f&&h<a&&a<c&&e!==this.dragEl&&(this.differ.to={node:this.dropEl,rect:n,offset:i},!e.animated))){this.captureAnimationState();o=this.options.onChange,l=b(this.dragEl);if(void 0!==o){if("function"!=typeof o)throw new Error('onChange expected "function" but received "'.concat(d(o),'"'));o(this.differ.from,this.differ.to,r,t)}l.top<i.top||l.left<i.left?this.$el.insertBefore(this.dragEl,e.nextElementSibling):this.$el.insertBefore(this.dragEl,e),this.animateRange()}}}},_onDrop:function(t){this._unbindMoveEvents(),this._unbindUpEvents(),clearTimeout(this.dragStartTimer);var e=this.options,o=e.onDrop,n=e.chosenClass;if(e.stopPropagation&&t.stopPropagation(),_(this.dragEl,n,!1),window.sortableDndOnDownState&&window.sortableDndOnMoveState){this.differ.to.offset=b(this.dragEl),this.differ.to.rect=E(this.dragEl);e=this.differ,n=e.from,e=e.to,n=n.offset.top!==e.offset.top||n.offset.left!==e.offset.left;if(void 0!==o){if("function"!=typeof o)throw new Error('onDrop expected "function" but received "'.concat(d(o),'"'));o(n,t)}this.ghost.destroy(E(this.dragEl))}this.differ.destroy(),this._removeWindowState()},_resetState:function(){this.dragEl=null,this.dropEl=null,this.ghost.destroy(),this.differ.destroy(),this._removeWindowState()},_removeWindowState:function(){window.sortableDndOnDownState=null,window.sortableDndOnMoveState=null,window.sortableDndAnimationEnd=null,delete window.sortableDndOnDownState,delete window.sortableDndOnMoveState,delete window.sortableDndAnimationEnd},_handleDestroy:function(){var t=this,e=null,o=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver;if(o){var n=this.options.ownerDocument;if(!n)return;(e=new o(function(){n.body.contains(t.$el)||(e.disconnect(),e=null,t._unbindEventListener(),t._resetState())})).observe(this.$el.parentNode,{childList:!0,attributes:!1,subtree:!1})}window.onbeforeunload=function(){e&&e.disconnect(),e=null,t._unbindEventListener(),t._resetState()}}},O});
{
"name": "sortable-dnd",
"version": "0.2.1",
"version": "0.2.2",
"description": "JS Library for Drag and Drop, supports Sortable and Draggable",
"main": "dist/sortable.js",
"main": "dist/sortable.min.js",
"files": [

@@ -7,0 +7,0 @@ "src",

@@ -18,7 +18,2 @@ import { on, off, debounce } from './utils.js'

}
if (this.nativeDraggable) {
on(this.$el, 'dragover', this)
on(this.$el, 'dragenter', this)
}
},

@@ -31,10 +26,5 @@

off(this.$el, 'mousedown', this._onStart, supportPassive)
if (this.nativeDraggable) {
off(this.$el, 'dragover', this)
off(this.$el, 'dragenter', this)
}
},
_onMoveEvents(touch) {
_bindMoveEvents(touch) {
const { supportPointer, ownerDocument, supportPassive } = this.options

@@ -50,3 +40,3 @@ if (supportPointer) {

_onUpEvents() {
_bindUpEvents() {
const { ownerDocument, supportPassive } = this.options

@@ -60,3 +50,3 @@ on(ownerDocument, 'pointerup', this._onDrop, supportPassive)

_offMoveEvents() {
_unbindMoveEvents() {
const { ownerDocument, supportPassive } = this.options

@@ -68,3 +58,3 @@ off(ownerDocument, 'pointermove', this._onMove, supportPassive)

_offUpEvents() {
_unbindUpEvents() {
const { ownerDocument, supportPassive } = this.options

@@ -71,0 +61,0 @@ off(ownerDocument, 'pointerup', this._onDrop, supportPassive)

@@ -32,4 +32,5 @@ import { css, getRect, setTransition, setTransform } from './utils.js'

this.options = options
this.x = 0
this.y = 0
this.diff = { x: 0, y: 0 }
this.position = { x: 0, y: 0 }
this.exist = false

@@ -39,2 +40,3 @@ }

init(el, rect) {
if (this.$el) this.$el.remove()
if (!el) return

@@ -62,11 +64,9 @@ this.$el = el

get (key) {
return this[key]
setPosition(x, y) {
this.position = {
x: x - this.diff.x,
y: y - this.diff.y
}
}
set (key, value) {
this[key] = value
this[key] = value
}
setStyle(style) {

@@ -83,2 +83,3 @@ for (const key in style) {

move(smooth) {
if (!this.$el) return
const { ghostAnimation } = this.options

@@ -92,3 +93,3 @@ if (smooth) setTransition(this.$el, `${ghostAnimation}ms`)

}
setTransform(this.$el, `translate3d(${this.x}px, ${this.y}px, 0)`)
setTransform(this.$el, `translate3d(${this.position.x}px, ${this.position.y}px, 0)`)
if (this.$el.style.cursor !== 'move') this.$el.style.cursor = 'move'

@@ -99,4 +100,3 @@ }

if (rect) {
this.x = rect.left
this.y = rect.top
this.position = { x: rect.left, y: rect.top }
this.move(true)

@@ -111,6 +111,6 @@ }

this.$el = null
this.x = 0
this.y = 0
this.diff = { x: 0, y: 0 }
this.position = { x: 0, y: 0 }
this.exist = false
}
}

@@ -33,3 +33,2 @@ import {

this.options = options = Object.assign({}, options)
this.scrollEl = getParentAutoScrollElement(this.$el, true) // 获取页面滚动元素

@@ -40,3 +39,2 @@ this.dragEl = null // 拖拽元素

this.ghost = null // 拖拽时蒙版元素
this.calcXY = { x: 0, y: 0 } // 记录拖拽移动时坐标

@@ -155,9 +153,11 @@ const defaults = {

this.ghost.set('x', rect.left)
this.ghost.set('y', rect.top)
this.ghost.setPosition(rect.left, rect.top)
this.ghost.diff = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
}
this.differ.from = { node: this.dragEl, rect, offset}
this.calcXY = { x: e.clientX, y: e.clientY }
this._onMoveEvents(touch)
this._onUpEvents(touch)
this._bindMoveEvents(touch)
this._bindUpEvents(touch)
},

@@ -200,5 +200,3 @@ _onMove: function(/** Event|TouchEvent */evt) {

this.ghost.set('x', this.ghost.x + clientX - this.calcXY.x)
this.ghost.set('y', this.ghost.y + clientY - this.calcXY.y)
this.calcXY = { x: clientX, y: clientY }
this.ghost.setPosition(clientX, clientY)
this.ghost.move()

@@ -217,14 +215,3 @@

if (!el || index < 0 || top < 0) return
// 加上当前滚动距离
const { scrollTop, scrollLeft } = this.scrollEl
const boundaryL = rc.left + scrollLeft
const boundaryT = rc.top + scrollTop
// 如果目标元素超出当前可视区,不允许拖动
if (this.scrollEl !== this.$el && (rc.left < 0 || rc.top < 0)) {
if ((rc.top < 0 && top < boundaryT) || (rc.left < 0 && left < boundaryL)) return
} else if (top < rc.top || left < rc.left) return
if (!el || index < 0) return

@@ -263,4 +250,4 @@ this.dropEl = el

_onDrop: function(/** Event|TouchEvent */evt) {
this._offMoveEvents()
this._offUpEvents()
this._unbindMoveEvents()
this._unbindUpEvents()
clearTimeout(this.dragStartTimer)

@@ -303,3 +290,2 @@

this.differ.destroy()
this.calcXY = { x: 0, y: 0 }
this._removeWindowState()

@@ -306,0 +292,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc