Browse Source

Merge branch 'dev' of github.com:RubaXa/Sortable into dev

pull/754/merge
Lebedev Konstantin 9 years ago
parent
commit
cfae80664f
  1. 12
      README.md
  2. 28
      Sortable.js
  3. 2
      Sortable.min.js
  4. 2
      jquery.binding.js
  5. 265
      ng-sortable.js

12
README.md

@ -74,6 +74,7 @@ var sortable = new Sortable(el, {
fallbackTolerance: 0 // Specify in pixels how far the mouse should move before it's considered as a drag. fallbackTolerance: 0 // Specify in pixels how far the mouse should move before it's considered as a drag.
scroll: true, // or HTMLElement scroll: true, // or HTMLElement
scrollFn: function(offsetX, offsetY, originalEvent) { ... }, // if you have custom scrollbar scrollFn may be used for autoscrolling
scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling. scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.
scrollSpeed: 10, // px scrollSpeed: 10, // px
@ -126,12 +127,13 @@ var sortable = new Sortable(el, {
}, },
// Event when you move an item in the list or between lists // Event when you move an item in the list or between lists
onMove: function (/**Event*/evt) { onMove: function (/**Event*/evt, /**Event*/originalEvent) {
// Example: http://jsbin.com/tuyafe/1/edit?js,output // Example: http://jsbin.com/tuyafe/1/edit?js,output
evt.dragged; // dragged HTMLElement evt.dragged; // dragged HTMLElement
evt.draggedRect; // TextRectangle {left, top, right и bottom} evt.draggedRect; // TextRectangle {left, top, right и bottom}
evt.related; // HTMLElement on which have guided evt.related; // HTMLElement on which have guided
evt.relatedRect; // TextRectangle evt.relatedRect; // TextRectangle
originalEvent.clientY; // mouse position
// return false; — for cancel // return false; — for cancel
}, },
@ -335,6 +337,14 @@ Demo:
--- ---
#### `scrollFn` option
Defines function that will be used for autoscrolling. el.scrollTop/el.scrollLeft is used by default.
Useful when you have custom scrollbar with dedicated scroll function.
---
#### `scrollSensitivity` option #### `scrollSensitivity` option
Defines how near the mouse must be to an edge to start scrolling. Defines how near the mouse must be to an edge to start scrolling.

28
Sortable.js

@ -39,6 +39,7 @@
scrollEl, scrollEl,
scrollParentEl, scrollParentEl,
scrollCustomFn,
lastEl, lastEl,
lastCSS, lastCSS,
@ -99,13 +100,17 @@
winHeight = window.innerHeight, winHeight = window.innerHeight,
vx, vx,
vy vy,
scrollOffsetX,
scrollOffsetY
; ;
// Delect scrollEl // Delect scrollEl
if (scrollParentEl !== rootEl) { if (scrollParentEl !== rootEl) {
scrollEl = options.scroll; scrollEl = options.scroll;
scrollParentEl = rootEl; scrollParentEl = rootEl;
scrollCustomFn = options.scrollFn;
if (scrollEl === true) { if (scrollEl === true) {
scrollEl = rootEl; scrollEl = rootEl;
@ -147,11 +152,18 @@
if (el) { if (el) {
autoScroll.pid = setInterval(function () { autoScroll.pid = setInterval(function () {
scrollOffsetY = vy ? vy * speed : 0;
scrollOffsetX = vx ? vx * speed : 0;
if ('function' === typeof(scrollCustomFn)) {
return scrollCustomFn.call(_this, scrollOffsetX, scrollOffsetY, evt);
}
if (el === win) { if (el === win) {
win.scrollTo(win.pageXOffset + vx * speed, win.pageYOffset + vy * speed); win.scrollTo(win.pageXOffset + scrollOffsetX, win.pageYOffset + scrollOffsetY);
} else { } else {
vy && (el.scrollTop += vy * speed); el.scrollTop += scrollOffsetY;
vx && (el.scrollLeft += vx * speed); el.scrollLeft += scrollOffsetX;
} }
}, 24); }, 24);
} }
@ -681,7 +693,7 @@
_cloneHide(isOwner); _cloneHide(isOwner);
if (_onMove(rootEl, el, dragEl, dragRect, target, targetRect) !== false) { if (_onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt) !== false) {
if (!dragEl.contains(el)) { if (!dragEl.contains(el)) {
el.appendChild(dragEl); el.appendChild(dragEl);
parentEl = el; // actualization parentEl = el; // actualization
@ -708,7 +720,7 @@
isLong = (target.offsetHeight > dragEl.offsetHeight), isLong = (target.offsetHeight > dragEl.offsetHeight),
halfway = (floating ? (evt.clientX - targetRect.left) / width : (evt.clientY - targetRect.top) / height) > 0.5, halfway = (floating ? (evt.clientX - targetRect.left) / width : (evt.clientY - targetRect.top) / height) > 0.5,
nextSibling = target.nextElementSibling, nextSibling = target.nextElementSibling,
moveVector = _onMove(rootEl, el, dragEl, dragRect, target, targetRect), moveVector = _onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt),
after after
; ;
@ -1158,7 +1170,7 @@
} }
function _onMove(fromEl, toEl, dragEl, dragRect, targetEl, targetRect) { function _onMove(fromEl, toEl, dragEl, dragRect, targetEl, targetRect, originalEvt) {
var evt, var evt,
sortable = fromEl[expando], sortable = fromEl[expando],
onMoveFn = sortable.options.onMove, onMoveFn = sortable.options.onMove,
@ -1177,7 +1189,7 @@
fromEl.dispatchEvent(evt); fromEl.dispatchEvent(evt);
if (onMoveFn) { if (onMoveFn) {
retVal = onMoveFn.call(sortable, evt); retVal = onMoveFn.call(sortable, evt, originalEvt);
} }
return retVal; return retVal;

2
Sortable.min.js vendored

File diff suppressed because one or more lines are too long

2
jquery.binding.js

@ -41,7 +41,7 @@
if (sortable) { if (sortable) {
if (options === 'widget') { if (options === 'widget') {
return sortable; retVal = sortable;
} }
else if (options === 'destroy') { else if (options === 'destroy') {
sortable.destroy(); sortable.destroy();

265
ng-sortable.js

@ -35,169 +35,174 @@
.constant('ngSortableConfig', {}) .constant('ngSortableConfig', {})
.directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) { .directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) {
var removed, var removed,
nextSibling, nextSibling;
getSourceFactory = function getSourceFactory(el, scope) {
var ngRepeat = [].filter.call(el.childNodes, function (node) { // Export
return {
restrict: 'AC',
scope: { ngSortable: "=?" },
priority: 1001,
compile: function ($element, $attr) {
var ngRepeat = [].filter.call($element[0].childNodes, function (node) {
return ( return (
(node.nodeType === 8) && (node.nodeType === 1) &&
(node.nodeValue.indexOf('ngRepeat:') !== -1) (node.attributes['ng-repeat'])
); );
})[0]; })[0];
if (!ngRepeat) { if (!ngRepeat) {
// Without ng-repeat return;
return function () {
return null;
};
} }
// tests: http://jsbin.com/kosubutilo/1/edit?js,output var expression = ngRepeat.attributes['ng-repeat'].nodeValue;
ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/); var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
var itemsExpr = $parse(ngRepeat[2]); if (!match) {
return;
}
return function () { var rhs = match[2];
return itemsExpr(scope.$parent) || [];
};
};
return function postLink(scope, $el) {
var itemsExpr = $parse(rhs);
var getSource = function getSource() {
return itemsExpr(scope.$parent) || [];
};
// Export
return {
restrict: 'AC',
scope: { ngSortable: "=?" },
link: function (scope, $el) {
var el = $el[0],
options = angular.extend(scope.ngSortable || {}, ngSortableConfig),
watchers = [],
getSource = getSourceFactory(el, scope),
offDestroy,
sortable
;
el[expando] = getSource;
function _emitEvent(/**Event*/evt, /*Mixed*/item) {
var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1);
var source = getSource();
/* jshint expr:true */
options[name] && options[name]({
model: item || source[evt.newIndex],
models: source,
oldIndex: evt.oldIndex,
newIndex: evt.newIndex,
originalEvent: evt
});
}
var el = $el[0],
options = angular.extend(scope.ngSortable || {}, ngSortableConfig),
watchers = [],
offDestroy,
sortable
;
function _sync(/**Event*/evt) { el[expando] = getSource;
var items = getSource();
if (!items) { function _emitEvent(/**Event*/evt, /*Mixed*/item) {
// Without ng-repeat var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1);
return; var source = getSource();
}
var oldIndex = evt.oldIndex, /* jshint expr:true */
newIndex = evt.newIndex; options[name] && options[name]({
model: item || source[evt.newIndex],
models: source,
oldIndex: evt.oldIndex,
newIndex: evt.newIndex,
originalEvent: evt
});
}
if (el !== evt.from) {
var prevItems = evt.from[expando]();
removed = prevItems[oldIndex]; function _sync(/**Event*/evt) {
var items = getSource();
if (evt.clone) { if (!items) {
removed = angular.copy(removed); // Without ng-repeat
prevItems.splice(Sortable.utils.index(evt.clone, sortable.options.draggable), 0, prevItems.splice(oldIndex, 1)[0]); return;
evt.from.removeChild(evt.clone);
}
else {
prevItems.splice(oldIndex, 1);
} }
items.splice(newIndex, 0, removed); var oldIndex = evt.oldIndex,
newIndex = evt.newIndex;
evt.from.insertBefore(evt.item, nextSibling); // revert element if (el !== evt.from) {
} var prevItems = evt.from[expando]();
else {
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
// move ng-repeat comment node to right position removed = prevItems[oldIndex];
if (nextSibling.nodeType === Node.COMMENT_NODE) {
evt.from.insertBefore(nextSibling, evt.item.nextSibling);
}
}
scope.$apply(); if (evt.clone) {
} removed = angular.copy(removed);
prevItems.splice(Sortable.utils.index(evt.clone, sortable.options.draggable), 0, prevItems.splice(oldIndex, 1)[0]);
evt.from.removeChild(evt.clone);
}
else {
prevItems.splice(oldIndex, 1);
}
function _destroy() { items.splice(newIndex, 0, removed);
offDestroy();
angular.forEach(watchers, function (/** Function */unwatch) { evt.from.insertBefore(evt.item, nextSibling); // revert element
unwatch(); }
}); else {
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
sortable.destroy(); // move ng-repeat comment node to right position
if (nextSibling.nodeType === Node.COMMENT_NODE) {
evt.from.insertBefore(nextSibling, evt.item.nextSibling);
}
}
el[expando] = null; scope.$apply();
el = null; }
watchers = null;
sortable = null;
nextSibling = null;
}
function _destroy() {
offDestroy();
// Initialization angular.forEach(watchers, function (/** Function */unwatch) {
sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) { unwatch();
opts[name] = opts[name] || options[name]; });
return opts;
}, { sortable.destroy();
onStart: function (/**Event*/evt) {
nextSibling = evt.from === evt.item.parentNode ? evt.item.nextSibling : evt.clone.nextSibling; el[expando] = null;
_emitEvent(evt); el = null;
scope.$apply(); watchers = null;
}, sortable = null;
onEnd: function (/**Event*/evt) { nextSibling = null;
_emitEvent(evt, removed);
scope.$apply();
},
onAdd: function (/**Event*/evt) {
_sync(evt);
_emitEvent(evt, removed);
scope.$apply();
},
onUpdate: function (/**Event*/evt) {
_sync(evt);
_emitEvent(evt);
},
onRemove: function (/**Event*/evt) {
_emitEvent(evt, removed);
},
onSort: function (/**Event*/evt) {
_emitEvent(evt);
} }
}));
// Create watchers for `options` // Initialization
angular.forEach([ sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) {
'sort', 'disabled', 'draggable', 'handle', 'animation', 'group', 'ghostClass', 'filter', opts[name] = opts[name] || options[name];
'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort', 'onMove', 'onClone', 'setData' return opts;
], function (name) { }, {
watchers.push(scope.$watch('ngSortable.' + name, function (value) { onStart: function (/**Event*/evt) {
if (value !== void 0) { nextSibling = evt.from === evt.item.parentNode ? evt.item.nextSibling : evt.clone.nextSibling;
options[name] = value; _emitEvent(evt);
scope.$apply();
if (!/^on[A-Z]/.test(name)) { },
sortable.option(name, value); onEnd: function (/**Event*/evt) {
} _emitEvent(evt, removed);
scope.$apply();
},
onAdd: function (/**Event*/evt) {
_sync(evt);
_emitEvent(evt, removed);
scope.$apply();
},
onUpdate: function (/**Event*/evt) {
_sync(evt);
_emitEvent(evt);
},
onRemove: function (/**Event*/evt) {
_emitEvent(evt, removed);
},
onSort: function (/**Event*/evt) {
_emitEvent(evt);
} }
})); }));
});
offDestroy = scope.$on('$destroy', _destroy); // Create watchers for `options`
angular.forEach([
'sort', 'disabled', 'draggable', 'handle', 'animation', 'group', 'ghostClass', 'filter',
'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort', 'onMove', 'onClone', 'setData'
], function (name) {
watchers.push(scope.$watch('ngSortable.' + name, function (value) {
if (value !== void 0) {
options[name] = value;
if (!/^on[A-Z]/.test(name)) {
sortable.option(name, value);
}
}
}));
});
offDestroy = scope.$on('$destroy', _destroy);
}
} }
}; };
}]); }]);

Loading…
Cancel
Save