Skip to content

Commit

Permalink
#236: + ngSortEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
RubaXa committed Feb 9, 2015
1 parent 21bf07a commit 85292d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ angular.module('myApp', ['ng-sortable'])
$scope.items = ['item 1', 'item 2'];
$scope.foo = ['foo 1', '..'];
$scope.bar = ['bar 1', '..'];
$scope.barConfig = { group: 'foobar', animation: 150 };
$scope.barConfig = {
group: 'foobar',
animation: 150,
onSort: function (/** ngSortEvent */evt){
// @see https://github.com/RubaXa/Sortable/blob/master/ng-sortable.js#L18-L24
}
};
}]);
```

Expand Down
44 changes: 31 additions & 13 deletions ng-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
})(function (angular, Sortable) {
'use strict';


/**
* @typedef {Object} ngSortEvent
* @property {*} model List item
* @property {Object|Array} models List of items
* @property {number} oldIndex before sort
* @property {number} newIndex after sort
*/


angular.module('ng-sortable', [])
.constant('$version', '0.3.5')
.directive('ngSortable', ['$parse', function ($parse) {
Expand Down Expand Up @@ -58,12 +68,20 @@
;


'Start End Add Update Remove Sort'.split(' ').forEach(function (name) {
options['on' + name] = options['on' + name] || function () {};
});
function _emitEvent(/**Event*/evt, /*Mixed*/item) {
var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1);

/* jshint expr:true */
options[name] && options[name]({
model: item,
models: source.items(),
oldIndex: evt.oldIndex,
newIndex: evt.newIndex
});
}


function _sync(evt) {
function _sync(/**Event*/evt) {
var oldIndex = evt.oldIndex,
newIndex = evt.newIndex,
items = source.items();
Expand Down Expand Up @@ -101,27 +119,27 @@
}, {
onStart: function (/**Event*/evt) {
nextSibling = evt.item.nextSibling;
options.onStart(source.items());
_emitEvent(evt);
scope.$apply();
},
onEnd: function () {
options.onEnd(source.items());
onEnd: function (/**Event*/evt) {
_emitEvent(evt, removed);
scope.$apply();
},
onAdd: function (/**Event*/evt) {
_sync(evt);
options.onAdd(source.items(), removed);
_emitEvent(evt, removed);
scope.$apply();
},
onUpdate: function (/**Event*/evt) {
_sync(evt);
options.onUpdate(source.items(), source.item(evt.item));
_emitEvent(evt, source.item(evt.item));
},
onRemove: function () {
options.onRemove(source.items(), removed);
onRemove: function (/**Event*/evt) {
_emitEvent(evt, removed);
},
onSort: function () {
options.onSort(source.items());
onSort: function (/**Event*/evt) {
_emitEvent(evt, source.item(evt.item));
}
}));

Expand Down

0 comments on commit 85292d9

Please sign in to comment.