Browse Source

#236: + ngSortEvent

ng-sortable
RubaXa 10 years ago
parent
commit
85292d9558
  1. 8
      README.md
  2. 44
      ng-sortable.js

8
README.md

@ -274,7 +274,13 @@ angular.module('myApp', ['ng-sortable'])
$scope.items = ['item 1', 'item 2']; $scope.items = ['item 1', 'item 2'];
$scope.foo = ['foo 1', '..']; $scope.foo = ['foo 1', '..'];
$scope.bar = ['bar 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
}
};
}]); }]);
``` ```

44
ng-sortable.js

@ -14,6 +14,16 @@
})(function (angular, Sortable) { })(function (angular, Sortable) {
'use strict'; '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', []) angular.module('ng-sortable', [])
.constant('$version', '0.3.5') .constant('$version', '0.3.5')
.directive('ngSortable', ['$parse', function ($parse) { .directive('ngSortable', ['$parse', function ($parse) {
@ -58,12 +68,20 @@
; ;
'Start End Add Update Remove Sort'.split(' ').forEach(function (name) { function _emitEvent(/**Event*/evt, /*Mixed*/item) {
options['on' + name] = options['on' + name] || function () {}; 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, var oldIndex = evt.oldIndex,
newIndex = evt.newIndex, newIndex = evt.newIndex,
items = source.items(); items = source.items();
@ -101,27 +119,27 @@
}, { }, {
onStart: function (/**Event*/evt) { onStart: function (/**Event*/evt) {
nextSibling = evt.item.nextSibling; nextSibling = evt.item.nextSibling;
options.onStart(source.items()); _emitEvent(evt);
scope.$apply(); scope.$apply();
}, },
onEnd: function () { onEnd: function (/**Event*/evt) {
options.onEnd(source.items()); _emitEvent(evt, removed);
scope.$apply(); scope.$apply();
}, },
onAdd: function (/**Event*/evt) { onAdd: function (/**Event*/evt) {
_sync(evt); _sync(evt);
options.onAdd(source.items(), removed); _emitEvent(evt, removed);
scope.$apply(); scope.$apply();
}, },
onUpdate: function (/**Event*/evt) { onUpdate: function (/**Event*/evt) {
_sync(evt); _sync(evt);
options.onUpdate(source.items(), source.item(evt.item)); _emitEvent(evt, source.item(evt.item));
}, },
onRemove: function () { onRemove: function (/**Event*/evt) {
options.onRemove(source.items(), removed); _emitEvent(evt, removed);
}, },
onSort: function () { onSort: function (/**Event*/evt) {
options.onSort(source.items()); _emitEvent(evt, source.item(evt.item));
} }
})); }));

Loading…
Cancel
Save