Skip to content

Commit

Permalink
feat(droppable): add css class when any element is being dragged
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlewis92 committed Jun 17, 2018
1 parent 7e50d74 commit 5995f81
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/droppable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export class DroppableDirective implements OnInit, OnDestroy {
*/
@Input() dragOverClass: string;

/**
* Added to the element any time a draggable element is being dragged
*/
@Input() dragActiveClass: string;

/**
* Called when a draggable element starts overlapping the element
*/
Expand Down Expand Up @@ -71,6 +76,10 @@ export class DroppableDirective implements OnInit, OnDestroy {
ngOnInit(): void {
this.currentDragSubscription = this.draggableHelper.currentDrag.subscribe(
drag$ => {
this.renderer.addClass(
this.element.nativeElement,
this.dragActiveClass
);
let droppableRectangle = this.element.nativeElement.getBoundingClientRect();

/* istanbul ignore next */
Expand Down Expand Up @@ -142,6 +151,10 @@ export class DroppableDirective implements OnInit, OnDestroy {
drag$.subscribe({
complete: () => {
deregisterScrollListener();
this.renderer.removeClass(
this.element.nativeElement,
this.dragActiveClass
);
if (dragOverActive) {
this.renderer.removeClass(
this.element.nativeElement,
Expand Down
26 changes: 25 additions & 1 deletion test/droppable.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as sinon from 'sinon';
import { triggerDomEvent } from './util';
import { DragAndDropModule } from '../src/index';
import { DraggableDirective } from '../src/draggable.directive';
import { st } from '@angular/core/src/render3';

describe('droppable directive', () => {
@Component({
Expand All @@ -17,7 +18,8 @@ describe('droppable directive', () => {
(dragOver)="dragEvent('over', $event)"
(dragLeave)="dragEvent('leave', $event)"
(drop)="drop($event)"
[dragOverClass]="dragOverClass">
[dragOverClass]="dragOverClass"
[dragActiveClass]="dragActiveClass">
Drop here
</div>
`,
Expand Down Expand Up @@ -50,6 +52,7 @@ describe('droppable directive', () => {
foo: 'bar';
};
dragOverClass: string;
dragActiveClass: string;
}

beforeEach(() => {
Expand Down Expand Up @@ -166,6 +169,7 @@ describe('droppable directive', () => {
fixture.componentInstance.droppableElement.nativeElement;
expect(droppableElement.classList.contains('drag-over')).to.be.false;
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
triggerDomEvent('mousemove', draggableElement, { clientX: 5, clientY: 10 });
expect(droppableElement.classList.contains('drag-over')).to.be.false;
triggerDomEvent('mousemove', draggableElement, {
clientX: 5,
Expand All @@ -190,4 +194,24 @@ describe('droppable directive', () => {
fixture.componentInstance.dragEnd
);
});

it('should add a class to the droppable element when an element is being dragged anywhere', () => {
fixture.componentInstance.dragActiveClass = 'drag-active';
fixture.detectChanges();
const draggableElement: HTMLElement =
fixture.componentInstance.draggable.element.nativeElement;
const droppableElement: HTMLElement =
fixture.componentInstance.droppableElement.nativeElement;
expect(droppableElement.classList.contains('drag-active')).to.be.false;
triggerDomEvent('mousedown', draggableElement, { clientX: 5, clientY: 10 });
triggerDomEvent('mousemove', draggableElement, { clientX: 5, clientY: 10 });
expect(droppableElement.classList.contains('drag-active')).to.be.true;
triggerDomEvent('mousemove', draggableElement, {
clientX: 5,
clientY: 100
});
expect(droppableElement.classList.contains('drag-active')).to.be.true;
triggerDomEvent('mouseup', draggableElement, { clientX: 5, clientY: 120 });
expect(droppableElement.classList.contains('drag-active')).to.be.false;
});
});

0 comments on commit 5995f81

Please sign in to comment.