Skip to content

Commit c3f19b1

Browse files
committed
feat(sortable): allow extra element before/after ng-repeat
Fixes angular-ui#41, Fixed angular-ui#177, Fixed angular-ui#98 and Fixes angular-ui#207
1 parent a258424 commit c3f19b1

File tree

3 files changed

+746
-5
lines changed

3 files changed

+746
-5
lines changed

src/sortable.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,25 @@ angular.module('ui.sortable', [])
6363
ui.item.sortable._destroy();
6464
}
6565

66-
var opts = {};
66+
// return the index of ui.item among the items
67+
// we can't just do ui.item.index() because there it might have siblings
68+
// which are not items
69+
function getItemIndex(ui) {
70+
return ui.item.parent().find('> [ng-repeat], > [data-ng-repeat], > [x-ng-repeat]')
71+
.index(ui.item);
72+
}
73+
74+
// restrict the items option to include only ng-repeat items
75+
function restrictItemsOption(items) {
76+
items = items.trim();
77+
return items + '[ng-repeat],' + items + '[data-ng-repeat],' + items + '[x-ng-repeat]';
78+
}
79+
80+
var opts = {
81+
// this is jquery-ui sortable's default, we state it explicitly so even
82+
// if it's not specified, we will apply restrictItemsOption on it
83+
items: '> *'
84+
};
6785

6886
// directive specific options
6987
var directiveOpts = {
@@ -84,6 +102,8 @@ angular.module('ui.sortable', [])
84102

85103
angular.extend(opts, directiveOpts, uiSortableConfig, scope.uiSortable);
86104

105+
opts.items = restrictItemsOption(opts.items);
106+
87107
if (!angular.element.fn || !angular.element.fn.jquery) {
88108
$log.error('ui.sortable: jQuery should be included before AngularJS!');
89109
return;
@@ -114,9 +134,10 @@ angular.module('ui.sortable', [])
114134
}
115135

116136
// Save the starting position of dragged item
137+
var index = getItemIndex(ui);
117138
ui.item.sortable = {
118-
model: ngModel.$modelValue[ui.item.index()],
119-
index: ui.item.index(),
139+
model: ngModel.$modelValue[index],
140+
index: index,
120141
source: ui.item.parent(),
121142
sourceModel: ngModel.$modelValue,
122143
cancel: function () {
@@ -184,7 +205,7 @@ angular.module('ui.sortable', [])
184205
// update that happens when moving between lists because then
185206
// the value will be overwritten with the old value
186207
if(!ui.item.sortable.received) {
187-
ui.item.sortable.dropindex = ui.item.index();
208+
ui.item.sortable.dropindex = getItemIndex(ui);
188209
var droptarget = ui.item.parent();
189210
ui.item.sortable.droptarget = droptarget;
190211

@@ -325,6 +346,10 @@ angular.module('ui.sortable', [])
325346
value = wrappers[key](value);
326347
}
327348

349+
if (key === 'items') {
350+
value = restrictItemsOption(value);
351+
}
352+
328353
opts[key] = value;
329354
element.sortable('option', key, value);
330355
});

0 commit comments

Comments
 (0)