提交 a1790fdf 编写于 作者: R RubaXa

#677: + pull/put as functions

上级 9eb3ef5b
...@@ -150,8 +150,12 @@ To drag elements from one list into another, both lists must have the same `grou ...@@ -150,8 +150,12 @@ To drag elements from one list into another, both lists must have the same `grou
You can also define whether lists can give away, give and keep a copy (`clone`), and receive elements. You can also define whether lists can give away, give and keep a copy (`clone`), and receive elements.
* name: `String` — group name * name: `String` — group name
* pull: `true|false|'clone'` — ability to move from the list. `clone` — copy the item, rather than move. * pull: `true|false|'clone'|function` — ability to move from the list. `clone` — copy the item, rather than move.
* put: `true|false|["foo", "bar"]` — whether elements can be added from other lists, or an array of group names from which elements can be taken. Demo: http://jsbin.com/naduvo/2/edit?html,js,output * put: `true|false|["foo", "bar"]|function` — whether elements can be added from other lists, or an array of group names from which elements can be taken.
Demo:
- http://jsbin.com/naduvo/edit?js,output
- http://jsbin.com/tisequvute/edit?js,output — use of complex logic in the `pull` and` put`
--- ---
......
...@@ -42,6 +42,8 @@ ...@@ -42,6 +42,8 @@
newIndex, newIndex,
activeGroup, activeGroup,
putSortable,
autoScroll = {}, autoScroll = {},
tapEvt, tapEvt,
...@@ -152,19 +154,35 @@ ...@@ -152,19 +154,35 @@
}, 30), }, 30),
_prepareGroup = function (options) { _prepareGroup = function (options) {
function toFn(value, pull) {
if (value === void 0) {
value = true;
}
if (typeof value === 'function') {
return value;
} else {
return function (to, from) {
var fromGroup = from.options.group.name;
return pull
? value
: value && (value.join
? value.indexOf(fromGroup) > -1
: (fromGroup == value)
);
};
}
}
var group = options.group; var group = options.group;
if (!group || typeof group != 'object') { if (!group || typeof group != 'object') {
group = options.group = {name: group}; group = options.group = {name: group};
} }
['pull', 'put'].forEach(function (key) { group.checkPull = toFn(group.pull, true);
if (!(key in group)) { group.checkPut = toFn(group.put);
group[key] = true;
}
});
options.groups = ' ' + group.name + (group.put.join ? ' ' + group.put.join(' ') : '') + ' ';
} }
; ;
...@@ -451,7 +469,7 @@ ...@@ -451,7 +469,7 @@
if (parent) { if (parent) {
do { do {
if (parent[expando] && parent[expando].options.groups.indexOf(groupName) > -1) { if (parent[expando]) {
while (i--) { while (i--) {
touchDragOverListeners[i]({ touchDragOverListeners[i]({
clientX: touchEvt.clientX, clientX: touchEvt.clientX,
...@@ -548,7 +566,7 @@ ...@@ -548,7 +566,7 @@
this._offUpEvents(); this._offUpEvents();
if (activeGroup.pull == 'clone') { if (activeGroup.checkPull(this, this, dragEl, evt) == 'clone') {
cloneEl = _clone(dragEl); cloneEl = _clone(dragEl);
_css(cloneEl, 'display', 'none'); _css(cloneEl, 'display', 'none');
rootEl.insertBefore(cloneEl, dragEl); rootEl.insertBefore(cloneEl, dragEl);
...@@ -587,7 +605,7 @@ ...@@ -587,7 +605,7 @@
revert, revert,
options = this.options, options = this.options,
group = options.group, group = options.group,
groupPut = group.put, activeSortable = Sortable.active,
isOwner = (activeGroup === group), isOwner = (activeGroup === group),
canSort = options.sort; canSort = options.sort;
...@@ -601,9 +619,9 @@ ...@@ -601,9 +619,9 @@
if (activeGroup && !options.disabled && if (activeGroup && !options.disabled &&
(isOwner (isOwner
? canSort || (revert = !rootEl.contains(dragEl)) // Reverting item into the original list ? canSort || (revert = !rootEl.contains(dragEl)) // Reverting item into the original list
: activeGroup.pull && groupPut && ( : (
(activeGroup.name === group.name) || // by Name putSortable === this ||
(groupPut.indexOf && ~groupPut.indexOf(activeGroup.name)) // by Array activeGroup.checkPull(this, activeSortable, dragEl, evt) && group.checkPut(this, activeSortable, dragEl, evt)
) )
) && ) &&
(evt.rootEl === void 0 || evt.rootEl === this.el) // touch fallback (evt.rootEl === void 0 || evt.rootEl === this.el) // touch fallback
...@@ -617,6 +635,7 @@ ...@@ -617,6 +635,7 @@
target = _closest(evt.target, options.draggable, el); target = _closest(evt.target, options.draggable, el);
dragRect = dragEl.getBoundingClientRect(); dragRect = dragEl.getBoundingClientRect();
putSortable = this;
if (revert) { if (revert) {
_cloneHide(true); _cloneHide(true);
...@@ -860,7 +879,9 @@ ...@@ -860,7 +879,9 @@
lastEl = lastEl =
lastCSS = lastCSS =
putSortable =
activeGroup = activeGroup =
Sortable.active = null; Sortable.active = null;
}, },
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册