Skip to content

Commit

Permalink
.end api method
Browse files Browse the repository at this point in the history
  • Loading branch information
bevacqua committed Apr 16, 2015
1 parent 88fe914 commit d6cba3b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 82 deletions.
4 changes: 4 additions & 0 deletions changelog.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.3.0 Terror

Introduced an `.end` instance API method that gracefully ends the drag event using the last known valid drop target.

# 1.2.4 Brother in Arms

- The `accepts` option now takes a fourth argument, `sibling`, giving us a hint of the precise position the item would be dropped in
Expand Down
185 changes: 111 additions & 74 deletions dist/dragula.js

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions dragula.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function dragula (containers, options) {
if (o.direction === void 0) { o.direction = 'vertical'; }

var api = emitter({
end: end,
cancel: cancel,
remove: remove,
destroy: destroy
Expand Down Expand Up @@ -103,6 +104,14 @@ function dragula (containers, options) {
return el.tagName === 'A' || el.tagName === 'BUTTON';
}

function end () {
if (!_dragging) {
return;
}
var item = _copy || _item;
drop(item, item.parentElement);
}

function release (e) {
if (!_dragging) {
return;
Expand All @@ -114,21 +123,21 @@ function dragula (containers, options) {
var elementBehindCursor = getElementBehindPoint(_mirror, clientX, clientY);
var dropTarget = findDropTarget(elementBehindCursor, clientX, clientY);
if (dropTarget && (o.copy === false || dropTarget !== _source)) {
drop();
drop(item, dropTarget);
} else if (o.removeOnSpill) {
remove();
} else {
cancel();
}
}

function drop () {
if (isInitialPlacement(dropTarget)) {
api.emit('cancel', item, _source);
} else {
api.emit('drop', item, dropTarget, _source);
}
cleanup();
function drop (item, target) {
if (isInitialPlacement(target)) {
api.emit('cancel', item, _source);
} else {
api.emit('drop', item, target, _source);
}
cleanup();
}

function remove () {
Expand Down
4 changes: 4 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ When an element is dropped onto a container, it'll be placed near the point wher

The `dragula` method returns a tiny object with a concise API. We'll refer to the API returned by `dragula` as `drake`.

#### `drake.end()`

Gracefully end the drag event as if using **the last position marked by the preview shadow** as the drop target. The proper `cancel` or `drop` event will be fired, depending on whether the item was dropped back where it was originally lifted from _(which is essentially a no-op that's treated as a `cancel` event)_.

#### `drake.cancel(revert)`

If an element managed by `drake` is currently being dragged, this method will gracefully cancel the drag action. You can also pass in `revert` at the method invocation level, effectively producing the same result as if `revertOnSpill` was `true`.
Expand Down

0 comments on commit d6cba3b

Please sign in to comment.