-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalendar.js
183 lines (159 loc) · 5.22 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
/*
Calendar aside/plugin for octopress blogging framework.
Copyright (C) 2012 Neeraj Kumar ( [email protected], [email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// these are labels for the days of the week
cal_days_labels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
// these are human-readable month name labels, in order
cal_months_labels = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'];
// these are the days of the week for each month, in order
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// this is the current date
cal_current_date = new Date();
function generateGuid()
{
var result, i, j;
result = '';
for(j=0; j<32; j++) {
if( j == 8 || j == 12|| j == 16|| j == 20)
result = result + '-';
i = Math.floor(Math.random()*16).toString(16).toUpperCase();
result = result + i;
}
return result;
}
function createCalendar(id)
{
}
function Calendar(month, year, highlightArray, highlightLinks) {
this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
this.year = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
this.html = '';
this.guid = '';
//array of dates which are to be highlighted.
//Expects the dates in the form MM/DD/YYYY
this.highlightArray = highlightArray;
this.highlightLinks = highlightLinks;
}
Calendar.prototype.generateHTML = function(id) {
if(id == undefined)
{
id = generateGuid();
}
this.guid = id
// get first day of month
var firstDay = new Date(this.year, this.month, 1);
var startingDay = firstDay.getDay();
// find number of days in month
var monthLength = cal_days_in_month[this.month];
// compensate for leap year
if (this.month == 1) { // February only!
if((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
monthLength = 29;
}
}
// do the header
var monthName = cal_months_labels[this.month]
var prevYear;
var nextYear;
var prevMonth;
var nextMonth;
if(this.month == 0)
{
prevYear = this.year -1;
nextYear = this.year;
prevMonth = 11;
nextMonth = this.month +1;
}
else if(this.month == 11)
{
prevYear = this.year;
nextYear = this.year+1;
prevMonth = this.month -1;
nextMonth = 1;
}
else
{
prevYear = this.year;
nextYear = this.year;
prevMonth = this.month - 1;
nextMonth = this.month + 1;
}
var html = '<table id="'+id+'-calendar-table" class="calendar-table">';
html += '<tr><th colspan="7">';
html += '<a id="'+this.id+'-prev-calendar-link" class="prev-link" style="height:25px;width:25px" href="#" onclick="monthCalendar(\''+this.guid+'\','+prevYear+','+prevMonth+');return false;" ><img class="prevCalendarLink" style="height:25px;width:25px;border:none 0px;padding-top:2px" src="/images/prev.png"></img></a>';
html += monthName + " " + this.year;
html += '<a id="'+this.id+'-next-calendar-link" class="next-link" style="height:25px;width:25px" href="#" onclick="monthCalendar(\''+this.guid+'\','+nextYear+','+nextMonth+');return false;"><img class="nextCalendarLink" style="height:25px;width:25px;border:none 0px;padding-top:2px" src="/images/next.png"></img></a>';
html += '</th></tr>';
html += '<tr class="calendar-header">';
for(var i = 0; i <= 6; i++ ){
html += '<td class="calendar-header-day">';
html += cal_days_labels[i];
html += '</td>';
}
html += '</tr><tr>';
// fill in the days
var day = 1;
// this loop is for is weeks (rows)
for (var i = 0; i < 9; i++) {
// this loop is for weekdays (cells)
for (var j = 0; j <= 6; j++) {
var strMonth;
var strDay;
if(day < 10 )
{
strDay = '0'+day;
}
else
{
strDay = day;
}
if( (this.month+1) < 10 )
{
strMonth = '0' + (this.month +1);
}
else
{
strMonth = this.month+1;
}
var date = strMonth+"/"+strDay+"/"+this.year;
var isEvent = this.highlightArray.indexOf(date) > -1;
if(isEvent){
html += '<td><span class="highlighted-calendar-day"><a href="'+this.highlightLinks[this.highlightArray.indexOf(date)]+'">';
}else{
html += '<td><span class="calendar-day">';
}
if (day <= monthLength && (i > 0 || j >= startingDay)) {
html += day;
day++;
}
if(isEvent)
html += '</a></span></td>';
else
html += '</span></td>';
}
// stop making rows if we've run out of days
if (day > monthLength) {
break;
} else {
html += '</tr><tr>';
}
}
html += '</tr></table>';
this.html = html;
}
Calendar.prototype.getHTML = function() {
return this.html;
}