-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
137 lines (116 loc) · 4.51 KB
/
main.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
let deck
getDeck()
async function getDeck() {
await fetch('tarot.json').then(response => response.json()).then((data) => {
deck = data["cards"]
})
}
document.querySelector("#draw").addEventListener('click', selectCard)
function selectCard(event) {
event.preventDefault() //prevents submit button("Draw") from refreshing page
let errorArea = document.querySelector(".error-message")
errorArea.innerHTML = ""
let cardArea = document.querySelector("#drawing")
cardArea.innerHTML=""
// Filter the deck
const userFilter = getFilter()
const filteredDeck = deck.filter((card) => (userFilter.includes(card.type) || userFilter.includes(card.suit)) ? true : false)
if(filteredDeck.length === 0){
errorArea.innerHTML = "There are no cards to draw from. Select more filters/suits."
return;
}
// Returns a random integer based on filteredDeck length
const randomNumber = Math.floor(Math.random() * filteredDeck.length)
const card = filteredDeck[randomNumber]
// Check if card is drawn reversed or not (if filter allows it)
const isReversed = userFilter.includes("reversals") ? Math.floor(Math.random() * 2) : 0
// Prints all data to console for testing
console.log(
`Drawing random card from deck. \n(Filter: ${userFilter} | Deck size: ${filteredDeck.length}/${deck.length})
\nCard drawn: ${card.name} ${isReversed?"(reversed)":""}
\nMeaning: ${card.meaning_up}
\nReverse Meaning: ${card.meaning_rev}
\nImage Description: ${card.desc}`
)
displayCard(card, isReversed)
}
function getFilter() {
let selectedCheckboxes = []
let checkboxes = document.querySelectorAll("input[type='checkbox']")
console.log("************")
console.log("UPDATING FILTER:")
console.log("************")
checkboxes.forEach((checkbox) => {
if (checkbox.checked) {
console.log(`${checkbox.name} ON`)
selectedCheckboxes.push(checkbox.name)
} else {
console.log(`${checkbox.name} OFF`)
}
})
console.log("************")
return selectedCheckboxes
}
function displayCard(card, isReversed) {
addCardToDom()
function addCardToDom() {
const fragment = document.createDocumentFragment()
fragment.appendChild(createFrontOfCard())
fragment.appendChild(createBackOfCard())
const section = document.querySelector("#drawing")
section.innerHTML = ""
section.appendChild(fragment)
document.querySelector("div.front").focus()
}
function createFrontOfCard() {
const divFront = document.createElement('div')
divFront.addEventListener('click', flipCard)
divFront.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
flipCard(e)
}
})
divFront.classList.add("front")
divFront.setAttribute("tabindex", "0")
const heading = document.createElement('h2')
heading.innerText = isReversed ? `${card.name} (reversed)` : card.name
divFront.appendChild(heading)
const img = document.createElement('img')
img.src = card.image
if (isReversed) {
img.classList.add("reverse")
}
divFront.appendChild(img)
return divFront
}
function createBackOfCard() {
const divBack = document.createElement('div')
divBack.classList.add("back");
divBack.classList.add("hide");
divBack.setAttribute("tabindex", "0")
divBack.addEventListener('click', flipCard)
divBack.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
flipCard(e)
}
})
const paragraph1 = document.createElement('p')
paragraph1.innerText = `${card.name} Meaning: \r\n${card.meaning_up}`
divBack.appendChild(paragraph1)
const paragraph2 = document.createElement('p')
paragraph2.innerText = `Reverse Meaning: \r\n${card.meaning_rev}`
divBack.appendChild(paragraph2)
const paragraph3 = document.createElement('p')
paragraph3.innerText = `Image Description: \r\n${card.desc}`
divBack.appendChild(paragraph3)
return divBack
}
}
function flipCard(event) {
console.log("*flipping card*")
document.querySelector('.back').classList.toggle("hide")
document.querySelector('.front').classList.toggle("hide")
if (event.type == 'keypress') {
document.querySelector('.cards div:not(.hide)').focus()
}
}