-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrhythmweb.js
371 lines (304 loc) · 10.4 KB
/
rhythmweb.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
Rhythmweb - a web site for your Rhythmbox.
GTK3 port to work with v2.96 Rhythmbox
Copyright (c) 2012 fossfreedom and Taylor Raack
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
var services = new function() {
var baseUrl = '/playlist/',
getInitial = function() {
return $.getJSON(baseUrl + 'initial');
},
getCurrent = function() {
return $.getJSON(baseUrl + 'current');
},
getSlice = function(data) {
return $.post(baseUrl + 'slice', data);
};
return {
getInitial: getInitial,
getCurrent: getCurrent,
getSlice: getSlice
};
}();
function Rhythmweb() {
var toggleShuffleEl = $('#toggle-shuffle'),
toggleRepeatEl = $('#toggle-repeat'),
playlistBoxEl = $('#playlistbox'),
selectedPlaylist = '',
selectedTrack = '',
sliceStep = 524;
function post(params, reload, callback) {
$.post('/',
params,
function(data, textstatus, xhr) {
if (reload) {
loadCurrentPlayingSong();
}
if (callback) {
callback(params, reload, xhr);
}
});
}
function setPlaybutton(playing){
var playbutton = $('#play'),
img = $('#play img');
playbutton.attr('isplaying', playing);
if (playing) {
img.attr('src', 'stock/gtk-media-pause');
playbutton.html('Pause').prepend(img);
playbutton.addClass('active');
}
else {
img.attr('src', 'stock/gtk-media-play-ltr');
playbutton.html('Play').prepend(img);
playbutton.removeClass('active');
}
}
function play() {
var params = {
'action': $("#play").attr("isplaying").toLowerCase() === "true" ? "pause" : "play",
'playlist': selectedPlaylist
};
if (selectedTrack !== '') {
params.track = selectedTrack;
}
post(params, false,
function(ig, nore, xhr){
var obj = JSON.parse(xhr.responseText || "");
setPlaybutton(String(obj.playing).toLowerCase() === "true");
}
);
}
function previousTrack() {
post({'action': 'prev'}, false);
loadCurrentPlayingSong();
}
function nextTrack() {
post({'action': 'next'}, false);
loadCurrentPlayingSong();
}
function volumeUp() {
post({'action': 'vol-up'});
}
function volumeDown() {
post({'action':'vol-down'});
}
function toggleShuffle() {
post({'action':'toggle-shuffle'});
// change active flag
toggleShuffleEl.toggleClass('active');
}
function toggleRepeat() {
post({'action':'toggle-repeat'});
// change active flag
toggleRepeatEl.toggleClass('active');
}
function loadPlayQueue() {
// ajax load of play queue
$('#playlist tbody').empty();
$('#loading').show();
loadPlaylistSlice(0, sliceStep);
}
function loadPlaylist(playlistName) {
// ajax load of playlist
$('#playlist tbody').empty();
$('#loading').show();
$.get('/playlist/' + playlistName,loadPlaylistData);
}
function loadPlaylistData(playlistData) {
var tableData = '',
data = playlistData.tracks;
// add each track to the playlist
for(var i in data) {
var item = data[i];
tableData += '<tr id="' + item.id + '"><td>' + item.title + '</td><td>' + item.artist + '</td><td>' + item.album + '</td></tr>';
}
// show the new playlist
$('#playlist').append('<tbody>' + tableData + '</tbody>');
$("#playlist tr:even").css( "background-color", "#C0C0C0" );
selectedPlaylist = playlistData.name;
$('#loading').hide();
}
function installClickHandlers(elementLocator, clickFunction, doubleClickFunction) {
// add click handlers to all table elements, current and future
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0 || agent.indexOf('android') >= 0){
// register double tap handler, but don't use the single tap handler as it's broken
if(doubleClickFunction !== null) {
$(elementLocator).doubletap(
doubleClickFunction,
null,
300
);
}
// install single tap handler
if (clickFunction !== null) {
$(elementLocator).live('touchend', clickFunction);
}
} else {
// non mobile safari - use standard jquery click handlers
if(clickFunction !== null) {
$(elementLocator).live('click', clickFunction);
}
if(doubleClickFunction !== null) {
$(elementLocator).live('dblclick', doubleClickFunction);
}
}
}
function addTrackTableClickHandlers() {
installClickHandlers('#playlist tr', handleTrackClicked, handleTrackDoubleClicked);
}
function handleTrackClicked(event) {
var tr = $(event.currentTarget);
// remove previous selection
$('#playlist tr').removeClass('selected');
// select the track row
tr.addClass('selected');
selectedTrack = event.currentTarget.id;
}
function handlePlaylistClicked(event) {
var div = $(event.currentTarget);
// load the playlist into the window
if (div.hasClass("playqueue")) {
// this is the global play queue
loadPlayQueue();
} else {
loadPlaylist(div.html());
}
// remove previous selection
$('#playlistbox .playlist_item').removeClass('selected');
// select the track row
div.addClass('selected');
// clear the selected track
selectedTrack = '';
}
function handleTrackDoubleClicked(event) {
var targ = event.currentTarget,
trData = $("td", $(targ)),
title = $(trData[0]).text();
post({'action':'play-track','track':targ.id,'playlist':$('#playlistbox .selected').html()}, false);
$('#playing').html('<cite id="title">' + title + '</cite> by <cite id="artist">' + $(trData[1]).text() +
'</cite> from <cite id="album">' + $(trData[2]).text() + '</cite>' );
$(document).attr('title', title);
handleTrackClicked(event);
}
function togglePlaylistPaneVisibility(){
$('#main').toggleClass('use-sidebar');
var hasSidebar = $('#main').hasClass('use-sidebar') + '';
$.cookie("show_playlist_sidebar", hasSidebar);
$('#toggle-playlist-view').toggleClass('active');
}
function createPlaylistListReloader() {
// create double click handler for playlist
installClickHandlers('.playlist_item', handlePlaylistClicked, playPlaylist);
reloadPlaylists();
}
function playPlaylist(event) {
var div = $(event.currentTarget),
requestedPlaylist = $(event.currentTarget).html();
post({'action':'play-playlist', 'playlist': requestedPlaylist}, true);
}
function reloadPlaylists() {
// ajax load of playlists
$.get('/playlists',displayPlaylists);
}
function displayPlaylists(playlistData) {
// display the new playlists
var playlistHTMLs = '',
// add play queue
playQueueEl = '<div class="playlist_item playqueue';
if (playlistData.selected == 'Play Queue') {
playQueueEl += ' selected';
}
playQueueEl += '" >Play Queue</div>';
playlistHTMLs += playQueueEl;
for (var i in playlistData.playlists) {
var div = '<div class="playlist_item';
if (playlistData.selected === playlistData.playlists[i]) {
div += ' selected';
}
div += '">' + playlistData.playlists[i] + '</div>';
playlistHTMLs += div;
}
// reload playlist content
playlistBoxEl.html(playlistHTMLs);
}
function loadCurrentPlayingSong() {
services.getCurrent().done(function(data) {
var incomingTitle = data.title;
if (incomingTitle !== '') {
$('#playing').html('<cite id="title">' + incomingTitle + '</cite> by <cite id="artist">' + data.artist + '</cite> from <cite id="album">' +
data.album + '</cite> <cite id="stream">' + data.stream + '</cite>' );
$(document).attr('title', incomingTitle);
} else {
$('#playing').html('<span id="not-playing">Not playing</span>');
$(document).attr('title', 'Rhythmweb');
}
if (data.cover !== '' && data.cover !== null) {
$("#cover").attr('src', data.cover);
} else {
$("#cover").attr('src', 'rhythmbox-missing-artwork.svg');
}
}, 'json');
}
function loadInitialPlayList() {
loadPlaylistSlice(0, sliceStep);//loading in parts with prevent browser and rb from freezing for large lists
}
function loadPlaylistSlice(start, end) {
services.getSlice({'start': start, 'end': end}).done(function(data) {
var playlist = '',
playlistTable = $('#playlist');
$(data.tracks).each(function (i, item) {
playlist += '<tr id="' + item.id + '"><td>' + item.title + '</td><td>' + item.artist + '</td><td>' + item.album + '</td></tr>';
});
if (start === 0) {
$('#playlist tbody').empty();
playlistTable.append('<tbody>');
}
playlistTable.append(playlist);
$("#playlist tr:even").css( "background-color", "#C0C0C0");
if (data.tracks.length === sliceStep) {
setTimeout(loadPlaylistSlice(end, end += sliceStep), 500);
} else {
playlistTable.append('</tbody>');
$('#loading').hide();
}
});
}
function initialize() {
loadInitialPlayList();
var playlistViewCookie = $.cookie("show_playlist_sidebar"),
playBtn = $('#play');
if (playlistViewCookie !== null && playlistViewCookie === "true") {
$('#main').addClass('use-sidebar');
}
playBtn.click(play);
$('#previous-track').click(previousTrack);
$('#next-track').click(nextTrack);
$('#volume-up').click(volumeUp);
$('#volume-down').click(volumeDown);
$('#toggle-playlist-view').click(togglePlaylistPaneVisibility);
toggleShuffleEl.click(toggleShuffle);
toggleRepeatEl.click(toggleRepeat);
addTrackTableClickHandlers();
createPlaylistListReloader();
setPlaybutton(playBtn.attr("isplaying").toLowerCase() === "true");
//refresh currently playing song from server every 10s (no worries it takes about 12ms)
setInterval(loadCurrentPlayingSong, 10000);
}
initialize();
}
$(function() {
new Rhythmweb();
});