Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5 and add tests for touch functionality #6

Merged
merged 2 commits into from
Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 71 additions & 46 deletions lib/draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
var React = require('react/addons');
var emptyFunction = function () {};

// for accessing browser globals
var root = typeof window !== 'undefined' ? window : this;
var bodyElement;
if (typeof document !== 'undefined' && 'body' in document) {
bodyElement = document.body;
}

function updateBoundState (state, bound) {
if (!bound) return state;
bound = String(bound);
Expand Down Expand Up @@ -72,10 +65,6 @@ function matchesSelector(el, selector) {
return el[method].call(el, selector);
}

// @credits: http://stackoverflow.com/questions/4817029/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript/4819886#4819886
var isTouchDevice = 'ontouchstart' in root // works on most browsers
|| 'onmsgesturechange' in root; // works on ie10 on ms surface

// look ::handleDragStart
//function isMultiTouch(e) {
// return e.touches && Array.isArray(e.touches) && e.touches.length > 1
Expand All @@ -84,21 +73,18 @@ var isTouchDevice = 'ontouchstart' in root // works on most browsers
/**
* simple abstraction for dragging events names
* */
var dragEventFor = (function () {
var eventsFor = {
touch: {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
},
mouse: {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
}
};
return eventsFor[isTouchDevice ? 'touch' : 'mouse'];
})();
var dragEventsFor = {
touch: {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
},
mouse: {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
}
};

/**
* get {clientX, clientY} positions of control
Expand Down Expand Up @@ -386,6 +372,13 @@ module.exports = React.createClass({
onMouseDown: React.PropTypes.func
},

componentWillMount: function() {
this._dragHandlers = {
mouse: {start: this.handleMouseDown, move: this.handleMouseMove, end: this.handleMouseUp},
touch: {start: this.handleTouchStart, move: this.handleTouchMove, end: this.handleTouchEnd}
};
},

getDefaultProps: function () {
return {
axis: 'both',
Expand Down Expand Up @@ -435,11 +428,42 @@ module.exports = React.createClass({

componentWillUnmount: function() {
// Remove any leftover event handlers
removeEvent(bodyElement, dragEventFor['move'], this.handleDrag);
removeEvent(bodyElement, dragEventFor['end'], this.handleDragEnd);
var bodyElement = this.getDOMNode().ownerDocument.body;
removeEvent(bodyElement, dragEventsFor.mouse.move, this._dragHandlers.mouse.move);
removeEvent(bodyElement, dragEventsFor.mouse.end, this._dragHandlers.mouse.end);
removeEvent(bodyElement, dragEventsFor.touch.move, this._dragHandlers.touch.move);
removeEvent(bodyElement, dragEventsFor.touch.end, this._dragHandlers.touch.end);
},

handleMouseDown: function(e) {
return this.handleDragStart(e, 'mouse');
},

handleMouseMove: function(e) {
return this.handleDrag(e, 'mouse');
},

handleDragStart: function (e) {
handleMouseUp: function(e) {
return this.handleDragEnd(e, 'mouse');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add e.preventDefault() here too?
http://www.html5rocks.com/en/mobile/touchandmouse/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, absolutely, just ran into that myself. (Though in that case the preventDefault line should probably just move to handleDrag)

},

handleTouchStart: function(e) {
e.preventDefault(); // prevent for scroll
return this.handleDragStart(e, 'touch');
},

handleTouchMove: function(e) {
return this.handleDrag(e, 'touch');
},

handleTouchEnd: function(e) {
return this.handleDragEnd(e, 'touch');
},

handleDragStart: function (e, device) {
if (this.state.dragging)
return;

// todo: write right implementation to prevent multitouch drag
// prevent multi-touch events
// if (isMultiTouch(e)) {
Expand All @@ -460,25 +484,27 @@ module.exports = React.createClass({

// Initiate dragging
this.setState({
dragging: true,
dragging: device,
clientX: dragPoint.clientX,
clientY: dragPoint.clientY
});

// Call event handler
this.props.onStart(e, createUIEvent(this));

var bodyElement = this.getDOMNode().ownerDocument.body;

// Add event handlers
addEvent(bodyElement, dragEventFor['move'], this.handleDrag);
addEvent(bodyElement, dragEventFor['end'], this.handleDragEnd);
addEvent(bodyElement, dragEventsFor[device].move, this._dragHandlers[device].move);
addEvent(bodyElement, dragEventsFor[device].end, this._dragHandlers[device].end);

// Add dragging class to body element
if (bodyElement) bodyElement.className += ' react-draggable-dragging';
},

handleDragEnd: function (e) {
handleDragEnd: function (e, device) {
// Short circuit if not currently dragging
if (!this.state.dragging) {
if (!this.state.dragging || (this.state.dragging !== device)) {
return;
}

Expand All @@ -490,9 +516,13 @@ module.exports = React.createClass({
// Call event handler
this.props.onStop(e, createUIEvent(this));


var bodyElement = this.getDOMNode().ownerDocument.body;

// Remove event handlers
removeEvent(root, dragEventFor['move'], this.handleDrag);
removeEvent(root, dragEventFor['end'], this.handleDragEnd);
removeEvent(bodyElement, dragEventsFor[device].move, this._dragHandlers[device].move);
removeEvent(bodyElement, dragEventsFor[device].end, this._dragHandlers[device].end);


// Remove dragging class from body element
if (bodyElement) {
Expand All @@ -502,7 +532,7 @@ module.exports = React.createClass({
}
},

handleDrag: function (e) {
handleDrag: function (e, device) {
var dragPoint = getControlPosition(e);
var offsetLeft = this._toPixels(this.state.offsetLeft);
var offsetTop = this._toPixels(this.state.offsetTop);
Expand Down Expand Up @@ -615,11 +645,6 @@ module.exports = React.createClass({
this.props.onDrag(e, createUIEvent(this));
},

onTouchStart: function (e) {
e.preventDefault(); // prevent for scroll
return this.handleDragStart.apply(this, arguments);
},

render: function () {
var style = {
top: this.state.offsetTop,
Expand All @@ -635,11 +660,11 @@ module.exports = React.createClass({
style: style,
className: 'react-draggable',

onMouseDown: this.handleDragStart,
onTouchStart: this.onTouchStart,
onMouseDown: this._dragHandlers.mouse.start,
onTouchStart: this._dragHandlers.touch.start,

onMouseUp: this.handleDragEnd,
onTouchEnd: this.handleDragEnd
onMouseUp: this._dragHandlers.mouse.end,
onTouchEnd: this._dragHandlers.touch.end,
};

// Reuse the child provided
Expand Down
90 changes: 85 additions & 5 deletions test/draggable_test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var Draggable = require('../lib/draggable');
React.initializeTouchEvents(true);

describe('react-draggable', function () {
describe('props', function () {
Expand Down Expand Up @@ -86,6 +87,32 @@ describe('react-draggable', function () {
expect(called).toEqual(true);
});

it('should call onStart when touch dragging begins', function () {
var called = false;
var drag = TestUtils.renderIntoDocument(
<Draggable onStart={function () { called = true; }}>
<div/>
</Draggable>
);

TestUtils.Simulate.touchStart(drag.getDOMNode());
expect(called).toEqual(true);
});

it('should call onStop when touch dragging ends', function () {
var called = false;
var drag = TestUtils.renderIntoDocument(
<Draggable onStop={function () { called = true; }}>
<div/>
</Draggable>
);

TestUtils.Simulate.touchStart(drag.getDOMNode());
TestUtils.Simulate.touchEnd(drag.getDOMNode());
expect(called).toEqual(true);
});


it('should add react-draggable-dragging CSS class to body element when dragging', function () {
var drag = TestUtils.renderIntoDocument(
<Draggable>
Expand All @@ -99,12 +126,12 @@ describe('react-draggable', function () {
});
});

describe('interaction', function () {
describe('mouse interaction', function () {
it('should initialize dragging onmousedown', function () {
var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);

TestUtils.Simulate.mouseDown(drag.getDOMNode());
expect(drag.state.dragging).toEqual(true);
expect(drag.state.dragging).toEqual('mouse');
});

it('should only initialize dragging onmousedown of handle', function () {
Expand All @@ -121,7 +148,7 @@ describe('react-draggable', function () {
expect(drag.state.dragging).toEqual(false);

TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.handle'));
expect(drag.state.dragging).toEqual(true);
expect(drag.state.dragging).toEqual('mouse');
});

it('should not initialize dragging onmousedown of cancel', function () {
Expand All @@ -138,19 +165,72 @@ describe('react-draggable', function () {
expect(drag.state.dragging).toEqual(false);

TestUtils.Simulate.mouseDown(drag.getDOMNode().querySelector('.content'));
expect(drag.state.dragging).toEqual(true);
expect(drag.state.dragging).toEqual('mouse');
});

it('should discontinue dragging onmouseup', function () {
var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);

TestUtils.Simulate.mouseDown(drag.getDOMNode());
expect(drag.state.dragging).toEqual(true);
expect(drag.state.dragging).toEqual('mouse');

TestUtils.Simulate.mouseUp(drag.getDOMNode());
expect(drag.state.dragging).toEqual(false);
});
});

describe('touch interaction', function () {
it('should initialize dragging ontouchstart', function () {
var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);

TestUtils.Simulate.touchStart(drag.getDOMNode());
expect(drag.state.dragging).toEqual('touch');
});

it('should only initialize dragging ontouchstart of handle', function () {
var drag = TestUtils.renderIntoDocument(
<Draggable handle=".handle">
<div>
<div className="handle">Handle</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);

TestUtils.Simulate.touchStart(drag.getDOMNode().querySelector('.content'));
expect(drag.state.dragging).toEqual(false);

TestUtils.Simulate.touchStart(drag.getDOMNode().querySelector('.handle'));
expect(drag.state.dragging).toEqual('touch');
});

it('should not initialize dragging ontouchstart of cancel', function () {
var drag = TestUtils.renderIntoDocument(
<Draggable cancel=".cancel">
<div>
<div className="cancel">Cancel</div>
<div className="content">Lorem ipsum...</div>
</div>
</Draggable>
);

TestUtils.Simulate.touchStart(drag.getDOMNode().querySelector('.cancel'));
expect(drag.state.dragging).toEqual(false);

TestUtils.Simulate.touchStart(drag.getDOMNode().querySelector('.content'));
expect(drag.state.dragging).toEqual('touch');
});

it('should discontinue dragging ontouchend', function () {
var drag = TestUtils.renderIntoDocument(<Draggable><div/></Draggable>);

TestUtils.Simulate.touchStart(drag.getDOMNode());
expect(drag.state.dragging).toEqual('touch');

TestUtils.Simulate.touchEnd(drag.getDOMNode());
expect(drag.state.dragging).toEqual(false);
});
});

describe('validation', function () {
it('should result with invariant when there isn\'t any children', function () {
Expand Down