From a1c5c8bd9320886c069d87e32e5e5db9ac42c4f0 Mon Sep 17 00:00:00 2001 From: Thibaut Tropardy Date: Sat, 17 Jun 2017 00:40:26 +0200 Subject: [PATCH] beforeDropPromise back to originalPosition --- src/angular-dragdrop.js | 9 ++++++++- src/angular-dragdrop.min.js | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/angular-dragdrop.js b/src/angular-dragdrop.js index 5522936..39cf143 100644 --- a/src/angular-dragdrop.js +++ b/src/angular-dragdrop.js @@ -387,7 +387,14 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti ngDragDropService.callEventCallback(scope, dropSettings.onDrop, event, ui); } }), function() { - ui.draggable.animate({left: '', top: ''}, jqyouiOptions.revertDuration || 0); + var originalPosition=ui.draggable.data('uiDraggable').originalPosition; + if(!originalPosition){ + originalPosition={ + left:'', + top:'', + } + } + ui.draggable.animate(originalPosition, jqyouiOptions.revertDuration || 0); }); } }); diff --git a/src/angular-dragdrop.min.js b/src/angular-dragdrop.min.js index fbcb739..f71c98f 100644 --- a/src/angular-dragdrop.min.js +++ b/src/angular-dragdrop.min.js @@ -22,8 +22,8 @@ * Implementing Drag and Drop functionality in AngularJS is easier than ever. * Demo: http://codef0rmer.github.com/angular-dragdrop/ * - * @version 1.0.13 + * @version 1.0.13+ * * (c) 2013 Amit Gharat a.k.a codef0rmer - amitgharat.wordpress.com */ -!function(e,a,t,n){"use strict";var i=a.module("ngDragDrop",[]).service("ngDragDropService",["$timeout","$parse","$q",function(r,l,o){this.draggableScope=null,this.droppableScope=null,t("head").prepend(''),this.callEventCallback=function(e,a,n,i){function r(a){var n=-1!==a.indexOf("(")?a.indexOf("("):a.length,i=-1!==a.lastIndexOf(")")?a.lastIndexOf(")"):a.length,r=a.substring(n+1,i),o=-1!==a.indexOf(".")?a.substr(0,a.indexOf(".")):null;return o=e[o]&&"function"==typeof e[o].constructor?o:null,{callback:a.substring(o&&o.length+1||0,n),args:t.map(r&&r.split(",")||[],function(a){return[l(a)(e)]}),constructor:o}}if(a){var o=r(a),d=o.callback,s=o.constructor,p=[n,i].concat(o.args);return(e[d]||e[s][d]).apply(e[d]?e:e[s],p)}},this.invokeDrop=function(e,l,d,s){var p,c,u,g="",f="",b={},h={},v=null,y={},x={},m=null,D=this.droppableScope,q=this.draggableScope,j=null,k=[];g=e.ngattr("ng-model"),f=l.ngattr("ng-model"),p=q.$eval(g),c=D.$eval(f),m=l.find("[jqyoui-draggable]:last,[data-jqyoui-draggable]:last"),h=D.$eval(l.attr("jqyoui-droppable")||l.attr("data-jqyoui-droppable"))||[],b=q.$eval(e.attr("jqyoui-draggable")||e.attr("data-jqyoui-draggable"))||[],b.index=this.fixIndex(q,b,p),h.index=this.fixIndex(D,h,c),v=a.isArray(p)?b.index:null,y=a.isArray(p)?p[v]:p,b.deepCopy&&(y=a.copy(y)),x=a.isArray(c)&&h&&h.index!==n?c[h.index]:a.isArray(c)?{}:c,h.deepCopy&&(x=a.copy(x)),b.beforeDrop&&k.push(this.callEventCallback(q,b.beforeDrop,d,s)),o.all(k).then(a.bind(this,function(){if(b.insertInline&&g===f){if(b.index>h.index){u=p[b.index];for(var n=b.index;n>h.index;n--)c[n]=a.copy(c[n-1]),c[n-1]={},c[n][b.direction]="left";c[h.index]=u}else{u=p[b.index];for(var n=b.index;n0?m:l,null,"fast",h,function(){j.remove()}),this.move(m.length>0&&!h.multiple?m:[],e.parent("[jqyoui-droppable],[data-jqyoui-droppable]"),i.startXY,"fast",h,a.bind(this,function(){r(a.bind(this,function(){e.css({position:"relative",left:"",top:""}).removeClass("angular-dragdrop-hide"),m.css({position:"relative",left:"",top:"",display:"none"===m.css("display")?"":m.css("display")}),this.mutateDraggable(q,h,b,g,f,x,e),this.mutateDroppable(D,h,b,f,y,v),this.callEventCallback(D,h.onDrop,d,s)}))}))):r(a.bind(this,function(){this.mutateDraggable(q,h,b,g,f,x,e),this.mutateDroppable(D,h,b,f,y,v),this.callEventCallback(D,h.onDrop,d,s)}))}))["finally"](a.bind(this,function(){this.restore(e)}))},this.move=function(a,t,i,r,l,o){if(0===a.length)return o&&e.setTimeout(function(){o()},300),!1;var d=a.css("z-index"),s=a[l.containment||"offset"](),p=t.css("display"),c=t.hasClass("ng-hide"),u=t.hasClass("angular-dragdrop-hide");null===i&&t.length>0&&((t.attr("jqyoui-draggable")||t.attr("data-jqyoui-draggable"))!==n&&t.ngattr("ng-model")!==n&&t.is(":visible")&&l&&l.multiple?(i=t[l.containment||"offset"](),l.stack===!1?i.left+=t.outerWidth(!0):i.top+=t.outerHeight(!0)):(c&&t.removeClass("ng-hide"),u&&t.removeClass("angular-dragdrop-hide"),i=t.css({visibility:"hidden",display:"block"})[l.containment||"offset"](),t.css({visibility:"",display:p}))),a.css({position:"absolute","z-index":9999}).css(s).animate(i,r,function(){c&&t.addClass("ng-hide"),u&&t.addClass("angular-dragdrop-hide"),a.css("z-index",d),o&&o()})},this.mutateDroppable=function(e,t,n,i,r,o){var d=e.$eval(i);e.dndDragItem=r,a.isArray(d)?(t&&t.index>=0?d[t.index]=r:d.push(r),n&&n.placeholder===!0&&(d[d.length-1].jqyoui_pos=o)):(l(i+" = dndDragItem")(e),n&&n.placeholder===!0&&(d.jqyoui_pos=o))},this.mutateDraggable=function(e,t,i,r,o,d,s){var p=a.equals(d,{})||!d,c=e.$eval(r);e.dndDropItem=d,i&&i.placeholder?"keep"!=i.placeholder&&(a.isArray(c)&&i.index!==n?c[i.index]=d:l(r+" = dndDropItem")(e)):a.isArray(c)?p?i&&i.placeholder!==!0&&"keep"!==i.placeholder&&c.splice(i.index,1):c[i.index]=d:(l(r+" = dndDropItem")(e),e.$parent&&l(r+" = dndDropItem")(e.$parent)),this.restore(s)},this.restore=function(e){e.css({"z-index":"",left:"",top:""})},this.fixIndex=function(e,t,i){if(t.applyFilter&&a.isArray(i)&&i.length>0){var r=e[t.applyFilter](),l=r[t.index],o=n;return i.forEach(function(e,t){a.equals(e,l)&&(o=t)}),o}return t.index}}]).directive("jqyouiDraggable",["ngDragDropService",function(e){return{require:"?jqyouiDroppable",restrict:"A",link:function(n,r,l){var o,d,s,p,c=t(r),u=function(r,u){r?(o=n.$eval(c.attr("jqyoui-draggable")||c.attr("data-jqyoui-draggable"))||{},d=n.$eval(l.jqyouiOptions)||{},c.draggable({disabled:!1}).draggable(d).draggable({start:function(a,r){e.draggableScope=n,s=t(d.helper?r.helper:this).css("z-index"),t(d.helper?r.helper:this).css("z-index",9999),i.startXY=t(this)[o.containment||"offset"](),e.callEventCallback(n,o.onStart,a,r)},stop:function(a,i){t(d.helper?i.helper:this).css("z-index",s),e.callEventCallback(n,o.onStop,a,i)},drag:function(a,t){e.callEventCallback(n,o.onDrag,a,t)}})):c.draggable({disabled:!0}),p&&a.isDefined(r)&&(a.equals(l.drag,"true")||a.equals(l.drag,"false"))&&(p(),p=null)};p=n.$watch(function(){return n.$eval(l.drag)},u),u(),c.on("$destroy",function(){c.draggable({disabled:!0}).draggable("destroy")})}}}]).directive("jqyouiDroppable",["ngDragDropService","$q",function(e,n){return{restrict:"A",priority:1,link:function(i,r,l){var o,d,s,p=t(r),c=function(r,c){r?(o=i.$eval(t(p).attr("jqyoui-droppable")||t(p).attr("data-jqyoui-droppable"))||{},d=i.$eval(l.jqyouiOptions)||{},p.droppable({disabled:!1}).droppable(d).droppable({over:function(a,t){e.callEventCallback(i,o.onOver,a,t)},out:function(a,t){e.callEventCallback(i,o.onOut,a,t)},drop:function(r,s){var p=null;p=o.beforeDrop?e.callEventCallback(i,o.beforeDrop,r,s):function(){var e=n.defer();return e.resolve(),e.promise}(),p.then(a.bind(this,function(){t(s.draggable).ngattr("ng-model")&&l.ngModel?(e.droppableScope=i,e.invokeDrop(t(s.draggable),t(this),r,s)):e.callEventCallback(i,o.onDrop,r,s)}),function(){s.draggable.animate({left:"",top:""},d.revertDuration||0)})}})):p.droppable({disabled:!0}),s&&a.isDefined(r)&&(a.equals(l.drop,"true")||a.equals(l.drop,"false"))&&(s(),s=null)};s=i.$watch(function(){return i.$eval(l.drop)},c),c(),p.on("$destroy",function(){p.droppable({disabled:!0}).droppable("destroy")})}}}]);t.fn.ngattr=function(e,a){var t=this[0];return t.getAttribute(e)||t.getAttribute("data-"+e)}}(window,window.angular,window.jQuery); \ No newline at end of file +!function(window,angular,$,undefined){"use strict";var jqyoui=angular.module("ngDragDrop",[]).service("ngDragDropService",["$timeout","$parse","$q",function($timeout,$parse,$q){this.draggableScope=null,this.droppableScope=null,$("head").prepend(''),this.callEventCallback=function(scope,callbackName,event,ui){if(callbackName){var objExtract=function(callbackName){var atStartBracket=-1!==callbackName.indexOf("(")?callbackName.indexOf("("):callbackName.length,atEndBracket=-1!==callbackName.lastIndexOf(")")?callbackName.lastIndexOf(")"):callbackName.length,args=callbackName.substring(atStartBracket+1,atEndBracket),constructor=-1!==callbackName.indexOf(".")?callbackName.substr(0,callbackName.indexOf(".")):null;return constructor=scope[constructor]&&"function"==typeof scope[constructor].constructor?constructor:null,{callback:callbackName.substring(constructor&&constructor.length+1||0,atStartBracket),args:$.map(args&&args.split(",")||[],function(item){return[$parse(item)(scope)]}),constructor:constructor}}(callbackName),callback=objExtract.callback,constructor=objExtract.constructor,args=[event,ui].concat(objExtract.args);return(scope[callback]||scope[constructor][callback]).apply(scope[callback]?scope:scope[constructor],args)}},this.invokeDrop=function($draggable,$droppable,event,ui){var dragModelValue,dropModelValue,temp,dragModel="",dropModel="",dragSettings={},dropSettings={},jqyoui_pos=null,dragItem={},dropItem={},$droppableDraggable=null,droppableScope=this.droppableScope,draggableScope=this.draggableScope,$helper=null,promises=[];dragModel=$draggable.ngattr("ng-model"),dropModel=$droppable.ngattr("ng-model"),dragModelValue=draggableScope.$eval(dragModel),dropModelValue=droppableScope.$eval(dropModel),$droppableDraggable=$droppable.find("[jqyoui-draggable]:last,[data-jqyoui-draggable]:last"),dropSettings=droppableScope.$eval($droppable.attr("jqyoui-droppable")||$droppable.attr("data-jqyoui-droppable"))||[],(dragSettings=draggableScope.$eval($draggable.attr("jqyoui-draggable")||$draggable.attr("data-jqyoui-draggable"))||[]).index=this.fixIndex(draggableScope,dragSettings,dragModelValue),dropSettings.index=this.fixIndex(droppableScope,dropSettings,dropModelValue),jqyoui_pos=angular.isArray(dragModelValue)?dragSettings.index:null,dragItem=angular.isArray(dragModelValue)?dragModelValue[jqyoui_pos]:dragModelValue,dragSettings.deepCopy&&(dragItem=angular.copy(dragItem)),dropItem=angular.isArray(dropModelValue)&&dropSettings&&void 0!==dropSettings.index?dropModelValue[dropSettings.index]:angular.isArray(dropModelValue)?{}:dropModelValue,dropSettings.deepCopy&&(dropItem=angular.copy(dropItem)),dragSettings.beforeDrop&&promises.push(this.callEventCallback(draggableScope,dragSettings.beforeDrop,event,ui)),$q.all(promises).then(angular.bind(this,function(){if(dragSettings.insertInline&&dragModel===dropModel){if(dragSettings.index>dropSettings.index){temp=dragModelValue[dragSettings.index];for(i=dragSettings.index;i>dropSettings.index;i--)dropModelValue[i]=angular.copy(dropModelValue[i-1]),dropModelValue[i-1]={},dropModelValue[i][dragSettings.direction]="left";dropModelValue[dropSettings.index]=temp}else{temp=dragModelValue[dragSettings.index];for(var i=dragSettings.index;i0?$droppableDraggable:$droppable,null,"fast",dropSettings,function(){$helper.remove()}),this.move($droppableDraggable.length>0&&!dropSettings.multiple?$droppableDraggable:[],$draggable.parent("[jqyoui-droppable],[data-jqyoui-droppable]"),jqyoui.startXY,"fast",dropSettings,angular.bind(this,function(){$timeout(angular.bind(this,function(){$draggable.css({position:"relative",left:"",top:""}).removeClass("angular-dragdrop-hide"),$droppableDraggable.css({position:"relative",left:"",top:"",display:"none"===$droppableDraggable.css("display")?"":$droppableDraggable.css("display")}),this.mutateDraggable(draggableScope,dropSettings,dragSettings,dragModel,dropModel,dropItem,$draggable),this.mutateDroppable(droppableScope,dropSettings,dragSettings,dropModel,dragItem,jqyoui_pos),this.callEventCallback(droppableScope,dropSettings.onDrop,event,ui)}))}))):$timeout(angular.bind(this,function(){this.mutateDraggable(draggableScope,dropSettings,dragSettings,dragModel,dropModel,dropItem,$draggable),this.mutateDroppable(droppableScope,dropSettings,dragSettings,dropModel,dragItem,jqyoui_pos),this.callEventCallback(droppableScope,dropSettings.onDrop,event,ui)}))})).finally(angular.bind(this,function(){this.restore($draggable)}))},this.move=function($fromEl,$toEl,toPos,duration,dropSettings,callback){if(0===$fromEl.length)return callback&&window.setTimeout(function(){callback()},300),!1;var zIndex=$fromEl.css("z-index"),fromPos=$fromEl[dropSettings.containment||"offset"](),displayProperty=$toEl.css("display"),hadNgHideCls=$toEl.hasClass("ng-hide"),hadDNDHideCls=$toEl.hasClass("angular-dragdrop-hide");null===toPos&&$toEl.length>0&&(void 0!==($toEl.attr("jqyoui-draggable")||$toEl.attr("data-jqyoui-draggable"))&&void 0!==$toEl.ngattr("ng-model")&&$toEl.is(":visible")&&dropSettings&&dropSettings.multiple?(toPos=$toEl[dropSettings.containment||"offset"](),!1===dropSettings.stack?toPos.left+=$toEl.outerWidth(!0):toPos.top+=$toEl.outerHeight(!0)):(hadNgHideCls&&$toEl.removeClass("ng-hide"),hadDNDHideCls&&$toEl.removeClass("angular-dragdrop-hide"),toPos=$toEl.css({visibility:"hidden",display:"block"})[dropSettings.containment||"offset"](),$toEl.css({visibility:"",display:displayProperty}))),$fromEl.css({position:"absolute","z-index":9999}).css(fromPos).animate(toPos,duration,function(){hadNgHideCls&&$toEl.addClass("ng-hide"),hadDNDHideCls&&$toEl.addClass("angular-dragdrop-hide"),$fromEl.css("z-index",zIndex),callback&&callback()})},this.mutateDroppable=function(scope,dropSettings,dragSettings,dropModel,dragItem,jqyoui_pos){var dropModelValue=scope.$eval(dropModel);scope.dndDragItem=dragItem,angular.isArray(dropModelValue)?(dropSettings&&dropSettings.index>=0?dropModelValue[dropSettings.index]=dragItem:dropModelValue.push(dragItem),dragSettings&&!0===dragSettings.placeholder&&(dropModelValue[dropModelValue.length-1].jqyoui_pos=jqyoui_pos)):($parse(dropModel+" = dndDragItem")(scope),dragSettings&&!0===dragSettings.placeholder&&(dropModelValue.jqyoui_pos=jqyoui_pos))},this.mutateDraggable=function(scope,dropSettings,dragSettings,dragModel,dropModel,dropItem,$draggable){var isEmpty=angular.equals(dropItem,{})||!dropItem,dragModelValue=scope.$eval(dragModel);scope.dndDropItem=dropItem,dragSettings&&dragSettings.placeholder?"keep"!=dragSettings.placeholder&&(angular.isArray(dragModelValue)&&void 0!==dragSettings.index?dragModelValue[dragSettings.index]=dropItem:$parse(dragModel+" = dndDropItem")(scope)):angular.isArray(dragModelValue)?isEmpty?dragSettings&&!0!==dragSettings.placeholder&&"keep"!==dragSettings.placeholder&&dragModelValue.splice(dragSettings.index,1):dragModelValue[dragSettings.index]=dropItem:($parse(dragModel+" = dndDropItem")(scope),scope.$parent&&$parse(dragModel+" = dndDropItem")(scope.$parent)),this.restore($draggable)},this.restore=function($draggable){$draggable.css({"z-index":"",left:"",top:""})},this.fixIndex=function(scope,settings,modelValue){if(settings.applyFilter&&angular.isArray(modelValue)&&modelValue.length>0){var lookup=scope[settings.applyFilter]()[settings.index],actualIndex=void 0;return modelValue.forEach(function(item,i){angular.equals(item,lookup)&&(actualIndex=i)}),actualIndex}return settings.index}}]).directive("jqyouiDraggable",["ngDragDropService",function(ngDragDropService){return{require:"?jqyouiDroppable",restrict:"A",link:function(scope,elem,attrs){var dragSettings,jqyouiOptions,zIndex,killWatcher,element=$(elem),updateDraggable=function(newValue,oldValue){newValue?(dragSettings=scope.$eval(element.attr("jqyoui-draggable")||element.attr("data-jqyoui-draggable"))||{},jqyouiOptions=scope.$eval(attrs.jqyouiOptions)||{},element.draggable({disabled:!1}).draggable(jqyouiOptions).draggable({start:function(event,ui){ngDragDropService.draggableScope=scope,zIndex=$(jqyouiOptions.helper?ui.helper:this).css("z-index"),$(jqyouiOptions.helper?ui.helper:this).css("z-index",9999),jqyoui.startXY=$(this)[dragSettings.containment||"offset"](),ngDragDropService.callEventCallback(scope,dragSettings.onStart,event,ui)},stop:function(event,ui){$(jqyouiOptions.helper?ui.helper:this).css("z-index",zIndex),ngDragDropService.callEventCallback(scope,dragSettings.onStop,event,ui)},drag:function(event,ui){ngDragDropService.callEventCallback(scope,dragSettings.onDrag,event,ui)}})):element.draggable({disabled:!0}),killWatcher&&angular.isDefined(newValue)&&(angular.equals(attrs.drag,"true")||angular.equals(attrs.drag,"false"))&&(killWatcher(),killWatcher=null)};killWatcher=scope.$watch(function(){return scope.$eval(attrs.drag)},updateDraggable),updateDraggable(),element.on("$destroy",function(){element.draggable({disabled:!0}).draggable("destroy")})}}}]).directive("jqyouiDroppable",["ngDragDropService","$q",function(ngDragDropService,$q){return{restrict:"A",priority:1,link:function(scope,elem,attrs){var dropSettings,jqyouiOptions,killWatcher,element=$(elem),updateDroppable=function(newValue,oldValue){newValue?(dropSettings=scope.$eval($(element).attr("jqyoui-droppable")||$(element).attr("data-jqyoui-droppable"))||{},jqyouiOptions=scope.$eval(attrs.jqyouiOptions)||{},element.droppable({disabled:!1}).droppable(jqyouiOptions).droppable({over:function(event,ui){ngDragDropService.callEventCallback(scope,dropSettings.onOver,event,ui)},out:function(event,ui){ngDragDropService.callEventCallback(scope,dropSettings.onOut,event,ui)},drop:function(event,ui){(dropSettings.beforeDrop?ngDragDropService.callEventCallback(scope,dropSettings.beforeDrop,event,ui):function(){var deferred=$q.defer();return deferred.resolve(),deferred.promise}()).then(angular.bind(this,function(){$(ui.draggable).ngattr("ng-model")&&attrs.ngModel?(ngDragDropService.droppableScope=scope,ngDragDropService.invokeDrop($(ui.draggable),$(this),event,ui)):ngDragDropService.callEventCallback(scope,dropSettings.onDrop,event,ui)}),function(){var originalPosition=ui.draggable.data("uiDraggable").originalPosition;originalPosition||(originalPosition={left:"",top:""}),ui.draggable.animate(originalPosition,jqyouiOptions.revertDuration||0)})}})):element.droppable({disabled:!0}),killWatcher&&angular.isDefined(newValue)&&(angular.equals(attrs.drop,"true")||angular.equals(attrs.drop,"false"))&&(killWatcher(),killWatcher=null)};killWatcher=scope.$watch(function(){return scope.$eval(attrs.drop)},updateDroppable),updateDroppable(),element.on("$destroy",function(){element.droppable({disabled:!0}).droppable("destroy")})}}}]);$.fn.ngattr=function(name,value){var element=this[0];return element.getAttribute(name)||element.getAttribute("data-"+name)}}(window,window.angular,window.jQuery); \ No newline at end of file