Skip to content

Instantly share code, notes, and snippets.

@nl5887
Created December 20, 2013 14:24
Show Gist options
  • Select an option

  • Save nl5887/8055449 to your computer and use it in GitHub Desktop.

Select an option

Save nl5887/8055449 to your computer and use it in GitHub Desktop.
// based on http://jsfiddle.net/7eTMV/20/
// https://github.com/angular-ui/ui-sortable
// https://github.com/johnny/jquery-sortable
angular.module('ui.directives').directive('uiSortable', [
'ui.config', function(uiConfig) {
var options;
options = {};
if (uiConfig.sortable != null) {
angular.extend(options, uiConfig.sortable);
}
return {
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
var onStart, onUpdate, opts, _start, _update;
opts = angular.extend({}, options, scope.$eval(attrs.uiSortable));
if (ngModel != null) {
onStart = function($item, container, _super) {
return $item.data('ui-sortable-start', $item.index());
};
onUpdate = function($item, container, _super) {
var end, start;
start = $item.data('ui-sortable-start');
end = $item.index();
ngModel.$modelValue.splice(end, 0, ngModel.$modelValue.splice(start, 1)[0]);
return scope.$apply();
};
if (opts.onDragStart != null) {
_start = opts.onDragStart;
opts.onDragStart = function($item, container, _super) {
onStart($item, container, _super);
_start($item, container, _super);
return scope.$apply();
};
} else {
opts.onDragStart = onStart;
}
if (opts.onDrop != null) {
_update = opts.onDrop;
opts.onDrop = function($item, targetContainer, _super) {
onUpdate($item, targetContainer, _super);
_update($item, targetContainer, _super);
return scope.$apply();
};
} else {
opts.onDrop = onUpdate;
}
}
return element.sortable(opts);
}
};
}
]);
@nl5887

nl5887 commented Dec 20, 2013

Copy link
Copy Markdown
Author

jQuery Sortable directive for AngularJS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment