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

Commit

Permalink
feat(sortable): access sortable object inside helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thgreasi committed May 27, 2016
1 parent ed85847 commit 74a1fe4
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ angular.module('ui.sortable', [])
// return the index of ui.item among the items
// we can't just do ui.item.index() because there it might have siblings
// which are not items
function getItemIndex(ui) {
return ui.item.parent()
function getItemIndex(item) {
return item.parent()
.find(opts['ui-model-items'])
.index(ui.item);
.index(item);
}

var opts = {};
Expand Down Expand Up @@ -266,7 +266,7 @@ angular.module('ui.sortable', [])
}

// Save the starting position of dragged item
var index = getItemIndex(ui);
var index = getItemIndex(ui.item);
ui.item.sortable = {
model: ngModel.$modelValue[index],
index: index,
Expand Down Expand Up @@ -322,7 +322,7 @@ angular.module('ui.sortable', [])
// update that happens when moving between lists because then
// the value will be overwritten with the old value
if(!ui.item.sortable.received) {
ui.item.sortable.dropindex = getItemIndex(ui);
ui.item.sortable.dropindex = getItemIndex(ui.item);
var droptarget = ui.item.parent();
ui.item.sortable.droptarget = droptarget;

Expand Down Expand Up @@ -431,7 +431,24 @@ angular.module('ui.sortable', [])
wrappers.helper = function (inner) {
if (inner && typeof inner === 'function') {
return function (e, item) {
var oldItemSortable = item.sortable;
var index = getItemIndex(item);
item.sortable = {
model: ngModel.$modelValue[index],
index: index,
source: item.parent(),
sourceModel: ngModel.$modelValue,
_restore: function () {
angular.forEach(item.sortable, function(value, key) {
item.sortable[key] = undefined;
});

item.sortable = oldItemSortable;
}
};

var innerResult = inner.apply(this, arguments);
item.sortable._restore();
item.sortable._isCustomHelperUsed = item !== innerResult;
return innerResult;
};
Expand Down

0 comments on commit 74a1fe4

Please sign in to comment.