Skip to content

Commit fc5f0eb

Browse files
committed
updated application to vanilla JS
1 parent 908e1b9 commit fc5f0eb

File tree

2 files changed

+59
-57
lines changed

2 files changed

+59
-57
lines changed

app.js

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,77 @@
1-
$(document).ready(function () {
2-
// Create a variable for the container to hold the emoji cards.
3-
var emojiCardContainer = $("#books");
4-
5-
// Create a variable for the emoji cards.
6-
var emojiCard = "";
7-
8-
// Run the random order function below on the data inside data.js. This will display the cards in a random order on the page every time the page is refreshed.
9-
shuffle(emojiItems);
10-
11-
// Loop through the data from the data.js file and insert parts of the data into HTML. On each loop, we are appending a new card with the HTML below.
12-
for (var i in emojiItems) {
13-
14-
emojiCard +=
15-
"<div class='emoji-card'><div class='emoji-card-wrapper'><div class='hint-container' tabindex='0'><i class='fas fa-question-circle'></i><p class='hint'><span class='type'>" + emojiItems[i].year +
16-
"</span></p></div><div class='emoji-images' tabindex='0'>" + emojiItems[i].emojiImgs +
17-
"</div><div class='emoji-card-title hide-card'>";
18-
19-
emojiCard += "<div class='title-content'><h3>" + emojiItems[i].title +
20-
" (" + emojiItems[i].year + ")" + "</h3><div class='author-container'><h4>" + emojiItems[i].author + "</h4></div>";
21-
22-
emojiCard += "</div></div></div></div>";
23-
}
1+
// Create a variable for the container to hold the emoji cards.
2+
const emojiCardContainer = document.querySelector("#books");
3+
4+
// Create a variable for the emoji cards.
5+
let emojiCard = "";
6+
7+
// Run the random order function below on the data inside data.js. This will display the cards in a random order on the page every time the page is refreshed.
8+
shuffle(emojiItems);
9+
10+
// Loop through the data from the data.js file and insert parts of the data into HTML. On each loop, we are appending a new card with the HTML below.
11+
for (let i in emojiItems) {
12+
emojiCard +=
13+
"<div class='emoji-card'><div class='emoji-card-wrapper'><div class='hint-container' tabindex='0'><i class='fas fa-question-circle'></i><p class='hint'><span class='type'>" + emojiItems[i].year +
14+
"</span></p></div><div class='emoji-images' tabindex='0'>" + emojiItems[i].emojiImgs +
15+
"</div><div class='emoji-card-title hide-card'>";
16+
17+
emojiCard += "<div class='title-content'><h3>" + emojiItems[i].title +
18+
" (" + emojiItems[i].year + ")" + "</h3><div class='author-container'><h4>" + emojiItems[i].author + "</h4></div>";
2419

25-
// Append the emoji card variable, which has all of the emoji cards to the initial variable we created that was for the container to hold the cards.
26-
emojiCardContainer.html(emojiCard);
20+
emojiCard += "</div></div></div></div>";
2721

28-
// Run Twemoji to change all emojis to Twitter emojis.
29-
twemoji.parse(document.body);
22+
}
3023

31-
// Add the count of number of shows/movies to the footer.
32-
$("footer span").append(emojiItems.length);
24+
// Append the emoji card variable, which has all of the emoji cards to the initial variable we created that was for the container to hold the cards.
25+
emojiCardContainer.innerHTML = emojiCard;
3326

27+
// Run Twemoji to change all emojis to Twitter emojis.
28+
twemoji.parse(document.body);
3429

35-
// Display movies and show in a random order, the random order will refresh on page reload. This function is used above before the cards are rendered on the page.
36-
function shuffle(array) {
37-
var currentIndex = array.length,
38-
temporaryValue,
39-
randomIndex;
30+
// Add the count of number of shows/movies to the footer.
31+
document.querySelector("footer span").innerHTML = emojiItems.length;
4032

41-
while (currentIndex !== 0) {
42-
randomIndex = Math.floor(Math.random() * currentIndex);
43-
currentIndex -= 1;
44-
temporaryValue = array[currentIndex];
45-
array[currentIndex] = array[randomIndex];
46-
array[randomIndex] = temporaryValue;
47-
}
33+
// Display movies and show in a random order, the random order will refresh on page reload. This function is used above before the cards are rendered on the page.
34+
function shuffle(array) {
35+
let currentIndex = array.length,
36+
temporaryValue,
37+
randomIndex;
4838

49-
return array;
39+
while (currentIndex !== 0) {
40+
randomIndex = Math.floor(Math.random() * currentIndex);
41+
currentIndex -= 1;
42+
temporaryValue = array[currentIndex];
43+
array[currentIndex] = array[randomIndex];
44+
array[randomIndex] = temporaryValue;
5045
}
5146

52-
// Expand the emoji card when clicked to reveal the song name, artist and music video link.
53-
$("#books").on("click keypress", ".emoji-images", function () {
54-
$(this)
55-
.siblings(".emoji-card-title")
56-
.toggleClass("hide-card");
47+
return array;
48+
}
49+
50+
// Expand the emoji card when clicked to reveal the song name, artist and music video link.
51+
const toggleShowCard = elem => elem && elem.classList.toggle("hide-card");
52+
document.querySelectorAll(".emoji-images").forEach(elem => {
53+
elem.addEventListener("click", function (e) {
54+
toggleShowCard(e.target.closest(".emoji-images").nextElementSibling);
5755
});
56+
});
5857

58+
const showHint = elem => elem && elem.classList.add("hint-reveal");
59+
const hideHint = elem => elem && elem.classList.remove("hint-reveal");
60+
document.querySelectorAll(".hint-container").forEach(elem => {
5961
// Display a hint (type ie tv, movie or musical) when hovering over the question mark.
60-
$("#books").on("mousedown keypress", ".hint-container", function () {
61-
$(this)
62-
.find(".hint")
63-
.addClass("hint-reveal");
62+
elem.addEventListener("mousedown", function (e) {
63+
showHint(e.target.closest(".hint-container").querySelector(".hint"));
64+
});
65+
elem.addEventListener("keypress", function (e) {
66+
showHint(e.target.querySelector(".hint"));
6467
});
6568

6669
// Hide hint (type ie tv, movie or musical) when the user stops hovering over the question mark.
67-
$("#books").on("mouseleave focusout", ".hint-container", function () {
68-
$(this)
69-
.find(".hint")
70-
.removeClass("hint-reveal");
70+
elem.addEventListener("mouseleave", function (e) {
71+
hideHint(e.target.querySelector(".hint"));
72+
});
73+
elem.addEventListener("focusout", function (e) {
74+
hideHint(e.target.querySelector(".hint"));
7175
});
72-
7376
});
7477

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ <h2>A listing of books depicted through emojis. <img draggable="false" class="em
4646
</footer>
4747
</main>
4848
<script src="https://twemoji.maxcdn.com/v/latest/twemoji.min.js" crossorigin="anonymous"></script>
49-
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
5049
<script src="data.js"></script>
5150
<script src="app.js"></script>
5251
</body>

0 commit comments

Comments
 (0)