Skip to content

Commit

Permalink
fix: when onDragInit return false it should stop (#979)
Browse files Browse the repository at this point in the history
- when `onDragInit` returns `false` then the row or cell dragging shouldn't be allowed
- this is similar to previous PR #978
  • Loading branch information
ghiscoding authored Jan 17, 2024
1 parent 8c659c9 commit 5471666
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/slick.interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function Draggable(options: DraggableOption) {

function executeDragCallbackWhenDefined(callback?: (e: DragEvent, dd: DragItem) => boolean | void, evt?: MouseEvent | Touch | TouchEvent, dd?: DragItem) {
if (typeof callback === 'function') {
callback(evt as DragEvent, dd as DragItem);
return callback(evt as DragEvent, dd as DragItem);
}
}

Expand All @@ -80,13 +80,15 @@ export function Draggable(options: DraggableOption) {
deltaX = targetEvent.clientX - targetEvent.clientX;
deltaY = targetEvent.clientY - targetEvent.clientY;
originaldd = Object.assign(originaldd, { deltaX, deltaY, startX, startY, target });
executeDragCallbackWhenDefined(onDragInit as (e: DragEvent, dd: DragPosition) => boolean | void, event, originaldd as DragItem);

document.body.addEventListener('mousemove', userMoved);
document.body.addEventListener('touchmove', userMoved);
document.body.addEventListener('mouseup', userReleased);
document.body.addEventListener('touchend', userReleased);
document.body.addEventListener('touchcancel', userReleased);
const result = executeDragCallbackWhenDefined(onDragInit as (e: DragEvent, dd: DragPosition) => boolean | void, event, originaldd as DragItem);

if (result !== false) {
document.body.addEventListener('mousemove', userMoved);
document.body.addEventListener('touchmove', userMoved);
document.body.addEventListener('mouseup', userReleased);
document.body.addEventListener('touchend', userReleased);
document.body.addEventListener('touchcancel', userReleased);
}
}
}

Expand Down

0 comments on commit 5471666

Please sign in to comment.