Browse Source

#231: correct 'clone' workaround

pull/242/head
RubaXa 10 years ago
parent
commit
d823817419
  1. 28
      ng-sortable.js

28
ng-sortable.js

@ -17,7 +17,8 @@
angular.module('ng-sortable', []) angular.module('ng-sortable', [])
.constant('$version', '0.3.3') .constant('$version', '0.3.3')
.directive('ngSortable', ['$parse', function ($parse) { .directive('ngSortable', ['$parse', function ($parse) {
var removed; var removed,
nextSibling;
function getSource(el) { function getSource(el) {
var scope = angular.element(el).scope(); var scope = angular.element(el).scope();
@ -70,19 +71,20 @@
prevItems = prevSource.items(); prevItems = prevSource.items();
oldIndex = prevItems.indexOf(prevSource.item(evt.item)); oldIndex = prevItems.indexOf(prevSource.item(evt.item));
removed = prevItems.splice(oldIndex, 1)[0]; removed = prevItems[oldIndex];
items.splice(newIndex, 0, removed);
if (evt.clone) { if (evt.clone) {
newIndex = Sortable.utils.index(evt.clone);
prevItems.splice(oldIndex, 0, removed);
evt.from.removeChild(evt.clone); evt.from.removeChild(evt.clone);
} }
else {
prevItems.splice(oldIndex, 1);
}
evt.from.appendChild(evt.item); // revert element items.splice(newIndex, 0, removed);
} else {
evt.from.insertBefore(evt.item, nextSibling); // revert element
}
else {
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]); items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
} }
@ -94,17 +96,18 @@
opts[name] = opts[name] || options[name]; opts[name] = opts[name] || options[name];
return opts; return opts;
}, { }, {
onStart: function () { onStart: function (/**Event*/) {
nextSibling = evt.item.nextSibling;
options.onStart(source.items()); options.onStart(source.items());
}, },
onEnd: function () { onEnd: function () {
options.onEnd(source.items()); options.onEnd(source.items());
}, },
onAdd: function (evt) { onAdd: function (/**Event*/evt) {
_sync(evt); _sync(evt);
options.onAdd(source.items(), removed); options.onAdd(source.items(), removed);
}, },
onUpdate: function (evt) { onUpdate: function (/**Event*/evt) {
_sync(evt); _sync(evt);
options.onUpdate(source.items(), source.item(evt.item)); options.onUpdate(source.items(), source.item(evt.item));
}, },
@ -119,6 +122,7 @@
$el.on('$destroy', function () { $el.on('$destroy', function () {
sortable.destroy(); sortable.destroy();
sortable = null; sortable = null;
nextSibling = null;
}); });
if (ngSortable && !/{|}/.test(ngSortable)) { // todo: ugly if (ngSortable && !/{|}/.test(ngSortable)) { // todo: ugly

Loading…
Cancel
Save