-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
182 lines (168 loc) · 8.98 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
<!DOCTYPE html>
<html lang="en">
<title>Round Robin Schedule Generator</title>
<meta name="description" content="Quickly generate round robin schedules">
<meta name="og:title" content="Round Robin Schedule Generator">
<meta name="og:description" content="Quickly generate round robin schedules">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css">
<body class="pa4 black-80 system-sans-serif center w-90">
<div id="generator" style="display: none">
<h1>Round Robin Schedule Generator</h1>
<form id="schedule-form" name="schedule-form">
<div class="mb4 measure">
<label for="teams" class="f6 b db mb2">Teams</span></label>
<textarea rows="10" id="teams" name="teams" class="db border-box hover-black w-100 measure ba b--black-20 pa2 br2 mb2" aria-describedby="teams-desc"></textarea>
<small id="teams-desc" class="f6 black-60">Enter each player or team name on separate lines here</a></small>
</div>
<div class="mb4 measure">
<label for="rounds" class="f6 b db mb2">Rounds <span class="normal black-60">(optional)</span></label>
<input id="rounds" class="input-reset ba b--black-20 pa2 mb2 db w-100" type="number" aria-describedby="rounds-desc">
<small id="rounds-desc" class="f6 black-60 db mb2">Enter the number of rounds -- defaults to the number of players or teams</small>
</div>
<div class="mb4 measure">
<label for="name" class="f6 b db mb2">Name <span class="normal black-60">(optional)</span></label>
<input id="name" class="input-reset ba b--black-20 pa2 mb2 db w-100" type="text" aria-describedby="name-desc">
<small id="name-desc" class="f6 black-60 db mb2">Enter a name for the schedule if desired</small>
</div>
<div class="mb4 measure">
<label for="description" class="f6 b db mb2">Description <span class="normal black-60">(optional)</span></span></label>
<textarea id="description" name="description" class="db border-box hover-black w-100 measure ba b--black-20 pa2 br2 mb2" aria-describedby="description-desc"></textarea>
<small id="description-desc" class="f6 black-60">Enter a description for the schedule if desired</a></small>
</div>
<div class="mb4 measure">
<div class="fl w-100 w-30-ns">
<div class="mb4">
<input class="mr2 pointer" type="checkbox" id="shuffle" value="shuffle">
<label for="shuffle" class="f6 b pointer" style="user-select: none;">Shuffle</label>
</div>
</div>
<div class="fl w-100 w-70-ns">
<div class="mb4">
<label for="shuffle-seed" class="f6 b db mb2">Shuffle Seed <span class="normal black-60">(optional)</span></label>
<input id="shuffle-seed" disabled class="input-reset ba b--black-20 pa2 mb2 db w-100" type="number" aria-describedby="shuffle-seed-desc">
<small id="shuffle-seed-desc" class="f6 black-60 db mb2">Enter the same number for predictable shuffles</small>
</div>
</div>
</div>
<div class="mb4 measure">
<input class="f6 fr pointer grow no-underline br-pill ph3 pv2 mb5 dib white bg-dark-pink" style="border: none" type="submit" id="generate" value="Generate Schedule">
</div>
</form>
</div>
<div id="create-new-schedule" class="fr-ns mb5" style="display: none">
<button id="create-new-schedule-button" class="f6 fr pointer grow no-underline br-pill ph3 pv2 mb2 dib white bg-dark-pink" style="border: none">Create New Schedule</button>
</div>
<div id="schedule"></div>
<script src="https://unpkg.com/[email protected]/src/lzma_worker-min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/purify.min.js"></script>
<script src="https://unpkg.com/[email protected]/marked.min.js"></script>
<script src="https://unpkg.com/[email protected]/base64.js"></script>
<script>
const elem = document.getElementById.bind(document);
const lzma = LZMA;
const showGenerator = () => {
elem('generator').style.visibility = 'visible';
elem('generator').style.display = 'block';
elem('create-new-schedule').style.visibility = 'hidden';
elem('create-new-schedule').style.display = 'none';
};
const hideGenerator = () => {
elem('generator').style.visibility = 'hidden';
elem('generator').style.display = 'none';
elem('create-new-schedule').style.visibility = 'visible';
elem('create-new-schedule').style.display = 'block';
};
const updateMeta = (schedule) => {
const scheduleLines = schedule.split('\n');
if (scheduleLines[0].startsWith('# ')) {
const title = DOMPurify.sanitize(scheduleLines[0].substr(2));
document.title = title;
document.getElementsByTagName('meta')['og:title'].content = title;
}
const metaDescription = document.getElementsByTagName('meta')['description'];
const metaOgDescription = document.getElementsByTagName('meta')['og:description'];
if (!scheduleLines[0].startsWith('# ') && !scheduleLines[0].startsWith("## ")) {
const description = DOMPurify.sanitize(scheduleLines[0]);
metaDescription.content = description;
metaOgDescription.content = description;
} else if (!scheduleLines[2].startsWith('## ') && !scheduleLines[2] == '\n') {
const description = DOMPurify.sanitize(scheduleLines[2]);
metaDescription.content = description;
metaOgDescription.content = description;
} else {
metaDescription.remove();
metaOgDescription.remove();
}
};
document.onreadystatechange = e => {
if (document.readyState !== "complete") {
return;
}
if (window.location.hash) {
try {
lzma.decompress(
Base64.toUint8Array(window.location.hash),
(schedule, error) => {
if (error) {
showGenerator();
return;
}
hideGenerator();
updateMeta(schedule);
elem('schedule').innerHTML = DOMPurify.sanitize(marked(schedule));
}
);
} catch {
showGenerator();
}
return;
}
showGenerator();
};
elem('teams').oninput = e => {
elem('rounds').defaultValue = e.target.value.split('\n').filter(v => Boolean(v)).length;
};
elem('shuffle').onchange = e => {
elem('shuffle-seed').disabled = !e.target.checked;
elem('shuffle-seed').value = "";
};
const SCHEDULE_URL = 'https://us-east4-miken-sandbox.cloudfunctions.net/round-robin-schedule';
elem('schedule-form').onsubmit = async e => {
e.preventDefault();
e.stopPropagation();
let response;
try {
response = await fetch(SCHEDULE_URL + '?' + new URLSearchParams({
teams: elem('teams').value.split('\n').filter(v => Boolean(v)),
rounds: elem('rounds').value || elem('rounds').defaultValue,
name: elem('name').value,
description: elem('description').value,
shuffle: elem('shuffle').checked,
shuffle_seed: elem('shuffle').checked ? elem('shuffle-seed').value : ''
}));
} catch {
alert("Something went wrong when trying to generate the schedule");
return;
}
if (response.status !== 200) {
alert(await response.text());
return;
}
hideGenerator();
const schedule = await response.text();
updateMeta(schedule);
elem('schedule').innerHTML = DOMPurify.sanitize(marked(schedule));
lzma.compress(schedule, 9, result => window.location.hash = Base64.fromUint8Array(Uint8Array.from(result), true));
};
window.onhashchange = e => {
if (window.location.hash === "") {
window.location.reload();
}
};
elem('create-new-schedule-button').onclick = e => {
window.location.hash = ""; // Will reload based on the above
};
</script>
</body>
</html>