Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the default dropCheck result to dropCheckers #161

Merged
merged 3 commits into from
Feb 27, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
allowFrom : null,
ignoreFrom : null,
_context : document,
rectChecker : null
dropChecker : null
},

drag: {
Expand Down Expand Up @@ -3988,24 +3988,15 @@
return this.options.drop;
},

/*\
* Interactable.dropCheck
[ method ]
*
* The default function to determine if a dragend event occured over
* this Interactable's element. Can be overridden using
* @Interactable.dropChecker.
*
- pointer (MouseEvent | PointerEvent | Touch) The event that ends a drag
- draggable (Interactable) The Interactable being dragged
- draggableElement (Element) The actual element that's being dragged
- dropElement (Element) The dropzone element
- rect (object) #optional The rect of dropElement
= (boolean) whether the pointer was over this Interactable
\*/
dropCheck: function (pointer, draggable, draggableElement, dropElement, rect) {
var dropped = false;

// if the dropzone has no rect (eg. display: none)
// call the custom dropChecker or just return false
if (!(rect = rect || this.getRect(dropElement))) {
return false;
return (this.options.dropChecker
? this.options.dropChecker(pointer, dropped, this, dropElement, draggable, draggableElement)
: false);
}

var dropOverlap = this.options.drop.overlap;
Expand All @@ -4022,7 +4013,7 @@
horizontal = (page.x > rect.left) && (page.x < rect.right);
vertical = (page.y > rect.top ) && (page.y < rect.bottom);

return horizontal && vertical;
dropped = horizontal && vertical;
}

var dragRect = draggable.getRect(draggableElement);
Expand All @@ -4031,16 +4022,22 @@
var cx = dragRect.left + dragRect.width / 2,
cy = dragRect.top + dragRect.height / 2;

return cx >= rect.left && cx <= rect.right && cy >= rect.top && cy <= rect.bottom;
dropped = cx >= rect.left && cx <= rect.right && cy >= rect.top && cy <= rect.bottom;
}

if (isNumber(dropOverlap)) {
var overlapArea = (Math.max(0, Math.min(rect.right , dragRect.right ) - Math.max(rect.left, dragRect.left))
* Math.max(0, Math.min(rect.bottom, dragRect.bottom) - Math.max(rect.top , dragRect.top ))),
overlapRatio = overlapArea / (dragRect.width * dragRect.height);

return overlapRatio >= dropOverlap;
dropped = overlapRatio >= dropOverlap;
}

if (this.options.dropChecker) {
dropped = this.options.dropChecker(pointer, dropped, this, dropElement, draggable, draggableElement);
}

return dropped;
},

/*\
Expand All @@ -4055,15 +4052,31 @@
* parameter and returns true or false to indicate if the the current
* draggable can be dropped into this Interactable
*
- checker (function) The function that will be called when checking for a drop
* The checker function takes the following arguments:
*
- pointer (MouseEvent | PointerEvent | Touch) The pointer/event that ends a drag
- dropped (boolean) The value from the default drop check
- dropzone (Interactable) The dropzone interactable
- dropElement (Element) The dropzone element
- draggable (Interactable) The Interactable being dragged
- draggableElement (Element) The actual element that's being dragged
*
= (Function | Interactable) The checker function or this Interactable
\*/
dropChecker: function (checker) {
if (isFunction(checker)) {
this.dropCheck = checker;
this.options.dropChecker = checker;

return this;
}
return this.dropCheck;
if (checker === null) {
delete this.options.getRect;

return this;
}

return this.options.dropChecker;
},

/*\
Expand Down