Skip to content
Open
Changes from all commits
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
68 changes: 67 additions & 1 deletion src/primitives/TouchableArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,78 @@
var React = require('react');

var TouchableArea = React.createClass({
getInitialState: function() {
return {
cumulativeScroll: {x:0, y:0},
gestureStart: {x:0, y:0},
isMouseWheeling: false,
};
},

getDefaultProps: function() {
return {
component: React.DOM.div,
touchable: true
};
},

handleWheel: function(e) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

TODO:

  • check for a maximum extend from boundary and fire scroll.doTouchEnd at that point.
  • eat all remaining intertial scroll events

if (!this.props.scroller || !this.props.touchable) {
return;
}

if (e.timeStamp) {
this.state.lastTimeStamp = e.timeStamp;
}

var self = this;
var timeStamp = e.timeStamp || this.state.lastTimeStamp;
var scrollDeltaX, scrollDeltaY;
if (!this.state.isMouseWheeling) {
this.state.isMouseWheeling = true;
this.state.cumulativeScroll.x = 0;
this.state.cumulativeScroll.y = 0;

// Start a scroll event
this.props.scroller.doTouchStart([{
pageX: 0,
pageY: 0,
}], e.timeStamp);
return;
}

// Convert the scrollwheel values to a scroll value
scrollDeltaX = e.deltaX / 2;
scrollDeltaY = -e.deltaY / 2;

// If the scroller is constrained to an x axis, convert y scroll to allow single-axis scroll
// wheels to scroll constrained content.
if (this.props.scrollAxis === 'x') {
scrollDeltaX = scrollDeltaY;
scrollDeltaY = 0;
}

this.state.cumulativeScroll.x = Math.round(this.state.cumulativeScroll.x + scrollDeltaX);
this.state.cumulativeScroll.y = Math.round(this.state.cumulativeScroll.y + scrollDeltaY);

var pageX = -this.state.gestureStart.x + this.state.cumulativeScroll.x;
var pageY = this.state.gestureStart.y + this.state.cumulativeScroll.y;

this.props.scroller.doTouchMove([{pageX: pageX, pageY: pageY}], timeStamp);

//_updateScroll(_gestureStart.x + _cumulativeScroll.x, _gestureStart.y + _cumulativeScroll.y, event.timeStamp, event);

// End scrolling state
if (this.state._scrollWheelEndDebouncer) {
clearTimeout(this.state._scrollWheelEndDebouncer);
}
this.state._scrollWheelEndDebouncer = setTimeout(function () {
self.props.scroller.doTouchEnd(timeStamp);
//_releaseInputCapture();
self.state.isMouseWheeling = false;
}, 300);
},

handleTouchStart: function(e) {
if (!this.props.scroller || !this.props.touchable) {
return;
Expand Down Expand Up @@ -41,6 +106,7 @@ var TouchableArea = React.createClass({
var component = this.props.component;
return this.transferPropsTo(
<component
onWheel={this.handleWheel}
onTouchStart={this.handleTouchStart}
onTouchMove={this.handleTouchMove}
onTouchEnd={this.handleTouchEnd}
Expand All @@ -51,4 +117,4 @@ var TouchableArea = React.createClass({
}
});

module.exports = TouchableArea;
module.exports = TouchableArea;