diff --git a/bower.json b/bower.json index e800372..3d75624 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-ui-sortable", - "version": "0.12.2", + "version": "0.12.3", "main": "./sortable.js", "dependencies": { "angular": "~1.2.x", diff --git a/sortable.js b/sortable.js index f9a134c..c6de7f2 100644 --- a/sortable.js +++ b/sortable.js @@ -44,6 +44,8 @@ angular.module('ui.sortable', []) scope.$watch(attrs.ngModel+'.length', function() { // Timeout to let ng-repeat modify the DOM $timeout(function() { + // ensure that the jquery-ui-sortable widget instance + // is still bound to the directive's element if (!!element.data('ui-sortable')) { element.sortable('refresh'); } @@ -176,18 +178,23 @@ angular.module('ui.sortable', []) }; scope.$watch(attrs.uiSortable, function(newVal /*, oldVal*/) { - angular.forEach(newVal, function(value, key) { - if(callbacks[key]) { - if( key === 'stop' ){ - // call apply after stop - value = combineCallbacks( - value, function() { scope.$apply(); }); + // ensure that the jquery-ui-sortable widget instance + // is still bound to the directive's element + if (!!element.data('ui-sortable')) { + angular.forEach(newVal, function(value, key) { + if(callbacks[key]) { + if( key === 'stop' ){ + // call apply after stop + value = combineCallbacks( + value, function() { scope.$apply(); }); + } + // wrap the callback + value = combineCallbacks(callbacks[key], value); } - // wrap the callback - value = combineCallbacks(callbacks[key], value); - } - element.sortable('option', key, value); - }); + + element.sortable('option', key, value); + }); + } }, true); angular.forEach(callbacks, function(value, key) { diff --git a/sortable.min.js b/sortable.min.js index 4aaff97..ec0a285 100644 --- a/sortable.min.js +++ b/sortable.min.js @@ -1 +1 @@ -!function(a,b){"use strict";b.module("ui.sortable",[]).value("uiSortableConfig",{}).directive("uiSortable",["uiSortableConfig","$timeout","$log",function(a,c,d){return{require:"?ngModel",link:function(e,f,g,h){function i(a,b){return b&&"function"==typeof b?function(c,d){a(c,d),b(c,d)}:a}var j,k={},l={receive:null,remove:null,start:null,stop:null,update:null};b.extend(k,a),h?(e.$watch(g.ngModel+".length",function(){c(function(){f.data("ui-sortable")&&f.sortable("refresh")})}),l.start=function(a,b){b.item.sortable={index:b.item.index(),cancel:function(){b.item.sortable._isCanceled=!0},isCanceled:function(){return b.item.sortable._isCanceled},_isCanceled:!1}},l.activate=function(){j=f.contents();var a=f.sortable("option","placeholder");if(a&&a.element&&"function"==typeof a.element){var c=a.element();c.jquery||(c=b.element(c));var d=f.find('[class="'+c.attr("class")+'"]');j=j.not(d)}},l.update=function(a,b){b.item.sortable.received||(b.item.sortable.dropindex=b.item.index(),b.item.sortable.droptarget=b.item.parent(),f.sortable("cancel")),"clone"===f.sortable("option","helper")&&(j=j.not(j.last())),j.appendTo(f),b.item.sortable.received&&!b.item.sortable.isCanceled()&&e.$apply(function(){h.$modelValue.splice(b.item.sortable.dropindex,0,b.item.sortable.moved)})},l.stop=function(a,b){!b.item.sortable.received&&"dropindex"in b.item.sortable&&!b.item.sortable.isCanceled()?e.$apply(function(){h.$modelValue.splice(b.item.sortable.dropindex,0,h.$modelValue.splice(b.item.sortable.index,1)[0])}):"dropindex"in b.item.sortable&&!b.item.sortable.isCanceled()||"clone"===f.sortable("option","helper")||j.appendTo(f)},l.receive=function(a,b){b.item.sortable.received=!0},l.remove=function(a,b){b.item.sortable.isCanceled()||e.$apply(function(){b.item.sortable.moved=h.$modelValue.splice(b.item.sortable.index,1)[0]})},e.$watch(g.uiSortable,function(a){b.forEach(a,function(a,b){l[b]&&("stop"===b&&(a=i(a,function(){e.$apply()})),a=i(l[b],a)),f.sortable("option",b,a)})},!0),b.forEach(l,function(a,b){k[b]=i(a,k[b])})):d.info("ui.sortable: ngModel not provided!",f),f.sortable(k)}}}])}(window,window.angular); \ No newline at end of file +!function(a,b){"use strict";b.module("ui.sortable",[]).value("uiSortableConfig",{}).directive("uiSortable",["uiSortableConfig","$timeout","$log",function(a,c,d){return{require:"?ngModel",link:function(e,f,g,h){function i(a,b){return b&&"function"==typeof b?function(c,d){a(c,d),b(c,d)}:a}var j,k={},l={receive:null,remove:null,start:null,stop:null,update:null};b.extend(k,a),h?(e.$watch(g.ngModel+".length",function(){c(function(){f.data("ui-sortable")&&f.sortable("refresh")})}),l.start=function(a,b){b.item.sortable={index:b.item.index(),cancel:function(){b.item.sortable._isCanceled=!0},isCanceled:function(){return b.item.sortable._isCanceled},_isCanceled:!1}},l.activate=function(){j=f.contents();var a=f.sortable("option","placeholder");if(a&&a.element&&"function"==typeof a.element){var c=a.element();c.jquery||(c=b.element(c));var d=f.find('[class="'+c.attr("class")+'"]');j=j.not(d)}},l.update=function(a,b){b.item.sortable.received||(b.item.sortable.dropindex=b.item.index(),b.item.sortable.droptarget=b.item.parent(),f.sortable("cancel")),"clone"===f.sortable("option","helper")&&(j=j.not(j.last())),j.appendTo(f),b.item.sortable.received&&!b.item.sortable.isCanceled()&&e.$apply(function(){h.$modelValue.splice(b.item.sortable.dropindex,0,b.item.sortable.moved)})},l.stop=function(a,b){!b.item.sortable.received&&"dropindex"in b.item.sortable&&!b.item.sortable.isCanceled()?e.$apply(function(){h.$modelValue.splice(b.item.sortable.dropindex,0,h.$modelValue.splice(b.item.sortable.index,1)[0])}):"dropindex"in b.item.sortable&&!b.item.sortable.isCanceled()||"clone"===f.sortable("option","helper")||j.appendTo(f)},l.receive=function(a,b){b.item.sortable.received=!0},l.remove=function(a,b){b.item.sortable.isCanceled()||e.$apply(function(){b.item.sortable.moved=h.$modelValue.splice(b.item.sortable.index,1)[0]})},e.$watch(g.uiSortable,function(a){f.data("ui-sortable")&&b.forEach(a,function(a,b){l[b]&&("stop"===b&&(a=i(a,function(){e.$apply()})),a=i(l[b],a)),f.sortable("option",b,a)})},!0),b.forEach(l,function(a,b){k[b]=i(a,k[b])})):d.info("ui.sortable: ngModel not provided!",f),f.sortable(k)}}}])}(window,window.angular); \ No newline at end of file