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

Commit

Permalink
Merge pull request #461 from angular-ui/sortableObjectOnHelper
Browse files Browse the repository at this point in the history
feat(sortable): access sortable object inside helper
  • Loading branch information
thgreasi committed Jun 4, 2016
2 parents f5b9ce5 + e7a1263 commit 127cf26
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ui-sortable",
"version": "0.14.0",
"version": "0.14.1",
"description": "This directive allows you to jQueryUI Sortable.",
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ui-sortable",
"version": "0.14.0",
"version": "0.14.1",
"description": "This directive allows you to jQueryUI Sortable.",
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
"license": "MIT",
Expand Down
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
58 changes: 58 additions & 0 deletions test/sortable.e2e.callbacks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,64 @@ describe('uiSortable', function() {
});
});

it('should provide the item.sortable properties on helper callback', function() {
inject(function($compile, $rootScope) {
var element, helperItem, itemSortable_Restore, sortableAfterRestore, helperCallbackExpectations;
element = $compile(''.concat(
'<ul ui-sortable="opts" ng-model="items">',
beforeLiElement,
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
afterLiElement +
'</ul>'))($rootScope);
$rootScope.$apply(function() {
$rootScope.opts = {
helper: function(e, item) {
helperItem = item;

var oldRestore = item.sortable._restore;
item.sortable._restore = function () {
oldRestore.apply(this, arguments);
// hold the value of the sortable object
// right after the _restore method completes
sortableAfterRestore = item.sortable;
};

spyOn(item.sortable, '_restore').and.callThrough();
itemSortable_Restore = item.sortable._restore;
helperCallbackExpectations(item.sortable);
return item.clone();
}
};
$rootScope.items = ['One', 'Two', 'Three'];
});

host.append(element);

$rootScope.$apply(function() {
});

var li = element.find('[ng-repeat]:eq(0)');
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
helperCallbackExpectations = function(helperItemSortable) {
expect(helperItemSortable.model).toEqual('One');
expect(helperItemSortable.index).toEqual(0);
expect(helperItemSortable.source.length).toEqual(1);
expect(helperItemSortable.source[0]).toBe(host.children()[0]);
expect(helperItemSortable.sourceModel).toBe($rootScope.items);
};
li.simulate('drag', { dy: dy });
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
expect($rootScope.items).toEqual(listContent(element));
expect(itemSortable_Restore).toHaveBeenCalled();
expect(hasUndefinedProperties(helperItem.sortable)).toBe(true);
// this happens after the update callback, so everything is udnefined
expect(typeof sortableAfterRestore).toBe('function');
helperItem = itemSortable_Restore = sortableAfterRestore = helperCallbackExpectations = undefined;

$(element).remove();
});
});

it('should properly reset a deleted callback option', function() {
inject(function($compile, $rootScope) {
var element, logsElement;
Expand Down

0 comments on commit 127cf26

Please sign in to comment.