fathom/assets/js/components/DatePicker.js

165 lines
4.0 KiB
JavaScript
Raw Normal View History

2016-11-23 21:29:54 +01:00
'use strict';
2018-05-03 10:54:22 +02:00
import { h, Component } from 'preact';
2018-05-01 16:06:17 +02:00
import { bind } from 'decko';
2016-11-23 21:29:54 +01:00
const availablePeriods = [
{
2018-05-01 16:06:17 +02:00
id: 'day',
label: 'Today'
},
{
2018-05-01 16:06:17 +02:00
id: 'week',
label: 'This week'
},
{
2018-05-01 16:06:17 +02:00
id: 'month',
label: 'This month'
},
{
2018-05-01 16:06:17 +02:00
id: 'year',
label: 'This year'
}
]
2016-11-23 21:29:54 +01:00
class DatePicker extends Component {
constructor(props) {
super(props)
this.state = {
2018-05-03 10:54:22 +02:00
period: props.value,
before: 0,
after: 0,
2016-11-23 21:29:54 +01:00
}
this.updateDatesFromPeriod(this.state.period)
}
@bind
updateDatesFromPeriod(period) {
let startDate = new Date();
startDate.setHours(0);
startDate.setMinutes(0);
let endDate = new Date();
endDate.setHours(24);
endDate.setMinutes(0);
switch(period) {
case "week":
startDate.setDate(startDate.getDate() - (startDate.getDay() + 6) % 7);
endDate.setDate(startDate.getDate() + 7);
break;
case "month":
startDate.setDate(1);
endDate.setMonth(endDate.getMonth() + 1);
endDate.setDate(0);
break;
case "year":
startDate.setDate(1);
startDate.setMonth(0);
endDate.setYear(startDate.getFullYear() + 1);
endDate.setMonth(0);
endDate.setDate(0);
break;
}
this.setDateRange(startDate, endDate, period);
}
@bind
setDateRange(startDate, endDate, period) {
const timezoneOffset = (new Date()).getTimezoneOffset() * 60;
let before, after;
before = Math.round(((+endDate) / 1000) - timezoneOffset);
after = Math.round(((+startDate) / 1000) - timezoneOffset);
this.setState({
period: period || '',
startDate: startDate,
endDate: endDate,
before: before,
after: after,
picking: '',
});
this.props.onChange(this.state);
}
2018-05-01 16:06:17 +02:00
@bind
2016-11-23 21:29:54 +01:00
setPeriod(e) {
2018-05-01 16:06:17 +02:00
e.preventDefault();
2018-05-03 10:54:22 +02:00
let newPeriod = e.target.dataset.value;
if( newPeriod === this.state.period) {
return;
}
2018-05-03 10:54:22 +02:00
this.updateDatesFromPeriod(newPeriod);
2016-11-23 21:29:54 +01:00
}
2018-05-16 12:57:03 +02:00
dateValue(date) {
const addZero = function(n){return n<10? '0'+n:''+n;}
return date.getFullYear() + '-' + addZero(date.getMonth() + 1) + '-' + addZero(date.getDate());
2018-05-16 12:57:03 +02:00
}
@bind
startPicking(e) {
this.setState({ picking: e.target.dataset.value })
2018-05-16 12:57:03 +02:00
}
@bind
stopPicking(e) {
this.setState({ picking: '' })
2018-05-16 12:57:03 +02:00
}
@bind
setStartDate(e) {
let newStartDate = e.target.valueAsDate;
if(newStartDate) {
this.setDateRange(newStartDate, this.state.endDate, '')
}
2018-05-16 12:57:03 +02:00
}
@bind
setEndDate(e) {
let newEndDate = e.target.valueAsDate;
if(newEndDate) {
this.setDateRange(this.state.startDate, newEndDate, '')
}
2018-05-16 12:57:03 +02:00
}
2018-05-01 16:06:17 +02:00
render(props, state) {
const links = availablePeriods.map((p) => {
let className = ( p.id == state.period ) ? 'active' : '';
return <li class={className} ><a href="#" data-value={p.id} onClick={this.setPeriod}>{p.label}</a></li>
2016-11-23 21:29:54 +01:00
});
return (
2018-05-01 16:06:17 +02:00
<ul>
{links}
2018-05-16 12:57:03 +02:00
<li>
<span style="padding: 0 8px 0 0;">&mdash;</span>
<span class="datepicker-wrap">
<strong onclick={this.startPicking} data-value="start">{state.startDate.toLocaleDateString()}</strong>
<span class="datepicker" style={state.picking === 'start' ? '' : 'display: none'}>
<label>Choose start date</label>
<input type="date" value={this.dateValue(state.startDate)} onblur={this.stopPicking} onchange={this.setStartDate} />
</span>
2018-05-16 12:57:03 +02:00
</span>
<span> to </span>
<span class="datepicker-wrap">
<strong onclick={this.startPicking} data-value="end">{state.endDate.toLocaleDateString()}</strong>
<span class="datepicker" style={state.picking === 'end' ? '' : 'display: none'}>
<label>Choose end date</label>
<input type="date" value={this.dateValue(state.endDate)} onblur={this.stopPicking} onchange={this.setStartDate} />
</span>
2018-05-16 12:57:03 +02:00
</span>
</li>
2018-05-01 16:06:17 +02:00
</ul>
2016-11-23 21:29:54 +01:00
)
}
}
export default DatePicker