FossilRepo

fossilrepo / assets / admin / js / calendar.d64496bbf46d.js
Blame History Raw 240 lines
1
/*global gettext, pgettext, get_format, quickElement, removeChildren*/
2
/*
3
calendar.js - Calendar functions by Adrian Holovaty
4
depends on core.js for utility functions like removeChildren or quickElement
5
*/
6
'use strict';
7
{
8
// CalendarNamespace -- Provides a collection of HTML calendar-related helper functions
9
const CalendarNamespace = {
10
monthsOfYear: [
11
gettext('January'),
12
gettext('February'),
13
gettext('March'),
14
gettext('April'),
15
gettext('May'),
16
gettext('June'),
17
gettext('July'),
18
gettext('August'),
19
gettext('September'),
20
gettext('October'),
21
gettext('November'),
22
gettext('December')
23
],
24
monthsOfYearAbbrev: [
25
pgettext('abbrev. month January', 'Jan'),
26
pgettext('abbrev. month February', 'Feb'),
27
pgettext('abbrev. month March', 'Mar'),
28
pgettext('abbrev. month April', 'Apr'),
29
pgettext('abbrev. month May', 'May'),
30
pgettext('abbrev. month June', 'Jun'),
31
pgettext('abbrev. month July', 'Jul'),
32
pgettext('abbrev. month August', 'Aug'),
33
pgettext('abbrev. month September', 'Sep'),
34
pgettext('abbrev. month October', 'Oct'),
35
pgettext('abbrev. month November', 'Nov'),
36
pgettext('abbrev. month December', 'Dec')
37
],
38
daysOfWeek: [
39
gettext('Sunday'),
40
gettext('Monday'),
41
gettext('Tuesday'),
42
gettext('Wednesday'),
43
gettext('Thursday'),
44
gettext('Friday'),
45
gettext('Saturday')
46
],
47
daysOfWeekAbbrev: [
48
pgettext('abbrev. day Sunday', 'Sun'),
49
pgettext('abbrev. day Monday', 'Mon'),
50
pgettext('abbrev. day Tuesday', 'Tue'),
51
pgettext('abbrev. day Wednesday', 'Wed'),
52
pgettext('abbrev. day Thursday', 'Thur'),
53
pgettext('abbrev. day Friday', 'Fri'),
54
pgettext('abbrev. day Saturday', 'Sat')
55
],
56
daysOfWeekInitial: [
57
pgettext('one letter Sunday', 'S'),
58
pgettext('one letter Monday', 'M'),
59
pgettext('one letter Tuesday', 'T'),
60
pgettext('one letter Wednesday', 'W'),
61
pgettext('one letter Thursday', 'T'),
62
pgettext('one letter Friday', 'F'),
63
pgettext('one letter Saturday', 'S')
64
],
65
firstDayOfWeek: parseInt(get_format('FIRST_DAY_OF_WEEK')),
66
isLeapYear: function(year) {
67
return (((year % 4) === 0) && ((year % 100) !== 0 ) || ((year % 400) === 0));
68
},
69
getDaysInMonth: function(month, year) {
70
let days;
71
if (month === 1 || month === 3 || month === 5 || month === 7 || month === 8 || month === 10 || month === 12) {
72
days = 31;
73
}
74
else if (month === 4 || month === 6 || month === 9 || month === 11) {
75
days = 30;
76
}
77
else if (month === 2 && CalendarNamespace.isLeapYear(year)) {
78
days = 29;
79
}
80
else {
81
days = 28;
82
}
83
return days;
84
},
85
draw: function(month, year, div_id, callback, selected) { // month = 1-12, year = 1-9999
86
const today = new Date();
87
const todayDay = today.getDate();
88
const todayMonth = today.getMonth() + 1;
89
const todayYear = today.getFullYear();
90
let todayClass = '';
91
92
// Use UTC functions here because the date field does not contain time
93
// and using the UTC function variants prevent the local time offset
94
// from altering the date, specifically the day field. For example:
95
//
96
// ```
97
// var x = new Date('2013-10-02');
98
// var day = x.getDate();
99
// ```
100
//
101
// The day variable above will be 1 instead of 2 in, say, US Pacific time
102
// zone.
103
let isSelectedMonth = false;
104
if (typeof selected !== 'undefined') {
105
isSelectedMonth = (selected.getUTCFullYear() === year && (selected.getUTCMonth() + 1) === month);
106
}
107
108
month = parseInt(month);
109
year = parseInt(year);
110
const calDiv = document.getElementById(div_id);
111
removeChildren(calDiv);
112
const calTable = document.createElement('table');
113
quickElement('caption', calTable, CalendarNamespace.monthsOfYear[month - 1] + ' ' + year);
114
const tableBody = quickElement('tbody', calTable);
115
116
// Draw days-of-week header
117
let tableRow = quickElement('tr', tableBody);
118
for (let i = 0; i < 7; i++) {
119
quickElement('th', tableRow, CalendarNamespace.daysOfWeekInitial[(i + CalendarNamespace.firstDayOfWeek) % 7]);
120
}
121
122
const startingPos = new Date(year, month - 1, 1 - CalendarNamespace.firstDayOfWeek).getDay();
123
const days = CalendarNamespace.getDaysInMonth(month, year);
124
125
let nonDayCell;
126
127
// Draw blanks before first of month
128
tableRow = quickElement('tr', tableBody);
129
for (let i = 0; i < startingPos; i++) {
130
nonDayCell = quickElement('td', tableRow, ' ');
131
nonDayCell.className = "nonday";
132
}
133
134
function calendarMonth(y, m) {
135
function onClick(e) {
136
e.preventDefault();
137
callback(y, m, this.textContent);
138
}
139
return onClick;
140
}
141
142
// Draw days of month
143
let currentDay = 1;
144
for (let i = startingPos; currentDay <= days; i++) {
145
if (i % 7 === 0 && currentDay !== 1) {
146
tableRow = quickElement('tr', tableBody);
147
}
148
if ((currentDay === todayDay) && (month === todayMonth) && (year === todayYear)) {
149
todayClass = 'today';
150
} else {
151
todayClass = '';
152
}
153
154
// use UTC function; see above for explanation.
155
if (isSelectedMonth && currentDay === selected.getUTCDate()) {
156
if (todayClass !== '') {
157
todayClass += " ";
158
}
159
todayClass += "selected";
160
}
161
162
const cell = quickElement('td', tableRow, '', 'class', todayClass);
163
const link = quickElement('a', cell, currentDay, 'href', '#');
164
link.addEventListener('click', calendarMonth(year, month));
165
currentDay++;
166
}
167
168
// Draw blanks after end of month (optional, but makes for valid code)
169
while (tableRow.childNodes.length < 7) {
170
nonDayCell = quickElement('td', tableRow, ' ');
171
nonDayCell.className = "nonday";
172
}
173
174
calDiv.appendChild(calTable);
175
}
176
};
177
178
// Calendar -- A calendar instance
179
function Calendar(div_id, callback, selected) {
180
// div_id (string) is the ID of the element in which the calendar will
181
// be displayed
182
// callback (string) is the name of a JavaScript function that will be
183
// called with the parameters (year, month, day) when a day in the
184
// calendar is clicked
185
this.div_id = div_id;
186
this.callback = callback;
187
this.today = new Date();
188
this.currentMonth = this.today.getMonth() + 1;
189
this.currentYear = this.today.getFullYear();
190
if (typeof selected !== 'undefined') {
191
this.selected = selected;
192
}
193
}
194
Calendar.prototype = {
195
drawCurrent: function() {
196
CalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback, this.selected);
197
},
198
drawDate: function(month, year, selected) {
199
this.currentMonth = month;
200
this.currentYear = year;
201
202
if(selected) {
203
this.selected = selected;
204
}
205
206
this.drawCurrent();
207
},
208
drawPreviousMonth: function() {
209
if (this.currentMonth === 1) {
210
this.currentMonth = 12;
211
this.currentYear--;
212
}
213
else {
214
this.currentMonth--;
215
}
216
this.drawCurrent();
217
},
218
drawNextMonth: function() {
219
if (this.currentMonth === 12) {
220
this.currentMonth = 1;
221
this.currentYear++;
222
}
223
else {
224
this.currentMonth++;
225
}
226
this.drawCurrent();
227
},
228
drawPreviousYear: function() {
229
this.currentYear--;
230
this.drawCurrent();
231
},
232
drawNextYear: function() {
233
this.currentYear++;
234
this.drawCurrent();
235
}
236
};
237
window.Calendar = Calendar;
238
window.CalendarNamespace = CalendarNamespace;
239
}
240

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button