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

223 - added a cancelable USER_DROPPED consider event on the origin zone #232

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const TRIGGERS = {
DRAGGED_OVER_INDEX: DRAGGED_OVER_INDEX_EVENT_NAME,
DRAGGED_LEFT: DRAGGED_LEFT_EVENT_NAME,
DRAGGED_LEFT_ALL: "draggedLeftAll",
USER_DROPPED: "userDropped",
DROPPED_INTO_ZONE: "droppedIntoZone",
DROPPED_INTO_ANOTHER: "droppedIntoAnother",
DROPPED_OUTSIDE_OF_ANY: "droppedOutsideOfAny",
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ export function dispatchFinalizeEvent(el, items, info) {
* @param {Node} el
* @param {Array} items
* @param {Info} info
* @param {boolean} [cancelable] - should the event be cancelable. defaults to false
* @return {boolean} false if the event was cancelled
*/
export function dispatchConsiderEvent(el, items, info) {
el.dispatchEvent(
export function dispatchConsiderEvent(el, items, info, cancelable = false) {
return el.dispatchEvent(
new CustomEvent(CONSIDER_EVENT_NAME, {
cancelable,
detail: {items, info}
})
);
Expand Down
24 changes: 20 additions & 4 deletions src/pointerAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ function handleMouseMove(e) {
}px, 0)`;
}

function handleDrop() {
function handleDrop(e) {
printDebug(() => "dropped");
const c = e.touches ? e.touches[0] : e;
finalizingPreviousDrag = true;
// cleanup
window.removeEventListener("mousemove", handleMouseMove);
Expand Down Expand Up @@ -263,17 +264,32 @@ function handleDrop() {
unDecorateShadowElement(shadowElDropZone.children[shadowElIdx]);
cleanupPostDrop();
}
animateDraggedToFinalPosition(shadowElIdx, finalizeWithinZone);
let shouldCancelDropAnimation = !dispatchConsiderEvent(
originDropZone,
dzToConfig.get(originDropZone).items,
{
trigger: TRIGGERS.USER_DROPPED,
id: draggedElData[ITEM_ID_KEY],
draggedItemData: {...draggedElData},
source: SOURCES.POINTER,
draggedElement: draggedEl,
isDroppedOutsideOfAnyDzOfType: isDraggedOutsideOfAnyDz,
pointerClientXY: {x: c.clientX, y: c.clientY}
},
true
);
console.error({shouldCancelDropAnimation});
animateDraggedToFinalPosition(shadowElIdx, finalizeWithinZone, shouldCancelDropAnimation ? 0 : undefined);
}

// helper function for handleDrop
function animateDraggedToFinalPosition(shadowElIdx, callback) {
function animateDraggedToFinalPosition(shadowElIdx, callback, durationOverrideMs) {
const shadowElRect = getBoundingRectNoTransforms(shadowElDropZone.children[shadowElIdx]);
const newTransform = {
x: shadowElRect.left - parseFloat(draggedEl.style.left),
y: shadowElRect.top - parseFloat(draggedEl.style.top)
};
const {dropAnimationDurationMs} = dzToConfig.get(shadowElDropZone);
const {dropAnimationDurationMs} = durationOverrideMs !== undefined ? durationOverrideMs : dzToConfig.get(shadowElDropZone);
const transition = `transform ${dropAnimationDurationMs}ms ease`;
draggedEl.style.transition = draggedEl.style.transition ? draggedEl.style.transition + "," + transition : transition;
draggedEl.style.transform = `translate3d(${newTransform.x}px, ${newTransform.y}px, 0)`;
Expand Down