From 85292d9558472686321ff12d973cdb5c65f823ff Mon Sep 17 00:00:00 2001 From: RubaXa Date: Mon, 9 Feb 2015 18:10:20 +0300 Subject: [PATCH 1/2] #236: + ngSortEvent --- README.md | 8 +++++++- ng-sortable.js | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d548c23..132d385 100644 --- a/README.md +++ b/README.md @@ -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 + } + }; }]); ``` diff --git a/ng-sortable.js b/ng-sortable.js index d4fe47f..ec4aba6 100644 --- a/ng-sortable.js +++ b/ng-sortable.js @@ -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) { @@ -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(); @@ -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)); } })); From 55ae4457ca7bd8de31b0a98d527f8a0cb3204212 Mon Sep 17 00:00:00 2001 From: RubaXa Date: Mon, 9 Feb 2015 18:27:02 +0300 Subject: [PATCH 2/2] #267: + support without ng-repeat --- ng-sortable.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ng-sortable.js b/ng-sortable.js index ec4aba6..bef396c 100644 --- a/ng-sortable.js +++ b/ng-sortable.js @@ -39,6 +39,11 @@ ); })[0]; + if (!ngRepeat) { + // Without ng-repeat + return null; + } + // tests: http://jsbin.com/kosubutilo/1/edit?js,output ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/); @@ -74,7 +79,7 @@ /* jshint expr:true */ options[name] && options[name]({ model: item, - models: source.items(), + models: source && source.items(), oldIndex: evt.oldIndex, newIndex: evt.newIndex }); @@ -82,6 +87,11 @@ function _sync(/**Event*/evt) { + if (!source) { + // Without ng-repeat + return; + } + var oldIndex = evt.oldIndex, newIndex = evt.newIndex, items = source.items(); @@ -133,13 +143,13 @@ }, onUpdate: function (/**Event*/evt) { _sync(evt); - _emitEvent(evt, source.item(evt.item)); + _emitEvent(evt, source && source.item(evt.item)); }, onRemove: function (/**Event*/evt) { _emitEvent(evt, removed); }, onSort: function (/**Event*/evt) { - _emitEvent(evt, source.item(evt.item)); + _emitEvent(evt, source && source.item(evt.item)); } }));