-
Notifications
You must be signed in to change notification settings - Fork 4
/
calendar.js
172 lines (141 loc) · 6.34 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
/* Vanilla JS Calendar */
(function vanillaJsCalendar(){
"use strict";
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
document.addEventListener("DOMContentLoaded", function(event){
var theDate = new Date();
var DateObject = function DateObject(theDate) {
this.theDay = theDate.getDate();
this.dayName = dayNames[theDate.getDay()];
this.theMonth = monthNames[theDate.getMonth()];
this.theYear = theDate.getFullYear();
this.daysInMonth = new Date(theDate.getFullYear(), theDate.getMonth()+1, 0).getDate();
this.firstDayOfMonth = dayNames[new Date(theDate.getFullYear(), theDate.getMonth(), 1).getDay()];
};
var currentDate = new DateObject(theDate);
function renderCalendar(targetElem){
// Custom function to make new elements easier:
function addElem(elementType, elemClass, appendTarget){
appendTarget.innerHTML += "<"+elementType+" class="+elemClass+"> </"+elementType+">";
}
currentDate = new DateObject(theDate);
// Refreshing Calendar
var renderTarget = document.getElementById(targetElem);
renderTarget.remove();
renderTarget = document.createElement("div");
renderTarget.id = targetElem;
document.getElementsByTagName('body')[0].appendChild(renderTarget);
// Monday, dayView
addElem("div", "day-view", renderTarget);
var dayView = document.querySelector('.day-view');
var dayNameElem = document.createElement("div"); // i.e.: Wednesday
dayNameElem.className = "day-header";
var dayNameNode = document.createTextNode(currentDate.dayName);
dayNameElem.appendChild(dayNameNode);
dayView.appendChild(dayNameElem);
// 21st, dayNumber
addElem("time", "day-number", dayView);
var dayNumber = document.querySelector('.day-number');
var dayNumNode = document.createTextNode(currentDate.theDay);
dayNumber.appendChild(dayNumNode);
dayView.appendChild(dayNumber);
addElem("div", "month-view", renderTarget);
var monthView = document.querySelector('.month-view');
var prevMonthSpan = document.createElement("SPAN");
prevMonthSpan.addEventListener('click', function(){
goToMonth(currentDate, false); // Go To Previous Month
});
prevMonthSpan.classList.add('arrow', 'float-left', 'prev-arrow');
var backArrow = document.createTextNode("<");
prevMonthSpan.appendChild(backArrow);
var nextMonthSpan = document.createElement("SPAN");
nextMonthSpan.addEventListener('click', function(){
goToMonth(currentDate, true); // Go To Next Month
});
nextMonthSpan.classList.add('arrow', 'float-right', 'next-arrow');
var nextArrow = document.createTextNode(">");
nextMonthSpan.appendChild(nextArrow);
document.onkeydown = function() {
switch (window.event.keyCode) {
case 37: //Left key
goToMonth(currentDate, false);
break;
case 39: //Right key
goToMonth(currentDate, true);
break;
}
};
var monthSpan = document.createElement("SPAN");
monthSpan.className = "month-header";
var monthOf = document.createTextNode(
currentDate.theMonth +" "+ currentDate.theYear
);
monthSpan.appendChild(prevMonthSpan);
monthSpan.appendChild(monthOf);
monthSpan.appendChild(nextMonthSpan);
monthView.appendChild(monthSpan);
for(var i=0; i < dayNames.length; i++){
var dayOfWeek = document.createElement('div');
dayOfWeek.className = "day-of-week";
var charOfDay = document.createTextNode(dayNames[i].charAt(0));
dayOfWeek.appendChild(charOfDay);
monthView.appendChild(dayOfWeek);
}
// renderTarget.appendChild(document.createElement("ul"));
var calendarList = document.createElement("ul");
for(i = 0; i < currentDate.daysInMonth; i++){
var calendarCell = document.createElement("li");
var calCellTime = document.createElement("time");
calendarList.appendChild(calendarCell);
calendarCell.id = 'day_'+(i+1);
var dayDataDate = new Date(theDate.getFullYear(), theDate.getMonth(), (i+1));
calCellTime.setAttribute('datetime', dayDataDate.toISOString());
calCellTime.setAttribute('data-dayofweek', dayNames[dayDataDate.getDay()]);
calendarCell.className = "calendar-cell";
if(i === currentDate.theDay-1){
calendarCell.className = "today";
}
var dayOfMonth = document.createTextNode(i+1);
calCellTime.appendChild(dayOfMonth);
calendarCell.appendChild(calCellTime);
monthView.appendChild(calendarList);
} // daysInMonth for loop ends
var dayOne = document.getElementById('day_1');
if (currentDate.firstDayOfMonth == "Monday"){
dayOne.style.marginLeft = "49px";
} else if (currentDate.firstDayOfMonth == "Tuesday"){
dayOne.style.marginLeft = "98px";
} else if (currentDate.firstDayOfMonth == "Wednesday"){
dayOne.style.marginLeft = "147px";
} else if (currentDate.firstDayOfMonth == "Thursday"){
dayOne.style.marginLeft = "196px";
} else if (currentDate.firstDayOfMonth == "Friday"){
dayOne.style.marginLeft = "245px";
} else if (currentDate.firstDayOfMonth == "Saturday"){
dayOne.style.marginLeft = "304px";
}
var dayHeader = document.getElementsByClassName('day-header');
var dayNumNode = document.getElementsByClassName('day-number');
var updateDay = function(){
var thisCellTime = this.querySelector('time');
dayHeader[0].textContent = thisCellTime.getAttribute('data-dayofweek');
dayNumNode[0].textContent = this.textContent;
}
var calCells = document.getElementsByClassName('calendar-cell');
for(i = 0; i < calCells.length; i++){
calCells[i].addEventListener('click', updateDay, false);
}
} // renderCalener function ends
// console.log(new DateObject(theDate));
renderCalendar("calendarThis");
function goToMonth(currentDate, direction) {
if (direction == false){
theDate = new Date(theDate.getFullYear(), theDate.getMonth()-1, 1);
} else{
theDate = new Date(theDate.getFullYear(), theDate.getMonth()+1, 1);
}
return renderCalendar("calendarThis");
}
}); // DOMContentLoaded event listener ends
})(); // iife (immediately invoked function expressions) ends