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

feat(sortable): allow extra element before/after ng-repeat #344

Merged
merged 2 commits into from
Apr 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ angular.module('ui.sortable', [])
ui.item.sortable._destroy();
}

var opts = {};
// 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().find('> [ng-repeat],> [data-ng-repeat],> [x-ng-repeat]')
.index(ui.item);
}

var opts = {
// the default for jquery-ui sortable is "> *", we need to restrict this to
// ng-repeat items
// if the user uses
items: '> [ng-repeat],> [data-ng-repeat],> [x-ng-repeat]'
};

// directive specific options
var directiveOpts = {
Expand Down Expand Up @@ -114,9 +127,10 @@ angular.module('ui.sortable', [])
}

// Save the starting position of dragged item
var index = getItemIndex(ui);
ui.item.sortable = {
model: ngModel.$modelValue[ui.item.index()],
index: ui.item.index(),
model: ngModel.$modelValue[index],
index: index,
source: ui.item.parent(),
sourceModel: ngModel.$modelValue,
cancel: function () {
Expand Down Expand Up @@ -184,7 +198,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 = ui.item.index();
ui.item.sortable.dropindex = getItemIndex(ui);
var droptarget = ui.item.parent();
ui.item.sortable.droptarget = droptarget;

Expand Down Expand Up @@ -325,6 +339,10 @@ angular.module('ui.sortable', [])
value = wrappers[key](value);
}

if (key === 'items' && !value) {
value = '> [ng-repeat],> [data-ng-repeat],> [x-ng-repeat]';
}

opts[key] = value;
element.sortable('option', key, value);
});
Expand Down
Loading