Created
December 20, 2013 14:24
-
-
Save nl5887/8055449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jQuery Sortable directive for AngularJS.