-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendario.js
190 lines (155 loc) · 5.52 KB
/
calendario.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
182
183
184
185
186
187
188
189
190
'use strict';
window.Calendario = (function() {
const alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'z+'];
const monthFullNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const dayFullNames = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
const dayShortNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const fontFamily = 'Ubuntu';
const k = 10;
const w = 210 * k;
const h = 297 * k;
const wc = w / 9;
const baseFontSize = 60 * 2;
const imageURL = 'image.jpg';
function getWeek(date) {
let dayNumber = (date.getDay() + 6) % 7;
let target = new Date(date.valueOf());
target.setDate(target.getDate() - dayNumber + 3);
let firstThursday = target.valueOf();
target.setMonth(0, 1);
if (target.getDay() != 4) {
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
return 1 + Math.ceil((firstThursday - target) / (7 * 24 * 3600 * 1000));
}
function getWeekDay(date) {
let day = date.getDay();
return (day == 0) ? 6 : day - 1;
}
function dayNameCell(text) {
let dv = new SVG.G();
dv.rect(wc, wc);
let t = dv.text(text)
t.move(wc / 2, 0);
t.font({
family: fontFamily,
size: baseFontSize / 2.75,
anchor: 'middle',
leading: '3em'
});
return dv;
}
function dayCell(text) {
let dv = new SVG.G();
dv.rect(wc, wc);
if (text) {
let t = dv.text(text)
t.move(wc / 2, 0);
t.font({
family: fontFamily,
size: baseFontSize,
anchor: 'middle',
leading: '1.4em'
});
}
return dv;
}
function sprintIdCell(date) {
let sprintId = getWeek(date);
let sprintHalfId = Math.ceil((sprintId/2)) - 1;
let sprintAlphaId = alpha[sprintHalfId];
let dv = new SVG.G();
dv.rect(wc, wc);
let t = dv.text(sprintId + ' ' + sprintAlphaId);
t.move(wc / 2, 0);
t.font({
family: fontFamily,
size: baseFontSize * 0.3,
anchor: 'middle',
leading: '4em'
});
dv.attr({ class: 'sprint-id' });
return dv;
}
function renderMonth(year, month, highlightDays) {
let table = new SVG.G();
let yk = 0;
let monthBlock = table.group();
let daysMatrix = monthBlock.group();
let g = daysMatrix.group();
g.attr({ class: 'month-name' });
g.move(0, yk * wc);
let t = g.text(monthFullNames[month - 1]);
t.move(w - 2*wc, 0);
t.font({
family: fontFamily,
size: baseFontSize,
anchor: 'end',
leading: '1.5em'
});
yk++;
for (let d = 0; d < 7; d++) {
let className = 'day day-names' + ((d >= 5) ? ' day-holyday' : '');
let dv = dayNameCell(dayFullNames[d]);
dv.attr({ class: className });
dv.move(wc * d, yk * wc);
daysMatrix.add(dv);
}
let date = new Date(month + '/' + 1 + '/' + year);
let firstWeekDayOfMonth = getWeekDay(date);
if (firstWeekDayOfMonth > 0) {
yk++;
let sprintCell = sprintIdCell(date);
sprintCell.move(-wc, wc * yk);
daysMatrix.add(sprintCell);
}
date.setDate(date.getDate() - firstWeekDayOfMonth);
for (let d = 0; d < firstWeekDayOfMonth; d++) {
let dv = dayCell(date.getDate().toString());
dv.attr({ class: 'day of-prev-month' });
dv.move(d * wc, yk * wc);
daysMatrix.add(dv);
date.setDate(date.getDate() + 1);
}
for (let d = 1; d < 32; d++) {
let weekDay = getWeekDay(date);
if (weekDay == 0) {
yk++;
let sprintCell = sprintIdCell(date);
sprintCell.move(-wc, wc * yk);
daysMatrix.add(sprintCell);
}
let className = 'day';
if (weekDay > 4) { className += ' day-holyday'; }
let dv = dayCell(d.toString());
dv.attr({ class: className });
dv.move(wc * weekDay, wc * yk);
daysMatrix.add(dv);
date.setDate(date.getDate() + 1);
if ((date.getMonth() + 1) > month) {
break;
}
}
let lastWeekDayOfMonth = getWeekDay(date);
if (lastWeekDayOfMonth > 0) {
for (let d = lastWeekDayOfMonth; d < 7; d++) {
let className = 'day of-next-month';
if (d > 4) { className += ' day-holyday'; }
let dv = dayCell(date.getDate().toString());
dv.attr({ class: className });
dv.move(wc * d, wc * yk);
daysMatrix.add(dv);
date.setDate(date.getDate() + 1);
}
}
let tableHeight = (yk + 1.5) * wc;
daysMatrix.move(wc, h - tableHeight);
let image = table.image(imageURL, w, h - tableHeight);
image.move(0, 0);
image.attr({ preserveAspectRatio: 'xMaxYMid slice' });
return table;
}
return {
renderMonth: renderMonth
};
})();