-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
198 lines (180 loc) · 6.57 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Author: Ivanilson Melo
* https://github.com/ivmelo
*/
var app = new Vue({
el: '#app',
data: {
clientId: null,
userId: null,
accessToken: null,
spotifyEndpoint: 'https://api.spotify.com/v1/',
playlistName: 'Your Playlist',
playlistUrl: null,
savePlaylistButtonLabel: 'Save Playlist',
playlistId: '',
isEditingplaylistName: false,
queryBox: '',
currentPage: 0,
currentQuery: '',
results: 20,
tracks: [],
selectedTracks: [],
isLoading: false,
showResults: false,
savingOrSaved: false,
ap: null,
},
created: function(){
this.clientId = spotifyClientId;
var params = this.extractParamsFromURIFragment();
// history.replaceState(null, 'Playlistify', window.location.pathname);
if(params['access_token'] != null) {
this.accessToken = params['access_token'];
axios.defaults.headers.common['Authorization'] = 'Bearer ' + this.accessToken;
axios.defaults.baseURL = this.spotifyEndpoint;
axios.get('me').then(function(response){
app.userId = response.data.id;
}).catch(function(error){
alert('Authorization error, please refresh the page and try again.');
});
}
console.log(this.accessToken);
},
methods: {
search: function() {
if (this.queryBox == '') {
alert('Please type in something to search.');
this.showResults = false;
return;
} else {
this.showResults = true;
this.isLoading = true;
}
if (this.currentQuery === this.queryBox) {
this.currentPage++;
} else {
this.currentQuery = this.queryBox;
this.currentPage = 0;
}
axios.get('search' + '?q=' + this.queryBox + '&type=track&offset=' + this.currentPage * this.results).then(function(response){
if (app.currentPage > 0) {
for (var i = 0; i < response.data.tracks.items.length; i++) {
app.tracks.push(response.data.tracks.items[i]);
}
} else {
app.tracks = response.data.tracks.items;
}
app.isLoading = false;
}).catch(function(error){
alert('Error, please try again.');
});
},
add: function(index) {
var track = this.tracks[index];
this.tracks.splice(index, 1);
this.selectedTracks.push(track);
},
remove: function(index) {
this.selectedTracks.splice(index, 1);
},
playTrackPreview: function(index) {
var track = this.tracks[index];
console.log(track.preview_url);
},
checkout: function() {
if (this.playlistName.length > 0) {
this.savingOrSaved = true;
this.savePlaylistButtonLabel = 'Saving...';
this.createPlaylist();
} else {
alert('You must give a name to your playlist.')
}
},
extractParamsFromURIFragment: function () {
var fragmentParams = {};
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&;=]+)=?([^&;]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = window.location.hash.substring(1);
while (e = r.exec(q)) {
fragmentParams[d(e[1])] = d(e[2]);
}
return fragmentParams;
},
createPlaylist: function () {
axios.post('users/' + this.userId + '/playlists', {
name: this.playlistName
}).then(function(response){
app.playlistId = response.data.id;
app.addSongsToPlaylist();
}).catch(function(error){
alert('Error, please try again.');
});
},
addSongsToPlaylist: function () {
var uris = [];
for (var i = 0; i < this.selectedTracks.length; i++) {
uris.push(this.selectedTracks[i].uri);
}
if (uris.length > 0) {
axios.post('users/' + this.userId + '/playlists/' + this.playlistId + '/tracks', {
uris: uris
}).then(function(response){
console.log(response);
// success
app.saved(response.data);
}).catch(function(error){
console.log(error);
});
} else {
alert('Not enough songs.');
}
},
finishedEditingPlaylistName: function() {
if(this.playlistName.length > 0) {
this.isEditingplaylistName = false;
} else {
alert('The playlist name cannot be empty.');
}
},
saved: function(data) {
this.savePlaylistButtonLabel = 'Saved!';
this.playlistUrl = 'https://open.spotify.com/user/' + this.userId + '/playlist/' + this.playlistId;
},
authUrl: function() {
var authUrl = 'https://accounts.spotify.com/authorize';
var responseType = 'token';
var redirectUri = window.location.protocol + '//' + window.location.host + window.location.pathname;
var scopes = 'playlist-read-private%20playlist-modify-public%20playlist-modify-private';
console.log(redirectUri);
return authUrl
+ '?client_id=' + this.clientId
+ '&response_type=' + responseType
+ '&redirect_uri=' + redirectUri
+ '&scope=' + scopes;
},
playPreview: function (track) {
console.log(track);
var music = {
title: track.name,
author: track.artists[0].name,
url: track.preview_url,
pic: track.album.images[1].url,
};
this.ap = new APlayer({
element: document.getElementById('player'),
narrow: false,
autoplay: true,
showlrc: 0,
mutex: true,
theme: '#e6d0b2',
mode: 'random',
preload: 'metadata',
listmaxheight: '513px',
music: music
});
}
}
});