-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
171 lines (132 loc) · 6.89 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
var contentHandler = (async function () {
let response = await fetch(`https://rickandmortyapi.com/api/character/`);
//private variable
data = await response.json();
//private functions
var listData = function (data) {
$("article").empty("");
for (var i = 0; i < data.results.length; i++) {
var created =new Number((new Date().getTime() - new Date(data.results[i].created).getTime()) / 31536000000).toFixed(0);
var charCard = `<section> <div class="top-section"><img src="${data.results[i].image}" alt=""> <span class="image-text"> <span class="name">${data.results[i].name}</span><br> id : ${data.results[i].id} - created ${created} years ago </span> </div> <ul> <li> <div class="list-item"> <span class="property">STATUS</span> <div class="value">${data.results[i].status}</div> </div> </li> <li> <div class="list-item"> <span class="property">SPECIES</span> <div class="value">${data.results[i].species}</div> </div> </li> <li> <div class="list-item"> <span class="property">GENDER</span> <div class="value">${data.results[i].gender}</div> </div> </li> <li> <div class="list-item"> <span class="property">ORIGIN</span> <div class="value">${data.results[i].origin.name}</div> </div> </li> <li> <div class="list-item"> <span class="property">LAST LOCATION</span> <div class="value">${data.results[i].location.name}</div> </div> </li> </ul> </section>`;
$("article").append(charCard);
}
};
var listDataReverse = function (data) {
$("article").empty("");
for (var i = data.results.length - 1; i >= 0; i--) {
var created =new Number((new Date().getTime() - new Date(data.results[i].created).getTime()) / 31536000000).toFixed(0);
var charCard = `<section> <div class="top-section"><img src="${data.results[i].image}" alt=""> <span class="image-text"> <span class="name">${data.results[i].name}</span><br> id : ${data.results[i].id} - created ${created} years ago </span> </div> <ul> <li> <div class="list-item"> <span class="property">STATUS</span> <div class="value">${data.results[i].status}</div> </div> </li> <li> <div class="list-item"> <span class="property">SPECIES</span> <div class="value">${data.results[i].species}</div> </div> </li> <li> <div class="list-item"> <span class="property">GENDER</span> <div class="value">${data.results[i].gender}</div> </div> </li> <li> <div class="list-item"> <span class="property">ORIGIN</span> <div class="value">${data.results[i].origin.name}</div> </div> </li> <li> <div class="list-item"> <span class="property">LAST LOCATION</span> <div class="value">${data.results[i].location.name}</div> </div> </li> </ul> </section>`;
$("article").append(charCard);
}
};
var filterData = function (data, property, filter) {
var fliteredData = { results: [] };
var j = 0;
for (var i = 0; i < data.results.length; i++) {
if (property != "origin") {
if (data.results[i][property].toLowerCase() == filter) {
fliteredData.results.push(data.results[i]);
j++;
}
} else {
if (data.results[i][property].name.toLowerCase() == filter) {
fliteredData.results.push(data.results[i]);
j++;
}
}
}
return fliteredData;
};
var getCurrentData = function(){
var currentData = {};
var selectedFilterElement = $('.selected-filter');
var filters = { gender: null, species: null, origin: { name: null } };
for (var i = 0; i < selectedFilterElement.length; i++) {
var property = $($('.selected-filter')[i]).attr("id").substr(16);
if (property != "origin") {
filters[property] = $($('.selected-filter')[i]).data('name');
} else {
filters.origin.name = $($('.selected-filter')[i]).data('name');
}
}
if (filters.gender != null) {
currentData = filterData(data, "gender", filters.gender);
} else {
currentData = data;
}
if (filters.species != null) {
currentData = filterData(currentData, "species", filters.species);
}
if (filters.origin.name != null) {
currentData = filterData(currentData, "origin", filters.origin.name);
}
return currentData;
}
//exposed functions
var listAll = function () {
listData(data)
};
var sortData = function (order) {
var currentData = getCurrentData();
if (order == "dsc") {
listDataReverse(currentData);
} else {
listData(currentData);
}
}
var filterCharacters = function () {
var currentData = getCurrentData();
listData(currentData);
}
var searchCharacters = async function (name) {
let response = await fetch(`https://rickandmortyapi.com/api/character?name=${name}`);
fliteredData = await response.json();
listData(fliteredData);
}
return {
listAllCharacters: listAll,
search: searchCharacters,
filter: filterCharacters,
sort: sortData
};
})();
contentHandler.then(x => {
x.listAllCharacters();
$("#submit-search").on("click", () => {
var searchName = $("#search-box").val();
if (searchName != "") {
x.search(searchName);
}
});
$("#clear-search").on("click", () => {
x.listAllCharacters();
});
$("#clear-filters").on("click", () => {
x.listAllCharacters();
$('input:checked').prop("checked", false);
$('#selected-filter-container').empty();
});
$("#sort-by-id").on("change", () => {
var order = $("#sort-by-id").val()
x.sort(order);
});
$('input[type="checkbox"]').on('change', function (e) {
if (!$(this).is(":checked")) {
this.checked = !this.checked;
} else {
$(this).closest(".filters").find('input[type="checkbox"]').not(this).prop('checked', false);
$(`#selected-filter-${$(this).closest(".filters").attr("id")}`).remove();
$('#selected-filter-container').append(`
<div class="selected-filter" id="selected-filter-${$(this).closest(".filters").attr("id")}" data-name="${$(this).attr("name")}">${$(this).attr("name")} <div class="cross" id="cross-${$(this).closest(".filters").attr("id")}">X</div>
</div>
`);
x.filter($(this).closest(".filters").attr("id"), $(this).attr("name"));
}
});
$(document).on("click", ".cross", function () {
var prop = $(this).closest(".selected-filter").attr("id").substr(16);
$(this).closest(".selected-filter").remove();
$(`#${prop} input:checked`).prop("checked", false);
x.filter(prop, $($(this).closest(".selected-filter")).data('name'));
});
});