Skip to content

Commit

Permalink
feat(ghostElementCreated): emit new event after the ghost element is …
Browse files Browse the repository at this point in the history
…created
  • Loading branch information
mattlewis92 committed Jun 27, 2018
1 parent ecc96ec commit 22530b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/draggable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
*/
@Output() dragStart = new EventEmitter<DragStart>();

/**
* Called after the ghost element has been created
*/
@Output() ghostElementCreated = new EventEmitter();

/**
* Called when the element is being dragged
*/
Expand Down Expand Up @@ -309,12 +314,20 @@ export class DraggableDirective implements OnInit, OnChanges, OnDestroy {
this.ghostElementTemplate
);
clone.innerHTML = '';
clone.appendChild(viewRef.rootNodes[0]);
viewRef.rootNodes
.filter(node => node instanceof Node)
.forEach(node => {
clone.appendChild(node);
});
dragEnded$.subscribe(() => {
this.vcr.remove(this.vcr.indexOf(viewRef));
});
}

this.zone.run(() => {
this.ghostElementCreated.emit();
});

dragEnded$.subscribe(() => {
clone.parentElement!.removeChild(clone);
this.ghostElement = null;
Expand Down
14 changes: 11 additions & 3 deletions test/draggable.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ describe('draggable directive', () => {
[ghostElementTemplate]="ghostElementTemplate"
(dragPointerDown)="dragPointerDown($event)"
(dragStart)="dragStart($event)"
(ghostElementCreated)="ghostElementCreated($event)"
(dragging)="dragging($event)"
(dragEnd)="dragEnd($event)">
Drag me!
</div>
<ng-template #ghostElementTemplateRef><span>I'm being dragged!</span></ng-template>
<ng-template #ghostElementTemplateRef>
<span>{{ 1 + 1 }} test</span>
</ng-template>
`
})
class TestComponent {
Expand All @@ -37,6 +40,7 @@ describe('draggable directive', () => {
ghostElementTemplateRef: TemplateRef<any>;
dragPointerDown = sinon.spy();
dragStart = sinon.spy();
ghostElementCreated = sinon.spy();
dragging = sinon.spy();
dragEnd = sinon.spy();
dragAxis: any = { x: true, y: true };
Expand Down Expand Up @@ -313,6 +317,8 @@ describe('draggable directive', () => {
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
triggerDomEvent('mousemove', draggableElement, { clientX: 7, clientY: 10 });
expect(fixture.componentInstance.dragStart).to.have.been.calledOnce;
expect(fixture.componentInstance.ghostElementCreated).not.to.have.been
.called;
expect(fixture.componentInstance.dragging).to.have.been.calledWith({
x: 2,
y: 0
Expand Down Expand Up @@ -563,7 +569,7 @@ describe('draggable directive', () => {
expect(fixture.componentInstance.dragEnd).not.to.have.been.called;
});

it('should call the drag start, dragging and end events in order', () => {
it('should call the drag lifecycle events in order', () => {
const draggableElement =
fixture.componentInstance.draggableElement.nativeElement;
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
Expand All @@ -572,6 +578,7 @@ describe('draggable directive', () => {
sinon.assert.callOrder(
fixture.componentInstance.dragPointerDown,
fixture.componentInstance.dragStart,
fixture.componentInstance.ghostElementCreated,
fixture.componentInstance.dragging,
fixture.componentInstance.dragEnd
);
Expand Down Expand Up @@ -712,7 +719,8 @@ describe('draggable directive', () => {
fixture.componentInstance.draggableElement.nativeElement;
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
triggerDomEvent('mousemove', draggableElement, { clientX: 7, clientY: 10 });
fixture.detectChanges();
const ghostElement = draggableElement.nextSibling as HTMLElement;
expect(ghostElement.innerHTML).to.equal("<span>I'm being dragged!</span>");
expect(ghostElement.innerHTML).to.equal('<span>2 test</span>');
});
});

0 comments on commit 22530b9

Please sign in to comment.