Skip to content

Commit

Permalink
Merge pull request #873 from martianboy/time-picker-android
Browse files Browse the repository at this point in the history
[TimePicker][#230] Got the clock handles working on Android Chrome
  • Loading branch information
Hai Nguyen committed Jun 18, 2015
2 parents d55004e + 2c16d03 commit 1781ace
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
43 changes: 29 additions & 14 deletions src/time-picker/clock-hours.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ function rad2deg(rad){
return rad * 57.29577951308232
}

function getTouchEventOffsetValues(e) {
var el = e.target;
var boundingRect = el.getBoundingClientRect();

var offset = {
offsetX: e.clientX - boundingRect.left,
offsetY: e.clientY - boundingRect.top
};

return offset;
}

var ClockHours = React.createClass({

mixins: [StylePropable],
Expand Down Expand Up @@ -51,29 +63,32 @@ var ClockHours = React.createClass({
},
handleUp: function(e){
e.preventDefault();
this.setClock(e, true);
this.setClock(e.nativeEvent, true);
},
handleMove: function(e){
e.preventDefault();
e.preventDefault();
if(this.isMousePressed(e) != 1 ) return;
this.setClock(e, false);
this.setClock(e.nativeEvent, false);
},
handleTouchMove: function(e){
e.preventDefault();
this.setClock(e.changedTouches[0], false);
},
handleTouch: function(e){
handleTouchEnd: function(e){
e.preventDefault();
this.setClock(e, false);
this.setClock(e.changedTouches[0], true);
},
setClock: function(e, finish){
var ne = e.nativeEvent;

var pos = {
x: ne.offsetX === undefined ? ne.layerX : ne.offsetX,
y: ne.offsetY === undefined ? ne.layerY : ne.offsetY
};
if (typeof e.offsetX === 'undefined') {
var offset = getTouchEventOffsetValues(e);

e.offsetX = offset.offsetX;
e.offsetY = offset.offsetY;
}

var hours = this.getHours(pos.x, pos.y);
var hours = this.getHours(e.offsetX, e.offsetY);

this.props.onChange(hours, finish);

},
getHours: function(x, y){

Expand Down Expand Up @@ -168,7 +183,7 @@ var ClockHours = React.createClass({
<div ref="clock" style={this.mergeAndPrefix(styles.root)} >
<ClockPointer hasSelected={true} value={hours} type="hour" />
{numbers}
<div ref="mask" style={this.mergeAndPrefix(styles.hitMask)} onTouchMove={this.handleTouch} onTouchEnd={this.handleUp} onMouseUp={this.handleUp} onMouseMove={this.handleMove}/>
<div ref="mask" style={this.mergeAndPrefix(styles.hitMask)} onTouchMove={this.handleTouchMove} onTouchEnd={this.handleTouchEnd} onMouseUp={this.handleUp} onMouseMove={this.handleMove}/>
</div>
);
}
Expand Down
32 changes: 22 additions & 10 deletions src/time-picker/clock-minutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ function rad2deg(rad){
return rad * 57.29577951308232
}

function getTouchEventOffsetValues(e) {
var el = e.target;
var boundingRect = el.getBoundingClientRect();

var offset = {
offsetX: e.clientX - boundingRect.left,
offsetY: e.clientY - boundingRect.top
};

return offset;
}

var ClockMinutes = React.createClass({

mixins: [StylePropable],
Expand Down Expand Up @@ -54,26 +66,26 @@ var ClockMinutes = React.createClass({
},
handleUp: function(e){
e.preventDefault();
this.setClock(e, true);
this.setClock(e.nativeEvent, true);
},
handleMove: function(e){
e.preventDefault();
if(this.isMousePressed(e) != 1 ) return;
this.setClock(e, false);
this.setClock(e.nativeEvent, false);
},
handleTouch: function(e){
e.preventDefault();
this.setClock(e, false);
this.setClock(e.changedTouches[0], false);
},
setClock: function(e, finish){
var ne = e.nativeEvent;
if (typeof e.offsetX === 'undefined') {
var offset = getTouchEventOffsetValues(e);

var pos = {
x: ne.offsetX === undefined ? ne.layerX : ne.offsetX,
y: ne.offsetY === undefined ? ne.layerY : ne.offsetY
};
e.offsetX = offset.offsetX;
e.offsetY = offset.offsetY;
}

var minutes = this.getMinutes(pos.x, pos.y)
var minutes = this.getMinutes(e.offsetX, e.offsetY);

this.props.onChange(minutes, finish);
},
Expand Down Expand Up @@ -148,7 +160,7 @@ var ClockMinutes = React.createClass({
<div ref="clock" style={this.mergeAndPrefix(styles.root)} >
<ClockPointer value={minutes.selected} type="minute" />
{minutes.numbers}
<div ref="mask" style={this.mergeAndPrefix(styles.hitMask)} hasSelected={minutes.hasSelected} onTouchMove={this.handleTouch} onTouchEnd={this.handleUp} onMouseUp={this.handleUp} onMouseMove={this.handleMove} />
<div ref="mask" style={this.mergeAndPrefix(styles.hitMask)} hasSelected={minutes.hasSelected} onTouchMove={this.handleTouch} onTouchEnd={this.handleTouch} onMouseUp={this.handleUp} onMouseMove={this.handleMove} />
</div>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/time-picker/time-picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ var TimePicker = React.createClass({
},

_handleInputTouchTap: function(e) {
e.preventDefault();

this.setState({
dialogTime: this.getTime()
});
Expand Down

0 comments on commit 1781ace

Please sign in to comment.