Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Accepts a wider range of inputs when changing the reservation start/end
Browse files Browse the repository at this point in the history
Resolves #204
  • Loading branch information
Denis Krienbühl committed Jun 1, 2016
1 parent 01f1196 commit 5b09fa5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
---------

- Accepts a wider range of inputs when changing the reservation start/end.
[href]

- Fixes calendar performance regression introduced in 1.7.0.
[href]

Expand Down
52 changes: 51 additions & 1 deletion onegov/town/assets/js/reservationcalendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,58 @@ ReservationForm = React.createClass({
this.props.onSubmit.call(this.getDOMNode(), this.state);
e.preventDefault();
},
handleTimeInputFocus: function(e) {
if (!Modernizr.inputtypes.time) {
e.target.select();
e.preventDefault();
}
},
handleTimeInputMouseUp: function(e) {
if (!Modernizr.inputtypes.time) {
e.preventDefault();
}
},
handleTimeInputBlur: function(e) {
if (!Modernizr.inputtypes.time) {
e.target.value = this.inferTime(e.target.value);
this.handleInputChange(e);
}
},
inferTime: function(time) {
time = time.replace(':', '');

if (time.match(/^\d{1}$/)) {
time = '0' + time + '00';
} else if (time.match(/^\d{2}$/)) {
time += '00';
} else if (time.match(/^\d{3}$/)) {
time += '0';
}

if (time.match(/^\d{4}$/)) {
time = time.slice(0, 2) + ':' + time.slice(2, 4);
}

return time;
},
parseTime: function(date, time) {
if (!time.match(/^\d{2}:\d{2}$/)) {
time = this.inferTime(time);

if (!time.match(/^[0-2]{1}[0-9]{1}:?[0-5]{1}[0-9]{1}$/)) {
return null;
}

var hour = parseInt(time.split(':')[0], 10);
var minute = parseInt(time.split(':')[1], 10);

if (hour < 0 || 24 < hour) {
return null;
}

if (minute < 0 || 60 < minute) {
return null;
}

date.hour(hour);
date.minute(minute);

Expand Down Expand Up @@ -792,6 +836,9 @@ ReservationForm = React.createClass({
<input name="start" type="time" size="4"
defaultValue={this.state.start}
onChange={this.handleInputChange}
onFocus={this.handleTimeInputFocus}
onMouseUp={this.handleTimeInputMouseUp}
onBlur={this.handleTimeInputBlur}
className={this.isValidStart(this.state.start) && 'valid' || 'invalid'}
/>
</div>
Expand All @@ -800,6 +847,9 @@ ReservationForm = React.createClass({
<input name="end" type="time" size="4"
defaultValue={this.state.end}
onChange={this.handleInputChange}
onFocus={this.handleTimeInputFocus}
onMouseUp={this.handleTimeInputMouseUp}
onBlur={this.handleTimeInputBlur}
className={this.isValidEnd(this.state.end) && 'valid' || 'invalid'}
/>
</div>
Expand Down

0 comments on commit 5b09fa5

Please sign in to comment.