-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
158 lines (133 loc) · 4.66 KB
/
script.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
createEntries(true);
const dataTemplate = document.querySelector("[data-template]");
const entryContainer = document.querySelector("[data-entries-container");
const searchInput = document.querySelector("[data-search]");
let entries = [];
var searchEvent;
searchInput.addEventListener("input", (e) => {
searchEvent = e;
search(e);
});
function search(e){
console.log(e.target.value)
const value = e.target.value.toLowerCase();
entries.forEach((entry) => {
a = entry.artist.join(" ");
g = entry.genres.join(" ")
const isVisible =
entry.name.toLowerCase().includes(value) ||
entry.year.includes(value) ||
a.toLowerCase().includes(value) ||
g.toLowerCase().includes(value);
entry.element.classList.toggle("hide", !isVisible);
});
}
function createEntries(firstTime) {
fetch("./assets/albums.json")
.then((res) => res.json())
.then((data) => {
if (firstTime) {
albumData = data.reverse();
document.getElementById("num-albums").innerHTML = "A collection of " + (Object.keys(albumData).length) + " hand-picked albums";
} else {
entryContainer.innerHTML = "";
}
entries = albumData.map((entry) => {
const card = dataTemplate.content.cloneNode(true).children[0];
const url = card.querySelector("[data-url]");
const artist_card = card.querySelector("[data-artist]");
const year = card.querySelector("[data-year]");
const img = card.querySelector("[data-img]");
img.src = entry.image_url
img.href = entry.album_url
url.textContent = entry.name;
artist_card.textContent = entry.artists;
year.textContent = entry.release_date;
url.href = entry.album_url;
entryContainer.append(card);
return {
name: entry.name,
artist: entry.artists,
genres: entry.genres,
year: entry.release_date,
element: card,
};
});
search(searchEvent);
});
}
var enabled_sliders = {
'popularity' : true,
'acousticness' : true,
'danceability' : true,
'energy' : true,
'instrumentalness' : true,
'valence' : true
}
var popularitySlider = document.getElementById("popularity-slider");
var popularityValue = document.getElementById("popularity-value");
var acousticnessSlider = document.getElementById("acousticness-slider");
var acousticnessValue = document.getElementById("acousticness-value");
var danceabilitySlider = document.getElementById("danceability-slider");
var danceabilityValue = document.getElementById("danceability-value");
var energySlider = document.getElementById("energy-slider");
var energyValue = document.getElementById("energy-value");
var instrumentalnessSlider = document.getElementById("instrumentalness-slider");
var instrumentalnessValue = document.getElementById("instrumentalness-value");
var valenceSlider = document.getElementById("valence-slider");
var valenceValue = document.getElementById("valence-value");
popularitySlider.oninput = function () {
popularityValue.innerHTML = this.value;
filter()
};
acousticnessSlider.oninput = function () {
acousticnessValue.innerHTML = this.value;
filter()
};
danceabilitySlider.oninput = function () {
danceabilityValue.innerHTML = this.value;
filter()
};
energySlider.oninput = function () {
energyValue.innerHTML = this.value;
filter()
};
instrumentalnessSlider.oninput = function () {
instrumentalnessValue.innerHTML = this.value;
filter()
};
valenceSlider.oninput = function () {
valenceValue.innerHTML = this.value;
filter()
};
function filter() {
let parameters = new Array();
Object.keys(enabled_sliders).forEach(function(key) {
if (enabled_sliders[key]){
parameters.push(key);
}
});
for (let i = 0; i < albumData.length; i++) {
albumData[i].score = 0;
for(let j = 0; j < parameters.length; j++){
let p = parameters[j]
let s = document.getElementById(p+"-slider");
albumData[i].score += Math.pow(s.value - albumData[i][p] * 10, 2)
}
}
albumData.sort(function(a, b) {
return a.score - b.score;
});
createEntries(false);
}
function enable_checkbox(id) {
var checkBox = document.getElementById("checkbox-"+id);
if (checkBox.checked) {
document.getElementById(id+'-slider').disabled = false;
enabled_sliders[id] = true;
} else {
document.getElementById(id+'-slider').disabled = true;
enabled_sliders[id] = false;
}
filter()
}