-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
170 lines (156 loc) · 5.6 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* global $ */
/*
Name: Mark Theeranantachai
Date: May 30, 2019
Section: CSE 154 AF
A javascript for index.html, assignment for CP5.
A showcase and role picker for selecting Dota Hero with animation and
tooltip. The data are fetched from dota.php.
*/
(function() {
"use strict";
window.addEventListener("load", init);
/**
* Initialize the webpage by fetching three hero types in each row.
* Add click functionality of each hero role.
*/
function init() {
fetchHeroes("agi");
fetchHeroes("int");
fetchHeroes("str");
let navs = qsa("nav button");
for (let i = 0; i < navs.length; i++) {
navs[i].addEventListener("click", toggleRole);
}
}
/**
* Toggle the role button, change its appearance, and refresh the board
* correspond to the role settings.
*/
function toggleRole() {
this.classList.toggle("btn-outline-danger");
this.classList.toggle("btn-danger");
let navs = qsa("nav button");
let query = "dota.php?roles=";
for (let i = 0; i < navs.length; i++) {
if (navs[i].classList.contains("btn-danger")) {
query += navs[i].id + "|";
}
}
fetch(query.substring(0, query.length - 1))
.then(checkStatus)
.then(JSON.parse)
.then(refreshRole)
.catch((error) => displayError(error, "Cannot fetch heroes by roles"));
}
/**
* Refresh the showcase based on resulting JSON role with jQuery animation.
* @param {Object} json - JSON object of a filtered hero list
*/
function refreshRole(json) {
let heroes = qsa("main img");
let grid = Array();
for (let i = 0; i < heroes.length; i++) {
grid[heroes[i].id] = json.length? false: true;
}
for (let i = 0; i < json.length; i++) {
grid[json[i]] = true;
}
for (let hero in grid) {
if (grid[hero]) {
$("#" + hero).animate({
opacity: '1',
});
}
else {
$("#" + hero).animate({
opacity: '0.3',
});
}
}
}
/**
* Fetch heroes and populate the showcase by type\
* @param {String} type - A hero primary attribute.
*/
function fetchHeroes(type) {
const url = "dota.php?type=" + type;
fetch(url)
.then(checkStatus)
.then((text) => populate(text, type))
.catch((error) => displayError(error, "Cannot fetch hero lists"));
}
/**
* Populate the showcase by the fetch response,
* parse the plain text to get the hero name and short name to
* fill the hero view.
* @param {String} text - A text response from Pokedex API, containing
* hero's name and short name
* @param {String} type - A hero primary attribute to fill it in its own column
*/
function populate(text, type) {
let lines = text.split("\n");
for (let i = 0; i < lines.length - 1; i++) {
let name = lines[i].split(":");
let img = document.createElement("img");
img.id = name[0];
img.src = "hero-img/" + name[0] + "_full.png";
img.title = name[1];
img.alt = name[1];
$(img).tooltip();
id(type).appendChild(img);
}
}
/**
* This function display error notification and its message in the
* topmost section of the page.
* @param {String} error - error text from http response
* @param {String} type - a generic error description
*/
function displayError(error, type) {
id("error").classList.remove("hidden");
qs("#error > h1").innerText = type;
qs("#error > h2").innerText = error;
}
/* ------------------------------ Helper Functions ------------------------------ */
/**
* Returns the element that has the ID attribute with the specified value.
* @param {string} idName - element ID
* @param {Object} [object=document] - DOM object to be selected
* @returns {object} DOM object associated with id.
*/
function id(idName, object = document) {
return object.getElementById(idName);
}
/**
* Returns the first element that matches the given CSS selector.
* @param {string} query - CSS query selector.
* @returns {object[]} array of DOM objects matching the query.
*/
function qs(query) {
return document.querySelector(query);
}
/**
* Returns the array of elements that match the given CSS selector.
* @param {string} query - CSS query selector
* @param {Object} [object=document] - DOM object to be selected
* @returns {object[]} array of DOM objects matching the query.
*/
function qsa(query, object = document) {
return object.querySelectorAll(query);
}
/**
* Helper function to return the response's result text if successful, otherwise
* returns the rejected Promise result with an error status and corresponding text
* @param {object} response - response to check for success/error
* @returns {object} - valid result text if response was successful, otherwise rejected
* Promise result
*/
function checkStatus(response) { // boiler plate code given out
if (response.status >= 200 && response.status < 300) {
return response.text();
} else {
return Promise.reject(new Error(response.status + ": " + response.statusText));
}
}
})();