From a1790fdf233d270b56e34f432bd61a081bf5c32f Mon Sep 17 00:00:00 2001 From: RubaXa Date: Sat, 2 Jan 2016 21:09:11 +0300 Subject: [PATCH] #677: + pull/put as functions --- README.md | 8 ++++++-- Sortable.js | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 38aa4d0..55febf1 100644 --- a/README.md +++ b/README.md @@ -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. * name: `String` — group name - * pull: `true|false|'clone'` — 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 + * pull: `true|false|'clone'|function` — ability to move from the list. `clone` — copy the item, rather than move. + * 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` --- diff --git a/Sortable.js b/Sortable.js index 5bae1be..4fd112e 100644 --- a/Sortable.js +++ b/Sortable.js @@ -42,6 +42,8 @@ newIndex, activeGroup, + putSortable, + autoScroll = {}, tapEvt, @@ -152,19 +154,35 @@ }, 30), _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; if (!group || typeof group != 'object') { group = options.group = {name: group}; } - ['pull', 'put'].forEach(function (key) { - if (!(key in group)) { - group[key] = true; - } - }); - - options.groups = ' ' + group.name + (group.put.join ? ' ' + group.put.join(' ') : '') + ' '; + group.checkPull = toFn(group.pull, true); + group.checkPut = toFn(group.put); } ; @@ -451,7 +469,7 @@ if (parent) { do { - if (parent[expando] && parent[expando].options.groups.indexOf(groupName) > -1) { + if (parent[expando]) { while (i--) { touchDragOverListeners[i]({ clientX: touchEvt.clientX, @@ -548,7 +566,7 @@ this._offUpEvents(); - if (activeGroup.pull == 'clone') { + if (activeGroup.checkPull(this, this, dragEl, evt) == 'clone') { cloneEl = _clone(dragEl); _css(cloneEl, 'display', 'none'); rootEl.insertBefore(cloneEl, dragEl); @@ -587,7 +605,7 @@ revert, options = this.options, group = options.group, - groupPut = group.put, + activeSortable = Sortable.active, isOwner = (activeGroup === group), canSort = options.sort; @@ -601,9 +619,9 @@ if (activeGroup && !options.disabled && (isOwner ? canSort || (revert = !rootEl.contains(dragEl)) // Reverting item into the original list - : activeGroup.pull && groupPut && ( - (activeGroup.name === group.name) || // by Name - (groupPut.indexOf && ~groupPut.indexOf(activeGroup.name)) // by Array + : ( + putSortable === this || + activeGroup.checkPull(this, activeSortable, dragEl, evt) && group.checkPut(this, activeSortable, dragEl, evt) ) ) && (evt.rootEl === void 0 || evt.rootEl === this.el) // touch fallback @@ -617,6 +635,7 @@ target = _closest(evt.target, options.draggable, el); dragRect = dragEl.getBoundingClientRect(); + putSortable = this; if (revert) { _cloneHide(true); @@ -860,7 +879,9 @@ lastEl = lastCSS = + putSortable = activeGroup = + Sortable.active = null; }, -- GitLab