forked from AM1CODES/Poke-Dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (85 loc) · 2.95 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
const setNormalView = (isNormalView) => {
if (isNormalView) {
document.querySelector("#normal-view").classList.add("selected");
document.querySelector("#compact-view").classList.remove("selected");
document.querySelectorAll(".card-body").forEach((cardBody) => {
cardBody.classList.remove("hidden");
});
document.querySelectorAll(".row>div").forEach((ele) => {
ele.classList.replace("col-lg-2", "col-lg-4");
});
} else {
document.querySelector("#compact-view").classList.add("selected");
document.querySelector("#normal-view").classList.remove("selected");
document.querySelectorAll(".card-body").forEach((cardBody) => {
cardBody.classList.add("hidden");
});
document.querySelectorAll(".row>div").forEach((ele) => {
ele.classList.replace("col-lg-4", "col-lg-2");
});
}
};
setNormalView(true); // By default normal view is enabled.
document.querySelector("#normal-view").addEventListener("mousedown", () => {
setNormalView(true);
});
document.querySelector("#compact-view").addEventListener("mousedown", () => {
setNormalView(false);
});
const fixBrokenImages = () => {
const fallbackURL = "images/fallback_image.png";
const img = document.getElementsByTagName("img");
for (let i = 0; i < img.length; i++) {
let t = img[i];
if (t.naturalWidth === 0) {
t.src = fallbackURL;
}
}
//audio
let playSound = () => {
let sound = document.getElementsByTagName("audio")[0];
sound.play();
};
setTimeout(() => playSound(), 3000);
};
$(document).ready(function () {
$.getJSON("pokemon.json", function (allPokemon) {
const template = $("#cardTemplate").html();
allPokemon.forEach((pokemon) => {
const card = $(template).clone();
card
.find("img")
.attr("src", pokemon.pokemonImage)
.attr("alt", pokemon.pokemonName);
card.find(".card-title").text(pokemon.pokemonName);
card.find(".card-text").text(pokemon.pokemonDescription);
card.find("a").text(`Contributed by - ${pokemon.contributedByName}`);
let button = card.find("a").clone();
if (pokemon.contributedByUrl) {
card.find("a").attr("href", pokemon.contributedByUrl);
} else {
card.find("a").replaceWith(function () {
return $("<p />", { html: $(this).html() });
});
}
if (pokemon.improvedByName) {
button
.attr("style", "margin-top: 20px")
.text(`Improved by - ${pokemon.improvedByName}`);
if (pokemon.improvedByUrl) {
console.log("here");
button.attr("href", pokemon.contributedByUrl);
} else {
button = `<p>Improved by - ${pokemon.improvedByName}</p>`;
}
card.find(".card-body").append(button);
}
$("#pokemon-row").prepend(card);
});
}).fail(function () {
console.log(
"You should try running this within a server rather than through the browser"
);
});
});
window.onload = fixBrokenImages;