-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
100 lines (85 loc) · 2.68 KB
/
main.js
File metadata and controls
100 lines (85 loc) · 2.68 KB
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
var Groupify = angular.module('Groupify', []);
Groupify.controller('MainCtrl', function($scope, $http, $timeout) {
var spotifyApi = new SpotifyWebApi();
$scope.queue = [];
$scope.query = "";
$scope.trackResults = [];
$scope.loggedIn = false;
if( document.cookie.indexOf("groupify=") >= 0 )
$scope.loggedIn = true;
backup_avatar = "/images/140-proof-logo-anim.gif";
(function tick() {
$http.get('/api/v1/queue/list')
.then(function(res){
// sum time to play up to each track in collection
var sum = 0;
track = res.data.now_playing.track;
if (track && res.data.now_playing.time_remaining) {
if( ! track.queued_by_avatar )
track.queued_by_avatar = backup_avatar;
sum = track.time_remaining = parseInt(res.data.now_playing.time_remaining);
$scope.current_track = track;
}
$scope.queue = res.data.queue;
for(var i = 0; i < $scope.queue.length; i++) {
track = $scope.queue[i];
track.time_to_play = sum;
if( ! track.queued_by_avatar )
track.queued_by_avatar = backup_avatar;
sum += parseInt(track.time);
}
$timeout(tick, 1000);
});
})();
$scope.next = function(){
$http.post('/api/v1/queue/next')
.then(function(res){
console.log("skipping to next track");
});
};
$scope.search = function(){
spotifyApi.searchTracks($scope.query, {limit: 10, offset: 0}, function(err, data) {
$scope.trackResults = data.tracks.items;
});
};
$scope.enqueue = function(track){
$http.post('/api/v1/queue/add', {
track_id: track.id
})
.then(function(res){
console.log("Enqueued track " + track.name);
});
};
$scope.dequeue = function(track){
$http.post('/api/v1/queue/delete', {
track_id: track.id
})
.then(function(res){
// FIXME: handle errors
console.log("De-queued track " + track.name);
});
};
});
Groupify.filter('secondsToTime', function() {
// shameless copy/paste
// http://codeaid.net/javascript/convert-seconds-to-hours-minutes-and-seconds-(javascript)
return function(secs) {
var hr = Math.floor(secs / 3600);
var min = Math.floor((secs - (hr * 3600))/60);
var sec = secs - (hr * 3600) - (min * 60);
if (hr < 10) { hr = "0" + hr; }
if (min < 10) { min = "0" + min;}
if (sec < 10) { sec = "0" + sec;}
if (hr) { hr = "00"; }
return hr + ':' + min + ':' + sec;
};
});
Groupify.filter('secondsToMinutes', function() {
return function(secs) {
var min = Math.floor(secs / 60);
var sec = secs - (min * 60);
if (min < 10) { min = "0" + min;}
if (sec < 10) { sec = "0" + sec;}
return min + ':' + sec;
};
});