Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 74a1fe4

Browse files
committed
feat(sortable): access sortable object inside helper
1 parent ed85847 commit 74a1fe4

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Diff for: src/sortable.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ angular.module('ui.sortable', [])
209209
// return the index of ui.item among the items
210210
// we can't just do ui.item.index() because there it might have siblings
211211
// which are not items
212-
function getItemIndex(ui) {
213-
return ui.item.parent()
212+
function getItemIndex(item) {
213+
return item.parent()
214214
.find(opts['ui-model-items'])
215-
.index(ui.item);
215+
.index(item);
216216
}
217217

218218
var opts = {};
@@ -266,7 +266,7 @@ angular.module('ui.sortable', [])
266266
}
267267

268268
// Save the starting position of dragged item
269-
var index = getItemIndex(ui);
269+
var index = getItemIndex(ui.item);
270270
ui.item.sortable = {
271271
model: ngModel.$modelValue[index],
272272
index: index,
@@ -322,7 +322,7 @@ angular.module('ui.sortable', [])
322322
// update that happens when moving between lists because then
323323
// the value will be overwritten with the old value
324324
if(!ui.item.sortable.received) {
325-
ui.item.sortable.dropindex = getItemIndex(ui);
325+
ui.item.sortable.dropindex = getItemIndex(ui.item);
326326
var droptarget = ui.item.parent();
327327
ui.item.sortable.droptarget = droptarget;
328328

@@ -431,7 +431,24 @@ angular.module('ui.sortable', [])
431431
wrappers.helper = function (inner) {
432432
if (inner && typeof inner === 'function') {
433433
return function (e, item) {
434+
var oldItemSortable = item.sortable;
435+
var index = getItemIndex(item);
436+
item.sortable = {
437+
model: ngModel.$modelValue[index],
438+
index: index,
439+
source: item.parent(),
440+
sourceModel: ngModel.$modelValue,
441+
_restore: function () {
442+
angular.forEach(item.sortable, function(value, key) {
443+
item.sortable[key] = undefined;
444+
});
445+
446+
item.sortable = oldItemSortable;
447+
}
448+
};
449+
434450
var innerResult = inner.apply(this, arguments);
451+
item.sortable._restore();
435452
item.sortable._isCustomHelperUsed = item !== innerResult;
436453
return innerResult;
437454
};

0 commit comments

Comments
 (0)