-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (127 loc) · 4.06 KB
/
index.js
File metadata and controls
143 lines (127 loc) · 4.06 KB
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
138
139
140
141
142
143
$(document).ready(function() {
const apiUrl = "../data/collections.json";
// Fetch hero data
async function fetchHeroData(api) {
try {
const response = await fetch(api);
const data = await response.json();
swapIndicator(data);
displayData(data);
$('.loading-container').hide(300);
$('.main-container').show();
} catch (error) {
console.log(error);
}
}
async function fetchTop(api) {
try {
const response = await fetch(api);
const data = await response.json();
getMostRated(data);
getMostPopular(data);
} catch (error) {
console.log(error);
}
}
function swapIndicator(data) {
let resultsData = data;
let indicatorNum = 0;
const heroTitle = $("#hero-title");
const heroSynopsis = $("#hero-synopsis");
const heroContainer = $(".hero-container");
const heroBtn = $('#hero-btn');
heroContainer.css(
"background-image",
"url('" + resultsData[indicatorNum].backdrops + "')"
);
heroTitle.text(resultsData[indicatorNum].title_jp);
heroSynopsis.html(resultsData[indicatorNum].synopsis);
heroBtn.attr('data-src', 'page.html?id='+resultsData[indicatorNum].id);
function swapCount() {
const $selectedIndicator = $(".indicator-selected");
const $nextIndicator = $selectedIndicator.next(
".hero-indicator span"
);
if ($nextIndicator.length) {
$selectedIndicator.removeClass("indicator-selected");
$nextIndicator.addClass("indicator-selected");
indicatorNum++;
} else {
// If no next indicator, select the first one
$selectedIndicator.removeClass("indicator-selected");
$(".hero-indicator span:first-child").addClass(
"indicator-selected"
);
indicatorNum = 0;
}
heroContainer.css(
"background-image",
"url('" + resultsData[indicatorNum].backdrops + "')"
);
heroTitle.text(resultsData[indicatorNum].title_jp);
heroSynopsis.html(resultsData[indicatorNum].synopsis);
heroBtn.attr('data-src', 'page.html?id='+resultsData[indicatorNum].id);
}
setInterval(swapCount, 5000);
/*$('#hero-btn').click(function() {
window.location.href = $(this).data('src');
});*/
}
function displayData(data){
const results = data;
results.forEach(items => {
const item = `
<li data-src='page.html?id=${items.id}' style="background-image:url(${items.image})">
<div class='item-content'>
<span>${items.rating}</span>
</div>
</li>
`;
$('#item-display').append(item);
});
}
function getMostRated(data){
data.sort((a, b) => b.scores - a.scores);
data.forEach(items => {
const item = `
<li data-src='page.html?id=${items.id}' style="background-image:url(${items.image})">
<div class='item-content'>
<span>${items.rating}</span>
</div>
</li>
`;
$('#item-display-top').append(item);
});
}
function getMostPopular(data) {
data.sort((a, b) => b.popularity - a.popularity);
data.forEach(items => {
const item = `
<li data-src='page.html?id=${items.id}' style="background-image:url(${items.image})">
<div class='item-content'>
<span>${items.rating}</span>
</div>
</li>
`;
$('#item-display-popular').append(item);
});
}
fetchHeroData(apiUrl);
fetchTop(apiUrl);
//Actions
$('.menu-btn').click(function(){
$('.modal-menu-container').css('display','flex')
});
$('#close-modal').click(function() {
$('.modal-menu-container').css('display', 'none')
});
$('#item-display').on('click','li', function(){
window.location.href = $(this).data('src');
});
$('#item-display-top').on('click', 'li', function() {
window.location.href = $(this).data('src');
});
$('#item-display-popular').on('click', 'li', function() {
window.location.href = $(this).data('src');
});
});