Browse Source

* master

drag-over-refactoring
Lebedev Konstantin 8 years ago
parent
commit
3e3bb12f28
  1. 88
      Sortable.js
  2. 2
      Sortable.min.js
  3. 3
      package.json

88
Sortable.js

@ -56,7 +56,8 @@
moved,
/** @const */
RSPACE = /\s+/g,
R_SPACE = /\s+/g,
R_FLOAT = /left|right|inline/,
expando = 'Sortable' + (new Date).getTime(),
@ -67,7 +68,7 @@
$ = win.jQuery || win.Zepto,
Polymer = win.Polymer,
captureMode = {capture: false, passive: false},
captureMode = false,
supportDraggable = !!('draggable' in document.createElement('div')),
supportCssPointerEvents = (function (el) {
@ -85,6 +86,7 @@
abs = Math.abs,
min = Math.min,
savedInputChecked = [],
touchDragOverListeners = [],
_autoScroll = _throttle(function (/**Event*/evt, /**Object*/options, /**HTMLElement*/rootEl) {
@ -213,7 +215,6 @@
;
/**
* @class Sortable
* @param {HTMLElement} el
@ -231,7 +232,6 @@
// Export instance
el[expando] = this;
// Default options
var defaults = {
group: Math.random(),
@ -314,6 +314,9 @@
filter = options.filter,
startIndex;
_saveInputCheckedState(el);
// Don't trigger start event when an element is been dragged, otherwise the evt.oldindex always wrong when set option.group.
if (dragEl) {
return;
@ -420,6 +423,7 @@
_on(ownerDocument, 'touchend', _this._onDrop);
_on(ownerDocument, 'touchcancel', _this._onDrop);
_on(ownerDocument, 'pointercancel', _this._onDrop);
_on(ownerDocument, 'selectstart', _this);
if (options.delay) {
// If the user moves the pointer or let go the click or touch
@ -437,9 +441,7 @@
dragStartFn();
}
if (options.forceFallback) {
evt.preventDefault();
}
}
},
@ -457,6 +459,7 @@
_triggerDragStart: function (/** Event */evt, /** Touch */touch) {
touch = touch || (evt.pointerType == 'touch' ? evt : null);
if (touch) {
// Touch device support
tapEvt = {
@ -675,6 +678,7 @@
group = options.group,
activeSortable = Sortable.active,
isOwner = (activeGroup === group),
isMovingBetweenSortable = false,
canSort = options.sort;
if (evt.preventDefault !== void 0) {
@ -682,6 +686,10 @@
!options.dragoverBubble && evt.stopPropagation();
}
if (dragEl.animated) {
return;
}
moved = true;
// Check base state
@ -696,21 +704,29 @@
// Check access
if (isOwner) {
if (!canSort) {
// Reverting item into the original list
if (rootEl.contains(dragEl)) {
return; // exit
}
// Reverting item into the original list
revert = true;
}
} else if (putSortable !== this) {
activeSortable.lastPullMode = activeGroup.checkPull(this, activeSortable, dragEl, evt);
if (!(activeSortable.lastPullMode || group.checkPut(this, activeSortable, dragEl, evt))) {
if (!(activeSortable.lastPullMode && group.checkPut(this, activeSortable, dragEl, evt))) {
return; // exit;
}
}
target = _closest(evt.target, options.draggable, el);
dragRect = dragEl.getBoundingClientRect();
if (putSortable !== this) {
putSortable = this;
isMovingBetweenSortable = true;
}
// Smart auto-scrolling
_autoScroll(evt, options, this.el);
@ -770,14 +786,14 @@
var width = targetRect.right - targetRect.left,
height = targetRect.bottom - targetRect.top,
floating = /left|right|inline/.test(lastCSS.cssFloat + lastCSS.display)
floating = R_FLOAT.test(lastCSS.cssFloat + lastCSS.display)
|| (lastParentCSS.display == 'flex' && lastParentCSS['flex-direction'].indexOf('row') === 0),
isWide = (target.offsetWidth > dragEl.offsetWidth),
isLong = (target.offsetHeight > dragEl.offsetHeight),
halfway = (floating ? (evt.clientX - targetRect.left) / width : (evt.clientY - targetRect.top) / height) > 0.5,
nextSibling = target.nextElementSibling,
moveVector = _onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt),
after
after = false
;
if (moveVector !== false) {
@ -801,7 +817,7 @@
} else {
after = tgTop > elTop;
}
} else {
} else if (!isMovingBetweenSortable) {
after = (nextSibling !== dragEl) && !isLong || halfway && isLong;
}
@ -860,6 +876,7 @@
_off(ownerDocument, 'touchend', this._onDrop);
_off(ownerDocument, 'pointerup', this._onDrop);
_off(ownerDocument, 'touchcancel', this._onDrop);
_off(ownerDocument, 'selectstart', this);
},
_onDrop: function (/**Event*/evt) {
@ -974,19 +991,31 @@
putSortable =
activeGroup =
Sortable.active = null;
savedInputChecked.forEach(function (el) {
el.checked = true;
});
savedInputChecked.length = 0;
},
handleEvent: function (/**Event*/evt) {
var type = evt.type;
switch (evt.type) {
case 'drop':
case 'dragend':
this._onDrop(evt);
break;
if (type === 'dragover' || type === 'dragenter') {
case 'dragover':
case 'dragenter':
if (dragEl) {
this._onDragOver(evt);
_globalDragOver(evt);
}
}
else if (type === 'drop' || type === 'dragend') {
this._onDrop(evt);
break;
case 'selectstart':
evt.preventDefault();
break;
}
},
@ -1181,8 +1210,8 @@
el.classList[state ? 'add' : 'remove'](name);
}
else {
var className = (' ' + el.className + ' ').replace(RSPACE, ' ').replace(' ' + name + ' ', ' ');
el.className = (className + (state ? ' ' + name : '')).replace(RSPACE, ' ');
var className = (' ' + el.className + ' ').replace(R_SPACE, ' ').replace(' ' + name + ' ', ' ');
el.className = (className + (state ? ' ' + name : '')).replace(R_SPACE, ' ');
}
}
}
@ -1405,6 +1434,27 @@
);
}
function _saveInputCheckedState(root) {
var inputs = root.getElementsByTagName('input');
var idx = inputs.length;
while (idx--) {
var el = inputs[idx];
el.checked && savedInputChecked.push(el);
}
}
try {
window.addEventListener('test', null, Object.defineProperty({}, 'passive', {
get: function () {
captureMode = {
capture: false,
passive: false
};
}
}));
} catch (err) {}
// Export utils
Sortable.utils = {
on: _on,

2
Sortable.min.js vendored

File diff suppressed because one or more lines are too long

3
package.json

@ -11,7 +11,8 @@
"description": "Minimalist JavaScript library for reorderable drag-and-drop lists on modern browsers and touch devices. No jQuery. Supports AngularJS and any CSS library, e.g. Bootstrap.",
"main": "Sortable.js",
"scripts": {
"test": "grunt"
"test": "grunt",
"prepublish": "grunt"
},
"repository": {
"type": "git",

Loading…
Cancel
Save