2022-02-12 13:02:31 -05:00
|
|
|
|
import React from 'react';
|
2022-02-12 14:55:46 -05:00
|
|
|
|
import './Calendar.scss';
|
2022-02-12 17:46:33 -05:00
|
|
|
|
import {
|
2022-02-19 21:24:42 -05:00
|
|
|
|
dateName,
|
|
|
|
|
dateRuralName,
|
2022-02-12 17:46:33 -05:00
|
|
|
|
Day,
|
|
|
|
|
decadeNames,
|
2022-02-12 17:58:54 -05:00
|
|
|
|
endYear,
|
2022-02-12 17:46:33 -05:00
|
|
|
|
frIsLeap,
|
|
|
|
|
frJDN,
|
|
|
|
|
jdnFrench,
|
|
|
|
|
jdnGregorian,
|
|
|
|
|
jdnLongCount,
|
|
|
|
|
Month,
|
2022-02-12 17:58:54 -05:00
|
|
|
|
monthName,
|
|
|
|
|
startYear
|
2022-02-12 17:46:33 -05:00
|
|
|
|
} from './dates';
|
2022-02-12 13:02:31 -05:00
|
|
|
|
|
|
|
|
|
type MonthProps = {
|
|
|
|
|
year: number;
|
|
|
|
|
month: Month;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type DateProps = MonthProps & {
|
|
|
|
|
day: Day;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function DecadeName({name}: { name: string }): JSX.Element {
|
2022-02-12 13:27:14 -05:00
|
|
|
|
return <div className="DecadeName">{name}</div>;
|
2022-02-12 13:02:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function DayDetail({jdn}: { jdn: number }): JSX.Element {
|
|
|
|
|
return <div className="DayDetail">
|
2022-02-12 13:27:14 -05:00
|
|
|
|
<div className="DayDetail-gregorian">{jdnGregorian(jdn).toDateString()}</div>
|
2022-02-12 13:40:34 -05:00
|
|
|
|
<div className="DayDetail-lc">{jdnLongCount(jdn)}</div>
|
2022-02-12 13:02:31 -05:00
|
|
|
|
</div>;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 15:09:24 -05:00
|
|
|
|
function NormalDay({year, month, day, todayJDN}: DateProps & { todayJDN: number }): JSX.Element {
|
|
|
|
|
const jdn = frJDN(year, month, day);
|
2022-02-13 00:02:01 -05:00
|
|
|
|
const rural = dateRuralName(month, day)!;
|
2022-02-12 15:09:24 -05:00
|
|
|
|
return <div className={`Day NormalDay ${jdn === todayJDN ? 'Day-today' : ''}`}>
|
2022-02-12 13:02:31 -05:00
|
|
|
|
<div className="Day-name">{day}</div>
|
2022-02-12 13:27:14 -05:00
|
|
|
|
<div className="Day-decade">{decadeNames[(day - 1) % 10]}</div>
|
2022-02-13 15:13:19 -05:00
|
|
|
|
<div className="Day-rural" title={rural.title} tabIndex={0}>{rural.name}</div>
|
2022-02-12 15:09:24 -05:00
|
|
|
|
<DayDetail jdn={jdn}/>
|
2022-02-12 13:27:14 -05:00
|
|
|
|
</div>;
|
2022-02-12 13:02:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 15:09:24 -05:00
|
|
|
|
function NormalMonth({year, month, todayJDN}: MonthProps & { todayJDN: number }): JSX.Element {
|
2022-02-19 21:31:43 -05:00
|
|
|
|
const decadeHeads = decadeNames.map((name, i) => <DecadeName key={i} name={name}/>);
|
2022-02-12 13:27:14 -05:00
|
|
|
|
return <div className="Month">
|
|
|
|
|
<div className="Month-decadeHead">{decadeHeads}</div>
|
|
|
|
|
<div className="Month-decades">{
|
2022-02-19 21:31:43 -05:00
|
|
|
|
Array.from(Array(3).keys()).map(i => <div key={i} className="Month-decade">{
|
|
|
|
|
Array.from(Array(10).keys()).map(j => <React.Fragment key={j}>
|
2022-02-12 15:09:24 -05:00
|
|
|
|
<NormalDay year={year} month={month} day={i * 10 + j + 1 as Day} todayJDN={todayJDN}/>
|
2022-02-13 15:00:20 -05:00
|
|
|
|
{j % 2 === 1 && <div className="Month-decadeSplitter-small"/>}
|
|
|
|
|
{j === 4 && <div className="Month-decadeSplitter-medium"/>}
|
2022-02-19 21:31:43 -05:00
|
|
|
|
</React.Fragment>)
|
2022-02-12 13:27:14 -05:00
|
|
|
|
}</div>)
|
|
|
|
|
}</div>
|
|
|
|
|
</div>;
|
2022-02-12 13:02:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 16:58:32 -05:00
|
|
|
|
function ComplementaryDay({year, month, day, todayJDN}: DateProps & { todayJDN: number }): JSX.Element {
|
|
|
|
|
const jdn = frJDN(year, month, day);
|
|
|
|
|
return <div className={`Day ComplementaryDay ${jdn === todayJDN ? 'Day-today' : ''}`}>
|
|
|
|
|
<div className="Day-name">{dateName(month, day)}</div>
|
|
|
|
|
<DayDetail jdn={jdn}/>
|
|
|
|
|
</div>;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 17:46:33 -05:00
|
|
|
|
function ComplementaryDays({year, todayJDN}: { year: number, todayJDN: number }): JSX.Element {
|
2022-02-12 23:15:15 -05:00
|
|
|
|
const leap = frIsLeap(year);
|
2022-02-12 16:58:32 -05:00
|
|
|
|
return <div className="ComplementaryDays">{
|
2022-02-19 21:31:43 -05:00
|
|
|
|
Array.from(Array(6).keys()).map(i => <React.Fragment key={i}>
|
2022-02-12 23:15:15 -05:00
|
|
|
|
{(i < 5 || leap) && <ComplementaryDay year={year} month={13} day={i + 1 as Day} todayJDN={todayJDN}/>}
|
|
|
|
|
{i === 5 && !leap && <div className="ComplementaryDay-fake"/>}
|
2022-02-12 16:58:32 -05:00
|
|
|
|
{i % 2 === 1 && <div className="ComplementaryDays-splitter"/>}
|
2022-02-19 21:31:43 -05:00
|
|
|
|
</React.Fragment>)
|
2022-02-12 16:58:32 -05:00
|
|
|
|
}</div>;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 17:46:33 -05:00
|
|
|
|
export type CalendarProps = MonthProps & {
|
|
|
|
|
todayJDN: number;
|
|
|
|
|
onSwitch?: (year: number, month: Month) => void,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type CalendarState = {
|
|
|
|
|
selecting: boolean,
|
|
|
|
|
yearStr: string,
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-12 13:02:31 -05:00
|
|
|
|
export class Calendar extends React.Component<CalendarProps, CalendarState> {
|
2022-02-12 17:46:33 -05:00
|
|
|
|
selection: React.RefObject<HTMLDivElement>;
|
|
|
|
|
|
|
|
|
|
constructor(props: CalendarProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
selecting: false,
|
|
|
|
|
yearStr: this.props.year.toString(),
|
|
|
|
|
};
|
|
|
|
|
this.selection = React.createRef();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
document.addEventListener('click', this.handleClickOutside, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
document.removeEventListener('click', this.handleClickOutside, true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 15:40:45 -05:00
|
|
|
|
private goToNormalized(year: number, month: number) {
|
2022-02-12 16:58:32 -05:00
|
|
|
|
if (month < 1) {
|
2022-02-12 15:40:45 -05:00
|
|
|
|
--year;
|
|
|
|
|
month += 13;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (month > 13) {
|
|
|
|
|
++year;
|
|
|
|
|
month -= 13;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 17:58:54 -05:00
|
|
|
|
if (year < startYear) {
|
|
|
|
|
year = startYear;
|
|
|
|
|
month = 1;
|
|
|
|
|
} else if (year > endYear) {
|
|
|
|
|
year = endYear;
|
|
|
|
|
month = 13;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 15:40:45 -05:00
|
|
|
|
this.props.onSwitch && this.props.onSwitch(year, month as Month);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
prevYear = () => {
|
2022-02-12 15:40:45 -05:00
|
|
|
|
this.goToNormalized(this.props.year - 1, this.props.month);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
prevMonth = () => {
|
2022-02-12 15:40:45 -05:00
|
|
|
|
this.goToNormalized(this.props.year, this.props.month - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
nextYear = () => {
|
2022-02-12 15:40:45 -05:00
|
|
|
|
this.goToNormalized(this.props.year + 1, this.props.month);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
nextMonth = () => {
|
2022-02-12 15:40:45 -05:00
|
|
|
|
this.goToNormalized(this.props.year, this.props.month + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
startSelection = () => {
|
2022-02-12 17:46:33 -05:00
|
|
|
|
this.setState({selecting: true});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
handleClickOutside = (event: any) => {
|
2022-02-12 17:46:33 -05:00
|
|
|
|
if (this.state.selecting && this.selection.current && !this.selection.current.contains(event.target))
|
|
|
|
|
this.setState({selecting: false});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
handleKeyUp = (event: any) => {
|
2022-02-12 17:46:33 -05:00
|
|
|
|
if (event.key === 'Escape')
|
|
|
|
|
this.setState({selecting: false});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
monthChange = (event: any) => {
|
2022-02-13 14:49:14 -05:00
|
|
|
|
this.goToNormalized(this.props.year, +event.target.value as Month);
|
2022-02-12 17:46:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
yearChange = (event: any) => {
|
2022-02-12 17:46:33 -05:00
|
|
|
|
if (/^-?\d+/.test(event.target.value)) {
|
2022-02-12 17:58:54 -05:00
|
|
|
|
this.goToNormalized(+event.target.value, this.props.month);
|
2022-02-12 17:46:33 -05:00
|
|
|
|
}
|
|
|
|
|
this.setState({yearStr: event.target.value});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 21:24:42 -05:00
|
|
|
|
goToToday = () => {
|
|
|
|
|
const {year, month} = jdnFrench(this.props.todayJDN);
|
|
|
|
|
this.goToNormalized(year, month);
|
|
|
|
|
this.setState({selecting: false});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 17:46:33 -05:00
|
|
|
|
componentDidUpdate(prevProps: CalendarProps) {
|
|
|
|
|
if (prevProps.year !== this.props.year) {
|
|
|
|
|
const yearStr = this.props.year.toString();
|
|
|
|
|
if (this.state.yearStr !== yearStr) {
|
|
|
|
|
this.setState({
|
|
|
|
|
yearStr: yearStr,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 13:02:31 -05:00
|
|
|
|
render(): JSX.Element {
|
2022-02-12 15:40:45 -05:00
|
|
|
|
return <div className="Calendar">
|
|
|
|
|
<div className="Calendar-head">
|
|
|
|
|
<div className="Calendar-prev">
|
2022-02-19 21:24:42 -05:00
|
|
|
|
<button type="button" className="btn btn-secondary" title="Previous year" onClick={this.prevYear}>«
|
2022-02-12 15:40:45 -05:00
|
|
|
|
</button>
|
|
|
|
|
<button type="button" className="btn btn-secondary" title="Previous month"
|
2022-02-19 21:24:42 -05:00
|
|
|
|
onClick={this.prevMonth}>‹
|
2022-02-12 15:40:45 -05:00
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2022-02-19 21:24:42 -05:00
|
|
|
|
{!this.state.selecting && <div className="Calendar-month-name" onClick={this.startSelection}>
|
2022-02-12 16:58:32 -05:00
|
|
|
|
{this.props.month < 13 && monthName(this.props.month)} {this.props.year}
|
2022-02-12 17:46:33 -05:00
|
|
|
|
</div>}
|
|
|
|
|
{this.state.selecting && <div className="Calendar-month-name input-group" ref={this.selection}
|
2022-02-19 21:24:42 -05:00
|
|
|
|
onKeyUp={this.handleKeyUp}>
|
|
|
|
|
<select className="Calendar-month-input form-control" onChange={this.monthChange}
|
2022-02-12 17:46:33 -05:00
|
|
|
|
value={this.props.month}>{
|
|
|
|
|
Array.from(Array(13).keys()).map(i => {
|
|
|
|
|
const month = i + 1 as Month;
|
2022-02-19 21:31:43 -05:00
|
|
|
|
return <option key={i} value={month}>{monthName(month)}</option>;
|
2022-02-12 17:46:33 -05:00
|
|
|
|
})
|
|
|
|
|
}</select>
|
|
|
|
|
<input type="number" className="Calendar-year-input form-control" value={this.state.yearStr}
|
2022-02-19 21:24:42 -05:00
|
|
|
|
onChange={this.yearChange} min={startYear} max={endYear}/>
|
2022-02-12 17:46:33 -05:00
|
|
|
|
<button type="button" className="form-control btn btn-primary Calendar-today-button"
|
2022-02-19 21:24:42 -05:00
|
|
|
|
onClick={this.goToToday}>Today
|
2022-02-12 17:46:33 -05:00
|
|
|
|
</button>
|
|
|
|
|
</div>}
|
2022-02-12 15:40:45 -05:00
|
|
|
|
<div className="Calendar-next">
|
2022-02-19 21:24:42 -05:00
|
|
|
|
<button type="button" className="btn btn-secondary" title="Next month" onClick={this.nextMonth}>›
|
2022-02-12 15:40:45 -05:00
|
|
|
|
</button>
|
2022-02-19 21:24:42 -05:00
|
|
|
|
<button type="button" className="btn btn-secondary" title="Next year" onClick={this.nextYear}>»
|
2022-02-12 15:40:45 -05:00
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2022-02-12 17:46:33 -05:00
|
|
|
|
{this.props.month < 13 &&
|
|
|
|
|
<NormalMonth year={this.props.year} month={this.props.month} todayJDN={this.props.todayJDN}/>}
|
2022-02-12 16:58:32 -05:00
|
|
|
|
{this.props.month === 13 && <ComplementaryDays year={this.props.year} todayJDN={this.props.todayJDN}/>}
|
2022-02-12 15:40:45 -05:00
|
|
|
|
</div>;
|
2022-02-12 13:02:31 -05:00
|
|
|
|
}
|
|
|
|
|
}
|