Skip to content

Commit

Permalink
cancalDrop continued
Browse files Browse the repository at this point in the history
  • Loading branch information
Kutlu Sahin committed Mar 29, 2019
1 parent 3448c9a commit 9bbb3d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 163,18 @@ function handleDrop({ element, draggables, layout, getOptions }: ContainerProps)
return function (draggableInfo: DraggableInfo, { addedIndex, removedIndex }: DragResult, forDispose: boolean = false) {
draggablesReset();
// if drop zone is valid => complete drag else do nothing everything will be reverted by draggablesReset()
if (draggableInfo.targetElement || getOptions().removeOnDropOut || forDispose) {
let actualAddIndex =
addedIndex !== null ? (removedIndex !== null && removedIndex < addedIndex ? addedIndex - 1 : addedIndex) : null;
const dropHandlerParams = {
removedIndex,
addedIndex: actualAddIndex,
payload: draggableInfo.payload,
// droppedElement: draggableInfo.element.firstElementChild,
};
dropHandler(dropHandlerParams, getOptions().onDrop);
if (!draggableInfo.cancelDrop) {
if (draggableInfo.targetElement || getOptions().removeOnDropOut || forDispose) {
let actualAddIndex =
addedIndex !== null ? (removedIndex !== null && removedIndex < addedIndex ? addedIndex - 1 : addedIndex) : null;
const dropHandlerParams = {
removedIndex,
addedIndex: actualAddIndex,
payload: draggableInfo.payload,
// droppedElement: draggableInfo.element.firstElementChild,
};
dropHandler(dropHandlerParams, getOptions().onDrop);
}
}
};
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 60,7 @@ export interface DraggableInfo {
groupName?: string;
ghostParent: HTMLElement | null;
relevantContainers: IContainer[];
cancelDrop?: boolean;
}

export interface ContainerProps {
Expand Down
21 changes: 19 additions & 2 deletions src/mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,7 @@ let ghostInfo: GhostInfo = null!;
let draggableInfo: DraggableInfo = null!;
let containers: IContainer[] = [];
let isDragging = false;
let isCanceling = false;
let dropAnimationStarted = false;
let missedDrag = false;
let handleDrag: (info: DraggableInfo) => boolean = null!;
Expand Down Expand Up @@ -206,7 207,7 @@ function handleDropAnimation(callback: Function) {
const container = containers.filter(p => p === draggableInfo.container)[0];
if (container) {
const { behaviour, removeOnDropOut } = container.getOptions();
if ((behaviour === 'move' || behaviour === 'copy') && !removeOnDropOut && container.getDragResult()) {
if ((behaviour === 'move' || behaviour === 'contain') && (isCanceling || !removeOnDropOut) && container.getDragResult()) {
const rectangles = container.layout.getContainerRectangles();

// container is hidden somehow
Expand Down Expand Up @@ -714,9 715,25 @@ function watchRectangles() {
}

function cancelDrag() {
if (isDragging) {
if (isDragging && !isCanceling && !dropAnimationStarted) {
isCanceling = true;
missedDrag = false;

const outOfBoundsDraggableInfo: DraggableInfo = Object.assign({}, draggableInfo, {
targetElement: null,
position: { x: Number.MAX_SAFE_INTEGER, y: Number.MAX_SAFE_INTEGER },
mousePosition: { x: Number.MAX_SAFE_INTEGER, y: Number.MAX_SAFE_INTEGER },
});

dragListeningContainers.forEach(container => {
container.handleDrag(outOfBoundsDraggableInfo);
});

draggableInfo.targetElement = null;
draggableInfo.cancelDrop = true;

onMouseUp();
isCanceling = false;
}
}

Expand Down

0 comments on commit 9bbb3d0

Please sign in to comment.