forked from cassie-from-yonder/quotebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
213 lines (190 loc) · 7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="resources/public/css/main.css" rel="stylesheet" type="text/css">
<link href="resources/public/css/style.css" rel="stylesheet" type="text/css">
<link rel="icon" href="https://clojurescript.org/images/cljs-logo-icon-32.png">
<title>M22504 Quotebook</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@1,500&family=Roboto:wght@100&display=swap" rel="stylesheet">
</head>
<body>
<link href="resources/public/css/themes/botanic-leaves.css" rel="stylesheet" type="text/css" id='theme-style'>
<div class="viewport">
<embed class="top-left" id='top-left'>
<div class="epigraph">
<blockquote>
<p class="quote" id="quote">
</p>
</blockquote>
<p class="citation">
</p>
</div>
</div>
<!-- <div class="controls flex-row">-->
<!-- <div class="flex-small text-center">-->
<!-- <label>-->
<!-- Pick theme-->
<!-- </label>-->
<!-- <select id='theme-select'>-->
<!-- <option selected value="Botanic Leaves">Botanic Leaves</option>-->
<!-- <option value="Smithsonian Backswatter">Smithsonian Backswatter</option>-->
<!-- </select>-->
<!-- </div>-->
<!-- </div>-->
<div class="options">
<div class="col">
<a id="option1-link" href="#">
<div class="option-box" id="option1-box">
<p id="option1" class="option-text">
</p>
</div>
</a>
<a id="option3-link" href="#">
<div class="option-box" id="option3-box">
<p id="option3" class="option-text">
</p>
</div>
</a>
</div>
<div class="col">
<a id="option2-link" href="#">
<div class="option-box" id="option2-box">
<p id="option2" class="option-text">
</p>
</div>
</a>
<a id="option4-link" href="#">
<div class="option-box" id="option4-box">
<p id="option4" class="option-text">
</p>
</div>
</a>
</div>
</div>
<div class="options">
<button class="reset" onclick="renderQuote()">Reset!</button>
</div>
<script src="resources/public/js/compiled/quotebook.js" type="text/javascript"></script>
</body>
</html>
<script>
const database_url = 'https://quotebook-dd68d-#default-rtdb.asia-southeast1.firebasedatabase.app/'.replace('#','');
let theme = 'Botanical Leaves';
let names = null;
let answer = null;
let answerIdx = null;
let arrayN = [];
async function renderQuote() {
fixColoring();
fetch(database_url + 'star/N.json')
.then(res => res.json())
.then(N => {
if(arrayN.length == N) { arrayN = []; }
let rint = Math.floor(Math.random()*N);
while(arrayN.indexOf(rint) != -1) { rint = Math.floor(Math.random()*N); }
arrayN.push(rint);
fetch(database_url + 'star/quotes/' + rint + '.json')
.then(res => res.json())
.then(async res => {
const text = res.text;
const citation = toTitleCase(
strip(
res.citation.toLowerCase().replaceAll("also ", "")
.split(" ").slice(0, 2).join(),
","
).replaceAll("prannaya", "prannay").replaceAll(".", "").trim()
.replaceAll("i-shiang", "bobby").replace("i-shang", "bobby").replace(",", " ").trim()
)
// const citation = capitalizeFirstLetter(res.citation.replace(",","").replace(".","").trim());
const date = res.date;
const quoteP = document.getElementById('quote');
quoteP.innerHTML = text;
const top_left_embed = document.getElementById("top-left");
top_left_embed.setAttribute("src","resources/public/css/themes/leaves-animation.svg");
if (names === null) {
names = (await getNames()).filter(it => it !== null).map(fixCitation)
}
answer = citation;
const options = names.filter(it => it !== null && it !== citation)
.shuffle()
.slice(0,3);
options.splice(Math.floor(Math.random() * 5),0,citation);
for (let i = 0; i < 4; ++i) {
const text = document.getElementById(`option${i+1}`);
text.innerHTML = options[i]
const link = document.getElementById(`option${i+1}-link`);
link.onclick = ev => {
showResult(i,options[i] === answer);
};
if (options[i] === answer) answerIdx = i;
}
});
});
}
function showResult(num,correct) {
if (answer === null)
return;
const optionSelected = document.getElementById(`option${num+1}-box`)
if (correct) {
optionSelected.style.backgroundColor = '#41B883FF'
optionSelected.style.boxShadow = '2px 2px 6px #41B883FF';
} else {
optionSelected.style.backgroundColor = 'tomato'
optionSelected.style.boxShadow = '2px 2px 6px tomato';
const answerOption = document.getElementById(`option${answerIdx+1}-box`)
answerOption.style.backgroundColor = '#41B883FF'
answerOption.style.boxShadow = '2px 2px 6px #41B883FF';
}
// const resultBox = document.getElementById('resultBox');
// resultBox.style.display = 'block'
}
function fixColoring() {
for(let i=1;i<=4; i++) {
const optionSelected = document.getElementById(`option${i}-box`);
optionSelected.style.backgroundColor ='#fbf1c7';
optionSelected.style.boxShadow = '2px 2px 6px #fbf1c7';
}
}
function getNames() {
return fetch(database_url + 'star/names.json')
.then(res => res.json())
}
function capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}
function fixCitation(citation) {
return toTitleCase(
strip(
citation.toLowerCase().replaceAll("also ", "")
.split(" ").slice(0, 2).join(),
","
).replaceAll("prannaya", "prannay").replaceAll(".", "").trim()
.replaceAll("i-shiang", "bobby").replace("i-shang", "bobby").replace(",", " ").trim()
)
}
function toTitleCase(str) {
return str.replace(
/\w\S*/g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);
}
function strip(str, substr) {
return str.replace(new RegExp(`^${substr}`), "").replace(new RegExp(`${substr}$`), "")
}
Array.prototype.shuffle = function() {
let m = this.length, i;
while (m) {
i = (Math.random() * m--) >>> 0;
[this[m], this[i]] = [this[i], this[m]]
}
return this;
}
function init() {
renderQuote();
}
init();
</script>