-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalendar.js
More file actions
156 lines (123 loc) · 4.27 KB
/
calendar.js
File metadata and controls
156 lines (123 loc) · 4.27 KB
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
/**
* calendar.js - Clean simple calendar for the Script on JavaScript.
*
* Copyright 2018 #!nit (initforcode.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
**/
let monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
/**
* This function resets any previously stored data and reinitializes the
* calendar from scratch. Accepts a single month as argument. Only constructs
* the calendar for the current year.
*
*/
var calendarSetup = function (month) {
// Explain short circuiting.
let currentDate = new Date();
month == null && (month = currentDate.getMonth());
// Remove all existing rows.
$('.week').each(function () { $(this).remove(); })
// Set the little arrow keys.
switch (month) {
case 0:
// No month before January.
$('#prev').attr('disabled', true);
break;
case 11:
// No month after December.
$('#next').attr('disabled', true);
break;
default:
$('#prev').attr('disabled', false);
$('#next').attr('disabled', false);
break;
}
let firstDayOfMonth = new Date(2018, month, 1).getDay();
let totalDaysInMonth = new Date(2018, month + 1, 0).getDate();
// Create the first week.
let calendarWeek = $('<tr></tr>');
calendarWeek.addClass('week');
// Add empty <td> for non existing days.
for (let i = 0; i < firstDayOfMonth; ++i) {
emptyDay = $('<td></td>'); // Create new data for row.
calendarWeek.append(emptyDay); // alt: prepend, after, before.
}
// Start adding all the actual days.
let dayCount = firstDayOfMonth;
for (let i = 1; i <= totalDaysInMonth; ++i) {
// Create a new <td> for the current date.
calendarDate = $('<td></td>')
calendarDate.attr('id', 'day' + i);
calendarDate.text(i);
calendarWeek.append(calendarDate);
dayCount++;
// If the week is over, add the row to calendar and reset the week.
if (dayCount == 7) {
$('#calendar').append(calendarWeek);
calendarWeek = $('<tr></tr>');
calendarWeek.addClass('week');
dayCount = 0;
}
}
// Add empty <td> for non existing days.
for (; dayCount < 7; dayCount++) {
emptyDay = $('<td></td>');
calendarWeek.append(emptyDay);
}
// Append the last week to calendar.
$('#calendar').append(calendarWeek);
// If the calendar shows the current month, display date.
if (currentDate.getMonth() == month) {
// Changing the class for current date.
$('#day' + currentDate.getDate()).addClass('current-date');
}
$('#month').text(monthNames[month]);
}
/**
* Function to move to the previous month.
*/
var prevMonth = function () {
let month = $('#month').text();
calendarSetup(monthNames.indexOf(month)-1);
}
/**
* Function to move to the next month.
*/
var nextMonth = function () {
let month = $('#month').text();
calendarSetup(monthNames.indexOf(month)+1);
}
$(document).ready(function () {
calendarSetup();
});