-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
200 lines (159 loc) · 4.59 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Author : James La Broy
*/
Object.prototype.calendar = function() {
var date_input = this;
var days = ['sun','mon','tue','wed','thu','fri','sat'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var date = date_input.value == '' ? new Date() : new Date(date_input.value);
var current_month = date.getMonth();
var current_year = date.getFullYear();
var day;
var month;
var year;
if (date_input.value != '') {
var selected_day = date.getDate(),
selected_month = date.getMonth() + 1,
selected_year = date.getFullYear();
} else {
var selected_day, selected_month, selected_year;
}
var calendar;
init();
function init() {
// wrap the input element with a div
var container = document.createElement("div");
container.classList.add("calendar");
date_input.parentNode.insertBefore(container, date_input);
date_input.parentNode.removeChild(date_input);
container.appendChild(date_input);
// hide html5 element
date_input.style.display = 'none';
// add calendar elements
var header = document.createElement("div");
header.classList.add("calendar-header");
container.appendChild(header);
month = document.createElement("div");
month.classList.add("month");
month.innerHTML = months[current_month];
header.appendChild(month);
year = document.createElement("div");
year.classList.add("year");
year.innerHTML = current_year;
header.appendChild(year);
var next = document.createElement("div");
next.classList.add("next");
next.classList.add("fa-right");
next.onclick = function() {
current_month++;
if (current_month == 12) {
current_year++;
current_month = 0;
}
changeDate(current_month, current_year);
};
header.appendChild(next);
var previous = document.createElement("div");
previous.classList.add("previous");
previous.classList.add("fa-left");
previous.onclick = function() {
current_month--;
if (current_month == -1) {
current_year--;
current_month = 11;
}
changeDate(current_month, current_year);
};
header.appendChild(previous);
calendar = document.createElement("table");
var days_row = document.createElement("tr");
calendar.appendChild(days_row);
for (var i in days) {
if (!isNaN(i)) {
var day = document.createElement("th");
day.innerHTML = days[i];
days_row.appendChild(day);
}
}
for (var i = 0; i < 6; i++) {
var row = document.createElement("tr");
row.classList.add("day-row");
calendar.appendChild(row);
for (var ii in days) {
if (!isNaN(ii)) {
var day = document.createElement("td");
day.classList.add("day");
row.appendChild(day);
}
}
}
container.appendChild(calendar);
changeDate(current_month, current_year);
}
function changeDate(m, y) {
var start = new Date(y, m, 1).getDay();
var end = new Date(y, m+1, 0).getDate();
month.innerHTML = months[m];
year.innerHTML = y;
// wipe out current calendar
var _days = calendar.querySelectorAll(".day");
for (var i in _days) {
if (!isNaN(i)) {
_days[i].innerHTML = '';
_days[i].classList.remove("filled");
}
}
if (calendar.querySelector(".selected") != null) {
calendar.querySelector(".selected").classList.remove("selected");
}
var rows = calendar.querySelectorAll(".day-row");
var cur = 1;
for (var i in rows) {
if (!isNaN(i)) {
var days = rows[i].querySelectorAll(".day");
for (var ii in days) {
if (!isNaN(ii)) {
days[ii].onclick = function() {
if (calendar.querySelector(".selected") != null) {
day = this.innerHTML;
calendar.querySelector(".selected").classList.remove("selected");
}
this.classList.add("selected");
setDate(this.innerHTML, m+1, y);
}
if (cur > end) {
continue;
}
if (i == 0) {
if (ii == start) {
if (cur == selected_day && m == selected_month - 1 && y == selected_year) {
days[ii].classList.add("selected");
}
days[ii].classList.add("filled");
days[ii].innerHTML = cur;
cur++;
continue;
}
}
if (cur > 1) {
if (cur == selected_day && m == selected_month - 1 && y == selected_year) {
days[ii].classList.add("selected");
}
days[ii].classList.add("filled");
days[ii].innerHTML = cur;
cur++;
}
}
}
}
}
}
function setDate(d, m, y) {
selected_day = d;
selected_month = m;
selected_year = y;
m = m < 10 ? '0'+m : m;
d = d < 10 ? '0'+d : d;
date_input.value = y + '-' + m + '-' + d;
}
}