-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
231 lines (211 loc) · 8 KB
/
popup.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const addWordButton = document.querySelector("#addWordButton");
const addWordInput = document.querySelector("#addWordInput");
const wordList = document.querySelector("#wordList");
const currentWord = document.querySelector("#currentWord");
const practiceInput = document.querySelector("#practiceInput");
const statsButton = document.querySelector("#statsButton");
const statsPopup = document.querySelector("#statsPopup");
const app = document.querySelector("#app");
const modalCloseButton = document.querySelector("#modalCloseButton");
const wordScoreList = document.querySelector("#wordScoreList");
const numScoreList = document.querySelector("#numScoreList");
const overlay = document.querySelector("#overlay");
let words = JSON.parse(localStorage.getItem("words"));
let wordData = JSON.parse(localStorage.getItem("wordData"));
currentWordArr = [];
// populate word list
if (words === null) {
words = [];
wordData = {};
} else {
for (let i = 0; i < words.length; i++) {
const newWord = document.createElement("div");
newWord.setAttribute("tabindex", 0);
newWord.innerText = words[i];
wordList.appendChild(newWord);
const newX = document.createElement("span");
newX.innerText = "X";
wordList.lastChild.appendChild(newX);
newX.addEventListener("click", function (e) {
const wordToDelete = words[i];
const index = words.indexOf(wordToDelete);
words.splice(index, 1);
delete wordData[wordToDelete];
localStorage.setItem("words", JSON.stringify(words));
localStorage.setItem("wordData", JSON.stringify(wordData));
newWord.remove();
practiceInput.value = "";
if (wordToDelete == currentWordArr.join("")) {
currentWordArr = [];
currentWord.innerText = "";
}
});
newWord.addEventListener("click", function (e) {
if (e.target === this) {
const word = this.innerText.substr(0, newWord.innerText.length - 2);
currentWord.innerText = replace(word);
currentWordArr = word.split("");
practiceInput.style.cssText = "rgb(243, 239, 239);";
practiceInput.value = "";
practiceInput.focus();
const otherWords = document.querySelectorAll("#wordList div");
for (let n = 0; n < otherWords.length; n++) {
otherWords[n].style.cssText = "color: black";
}
this.style.cssText =
"color: rgb(243, 239, 239); border-left: 3px solid #5cb85c";
}
});
newWord.addEventListener("keyup", function (e) {
if (e.target === this) {
if (e.key === "Enter") {
const word = this.innerText.substr(0, newWord.innerText.length - 2);
currentWord.innerText = replace(word);
currentWordArr = word.split("");
practiceInput.style.cssText = "rgb(243, 239, 239);";
practiceInput.value = "";
practiceInput.focus();
const otherWords = document.querySelectorAll("#wordList div");
for (let n = 0; n < otherWords.length; n++) {
otherWords[n].style.cssText = "color: black";
}
this.style.cssText =
"color: rgb(243, 239, 239); border-left: 3px solid #5cb85c";
}
}
});
}
}
addWordButton.addEventListener("click", function () {
const inputValue = addWordInput.value.toLowerCase().replace(/\s/g, "");
if (inputValue != "" && words.includes(inputValue) != true) {
// add to localStorage
words.push(inputValue);
wordData[inputValue] = 0;
localStorage.setItem("words", JSON.stringify(words));
localStorage.setItem("wordData", JSON.stringify(wordData));
// add to dom
const newWord = document.createElement("div");
newWord.setAttribute("tabindex", 0);
newWord.innerText = inputValue;
wordList.appendChild(newWord);
const newX = document.createElement("span");
newX.innerText = "X";
wordList.lastChild.appendChild(newX);
wordList.scrollTop = wordList.scrollHeight;
newX.addEventListener("click", function (e) {
const wordToDelete = newWord.innerText.substr(
0,
newWord.innerText.length - 2
);
const index = words.indexOf(wordToDelete);
words.splice(index, 1);
delete wordData[wordToDelete];
localStorage.setItem("words", JSON.stringify(words));
localStorage.setItem("wordData", JSON.stringify(wordData));
newWord.remove();
practiceInput.value = "";
if (wordToDelete == currentWordArr.join("")) {
currentWordArr = [];
currentWord.innerText = "";
}
});
newWord.addEventListener("click", function (e) {
if (e.target === this) {
// -2 gets rid of X from the span, as well as a phantom enter key press that was being logged when splitting the word into an array.
const word = newWord.innerText.substr(0, newWord.innerText.length - 2);
currentWord.innerText = replace(word);
currentWordArr = word.split("");
practiceInput.style.cssText = "rgb(243, 239, 239);";
practiceInput.value = "";
practiceInput.focus();
const otherWords = document.querySelectorAll("#wordList div");
for (let n = 0; n < otherWords.length; n++) {
otherWords[n].style.cssText = "color: black";
}
this.style.cssText =
"color: rgb(243, 239, 239); border-left: 3px solid #5cb85c";
}
});
newWord.addEventListener("keyup", function (e) {
if (e.target === this) {
if (e.key === "Enter") {
const word = newWord.innerText.substr(
0,
newWord.innerText.length - 2
);
currentWord.innerText = replace(word);
currentWordArr = word.split("");
practiceInput.style.cssText = "rgb(243, 239, 239);";
practiceInput.value = "";
practiceInput.focus();
const otherWords = document.querySelectorAll("#wordList div");
for (let n = 0; n < otherWords.length; n++) {
otherWords[n].style.cssText = "color: black";
}
this.style.cssText =
"color: rgb(243, 239, 239); border-left: 3px solid #5cb85c";
}
}
});
}
addWordInput.value = "";
});
practiceInput.addEventListener("input", function () {
// if no word selected, do nothing.
if (currentWordArr == "") {
return;
}
const currLen = this.value.length;
const currentWordString = currentWordArr.join("");
if (this.value === currentWordString) {
// word spelled correctly
practiceInput.readOnly = true;
practiceInput.style.cssText = "background-color: #5cb85c";
// add to word count and update localStorage
wordData[currentWordString] += 1;
localStorage.setItem("wordData", JSON.stringify(wordData));
setTimeout(() => {
practiceInput.classList.add("correct-spelling");
practiceInput.style.cssText = "rgb(243, 239, 239);";
}, 500);
setTimeout(() => {
practiceInput.classList.remove("correct-spelling");
practiceInput.value = "";
currentWord.innerText = replace(currentWordString);
practiceInput.readOnly = false;
}, 750);
} else if (this.value !== currentWordString.substr(0, currLen)) {
practiceInput.style.cssText = "background: #d9534f";
} else {
practiceInput.style.cssText = "rgb(243, 239, 239);";
}
});
statsButton.addEventListener("click", function () {
statsPopup.style.display = "block";
overlay.style.display = "block";
// render word list
for (word of words) {
const newLi = document.createElement("li");
const newLi2 = document.createElement("li");
newLi.innerText = word;
newLi2.innerText = wordData[word];
wordScoreList.appendChild(newLi);
numScoreList.appendChild(newLi2);
}
});
modalCloseButton.addEventListener("click", function () {
statsPopup.style.display = "none";
overlay.style.display = "none";
// remove words from score list
for (word of words) {
wordScoreList.removeChild(wordScoreList.lastChild);
numScoreList.removeChild(numScoreList.lastChild);
}
});
function replace(str) {
return str
.split("")
.map((char) => (Math.random() > 0.5 ? " " : char))
.join("");
}