Skip to content

Commit

Permalink
feat: expose ghost element and starting mouse position on ghost eleme…
Browse files Browse the repository at this point in the history
…nt created event

Closes #81
Closes #85
  • Loading branch information
mattlewis92 committed Mar 17, 2019
1 parent 5ad4d0d commit d233788
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,21 @@ describe('draggable directive', () => {
expect(draggableElement.style.visibility).not.to.be.ok;
});

it('should expose the mouse coordinates and ghost element', () => {
const draggableElement =
fixture.componentInstance.draggableElement.nativeElement;
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
triggerDomEvent('mousemove', draggableElement, { clientX: 6, clientY: 10 });
const ghostElement = draggableElement.nextSibling as HTMLElement;
expect(
fixture.componentInstance.ghostElementCreated
).to.have.been.calledWith({
element: ghostElement,
clientX: 5,
clientY: 10
});
});

it('should create a clone of the element and leave old element visible', () => {
fixture.componentInstance.showOriginalElementWhileDragging = true;
fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export interface TimeLongPress {
timerEnd: number;
}

export interface GhostElementCreatedEvent {
clientX: number;
clientY: number;
element: HTMLElement;
}

@Directive({
selector: '[mwlDraggable]'
})
Expand Down Expand Up @@ -154,7 +160,7 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
* Called after the ghost element has been created
*/
@Output()
ghostElementCreated = new EventEmitter();
ghostElementCreated = new EventEmitter<GhostElementCreatedEvent>();

/**
* Called when the element is being dragged
Expand Down Expand Up @@ -338,7 +344,7 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
share()
);

dragStarted$.subscribe(() => {
dragStarted$.subscribe(({ clientX, clientY, x, y }) => {
this.zone.run(() => {
this.dragStart.next({ cancelDrag$ });
});
Expand Down Expand Up @@ -398,7 +404,11 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
}

this.zone.run(() => {
this.ghostElementCreated.emit();
this.ghostElementCreated.emit({
clientX: clientX - x,
clientY: clientY - y,
element: clone
});
});

dragEnded$.subscribe(() => {
Expand Down
3 changes: 2 additions & 1 deletion projects/angular-draggable-droppable/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export {
DragPointerDownEvent,
DragStartEvent,
DragMoveEvent,
DragEndEvent
DragEndEvent,
GhostElementCreatedEvent
} from './lib/draggable.directive';

0 comments on commit d233788

Please sign in to comment.