Skip to content

Commit 729f24e

Browse files
author
Matt Lewis
committed
fix(draggable): when dragging is disabled, no drag events should be emitted
1 parent a77d07a commit 729f24e

File tree

2 files changed

+55
-48
lines changed

2 files changed

+55
-48
lines changed

src/draggable.directive.ts

+51-45
Original file line numberDiff line numberDiff line change
@@ -43,68 +43,70 @@ export class Draggable implements OnInit, OnDestroy {
4343

4444
ngOnInit(): void {
4545

46-
const mouseDrag: Observable<any> = this.mouseDown.flatMap((mouseDownEvent: MouseEvent) => {
46+
const mouseDrag: Observable<any> = this.mouseDown
47+
.filter(() => this.canDrag())
48+
.flatMap((mouseDownEvent: MouseEvent) => {
4749

48-
this.dragStart.next({x: 0, y: 0});
50+
this.dragStart.next({x: 0, y: 0});
4951

50-
if (this.ghostDragEnabled) {
51-
this.renderer.setElementStyle(this.element.nativeElement, 'pointerEvents', 'none');
52-
}
52+
if (this.ghostDragEnabled) {
53+
this.renderer.setElementStyle(this.element.nativeElement, 'pointerEvents', 'none');
54+
}
5355

54-
const currentDrag: Subject<any> = new Subject();
56+
const currentDrag: Subject<any> = new Subject();
5557

56-
this.draggableHelper.currentDrag.next(currentDrag);
58+
this.draggableHelper.currentDrag.next(currentDrag);
5759

58-
const mouseMove: Observable<Coordinates> = this.mouseMove
59-
.map((mouseMoveEvent: MouseEvent) => {
60+
const mouseMove: Observable<Coordinates> = this.mouseMove
61+
.map((mouseMoveEvent: MouseEvent) => {
6062

61-
mouseMoveEvent.preventDefault();
63+
mouseMoveEvent.preventDefault();
6264

63-
return {
64-
currentDrag,
65-
x: mouseMoveEvent.clientX - mouseDownEvent.clientX,
66-
y: mouseMoveEvent.clientY - mouseDownEvent.clientY
67-
};
65+
return {
66+
currentDrag,
67+
x: mouseMoveEvent.clientX - mouseDownEvent.clientX,
68+
y: mouseMoveEvent.clientY - mouseDownEvent.clientY
69+
};
6870

69-
})
70-
.map((moveData: Coordinates) => {
71+
})
72+
.map((moveData: Coordinates) => {
7173

72-
if (this.dragSnapGrid.x) {
73-
moveData.x = Math.floor(moveData.x / this.dragSnapGrid.x) * this.dragSnapGrid.x;
74-
}
74+
if (this.dragSnapGrid.x) {
75+
moveData.x = Math.floor(moveData.x / this.dragSnapGrid.x) * this.dragSnapGrid.x;
76+
}
7577

76-
if (this.dragSnapGrid.y) {
77-
moveData.y = Math.floor(moveData.y / this.dragSnapGrid.y) * this.dragSnapGrid.y;
78-
}
78+
if (this.dragSnapGrid.y) {
79+
moveData.y = Math.floor(moveData.y / this.dragSnapGrid.y) * this.dragSnapGrid.y;
80+
}
7981

80-
return moveData;
81-
})
82-
.map((moveData: Coordinates) => {
82+
return moveData;
83+
})
84+
.map((moveData: Coordinates) => {
8385

84-
if (!this.dragAxis.x) {
85-
moveData.x = 0;
86-
}
86+
if (!this.dragAxis.x) {
87+
moveData.x = 0;
88+
}
8789

88-
if (!this.dragAxis.y) {
89-
moveData.y = 0;
90-
}
90+
if (!this.dragAxis.y) {
91+
moveData.y = 0;
92+
}
9193

92-
return moveData;
93-
})
94-
.takeUntil(Observable.merge(this.mouseUp, this.mouseDown));
94+
return moveData;
95+
})
96+
.takeUntil(Observable.merge(this.mouseUp, this.mouseDown));
9597

96-
mouseMove.takeLast(1).subscribe(({x, y}) => {
97-
this.dragEnd.next({x, y});
98-
currentDrag.complete();
99-
this.setCssTransform('');
100-
if (this.ghostDragEnabled) {
101-
this.renderer.setElementStyle(this.element.nativeElement, 'pointerEvents', 'auto');
102-
}
103-
});
98+
mouseMove.takeLast(1).subscribe(({x, y}) => {
99+
this.dragEnd.next({x, y});
100+
currentDrag.complete();
101+
this.setCssTransform('');
102+
if (this.ghostDragEnabled) {
103+
this.renderer.setElementStyle(this.element.nativeElement, 'pointerEvents', 'auto');
104+
}
105+
});
104106

105-
return mouseMove;
107+
return mouseMove;
106108

107-
});
109+
});
108110

109111
mouseDrag.subscribe(({x, y, currentDrag}) => {
110112
this.dragging.next({x, y});
@@ -154,4 +156,8 @@ export class Draggable implements OnInit, OnDestroy {
154156
}
155157
}
156158

159+
private canDrag(): boolean {
160+
return this.dragAxis.x || this.dragAxis.y;
161+
}
162+
157163
}

test/draggable.directive.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ describe('draggable directive', () => {
116116
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement;
117117
triggerDomEvent('mousedown', draggableElement, {clientX: 5, clientY: 10});
118118
triggerDomEvent('mousemove', draggableElement, {clientX: 7, clientY: 12});
119-
expect(fixture.componentInstance.dragging).to.have.been.calledWith({x: 0, y: 0});
120-
expect(draggableElement.style.transform).to.equal('translate(0px, 0px)');
119+
expect(fixture.componentInstance.dragStart).not.to.have.been.called;
120+
expect(fixture.componentInstance.dragging).not.to.have.been.called;
121+
expect(draggableElement.style.transform).not.to.be.ok;
121122
triggerDomEvent('mouseup', draggableElement, {clientX: 7, clientY: 12});
122-
expect(fixture.componentInstance.dragEnd).to.have.been.calledWith({x: 0, y: 0});
123+
expect(fixture.componentInstance.dragEnd).not.to.have.been.called;
123124
});
124125

125126
it('should snap all horizontal drags to a grid', () => {

0 commit comments

Comments
 (0)