-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·143 lines (137 loc) · 3.76 KB
/
app.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
$(document).ready(function(){
// Get access token
var tokenMatches = window.location.hash.match(/access_token=(.*)&token_type=*/);
if (tokenMatches) {
var access_token = tokenMatches[1];
//console.log(access_token);
window.sessionStorage.setItem("spotify_access_token", access_token);
}
var source = $("#track-template").html();
var trackTemplate = Handlebars.compile(source);
//index.html page actions
if ($("#page_id").val() === "index") {
//Get all tracks based on search title
$("#search_track").on("submit", function(event){
event.preventDefault();
var track_title = encodeURIComponent($("#search").val());
$.ajax({
url: "https://api.spotify.com/v1/search?q="+track_title+"&type=track",
type: "GET",
success: function(results) {
$("#song-container").html('');
console.log(results.tracks.items[0].name);
if(results.tracks.items){
var result_arr = results.tracks.items;
result_arr.forEach(function (element) {
//console.log(element.name);
var element = {
name: element.name,
artist: element.artists[0].name,
image: element.album.images[0].url,
preview_url: element.preview_url,
album_name: element.album.name,
id: element.id
};
$("#song-container").append(trackTemplate(element));
});
}
else {
alert('Please enter a valid title!');
$("#search_track")[0].reset();
}
},
error: function() {
alert("Error!");
}
});
});
//Save tracks
$(document).on("click", "#save_track", function(e) {
e.preventDefault();
var ids = $(this).val();
$.ajax({
url: "https://api.spotify.com/v1/me/tracks?ids=" +ids,
headers: {
"Content-Type" : "application/json",
"Authorization": "Bearer " +
window.sessionStorage.getItem("spotify_access_token")
},
type: "PUT",
success: function(results) {
console.log('Track was saved');
$("."+ids+"").html('Track Saved!');
$("."+ids+"").prop("disabled",true);
},
error: function() {
alert("Error Saving!");
}
});
});
}
//saved.html page actions
if ($("#page_id").val() === "saved") {
$.ajax({
type: "GET",
url: "https://api.spotify.com/v1/me/tracks",
headers: {
"Authorization": "Bearer " +
window.sessionStorage.getItem("spotify_access_token")
},
success: function (results) {
if(results.items){
var saved_arr = results.items;
saved_arr.forEach(function (element) {
//console.log(element.name);
var element = {
name: element.track.name,
artist: element.track.artists[0].name,
image: element.track.album.images[0].url,
preview_url: element.track.preview_url,
album_name: element.track.album.name,
id: element.track.id
};
$("#song-container").append(trackTemplate(element));
});
}
else {
alert('Looks like you do not have any saved tracks yet!');
}
},
error: function () {
alert('cannot get tracks!');
}
});
//remove track
$(document).on("click", "#remove_track", function(e) {
e.preventDefault();
var ids = $(this).val();
console.log(ids);
$.ajax({
url: "https://api.spotify.com/v1/me/tracks?ids=" +ids,
headers: {
"Content-Type" : "application/json",
"Authorization": "Bearer " +
window.sessionStorage.getItem("spotify_access_token")
},
type: "DELETE",
success: function(results) {
console.log('Track was removed');
//hide it!
$("#"+ids+"").hide('slow');
},
error: function() {
alert("Error removing!");
}
});
});
}
//if one song plays, pause the others ?? fix
//$('audio').each(function() {
// var song = $(this);
// if(song.paused){
// song.play();
// } else {
// song.pause();
// }
//});
});