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
36 changes: 28 additions & 8 deletions projects/angular-gridster2/src/lib/gridsterScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type Position = Pick<MouseEvent, 'clientX' | 'clientY'>;

type CalculatePosition = (position: Position) => void;

export function isMovingTowardScrollEdge(lastPosition: number, currentPosition: number, scrollDirection: -1 | 1): boolean {
return scrollDirection === 1 ? lastPosition <= currentPosition : lastPosition >= currentPosition;
}

export function scroll(
gridster: Gridster,
left: number,
Expand Down Expand Up @@ -73,13 +77,21 @@ export function scroll(

if (elemBottomOffset < scrollSensitivity) {
cancelN();
if (!(resizeEvent && resizeEventType && !resizeEventType.south) && !scrollS) {
startVerticalScroll(1, calculateItemPosition, gridster);
if (isMovingTowardScrollEdge(lastMouse.clientY, clientY, 1)) {
if (!(resizeEvent && resizeEventType && !resizeEventType.south) && !scrollS) {
startVerticalScroll(1, calculateItemPosition, gridster);
}
} else {
cancelS();
}
} else if (offsetTop > 0 && elemTopOffset < scrollSensitivity) {
cancelS();
if (!(resizeEvent && resizeEventType && !resizeEventType.north) && !scrollN) {
startVerticalScroll(-1, calculateItemPosition, gridster);
if (isMovingTowardScrollEdge(lastMouse.clientY, clientY, -1)) {
if (!(resizeEvent && resizeEventType && !resizeEventType.north) && !scrollN) {
startVerticalScroll(-1, calculateItemPosition, gridster);
}
} else {
cancelN();
}
} else if (lastMouse.clientY !== clientY) {
cancelVerticalScroll();
Expand All @@ -94,13 +106,21 @@ export function scroll(

if (elemRightOffset <= scrollSensitivity) {
cancelW();
if (!(resizeEvent && resizeEventType && !resizeEventType.east) && !scrollE) {
startHorizontalScroll(1, calculateItemPosition, gridster, isRTL);
if (isMovingTowardScrollEdge(lastMouse.clientX, clientX, 1)) {
if (!(resizeEvent && resizeEventType && !resizeEventType.east) && !scrollE) {
startHorizontalScroll(1, calculateItemPosition, gridster, isRTL);
}
} else {
cancelE();
}
} else if (offsetLeft > 0 && elemLeftOffset < scrollSensitivity) {
cancelE();
if (!(resizeEvent && resizeEventType && !resizeEventType.west) && !scrollW) {
startHorizontalScroll(-1, calculateItemPosition, gridster, isRTL);
if (isMovingTowardScrollEdge(lastMouse.clientX, clientX, -1)) {
if (!(resizeEvent && resizeEventType && !resizeEventType.west) && !scrollW) {
startHorizontalScroll(-1, calculateItemPosition, gridster, isRTL);
}
} else {
cancelW();
}
} else if (lastMouse.clientX !== clientX) {
cancelHorizontalScroll();
Expand Down
27 changes: 27 additions & 0 deletions projects/angular-gridster2/src/lib/tests/gridsterScroll.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isMovingTowardScrollEdge } from '../gridsterScroll';

describe('scroll edge direction', () => {
it('allows bottom-edge scrolling only while the pointer moves down or stays in place', () => {
expect(isMovingTowardScrollEdge(100, 110, 1)).toBe(true);
expect(isMovingTowardScrollEdge(100, 100, 1)).toBe(true);
expect(isMovingTowardScrollEdge(110, 100, 1)).toBe(false);
});

it('allows top-edge scrolling only while the pointer moves up or stays in place', () => {
expect(isMovingTowardScrollEdge(110, 100, -1)).toBe(true);
expect(isMovingTowardScrollEdge(100, 100, -1)).toBe(true);
expect(isMovingTowardScrollEdge(100, 110, -1)).toBe(false);
});

it('allows right-edge scrolling only while the pointer moves right or stays in place', () => {
expect(isMovingTowardScrollEdge(100, 110, 1)).toBe(true);
expect(isMovingTowardScrollEdge(100, 100, 1)).toBe(true);
expect(isMovingTowardScrollEdge(110, 100, 1)).toBe(false);
});

it('allows left-edge scrolling only while the pointer moves left or stays in place', () => {
expect(isMovingTowardScrollEdge(110, 100, -1)).toBe(true);
expect(isMovingTowardScrollEdge(100, 100, -1)).toBe(true);
expect(isMovingTowardScrollEdge(100, 110, -1)).toBe(false);
});
});