-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
180 lines (140 loc) · 4.08 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
$().ready( function () {
Calendar.init();
});
var Calendar = (function() {
var months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
var today = new Date();
var startingYear = today.getFullYear();
var monthIndex = today.getMonth()
var startingMonth = months[monthIndex];
var init = function() {
//on page load the current calendar is shown
generateYear(startingMonth, startingYear);
getClicks();
}
var generateYear = function(month, year) {
//static vars
var monthLengths = [
Array(31), Array(28), Array(31), Array(30),
Array(31), Array(30), Array(31), Array(31),
Array(30), Array(31), Array(30),Array(31)];
//making change for Leap Year
if (checkForLeapYear(year)) {
monthLengths[1] = Array(29)
}
var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
//array to be filled with dates/day names
var thisMonth = [];
//current info supplied by function call
var dateInfo = new Date(month + ' 1, ' + year)
var startMonth = dateInfo.getMonth()
//length of the month determined by monthLenghts list
var startMonthLenght = monthLengths[startMonth].length;
var startDay = dateInfo.getDay()
//these vars will be manipulated by for loops to fill thisMonth array
var currentDay = 1;
var dayIndex = 0;
//generate first week of month
for (var i = startDay; i <=6; i++) {
thisMonth.push({'date': currentDay, 'day':daysOfWeek[i]})
currentDay++
}
for (var x = thisMonth.length+1; x <= startMonthLenght; x++) {
thisMonth.push({'date': x, 'day': daysOfWeek[dayIndex]})
currentDay++
if (dayIndex < 6) {
dayIndex++
}
else {
dayIndex = 0;
}
}
//startDay is used here to determine how many blank Divs are necessary
//when displaying the calendar
displayMonth(thisMonth, month, year, startDay);
//highlighting today
highlightDay(month, year);
//passing the current month info to the alert function
showPopUpsOnClick(thisMonth);
}
var displayMonth = function(month, name, year, blankDivTotal) {
//display Month Name
$('.calendarHeader').text(name + " " + year)
//accessing the calender div to insert the day divs
var dayHolder = $('.dayHolder');
dayHolder.empty();
if (blankDivTotal != 7) {
for (var x=0; x <=blankDivTotal-1; x++) {
dayHolder.append('<div class="blankBox">')
}
}
for (var i=0; i <= month.length-1; i++) {
var day = month[i].date
dayHolder.append('<div class="dayBox" id=day'+day+'>'+day+'</div>')
}
}
var getClicks = function() {
var arrowLeft = $('#arrowLeft');
var arrowRight = $('#arrowRight');
arrowLeft.on('click', function() {
if (monthIndex > 0) {
monthIndex--;
}
else {
monthIndex = 11
startingYear--;
}
generateNewYear(startingYear);
$('.alertBox').slideUp();
})
arrowRight.on('click', function() {
if (monthIndex < 11) {
monthIndex++;
}
else {
monthIndex = 0
startingYear++
}
generateNewYear(startingYear);
$('.alertBox').slideUp();
})
}
var generateNewYear = function(startingYear) {
var newMonth = months[monthIndex];
generateYear(newMonth, startingYear)
}
var highlightDay = function(month, year) {
var correctYear = today.getFullYear()
var correctMonth = months[today.getMonth()]
if (month === correctMonth && year === correctYear) {
$('#day' + today.getDate()).css('background-color', 'red')
}
}
var checkForLeapYear = function(year) {
var isLeapYear = false;
if (year % 4 === 0 && (year % 100 != 0 || year % 400 != 0) ){
//leap year
isLeapYear = true;
}
return isLeapYear
}
var showPopUpsOnClick = function(month) {
var day = $('.dayBox');
var alertBox = $('.alertBox')
day.on('click', function(e) {
var id = e.target.id;
var day = id.slice(3); //removes 'day' from ID tag
var dayObject = month[day-1] //0 indexed
alertBox.text(dayObject.day + ', ' + dayObject.date)
alertBox.slideDown();
})
alertBox.on('click', function() {
alertBox.slideUp();
})
}
return {
'init': init
}
})();