-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.js
105 lines (95 loc) · 2.76 KB
/
calendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(function(){
var monthSet = [
"January hello",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var weekSet = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var today = new Date();
var getMonthInfo = function(month, year) {
var firstDay = new Date(year, month, 1);
var lastDay = new Date(year, month + 1, 0);
var weeks = Math.ceil((firstDay.getDay() + lastDay.getDate()) / 7);
return { first: firstDay, last: lastDay, weeks: weeks };
};
class Calender extends React.Component {
constructor(props) {
super(props);
this.state = {
monthOffset: 0,
selected: 2
};
this.back = function() {
this.setState({ monthOffset: this.state.monthOffset - 1 });
}.bind(this);
this.foward = function() {
this.setState({ monthOffset: this.state.monthOffset + 1 });
}.bind(this);
}
render() {
var month = today.getMonth() + this.state.monthOffset;
var year = new Date(
today.getFullYear(),
month,
today.getDay()
).getFullYear(); // better way to do this
month = (month % 12 + 12) % 12;
return (
<div class="cal">
<div class="nav">
<button class="back" onClick={this.back}>back</button>
<h2>{monthSet[month % 12] + " " + year}</h2>
<button class="foward" onClick={this.foward}>foward</button>
</div>
<Days />
<div id="container">
<Month month={month} year={year} />
<div class="inset">
<Month month={month + 1} year={year} />
</div>
<Month month={month + 2} year={year} />
</div>
</div>
);
}
}
var Month = props => {
var data = getMonthInfo(props.month, props.year);
var weeks = Array.from({length:data.weeks},(e,i)=>
<Week
first={i * 7 - data.first.getDay() + 1}
last={((i + 1) * 7 - data.first.getDay()) % data.last.getDate()}
end={data.last.getDate()}
/>)
return <div>{weeks}</div>;}
var Week = function(props) {
var days = Array.from({length:7},(_,i) => {
var day = i + props.first;
var c =
day <= 0
? "before"
: day > props.end
? "after"
: i == 6 ? "g" : i == 0 ? "b" : i == 3 ? "y" : "in"; //set class name for 'Day' component
return <Day c={"day " + c} day={day % (props.end + 1)} />;
});
return <div class="week">{days}</div>;
};
var Day = function(props) {
return <div class={props.c}>{props.day}</div>;
};
var Days = function(props) {
d = weekSet.map(e => <div>{e}</div>);
return <div class="days">{d}</div>;
};
ReactDOM.render(<Calender />, document.getElementById("calender"));
}())