-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
183 lines (143 loc) · 5.04 KB
/
index.html
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
<html>
<head>
<title>Pitcher Cabin Calendar Generator</title>
<script type='text/javascript' src='js/ics.min.js'></script>
<script type='text/javascript' src='js/moment.min.js'></script>
<script type='text/javascript' src='js/jquery.min.js'></script>
<script type='text/javascript'>
function initializeFamilies() {
var families = [
'Grandma Pitcher',
'Kenton & Marcia Wride',
'Mark & Andi Davis',
//'<Renter>',
'Roger & Shauna Andersen',
'Steve & Mindy Pitcher',
'Tom & Karine Pitcher',
//'<Renter>'
].join('\n');
$('#families').text(families);
}
function initializeNumYears() {
var numYearsHTML = '';
for (var i = 1; i <= 10; i++) {
numYearsHTML += '<option>' + i + '</option>';
}
$('#numYears').html(numYearsHTML);
}
function initializeStartYear() {
var startYear = parseInt(moment().format('YYYY'), 10);
var startYearHTML = '';
for (var i = 0; i < 10; i++) {
startYearHTML += '<option>' + startYear + '</option>';
startYear++;
}
$('#startYear').html(startYearHTML);
}
function getFamilies() {
return $('#families').text().split('\n');
}
function getNumYears() {
return parseInt($('#numYears').val(), 10);
}
function getStartYear() {
return parseInt($('#startYear').val(), 10);
}
function addWeek(m) {
return moment(m).add(7, 'days').subtract(1, 'seconds');
}
function getBlockedDates(year) {
var makeDate = function(year, month, day) {
return moment({ year: year, month: month - 1, day: day});
};
return [
makeDate(year, 1, 1), // New Years
makeDate(year, 7, 4), // 4th of July
makeDate(year, 12, 25), // Christmas
makeDate(year + 1, 1, 1) // next year's New Years
];
}
function weekIsGood(weekStart, blockedDates) {
var startDate = moment(weekStart.format('MM/DD/YYYY'), 'MM/DD/YYYY');
var endDate = addWeek(startDate);
for (var i = 0; i < blockedDates.length; i++) {
var blockedDate = blockedDates[i];
if (blockedDate.isSame(startDate, 'day') ||
blockedDate.isSame(endDate, 'day') ||
blockedDate.isBetween(startDate, endDate, 'day')) {
// This week contains a blocked date.
return false;
}
/*
var diffWithEnd = blockedDate.diff(endDate, 'days');
var diffWithStart = startDate.diff(blockedDate, 'days');
var minDiff = 1; // Must be more than 1 day away from blocked date.
if (diffWithEnd > 0 &&
diffWithEnd <= minDiff) {
// This week is too close to the beginning of the blocked date.
return false;
}
if (diffWithStart > 0 &&
diffWithStart <= minDiff) {
// This week is too close to the end of the blocked date.
return false;
}*/
}
return true;
}
function getICS(startYear, numYears, families) {
var cal = ics();
for (var year = startYear; year < startYear + numYears; year++) {
var familyIndex = year;
var blockedDates = getBlockedDates(year);
var startDate = moment({year:0,month:0,day:0}).year(year).dayOfYear(1);
var firstDayOffset = (6 - startDate.weekday()) % 7;
if (firstDayOffset < 0) {
firstDayOffset += 7;
}
startDate.add(firstDayOffset, 'days').add(12, 'hours');
while (startDate.year() == year) {
if (weekIsGood(startDate, blockedDates)) {
var family = families[familyIndex++ % families.length];
addEvent(family, startDate, cal);
}
startDate.add(7, 'days');
}
}
return cal;
}
function addEvent(family, startDate, cal) {
var endDate = addWeek(startDate);
var formatString = 'MM/DD/YYYY h:mm a';
var dateStrings = {
start: startDate.format(formatString),
end: endDate.format(formatString)
};
cal.addEvent('Cabin: ' + family, 'Cabin assigned to ' + family, 'Cabin', dateStrings.start, dateStrings.end);
}
function downloadBtnClicked() {
var startYear = getStartYear();
var numYears = getNumYears();
var endYear = startYear + numYears - 1;
var fileName = 'Pitcher Cabin Assignments - ' + (startYear == endYear ? startYear : startYear + '-' + endYear);
getICS(startYear, numYears, getFamilies()).download(fileName);
}
$(function() {
initializeFamilies();
initializeNumYears();
initializeStartYear();
$('#downloadBtn').click(downloadBtnClicked);
});
</script>
</head>
<body>
<h2>Families</h2>
<textarea id='families' style='height: 150px'></textarea>
<h2>Configuration</h2>
Generate events for <select id='numYears'></select> years starting in <select id='startYear'></select>
<h2>Download</h2>
Click <a id='downloadBtn' href='javascript:void(0)'>here</a> to download the .ics file.
<h2>What next?</h2>
Import the .ics file into Google Calendar. Visit <a href='https://support.google.com/calendar/answer/37118' target='_blank'>this page</a> for instructions.
</body>
</html>