-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlong-tail.html
67 lines (62 loc) · 2.31 KB
/
long-tail.html
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
<script src="../jquery/dist/jquery.min.js"></script>
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-ajax/core-ajax.html">
<polymer-element name="long-tail" attributes="artist, size">
<template>
<div id="loading" hidden?="{{!loading}}">Loading...</div>
<iframe hidden?="{{loading}}" src="https://embed.spotify.com/?uri=spotify:trackset::{{tracks}}" width="300" height="380" frameborder="0" allowtransparency="true"></iframe>
</template>
<script>
Polymer({
created: function() {
this.api_root = 'https://api.spotify.com/v1/';
this.loading = false;
},
ready: function() {
if(this.artist) {
this.loading = true;
var self = this;
$.ajax(this.api_root + 'artists/' + this.artist + '/albums?album_type=album&limit=50').success(function(data) {
return self.getTracks(data, self.size);
})
}
},
getTracks: function(data, size) {
var size = size || 50;
this.tracks = [];
this.albums = [];
this.alltracks = [];
var self = this;
// Get all tracks of all albums (w/ 50-items limit)
$.each(data.items, function(i, album) {
self.albums.push($.ajax(self.api_root + 'albums/' + album.id + '/tracks').success(function(data) {
var album_tracks = $.map(data.items, function(track) {
return track.id
}).join(',');
self.tracks.push($.ajax(self.api_root + 'tracks/?ids=' + album_tracks).success(function(data) {
$.each(data.tracks, function(i, track) {
// Popularity=0 doesn't seem always OK
if(track.popularity) {
self.alltracks = self.alltracks.concat(track);
}
})
}));
}));
});
// Sort, limit and display when all ajax calls are done
$.when.apply($, self.albums).done(function() {
$.when.apply($, self.tracks).done(function() {
self.alltracks.sort(function(a, b) {
return a.popularity - b.popularity
});
var max = Math.min(size, self.alltracks.length);
self.tracks = $.map(self.alltracks.slice(0, max), function(track) {
return track.id;
}).join();
self.loading = false;
});
});
}
});
</script>
</polymer-element>