-
Notifications
You must be signed in to change notification settings - Fork 0
/
weplay.js
305 lines (257 loc) · 7.99 KB
/
weplay.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
var Playlists = new Meteor.Collection('playlists');
if (Meteor.isClient) {
// init/change view code
function play(track_url) {
// Called by playNext
if (!track_url) {
return false;
}
Session.set('playing', true);
SC.oEmbed(track_url, { auto_play: true }, function(oEmbed) {
console.log('oEmbed response: ', oEmbed);
$('#nowPlaying').html(oEmbed.html);
SC.Widget($('#nowPlaying iframe')[0]).bind(SC.Widget.Events.FINISH,
function() {
Session.set('playing', false);
playNext();
});
});
}
function playNext() {
var next_track = getNextTrack();
if (next_track) {
removeTrackFromTracks(next_track);
play(getTrackUrl(next_track));
//if (!play(getTrackUrl(next_track))) {
// playNext();
//}
} else {
Session.set('playing', false);
}
}
$(function() {
// Page is finished loading...
SC.initialize({
client_id: "e9b793758d29d627c75586e91d726191"
});
$('body').on('click', 'a', function() {
history.pushState(null, null, encodeURI($(this).attr('href')));
setPageState();
return false;
});
if (Session.get('curr_playlist')) {
playNext();
}
});
function addTrackToRecent(track) {
// Append track to the recent collection
var set = {};
set['recent.' + track.id] = track;
Playlists.update({'_id': getCurrPlaylist()._id},
{$set: set}, function(err) {
console.log(err);
});
Session.set('curr_playlist', null);
return false;
}
function removeTrackFromTracks(track) {
// Remove a track that is about to be played from the tracks list
// and store it in curr_track
// Remove from tracks
var unset = {};
unset['tracks.' + track.id] = '';
Playlists.update({ _id: getCurrPlaylist()._id },
{ $unset: unset }, function(err) {
console.log(err);
});
Session.set('curr_track', track);
return false;
}
function getNextTrack() {
// Returns the top-voted track and removes it from queued track list
var tracks = Playlists.findOne({ _id:
Session.get('curr_playlist')._id }).tracks;
return allValues(tracks).sort(compare_votes)[0];
}
function getTrackUrl(track) {
var track_url = track.stream_url;
if (track_url) {
return track_url.substring(0, track_url.length - 7);
} else {
return null;
}
}
var playlists_loaded = false;
window.Playlists = Playlists;
Meteor.subscribe('playlists', function() {
playlists_loaded = true;
setPageState();
});
window.onpopstate = function(event) {
setPageState();
};
function setPageState() {
if (!playlists_loaded) return;
changePlaylist(getPlaylistFromURI());
}
function getPlaylistFromURI() {
// Returns the playlist specified in the URI
return getPlaylistFromName(decodeURI(window.location.pathname.substr(1)));
}
function getPlaylistFromName(playlist_name) {
return playlist_name === '' ? null : Playlists.findOne({"name":
playlist_name});
}
Template.container.curr_playlist = function() {
return Session.get('curr_playlist');
};
//Template.home.playlists = function() {
//return Playlists.find({}).fetch();
//};
// Homepage view
Template.home.events({
'submit #create_playlist_form' : function () {
var $playlist_name = $('#playlist_name');
if ($playlist_name.val().length >= 1) {
var playlist = createPlaylist($playlist_name.val());
changePlaylist(playlist);
$playlist_name.val('');
}
return false;
}
});
Template.home.events({
'input #playlist_name': function() {
// Displays whether given playlist exists or not already by changing
// submit button value and colour
var $playlist_input = $('#playlist_name').val();
var $playlist_button = $('#playlist_submit');
if ($playlist_input.length < 1) {
// No playlist entered yet
$playlist_button.val(' ');
$playlist_button.css("background-color", "buttonface");
}
else if (Playlists.findOne({"name": $playlist_input})) {
// Playlist exists
$playlist_button.val('Find');
$playlist_button.css("background-color", "orange");
return false;
} else {
// New playlist
$playlist_button.val('Create');
$playlist_button.css("background-color", "purple");
}
return false;
}
});
function createPlaylist(name) {
// Returns a newly created Playlist with name name.
// Returns false if a Playlist with that name already exists
if (!Playlists.findOne({"name": name})) {
Playlists.insert({name: name, tracks: {}, recent: {}});
}
return Playlists.findOne({"name": name});
}
function changePlaylist(playlist) {
Session.set('curr_playlist', playlist);
playNext();
var playlist_name = decodeURI(window.location.pathname.substr(1));
if ((playlist && playlist.name == playlist_name) ||
(!playlist && playlist_name === '')) {
return;
}
history.pushState(null, null, '/' + (playlist ? encodeURI(playlist.name) : ''));
}
// Playlist view
//
function allValues(obj) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(obj[key]);
}
}
return list;
}
function compare_votes(a,b) {
if (a.votes < b.votes)
return 1;
if (a.votes > b.votes)
return -1;
return 0;
}
Template.playlist.tracks = function() {
tracks = Playlists.findOne({_id: Session.get('curr_playlist')._id}).tracks;
return allValues(tracks).sort(compare_votes).map(function(track) {
track.did_vote = (track.voters &&
track.voters.indexOf(Meteor.userId())) !== -1;
return track;
});
};
Template.playlist.events({
'input #query': function() {
// Displays search results for query if query has at least 3 char
var query = $('#query').val();
if (query.length < 3) {
Session.set('search_results', []);
return;
}
SC.get('/tracks', { q: query, limit: 10, streamable: true },
function(tracks) {
Session.set('search_results', tracks);
});
return false;
}
});
Template.playlist.results = function() {
return Session.get('search_results');
};
function getCurrPlaylist() {
return Session.get('curr_playlist');
}
Template.playlist.playlist = getCurrPlaylist;
Template.playlist.events({
// Add track
'click .add_track': function(event) {
var track = this;
track.votes = 0;
track.voters = [];
$(event.currentTarget).attr('disabled', true).val('Added');
var set = {};
set['tracks.' + track.id] = track;
Playlists.update({'_id': getCurrPlaylist()._id},
{$set: set}, function(err) {
console.log(err);
});
Session.set('search_results', null);
if (!Session.get('playing')) {
playNext();
}
return false;
}
});
Template.playlist.events({
// Vote for a track
'click .vote': function(event) {
var track = this;
var inc = {};
var push = {};
inc['tracks.' + track.id + '.votes'] = 1;
push['tracks.' + track.id + '.voters'] = Meteor.userId();
$(event.currentTarget).attr('disabled', true).val('Voted');
Playlists.update({'_id': getCurrPlaylist()._id },
{$inc: inc, $push: push}, function(err) {
console.log(err);
});
return false;
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Meteor.publish('playlists', function() {
return Playlists.find({});
});
});
}