-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(draggable): add mwlDraggable directive
- Loading branch information
Matt Lewis
committed
Nov 18, 2016
1 parent
6a98766
commit c6771eb
Showing
9 changed files
with
233 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {CommonModule} from '@angular/common'; | ||
import {HelloWorld} from './helloWorld.component'; | ||
import {Draggable} from './draggable.directive'; | ||
|
||
@NgModule({ | ||
declarations: [ | ||
HelloWorld | ||
Draggable | ||
], | ||
imports: [CommonModule], | ||
exports: [HelloWorld] | ||
exports: [ | ||
Draggable | ||
] | ||
}) | ||
export class DragAndDropModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {Directive, HostListener, OnInit, ElementRef, Renderer, Output, EventEmitter} from '@angular/core'; | ||
import {Subject} from 'rxjs/Subject'; | ||
import {Observable} from 'rxjs/Observable'; | ||
import 'rxjs/add/observable/merge'; | ||
import 'rxjs/add/operator/map'; | ||
import 'rxjs/add/operator/mergeMap'; | ||
import 'rxjs/add/operator/takeUntil'; | ||
import 'rxjs/add/operator/takeLast'; | ||
|
||
type Coordinates = {x: number, y: number}; | ||
|
||
@Directive({ | ||
selector: '[mwlDraggable]' | ||
}) | ||
export class Draggable implements OnInit { | ||
|
||
public mouseDown: Subject<any> = new Subject(); | ||
|
||
public mouseMove: Subject<any> = new Subject(); | ||
|
||
public mouseUp: Subject<any> = new Subject(); | ||
|
||
@Output() dragStart: EventEmitter<Coordinates> = new EventEmitter<Coordinates>(); | ||
|
||
@Output() dragging: EventEmitter<Coordinates> = new EventEmitter<Coordinates>(); | ||
|
||
@Output() dragEnd: EventEmitter<Coordinates> = new EventEmitter<Coordinates>(); | ||
|
||
constructor(public element: ElementRef, private renderer: Renderer) {} | ||
|
||
ngOnInit(): void { | ||
|
||
const mouseDrag: Observable<Coordinates> = this.mouseDown.flatMap((mouseDownEvent: MouseEvent) => { | ||
|
||
this.dragStart.next({x: 0, y: 0}); | ||
|
||
const mouseMove: Observable<Coordinates> = this.mouseMove | ||
.map((mouseMoveEvent: MouseEvent) => { | ||
return { | ||
x: mouseMoveEvent.clientX - mouseDownEvent.clientX, | ||
y: mouseMoveEvent.clientY - mouseDownEvent.clientY | ||
}; | ||
}) | ||
.takeUntil(Observable.merge(this.mouseUp, this.mouseDown)); | ||
|
||
mouseMove.takeLast(1).subscribe((finalCoords: Coordinates) => { | ||
this.dragEnd.next(finalCoords); | ||
this.renderer.setElementStyle(this.element.nativeElement, 'transform', ''); | ||
}); | ||
|
||
return mouseMove; | ||
|
||
}); | ||
|
||
mouseDrag.subscribe(({x, y}: Coordinates) => { | ||
this.dragging.next({x, y}); | ||
this.renderer.setElementStyle(this.element.nativeElement, 'transform', `translate(${x}px, ${y}px)`); | ||
}); | ||
|
||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
@HostListener('mousedown', ['$event']) | ||
onMouseDown(event: MouseEvent): void { | ||
this.mouseDown.next(event); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
@HostListener('document:mousemove', ['$event']) | ||
onMouseMove(event: MouseEvent): void { | ||
this.mouseMove.next(event); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
@HostListener('document:mouseup', ['$event']) | ||
onMouseUp(event: MouseEvent): void { | ||
this.mouseUp.next(event); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {Component, ViewChild} from '@angular/core'; | ||
import {TestBed, ComponentFixture} from '@angular/core/testing'; | ||
import {expect} from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import {DragAndDropModule} from '../src/index'; | ||
import {Draggable} from '../src/draggable.directive'; | ||
|
||
const triggerDomEvent: Function = (eventType: string, target: HTMLElement | Element, eventData: Object = {}) => { | ||
const event: Event = document.createEvent('Event'); | ||
Object.assign(event, eventData); | ||
event.initEvent(eventType, true, true); | ||
target.dispatchEvent(event); | ||
}; | ||
|
||
describe('draggable directive', () => { | ||
|
||
@Component({ | ||
template: ` | ||
<div | ||
mwlDraggable | ||
(dragStart)="dragStart($event)" | ||
(dragging)="dragging($event)" | ||
(dragEnd)="dragEnd($event)" > | ||
Drag me! | ||
</div>`, | ||
}) | ||
class TestCmp { | ||
|
||
@ViewChild(Draggable) public draggable: Draggable; | ||
public dragStart: sinon.SinonSpy = sinon.spy(); | ||
public dragging: sinon.SinonSpy = sinon.spy(); | ||
public dragEnd: sinon.SinonSpy = sinon.spy(); | ||
|
||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({imports: [DragAndDropModule], declarations: [TestCmp]}); | ||
}); | ||
|
||
let fixture: ComponentFixture<TestCmp>; | ||
beforeEach(() => { | ||
document.body.style.margin = '0px'; | ||
fixture = TestBed.createComponent(TestCmp); | ||
fixture.detectChanges(); | ||
document.body.appendChild(fixture.nativeElement.children[0]); | ||
}); | ||
|
||
afterEach(() => { | ||
fixture.destroy(); | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
it('should make the element draggable', () => { | ||
const draggableElement: HTMLElement = fixture.componentInstance.draggable.element.nativeElement; | ||
triggerDomEvent('mousedown', draggableElement, {clientX: 5, clientY: 10}); | ||
expect(fixture.componentInstance.dragStart).to.have.been.calledWith({x: 0, y: 0}); | ||
triggerDomEvent('mousemove', draggableElement, {clientX: 7, clientY: 10}); | ||
expect(fixture.componentInstance.dragging).to.have.been.calledWith({x: 2, y: 0}); | ||
expect(draggableElement.style.transform).to.equal('translate(2px, 0px)'); | ||
triggerDomEvent('mousemove', draggableElement, {clientX: 7, clientY: 8}); | ||
expect(fixture.componentInstance.dragging).to.have.been.calledWith({x: 2, y: -2}); | ||
expect(draggableElement.style.transform).to.equal('translate(2px, -2px)'); | ||
triggerDomEvent('mouseup', draggableElement, {clientX: 7, clientY: 8}); | ||
expect(fixture.componentInstance.dragEnd).to.have.been.calledWith({x: 2, y: -2}); | ||
expect(draggableElement.style.transform).to.equal(''); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters