-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (85 loc) · 2.81 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
import { catsData } from '/data.js'
const emotionRadios = document.getElementById('emotion-radios')
const getImageBtn = document.getElementById('get-image-btn')
const gifsOnlyOption = document.getElementById('gifs-only-option')
const memeModalInner = document.getElementById('meme-modal-inner')
const memeModal = document.getElementById('meme-modal')
const memeModalCloseBtn = document.getElementById('meme-modal-close-btn')
emotionRadios.addEventListener('change', highlightCheckedOption)
memeModalCloseBtn.addEventListener('click', closeModal)
getImageBtn.addEventListener('click', renderCat)
function highlightCheckedOption(e){
const radios = document.getElementsByClassName('radio')
for (let radio of radios){
radio.classList.remove('highlight')
}
document.getElementById(e.target.id).parentElement.classList.add('highlight')
}
function closeModal(){
memeModal.style.display = 'none'
}
function renderCat(){
const catObject = getSingleCatObject()
memeModalInner.innerHTML = `
<img
class="cat-img"
src="./images/${catObject.image}"
alt="${catObject.alt}"
>
`
memeModal.style.display = 'flex'
}
function getSingleCatObject(){
const catsArray = getMatchingCatsArray()
if(catsArray.length === 1){
return catsArray[0]
}
else{
const randomNumber = Math.floor(Math.random() * catsArray.length)
return catsArray[randomNumber]
}
}
function getMatchingCatsArray(){
if(document.querySelector('input[type="radio"]:checked')){
const selectedEmotion = document.querySelector('input[type="radio"]:checked').value
const isGif = gifsOnlyOption.checked
const matchingCatsArray = catsData.filter(function(cat){
if(isGif){
return cat.emotionTags.includes(selectedEmotion) && cat.isGif
}
else{
return cat.emotionTags.includes(selectedEmotion)
}
})
return matchingCatsArray
}
}
function getEmotionsArray(cats){
const emotionsArray = []
for (let cat of cats){
for (let emotion of cat.emotionTags){
if (!emotionsArray.includes(emotion)){
emotionsArray.push(emotion)
}
}
}
return emotionsArray
}
function renderEmotionsRadios(cats){
let radioItems = ``
const emotions = getEmotionsArray(cats)
for (let emotion of emotions){
radioItems += `
<div class="radio">
<label for="${emotion}">${emotion}</label>
<input
type="radio"
id="${emotion}"
value="${emotion}"
name="emotions"
>
</div>`
}
emotionRadios.innerHTML = radioItems
}
renderEmotionsRadios(catsData)