-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (177 loc) · 7.22 KB
/
index.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
191
192
193
194
195
// Globals ==============================================================
const haiku = [];
let isHaikuValid = false;
const PORT = 5001;
const haikuContainer = document.getElementById("haiku-container");
const line1 = document.getElementById("line-1");
const line2 = document.getElementById("line-2");
const line3 = document.getElementById("line-3");
const currentWordInput = document.getElementById("current-word");
const synonymContainer = document.getElementById("synonym-container");
const synonymPopup = document.getElementById("synonym-popup");
// Event Listeners =====================================================
document.getElementById("menu-btn").addEventListener("click", toggleMenu);
document.getElementById("hide-synonyms").addEventListener("click", hideSynonyms);
document.getElementById("close-form").addEventListener("click", closeSubmitForm);
document.getElementById("done").addEventListener("click", showSubmitForm);
document.getElementById("clear").addEventListener("click", clearHaiku);
// Basic page functionality ============================================
function toggleMenu() {
document.querySelector("nav").classList.toggle("display-none");
}
function hideSynonyms() {
synonymPopup.classList.add("display-none");
synonymContainer.innerHTML = null;
}
function showSubmitForm() {
if (isHaikuValid) {
document.getElementById("form-popup").classList.remove("display-none");
} else {
alert("Please enter a 5-7-5 haiku.");
}
}
function closeSubmitForm(ev) {
document.getElementById("form-popup").classList.add("display-none");
}
function clearHaiku() {
currentWordInput.value = "";
hideSynonyms();
while (haiku.length) { haiku.pop(); } // haiku = [] doesn't work?
updateHaikuDisplay();
}
// Haiku input ===========================================================
// The currentWordInput listens for space, enter or backspace, and calls the appropriate function to update the haiku
currentWordInput.addEventListener("keydown", function (ev) {
ev.preventDefault();
ev.stopPropagation();
if (ev.code === "Space" || ev.code === "Enter") {
addWord(currentWordInput.value.trim().toLowerCase());
currentWordInput.value = "";
}
else if (ev.code === "Backspace") {
deleteLastWord();
}
else {
currentWordInput.value += ev.key;
}
});
//connects to server to get the syllables/synonyms of the word to be added or changed
async function lookUp(word) {
const endpoint = new URL(`https://Haiku-Dictionary-API.drew-sarette.repl.co/words/${word}`);
const response = await fetch(endpoint);
const result = await response.json();
return result.data;
// Dummy response for testing
// return {
// word: "test",
// syllables: 1,
// synonyms: ["quiz", "trial"]
// }
}
// Use lookup data to create an object containing the word, syllables, and synonyms before pushing to array
async function addWord(wordToAdd) {
if (wordToAdd.trim()) {
const newWordObj = await lookUp(wordToAdd);
const newSpan = document.createElement("span");
newSpan.textContent = newWordObj.word;
newSpan.classList.add("haiku-word");
newWordObj.htmlElement = newSpan;
let newIndex = haiku.length;
newWordObj.htmlElement.addEventListener("click", () => showSynonyms(newWordObj.synonyms, newIndex));
haiku.push(newWordObj);
updateHaikuDisplay();
}
}
//Either resets the value of currentWordInput or removes the last word of the haiku
function deleteLastWord() {
if (currentWordInput.value.trim() != "") {
currentWordInput.value = null;
} else {
haiku.pop();
updateHaikuDisplay();
}
}
// Loop through array and count syllables, noting if the total is correct for a 5-7-5 haiku
function checkHaiku() {
let [isFirstLineValid, isSecondLineValid, isThirdLineValid] = [false, false, false];
let runningSyllableCount = 0;
for (word of haiku) {
runningSyllableCount += word.syllables;
if (runningSyllableCount === 5) {
isFirstLineValid = true;
} else if (runningSyllableCount === 12 && isFirstLineValid) {
isSecondLineValid = true;
} else if (runningSyllableCount === 17 && isSecondLineValid) {
isThirdLineValid = true;
} else if (runningSyllableCount > 17)
isThirdLineValid = false;
}
if (isThirdLineValid) {
isHaikuValid = true;
} else {
isHaikuValid = false;
}
return [isFirstLineValid, isSecondLineValid, isThirdLineValid];
}
// Removes all displayed words, and then loops through the haiku, placing each word on the appropriate line for the number of syllables.
function updateHaikuDisplay() {
document.querySelectorAll("span").forEach(span => span.parentElement.removeChild(span));
const [line1IsValid, line2IsValid, line3IsValid] = checkHaiku();
let runningSyllableCount = 0;
for (let i = 0; i < haiku.length; i++) {
runningSyllableCount += haiku[i].syllables;
if (runningSyllableCount < 6) {
if (line1IsValid) {
haiku[i].htmlElement.classList.add("valid-word");
} else {
haiku[i].htmlElement.classList.remove("valid-word");
}
line1.appendChild(haiku[i].htmlElement);
} else if (runningSyllableCount < 13) {
if (line2IsValid) {
haiku[i].htmlElement.classList.add("valid-word");
} else {
haiku[i].htmlElement.classList.remove("valid-word");
}
line2.appendChild(haiku[i].htmlElement);
} else if (runningSyllableCount < 18) {
if (line3IsValid) {
haiku[i].htmlElement.classList.add("valid-word");
} else {
haiku[i].htmlElement.classList.remove("valid-word");
}
line3.appendChild(haiku[i].htmlElement);
} else {
haiku[i].htmlElement.classList.remove("valid-word")
line3.appendChild(haiku[i].htmlElement);
}
if (haiku.length > 0) {
haiku[haiku.length - 1].htmlElement.insertAdjacentElement('afterend', currentWordInput);
currentWordInput.focus();
}
}
}
//display the synonyms of the selected word, adding event listener to replace the word selected.
function showSynonyms(arrOfSynonyms, index) {
synonymContainer.innerHTML = null;
arrOfSynonyms.forEach(synonym => {
const btn = document.createElement("btn");
btn.textContent = synonym;
btn.classList.add("synonym-button");
btn.addEventListener("click", () => substituteSynonym(synonym, index));
synonymContainer.appendChild(btn);
});
synonymPopup.classList.remove("display-none");
}
// looks up the selected synonym, creates a new word object, updates the haiku array, and redoes the display
async function substituteSynonym(synonym, index) {
const wordObj = await lookUp(synonym);
const newSpan = document.createElement("span");
newSpan.classList.add("haiku-word");
newSpan.addEventListener("click", () => showSynonyms(wordObj.synonyms, index));
newSpan.textContent = synonym;
wordObj.htmlElement = newSpan;
haiku[index] = wordObj;
updateHaikuDisplay();
hideSynonyms();
}