Skip to content

Commit df1a58f

Browse files
committed
fix(*): support beforeDrop for draggable element that in being swapped into
Closes #165
1 parent 4883224 commit df1a58f

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ angular.module('myApp', ['ngDragDrop'])
4242
* **applyFilter** - string - applies AngularJS $filter on the list before swapping items. Only applicable, if ngRepeat has any filter (such as orderBy, limitTo) associated with it.
4343
* **containment** – string - position/offset. Offset by default. This forces to use jQuery.position() or jQuery.offset() to calculate proper position with respect to parent element or document respectively.
4444
* **deepCopy** - boolean (optional) – If true, makes a deep copy of draggable that looses prototypical inheritance.
45+
* **beforeDrop** – promise (optional) – Ask for confirmation before swapping. Works with both window.confirm and custom popup.
4546
* **data-drag** – boolean – If true, element can be draggable. Disabled otherwise.
4647
* **data-jqyoui-options** – object – should hold all the valid options supported by [jQueryUI Draggable](http://api.jqueryui.com/draggable)
4748
* **ng-model** – string – An angular model defined in a controller. Should be a JS array or object

src/angular-dragdrop.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
(function (window, angular, $, undefined) {
3131
'use strict';
3232

33-
var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$timeout', '$parse', function($timeout, $parse) {
33+
var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$timeout', '$parse', '$q', function($timeout, $parse, $q) {
3434
this.draggableScope = null;
3535
this.droppableScope = null;
3636

@@ -51,7 +51,7 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
5151
var atStartBracket = callbackName.indexOf('(') !== -1 ? callbackName.indexOf('(') : callbackName.length,
5252
atEndBracket = callbackName.lastIndexOf(')') !== -1 ? callbackName.lastIndexOf(')') : callbackName.length,
5353
args = callbackName.substring(atStartBracket + 1, atEndBracket), // matching function arguments inside brackets
54-
constructor = callbackName.match(/^[^.]+.\s*/)[0].slice(0, -1); // matching a string upto a dot to check ctrl as syntax
54+
constructor = callbackName.indexOf('.') !== -1 ? callbackName.substr(0, callbackName.indexOf('.')) : null; // matching a string upto a dot to check ctrl as syntax
5555
constructor = scope[constructor] && typeof scope[constructor].constructor === 'function' ? constructor : null;
5656

5757
return {
@@ -74,7 +74,8 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
7474
dropModelValue,
7575
$droppableDraggable = null,
7676
droppableScope = this.droppableScope,
77-
draggableScope = this.draggableScope;
77+
draggableScope = this.draggableScope,
78+
promises = [];
7879

7980
dragModel = $draggable.ngattr('ng-model');
8081
dropModel = $droppable.ngattr('ng-model');
@@ -108,28 +109,36 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
108109
dropItem = angular.copy(dropItem);
109110
}
110111

111-
if (dragSettings.animate === true) {
112-
this.move($draggable, $droppableDraggable.length > 0 ? $droppableDraggable : $droppable, null, 'fast', dropSettings, null);
113-
this.move($droppableDraggable.length > 0 && !dropSettings.multiple ? $droppableDraggable : [], $draggable.parent('[jqyoui-droppable],[data-jqyoui-droppable]'), jqyoui.startXY, 'fast', dropSettings, angular.bind(this, function() {
114-
$timeout(angular.bind(this, function() {
115-
// Do not move this into move() to avoid flickering issue
116-
$draggable.css({'position': 'relative', 'left': '', 'top': ''});
117-
// Angular v1.2 uses ng-hide to hide an element not display property
118-
// so we've to manually remove display:none set in this.move()
119-
$droppableDraggable.css({'position': 'relative', 'left': '', 'top': '', 'display': $droppableDraggable.css('display') === 'none' ? '' : $droppableDraggable.css('display')});
112+
if (dragSettings.beforeDrop) {
113+
promises.push(this.callEventCallback(draggableScope, dragSettings.beforeDrop, event, ui));
114+
}
120115

116+
$q.all(promises).then(angular.bind(this, function() {
117+
if (dragSettings.animate === true) {
118+
this.move($draggable, $droppableDraggable.length > 0 ? $droppableDraggable : $droppable, null, 'fast', dropSettings, null);
119+
this.move($droppableDraggable.length > 0 && !dropSettings.multiple ? $droppableDraggable : [], $draggable.parent('[jqyoui-droppable],[data-jqyoui-droppable]'), jqyoui.startXY, 'fast', dropSettings, angular.bind(this, function() {
120+
$timeout(angular.bind(this, function() {
121+
// Do not move this into move() to avoid flickering issue
122+
$draggable.css({'position': 'relative', 'left': '', 'top': ''});
123+
// Angular v1.2 uses ng-hide to hide an element not display property
124+
// so we've to manually remove display:none set in this.move()
125+
$droppableDraggable.css({'position': 'relative', 'left': '', 'top': '', 'display': $droppableDraggable.css('display') === 'none' ? '' : $droppableDraggable.css('display')});
126+
127+
this.mutateDraggable(draggableScope, dropSettings, dragSettings, dragModel, dropModel, dropItem, $draggable);
128+
this.mutateDroppable(droppableScope, dropSettings, dragSettings, dropModel, dragItem, jqyoui_pos);
129+
this.callEventCallback(droppableScope, dropSettings.onDrop, event, ui);
130+
}));
131+
}));
132+
} else {
133+
$timeout(angular.bind(this, function() {
121134
this.mutateDraggable(draggableScope, dropSettings, dragSettings, dragModel, dropModel, dropItem, $draggable);
122135
this.mutateDroppable(droppableScope, dropSettings, dragSettings, dropModel, dragItem, jqyoui_pos);
123136
this.callEventCallback(droppableScope, dropSettings.onDrop, event, ui);
124137
}));
125-
}));
126-
} else {
127-
$timeout(angular.bind(this, function() {
128-
this.mutateDraggable(draggableScope, dropSettings, dragSettings, dragModel, dropModel, dropItem, $draggable);
129-
this.mutateDroppable(droppableScope, dropSettings, dragSettings, dropModel, dragItem, jqyoui_pos);
130-
this.callEventCallback(droppableScope, dropSettings.onDrop, event, ui);
131-
}));
132-
}
138+
}
139+
}), function() {
140+
ui.draggable.css({left: '', top: ''});
141+
});
133142
};
134143

135144
this.move = function($fromEl, $toEl, toPos, duration, dropSettings, callback) {

0 commit comments

Comments
 (0)