Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
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
233 changes: 134 additions & 99 deletions app/js/date-view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import moment from 'moment-timezone';
import MonthView from './month-view';
import classnames from 'classnames';

const DEFAULT_HOUR_VAL = 12;
const DEFAULT_MINUTE_VAL = 0;

class DatePicker extends React.Component {

static propTypes = {
Expand All @@ -19,142 +16,162 @@ class DatePicker extends React.Component {

constructor(props) {
super(props);
const {selectedDate} = this.props;

this.state = {
date: this.props.selectedDate,
currentHour: this.getHourIn12Format(selectedDate.hour()),
currentMinute: this.getMinuteFormatted(selectedDate.minute()),
currentAmpm: selectedDate.format("a"),
currentDateWithoutTime: selectedDate,
timepickerVisible: false
};
}

reset() {
this.setState({
date: this.props.selectedDate
});
const {selectedDate} = this.props;

this.setState({
currentHour: this.getHourIn12Format(selectedDate.hour()),
currentMinute: this.getMinuteFormatted(selectedDate.minute()),
currentAmpm: selectedDate.format("a"),
currentDateWithoutTime: selectedDate,
});
}

shiftDate(direction) {
var date = this.state.date.clone();
if(direction === "back") {
date = date.subtract(1, "months");
} else {
date = date.add(1, "months");
getHourIn12Format(hourVal) {
let result = hourVal;
if (result < 0) {
result = 0;
}

this.setState({
date: date
})
result = parseInt(result);
result = result % 12;
if (result === 0) {
result = 12;
}
return result;
}

getMinute() {
let minute = this.state.date.minute();
if (isNaN(minute)) {
minute = DEFAULT_MINUTE_VAL;
getMinuteFormatted(minuteVal) {
let result = minuteVal;
if (result < 0) {
result = 0;
}
result = parseInt(result);
return result % 60;
}

getDateObj(currentHour, currentMinute, currentAmpm, newDate) {
const {currentDateWithoutTime} = this.state;

if(minute < 10) {
minute = "0" + minute;
const dateToUse = newDate ? newDate : currentDateWithoutTime;

const currentDate = dateToUse.clone();
let hourIn24 = currentHour;
if (currentAmpm === "am" && currentHour === 12) {
hourIn24 = 0;
} else if (currentAmpm === "pm" && currentHour < 12) {
hourIn24 += 12;
}

return minute;
currentDate.hour(hourIn24);
currentDate.minute(currentMinute);

return currentDate;
}

getHour() {
let hour = this.state.date.hour();
if (isNaN(hour)) {
hour = DEFAULT_HOUR_VAL;
}
if(hour > 12) {
hour = hour - 12;
}
shiftDate(direction) {
const {currentHour, currentMinute, currentAmpm} = this.state;

if(hour === 0) {
hour = 12;
let currentDate = this.getDateObj(currentHour, currentMinute, currentAmpm);
if(direction === "back") {
currentDate = currentDate.subtract(1, "months");
} else {
currentDate = currentDate.add(1, "months");
}

return hour;
this.setState({
currentHour,
currentMinute,
currentAmpm,
currentDateWithoutTime: currentDate,
});
}

getAmPm() {
return this.state.date.format("a");
}
handleDateSelection(date, options) {
const {handleSelection} = this.props;
const {currentHour, currentMinute, currentAmpm} = this.state;

handleSelection(date, options) {
date.hour(this.state.date.hour());
date.minute(this.state.date.minute());
this.props.handleSelection(date, options);
const dateObj = this.getDateObj(currentHour, currentMinute, currentAmpm, date);
handleSelection(dateObj, options);
}

handleHourChange() {
let date = this.state.date;
const hourVal = parseInt(this.hour.value);
let value = DEFAULT_HOUR_VAL;

if (!isNaN(hourVal)) {
value = hourVal;
}
handleTimeSelection(date, options) {
const {handleSelection} = this.props;

if(this.getAmPm() === "pm") {
value += 12;
}
handleSelection(date, options);
}

date.hour(value);
handleHourChange() {
let hourVal = this.hour.value ? parseInt(this.hour.value) : 0;
hourVal = isNaN(hourVal) ? 0 : hourVal;

this.setState({
date: date
})
currentHour: hourVal,
});
}

handleMinuteChange() {
let date = this.state.date;
const minuteVal = parseInt(this.minute.value);
let value = DEFAULT_MINUTE_VAL;

if (!isNaN(minuteVal)) {
value = minuteVal;
}

date.minute(value);
const minuteVal = this.minute.value ? parseInt(this.minute.value) : 0;

this.setState({
date: date
})
currentMinute: minuteVal,
});
}

handleAmPmChange() {
var currentValue = this.getAmPm(),
changedValue = this.ampm.value,
hour = this.state.date.hour();

if(currentValue != changedValue) {
if(changedValue === "am" && hour >= 12) {
hour -= 12;
} else if(changedValue === "pm" && hour < 12) {
hour += 12;
}
const {currentHour, currentMinute} = this.state;

var date = this.state.date;
date.hour(hour);
const ampmVal = this.ampm.value;
const formattedHr = this.getHourIn12Format(currentHour);
const formattedMin = this.getMinuteFormatted(currentMinute);

this.setState({
date: date
})
}
this.setState({
currentHour: formattedHr,
currentMinute: formattedMin,
currentAmpm: ampmVal,
});

const dateObj = this.getDateObj(formattedHr, formattedMin, ampmVal);
this.handleTimeSelection(dateObj, {collapse: false});
}

handleKeyDown(e) {
const {currentHour, currentMinute} = this.state;
var key = e.which || e.keyCode,
wasEnter = key === 13;

if(wasEnter) {
const formattedHr = this.getHourIn12Format(currentHour);
const formattedMin = this.getMinuteFormatted(currentMinute);
this.setState({
currentHour: formattedHr,
currentMinute: formattedMin,
});
this.toggleTimepicker();
}
}

toggleTimepicker() {
const {currentHour, currentMinute, currentAmpm, timepickerVisible} = this.state;

const wasOpenButNowShouldBeClosed = timepickerVisible;

this.setState({
timepickerVisible: !this.state.timepickerVisible
timepickerVisible: !timepickerVisible
}, function() {
if(!this.state.timepickerVisible) {
this.handleSelection(this.state.date, {
if(wasOpenButNowShouldBeClosed) {
const dateObj = this.getDateObj(currentHour, currentMinute, currentAmpm);
this.handleTimeSelection(dateObj, {
collapse: false
});
}
Expand All @@ -163,8 +180,10 @@ class DatePicker extends React.Component {
}

renderArrow(direction) {
const {ignoreFontAwesome} = this.props;

var classes = "month-switcher " + direction, content;
if(this.props.ignoreFontAwesome) {
if(ignoreFontAwesome) {
if(direction === "back") {
content = <span>&lsaquo;</span>;
} else {
Expand Down Expand Up @@ -200,29 +219,41 @@ class DatePicker extends React.Component {
}

renderTimePickerHeaderContent() {
const {currentHour, currentMinute, currentAmpm, timepickerVisible} = this.state;

const currentDate = this.getDateObj(currentHour, currentMinute, currentAmpm);
const displayMin = currentDate.format("mm");

var content = (
<span>
<i className="fa fa-clock-o"></i>
<span className="header-time">
{this.getHour()}:{this.getMinute()}&nbsp;{this.getAmPm()}
{currentHour}:{displayMin}&nbsp;{currentAmpm}
</span>
</span>
);

if(this.state.timepickerVisible) {
if(timepickerVisible) {
content = <span>done</span>
}

return content;
}

renderTimePicker() {
if(!this.props.enableTime) { return; }
const {enableTime} = this.props;
const {currentHour, currentMinute, currentAmpm, timepickerVisible} = this.state;

if(!enableTime) { return; }

var classes = classnames("datepicker-timepicker", {
"visible": this.state.timepickerVisible
"visible": timepickerVisible
});

const currentDate = this.getDateObj(currentHour, currentMinute, currentAmpm);
const displayHr = currentHour === 0 ? currentHour : currentDate.format("hh");
const displayMin = currentDate.format("mm");

return (
<div className={classes}>
<div onClick={this.toggleTimepicker.bind(this)} className="datepicker-timepicker-header">
Expand All @@ -232,7 +263,7 @@ class DatePicker extends React.Component {
<div className="input-row">
<input className="input-hours"
ref={(h) => { this.hour = h; }}
value={this.getHour()}
value={displayHr}
type="number"
min={1}
max={12}
Expand All @@ -242,7 +273,7 @@ class DatePicker extends React.Component {
tabIndex="-1" />:
<input className="input-minutes"
ref={(m) => { this.minute = m; }}
value={this.getMinute()}
value={displayMin}
type="number"
min={0}
max={59}
Expand All @@ -252,7 +283,7 @@ class DatePicker extends React.Component {
tabIndex="-1" />
<select className="ampm-picker ignore-chosen"
ref={(ampm) => { this.ampm = ampm; }}
value={this.getAmPm()}
value={currentAmpm}
onChange={this.handleAmPmChange.bind(this)}
tabIndex="-1" >
<option value="am">AM</option>
Expand All @@ -265,19 +296,23 @@ class DatePicker extends React.Component {
}

render() {
var classes = classnames("datepicker", {
"time-enabled": this.props.enableTime
})
const {enableTime, maxDate, minDate, selectedDate, timezone} = this.props;
const {currentHour, currentMinute, currentAmpm} = this.state;

const classes = classnames("datepicker", {
"time-enabled": enableTime
});
const dateObj = this.getDateObj(currentHour, currentMinute, currentAmpm);

return (
<div className={classes}>
<h3>
{this.renderArrow("back")}
<span>{this.state.date.format("MMMM YYYY")}</span>
<span>{dateObj.format("MMMM YYYY")}</span>
{this.renderArrow("forward")}
</h3>
{this.renderDayLetters()}
<MonthView timezone={this.props.timezone} selectedDate={this.props.selectedDate} minDate={this.props.minDate} maxDate={this.props.maxDate} handleSelection={this.handleSelection.bind(this)} date={this.state.date} />
<MonthView timezone={timezone} selectedDate={selectedDate} minDate={minDate} maxDate={maxDate} handleSelection={this.handleDateSelection.bind(this)} date={dateObj} />
{this.renderTimePicker()}
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions app/js/datepicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class DatePicker extends React.Component {
const {startInputHasChanged, startDateInputValue, format} = this.state;
const {enableTime, timezone} = this.props;
const dateString = this.validateDateString(startDateInputValue);
const startDate = moment.tz(dateString, timezone);
const startDate = moment(startDateInputValue, "MM/DD/YYYY hh:mm a").tz(timezone, true);
const minDate = this.getMinDateForType("startDate");
const maxDate = this.getMaxDateForType("startDate");

Expand Down Expand Up @@ -331,7 +331,7 @@ class DatePicker extends React.Component {
const {endInputHasChanged, endDateInputValue, format} = this.state;
const {enableTime, timezone} = this.props;
const dateString = this.validateDateString(endDateInputValue);
const endDate = moment.tz(dateString, timezone);
const endDate = moment(startDateInputValue, "MM/DD/YYYY hh:mm a").tz(timezone, true);
const minDate = this.getMinDateForType("endDate");
const maxDate = this.getMaxDateForType("endDate");

Expand Down
Loading