Skip to content

Commit 9e5ac95

Browse files
author
Matt Lewis
committed
feat(validateDrag): add the validate drag input
Closes #8
1 parent 652d632 commit 9e5ac95

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/draggable.directive.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export type DragAxis = {x: boolean, y: boolean};
1616

1717
export type SnapGrid = {x?: number, y?: number};
1818

19+
export type ValidateDrag = (coordinates: Coordinates) => boolean;
20+
1921
const MOVE_CURSOR: string = 'move';
2022

2123
@Directive({
@@ -31,6 +33,8 @@ export class Draggable implements OnInit, OnDestroy {
3133

3234
@Input() ghostDragEnabled: boolean = true;
3335

36+
@Input() validateDrag: ValidateDrag;
37+
3438
@Output() dragStart: EventEmitter<Coordinates> = new EventEmitter<Coordinates>();
3539

3640
@Output() dragging: EventEmitter<Coordinates> = new EventEmitter<Coordinates>();
@@ -113,7 +117,8 @@ export class Draggable implements OnInit, OnDestroy {
113117

114118
return mouseMove;
115119

116-
});
120+
})
121+
.filter(({x, y}) => !this.validateDrag || this.validateDrag({x, y}));
117122

118123
mouseDrag.subscribe(({x, y, currentDrag, clientX, clientY}) => {
119124
this.setCssTransform(`translate(${x}px, ${y}px)`);

test/draggable.directive.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {expect} from 'chai';
44
import * as sinon from 'sinon';
55
import {triggerDomEvent} from './util';
66
import {DragAndDropModule} from '../src/index';
7-
import {Draggable} from '../src/draggable.directive';
7+
import {Draggable, ValidateDrag} from '../src/draggable.directive';
88

99
describe('draggable directive', () => {
1010

@@ -15,6 +15,7 @@ describe('draggable directive', () => {
1515
[dragAxis]="dragAxis"
1616
[dragSnapGrid]="dragSnapGrid"
1717
[ghostDragEnabled]="ghostDragEnabled"
18+
[validateDrag]="validateDrag"
1819
(dragStart)="dragStart($event)"
1920
(dragging)="dragging($event)"
2021
(dragEnd)="dragEnd($event)">
@@ -30,6 +31,7 @@ describe('draggable directive', () => {
3031
public dragAxis: any = {x: true, y: true};
3132
public dragSnapGrid: any = {};
3233
public ghostDragEnabled: boolean = true;
34+
public validateDrag: ValidateDrag;
3335

3436
}
3537

@@ -227,4 +229,18 @@ describe('draggable directive', () => {
227229
expect(fixture.componentInstance.dragging).to.have.been.calledWith({x: 0, y: 10});
228230
});
229231

232+
it('should allow drags to be validated', () => {
233+
fixture.componentInstance.validateDrag = ({x, y}) => x > 0 && y > 0;
234+
fixture.detectChanges();
235+
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement;
236+
triggerDomEvent('mousedown', draggableElement, {clientX: 5, clientY: 10});
237+
expect(fixture.componentInstance.dragStart).to.have.been.calledWith({x: 0, y: 0});
238+
triggerDomEvent('mousemove', draggableElement, {clientX: 3, clientY: 10});
239+
expect(fixture.componentInstance.dragging).not.to.have.been.calledWith({x: -2, y: 0});
240+
triggerDomEvent('mousemove', draggableElement, {clientX: 7, clientY: 8});
241+
expect(fixture.componentInstance.dragging).not.to.have.been.calledWith({x: 2, y: -2});
242+
triggerDomEvent('mousemove', draggableElement, {clientX: 7, clientY: 12});
243+
expect(fixture.componentInstance.dragging).to.have.been.calledWith({x: 2, y: 2});
244+
});
245+
230246
});

0 commit comments

Comments
 (0)