|
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>"; |
24 | 19 |
|
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>"; |
27 | 21 |
|
28 | | - // Run Twemoji to change all emojis to Twitter emojis. |
29 | | - twemoji.parse(document.body); |
| 22 | +} |
30 | 23 |
|
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; |
33 | 26 |
|
| 27 | +// Run Twemoji to change all emojis to Twitter emojis. |
| 28 | +twemoji.parse(document.body); |
34 | 29 |
|
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; |
40 | 32 |
|
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; |
48 | 38 |
|
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; |
50 | 45 | } |
51 | 46 |
|
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); |
57 | 55 | }); |
| 56 | +}); |
58 | 57 |
|
| 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 => { |
59 | 61 | // 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")); |
64 | 67 | }); |
65 | 68 |
|
66 | 69 | // 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")); |
71 | 75 | }); |
72 | | - |
73 | 76 | }); |
74 | 77 |
|
0 commit comments