-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
89 lines (60 loc) · 1.96 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
angular.module("app", [])
.config([
'$compileProvider',
function ($compileProvider) {
// allows linking to spotify app
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|spotify):/);
}
])
.controller("myCtrl", function ($scope, $http) {
$scope.myData = {};
$scope.search = function () {
console.log($scope.query);
if ($scope.query === undefined || $scope.query.length == 0) return;
makeQuery('artist', function (data) {
$scope.myData.artists = data.artists.items;
});
makeQuery('album', function (data) {
$scope.myData.albums = data.albums.items;
});
makeQuery('track', function (data) {
$scope.myData.tracks = data.tracks.items;
});
function makeQuery(type, callback) {
var url = 'https://api.spotify.com/v1/search?q=' + $scope.query +
'&type=' + type +
'&limit=20';
// GET request
$http.get(url).then(function successCallback(response) {
callback(response.data);
}, function errorCallback(response) {
console.log('error');
});
}
}
// Parse artist names for track
$scope.parseArtistsForTrack = function (artists) {
var str = 'by: ';
var first = true;
for (artist in artists) {
if (first) {
first = false;
} else {
str += ', ';
}
str += artists[artist].name;
}
return str;
}
// Get the best image url from array or placeholder
$scope.getImage = function (array) {
if (array === undefined) {
return 'http://placehold.it/64x64';
}
var last = array[array.length - 1];
if (last === undefined) {
return 'http://placehold.it/64x64';
}
return last.url;
}
});