-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
409 lines (342 loc) · 10.2 KB
/
scripts.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
const Modal = {
open(){
document.querySelector('.modal-overlay').classList.add('active')
},
close(){
document.querySelector('.modal-overlay').classList.remove('active')
},
}
const ModalList = {
open(){
document.querySelector('.modal-list-overlay').classList.add('active')
},
close(){
document.querySelector('.modal-list-overlay').classList.remove('active')
}
}
const ModalHardWordsList = {
open(){
document.querySelector('.modal-list-overlay.hard-words').classList.add('active')
},
close(){
document.querySelector('.modal-list-overlay.hard-words').classList.remove('active')
}
}
const ModalFile = {
open(){
document.querySelector('.modal-file-overlay').classList.add('active')
},
close(){
document.querySelector('.modal-file-overlay').classList.remove('active')
},
}
const Storage = {
get(){
return JSON.parse(localStorage.getItem("wordMeaning")) || []
},
set(wordsMeanings){
localStorage.setItem("wordMeaning", JSON.stringify(wordsMeanings))
},
getClassify(){
return JSON.parse(localStorage.getItem("difficultWords")) || []
},
setClassify(difficultWords){
localStorage.setItem("difficultWords", JSON.stringify(difficultWords))
}
}
const Signs = {
all: Storage.get(),
all2: [],
allDifficultWords: Storage.getClassify(),
add(sign){
Signs.all.push(sign)
App.init()
},
remove(count){
Signs.all.splice(count, 1)
App.init()
},
removeHardWord(count){
Signs.allDifficultWords.splice(count, 1)
App.init()
},
removeAllHardWords(){
const sure = confirm('Do you really want to delete all words in the hard words list?')
if(sure){
Signs.allDifficultWords = []
App.init()
}
},
removeAll(){
const sure = confirm('Do you really want to delete all words in the list?')
if(sure){
Signs.all = []
App.init()
}
},
swapValues(){
let arraySign = Signs.all
arraySign.forEach(sign => {
let wordValue = sign.wordValue
let meaningValue = sign.meaningValue
sign.wordValue = meaningValue
sign.meaningValue = wordValue
});
Signs.all = arraySign
App.init()
alert("Words and meanings have been reversed.")
},
addClassify(sign){
if(Signs.allDifficultWords.includes(sign)){
alert("This word has already been added to the hard words list. You can't do it again")
}else{
Signs.allDifficultWords.push(sign)
alert('The word has been added to the hard words list')
App.init()
}
},
drawHardWords(){
if(Signs.all == ''){
alert("You can't do it because your main list is void")
}else if (Signs.allDifficultWords == ''){
alert("You can't do it because your hard words list is void")
} else{
if(Signs.all != Signs.allDifficultWords){
Signs.all2 = Signs.all
Signs.all = Signs.allDifficultWords
alert("Now just words classified as 'hard words' are going to be drawn. To change it and draw all words, click on the blue button 'draw all words'")
App.init()
}else{
alert("Hard words are already selected")
}
}
},
drawRegularWords(){
if(Signs.all == ''){
alert("You can't do it because your main list is void")
}else{
if(Signs.all2 == '' || Signs.all == Signs.all2){
alert("Regular words are already selected")
}else{
console.log(Signs.all2)
Signs.all = Signs.all2
alert("Now all words are going to be drawn")
App.init()
}
}
}
}
const Raffle = {
draw(){
const anySign = Math.floor(Math.random() * Signs.all.length);
const randomSign = Signs.all[anySign]; // resultado aleatório
return randomSign
},
}
const DOM = {
wordPlace: document.querySelector('#word-place'),
wordDrawn: Raffle.draw(),
isTheFirstWord: true,
click(){
let countSign = 0
Signs.all.forEach(sign => {
countSign++
});
if(countSign == 0){
alert("Please type a word before drawing (in the 'Add words to main list' button).")
}else{
DOM.isTheFirstWord = false
DOM.wordDrawn = Raffle.draw()
DOM.showWord()
}
},
classifyWords(){
if(this.wordPlace.innerHTML == 'WORD'){
alert('Draw a word before it')
}else{
Signs.addClassify(this.wordDrawn)
}
},
makeDivWord(){
document.querySelector('#word-box').style.backgroundColor = "#0000ff"
let textTop = document.querySelector('.top-warning')
textTop.innerHTML = "Word drawn"
let textBottom = document.querySelector('.bottom-warning')
textBottom.innerHTML = "Tap to know the meaning"
},
makeDivMeaning(){
document.querySelector('#word-box').style.backgroundColor = "#009700"
let textTop = document.querySelector('.top-warning')
textTop.innerHTML = "Meaning"
let textBottom = document.querySelector('.bottom-warning')
textBottom.innerHTML = `${DOM.wordDrawn.wordValue}`
},
showWord(){
DOM.wordPlace.innerHTML = DOM.wordDrawn.wordValue
DOM.makeDivWord()
},
showMeaning(){
if(DOM.isTheFirstWord){
alert("Draw a word before it! (click on the red button).")
}else{
DOM.wordPlace.innerHTML = DOM.wordDrawn.meaningValue
DOM.makeDivMeaning()
}
}
}
const DOMWordsList = {
tableBody: document.querySelector('.modal-list tbody'),
addSignToList(sign){
const newSign = document.createElement('tr')
newSign.innerHTML = DOMWordsList.innerHTMLSign(sign);
DOMWordsList.tableBody.appendChild(newSign)
},
innerHTMLSign(sign){
const html = `
<tr>
<td>${sign.wordValue}</td>
<td>${sign.meaningValue}</td>
<td><img onclick="Signs.remove(${List.count})" src="./images/minus.svg" id="remove-words-button"></td>
</tr>
`
return html
},
clearTable(){
this.tableBody.innerHTML = ""
}
}
const List = {
count: 0,
eachSign(){
this.count = 0;
Signs.all.forEach(sign => {
DOMWordsList.addSignToList(sign)
this.count++
});
}
}
const DOMHardWordsList = {
tableBody: document.querySelector('.modal-list.hard-words tbody'),
addSignToList(sign){
const newSign = document.createElement('tr')
newSign.innerHTML = DOMHardWordsList.innerHTMLSign(sign);
DOMHardWordsList.tableBody.appendChild(newSign)
},
innerHTMLSign(sign){
const html = `
<tr>
<td>${sign.wordValue}</td>
<td>${sign.meaningValue}</td>
<td><img onclick="Signs.removeHardWord(${HardWordsList.count})" src="./images/minus.svg" id="remove-words-button"></td>
</tr>
`
return html
},
clearTable(){
this.tableBody.innerHTML = ""
}
}
const HardWordsList = {
count: 0,
eachSign(){
this.count = 0;
Signs.allDifficultWords.forEach(sign => {
DOMHardWordsList.addSignToList(sign)
this.count++
});
}
}
const File = {
file: document.querySelector('#input-file'),
isFirstTime: true,
arrayWords: [],
readFile(){
if(this.isFirstTime){
let files = this.file.files
if(files.length == 0) return;
const file = files[0]
let reader = new FileReader()
reader.onload = (e) => {
const file = e.target.result
this.arrayWords = file.split('\n')
}
reader.onerror = (e) => alert(e.target.error.name)
reader.readAsText(file)
alert("The file has been selected. Click the green button to add it to the list.")
this.isFirstTime = false;
}else{
alert("Click the 'add' button before selecting other file.")
}
},
handleArray(){
let count = 0;
this.arrayWords.forEach(sign => {
if(sign != ""){
const array = sign.split(" - ")
const wordMeaning = {
wordValue: array[0],
meaningValue: array[1]
}
Signs.add(wordMeaning)
}
});
},
submit(event){
event.preventDefault()
try{
this.handleArray()
ModalFile.close()
}catch(error){
alert(error.message)
}
}
}
const Form = {
word: document.querySelector('input#word'),
meaning: document.querySelector('input#meaning'),
//pegar dados
//ver se estão vazios
//guardá-los no localStorage
clearFields(){
Form.word.value = ""
Form.meaning.value = ""
},
getValues(){
return{
wordValue: Form.word.value,
meaningValue: Form.meaning.value
}
},
isVoid(){
const { wordValue, meaningValue } = Form.getValues()
if(wordValue.trim() === "" || meaningValue.trim() === ""){
throw new Error("You need to fill in all fields!")
}
},
submit(event){
event.preventDefault()
try{
Form.isVoid();
Signs.add(Form.getValues())
Form.clearFields()
alert('The word has been added.')
}catch(error){
alert(error.message)
}
}
}
const App = {
init(){
Storage.set(Signs.all)
Storage.setClassify(Signs.allDifficultWords)
DOMWordsList.clearTable()
DOMHardWordsList.clearTable()
List.eachSign()
HardWordsList.eachSign()
}
}
App.init()
/* PARA FAZER:
- lista de palavras classificadas como difíceis;
- fazer uma seção de sorteio só para palavras difíceis;
*/