-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.js
103 lines (82 loc) · 2.69 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
// Spanish days
moment.locale('es');
var startOnModay = true;
var from = moment('2015-01-01');
var to = moment('2016-01-01');
var months = moment.months();
var currentMonth = 0;
var week = 1;
var $table;
var holidays = ['01-01', '02-16', '02-17', '03-23', '03-24', '04-02', '04-03', '05-01', '05-25', '06-20', '07-09', '08-17', '10-12', '11-23', '12-07', '12-08', '12-25'];
// First week of the year (starts in Dec 28 2014 instead Jan 1 2015)
var date = moment(from)
if (startOnModay) {
// start the week on Monday
date.startOf('week').isoWeekday(1);
} else {
// start the week on Sunday
date.startOf('week').weekday(7);
date.week(0);
}
var insertPicture = function(month) {
$('<img src="photos/4/' + (month + 1) + '.jpg" />').appendTo('body');
};
var insertMonthName = function(month) {
$('<h1>' + months[month] + '</h1>').appendTo('body');
};
var insertCalendarTable = function() {
$table = $('<table class="calendar"></table>').appendTo('body');
};
var insertWeekdays = function() {
var weekdaysTemplate = '<thead><tr>';
var weekdays = moment.weekdays();
// make Monday the first day of the week
if (startOnModay) {
var sunday = weekdays.shift();
weekdays.push(sunday);
}
$.each(weekdays, function(index, dayName) {
weekdaysTemplate += '<th>' + dayName + '</th>';
});
weekdaysTemplate += '</tr></thead>';
$(weekdaysTemplate).appendTo($table);
};
insertPicture(0);
insertMonthName(0);
insertCalendarTable();
insertWeekdays();
while (date.isBefore(to)) {
var $tr = $('<tr/>').appendTo($table);
for (var i = 0; i < 7; i++) {
var day = date.format('DD');
if (holidays.indexOf(date.format('MM-DD')) >= 0) {
day = '<span class="holiday">' + day + '</span>';
}
var dayElement = '<td>' + day + '</td>';
// Checks if the current day is from last month OR last year
if (date.month() < currentMonth || date.year() < from.year()) {
dayElement = '<td class="prev-month">' + day + '</td>';
}
// Checks if the current day is from the next month and this year
// OR the day is from the next year
if ((date.month() > currentMonth && date.year() >= from.year()) || date.year() >= to.year()) {
dayElement = '<td class="next-month">' + day + '</td>';
}
$tr.append(dayElement);
date.add('days', 1);
};
if (date.month() > currentMonth) {
currentMonth++;
insertPicture(currentMonth);
insertMonthName(currentMonth);
insertCalendarTable();
insertWeekdays();
// if Monday is not 1st, we are going to show the last days of the
// last month
var temp = moment(date);
temp.subtract('days', 7);
if (date.day() !== 1 || date.format('DD') != "01") {
date.subtract('days', 7);
}
}
}