Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions projects/angular-gridster2/src/lib/gridsterDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export class GridsterDraggable {
e.stopPropagation();
e.preventDefault();

this.resetLastMouse(e);

this.zone.runOutsideAngular(() => {
this.mousemove = this.gridsterItem.renderer.listen('document', 'mousemove', this.dragMove);
this.touchmove = this.gridster.renderer.listen(this.gridster.el, 'touchmove', this.dragMove);
Expand Down Expand Up @@ -245,6 +247,7 @@ export class GridsterDraggable {
this.gridster.dragInProgress = false;
this.gridster.updateGrid();
this.path = [];
this.resetLastMouse();
const options = this.gridster.options();
if (options.draggable && options.draggable.stop) {
Promise.resolve(options.draggable.stop(this.gridsterItem.item(), this.gridsterItem, e)).then(this.makeDrag, this.cancelDrag);
Expand Down Expand Up @@ -446,4 +449,9 @@ export class GridsterDraggable {
}
return directions;
}

private resetLastMouse(e?: MouseEvent): void {
this.lastMouse.clientX = e?.clientX ?? 0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should reset to 0, since getDirections needs to set current position of mouse from dragMove mouse event.

this.lastMouse.clientY = e?.clientY ?? 0;
}
}
86 changes: 86 additions & 0 deletions projects/angular-gridster2/src/lib/tests/gridsterDraggable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { DirTypes } from '../gridsterConfig';
import { GridsterDraggable } from '../gridsterDraggable';

interface DraggableWithPrivate {
getDirections(e: MouseEvent): string[];
}

function createDraggable(): GridsterDraggable {
const gridsterEl = document.createElement('div');
const itemEl = document.createElement('div');
const removeListener = vi.fn();
const renderer = {
addClass: vi.fn(),
listen: vi.fn(() => removeListener),
removeClass: vi.fn()
};
const item = { cols: 1, rows: 1, x: 0, y: 0 };
const gridster = {
$options: () => ({
dirType: DirTypes.LTR,
margin: 10,
outerMarginBottom: null,
outerMarginLeft: null,
outerMarginRight: null,
outerMarginTop: null
}),
dragInProgress: false,
el: gridsterEl,
options: () => ({
draggable: {}
}),
previewStyle: vi.fn(),
renderer,
updateGrid: vi.fn()
};
const gridsterItem = {
$item: () => item,
el: itemEl,
gridster,
height: 100,
item: () => item,
left: 10,
renderer,
top: 20,
width: 100
};
const zone = {
run: (callback: () => void) => callback(),
runOutsideAngular: (callback: () => void) => callback()
};
const cdRef = {
markForCheck: vi.fn()
};

return new GridsterDraggable(gridsterItem as never, gridster as never, zone as never, cdRef as never);
}

describe('gridster draggable', () => {
it('starts each drag with the current pointer as the direction baseline', () => {
const draggable = createDraggable();
const draggablePrivate = draggable as unknown as DraggableWithPrivate;
draggable.lastMouse.clientX = 1000;
draggable.lastMouse.clientY = 600;

draggable.dragStart(
new MouseEvent('mousedown', {
button: 0,
clientX: 1100,
clientY: 700
})
);

expect(draggable.lastMouse).toEqual({
clientX: 1100,
clientY: 700
});
expect(
draggablePrivate.getDirections(
new MouseEvent('mousemove', {
clientX: 1090,
clientY: 700
})
)
).toEqual(['LEFT']);
});
});