Skip to content
Snippets Groups Projects
Commit 400c15d1 authored by ale's avatar ale
Browse files

handle playlists generation on the client side

parent f7524d2d
No related branches found
No related tags found
No related merge requests found
...@@ -46,13 +46,9 @@ djr.history.restore = function() { ...@@ -46,13 +46,9 @@ djr.history.restore = function() {
djr.state.player.loadPlaylist(newPlaylistID, function() { djr.state.player.loadPlaylist(newPlaylistID, function() {
djr.loadPlaylistHtml(); djr.loadPlaylistHtml();
}); });
djr.setQuery(hash_info.pl);
} }
}; };
// State-related functions
djr.getQuery = function() { djr.getQuery = function() {
return $('#queryField').val(); return $('#queryField').val();
}; };
...@@ -87,8 +83,6 @@ djr.doSearch = function() { ...@@ -87,8 +83,6 @@ djr.doSearch = function() {
$('#loading').show(); $('#loading').show();
$.getJSON('/json/search', {'q': djr.getQuery()}, $.getJSON('/json/search', {'q': djr.getQuery()},
function(data, status, jqxhr) { function(data, status, jqxhr) {
djr.history.save();
// Build an ordered array of unique hashes, which will be // Build an ordered array of unique hashes, which will be
// our new playlist. // our new playlist.
var hashes = Array(); var hashes = Array();
...@@ -97,13 +91,16 @@ djr.doSearch = function() { ...@@ -97,13 +91,16 @@ djr.doSearch = function() {
hashes.push(item.sha1); hashes.push(item.sha1);
} }
}); });
// Clear the current playlist and save a new one. // Clear the current playlist and save a new one.
djr.state.player.clearPlaylist(); if (hashes.length > 0) {
djr.state.player.setPlaylist(hashes); djr.state.player.loadSearchResults(hashes, false);
djr.state.player.savePlaylist(); }
// Load the HTML rendering of the playlist. // Load the HTML rendering of the playlist.
djr.loadPlaylistHtml(); djr.loadPlaylistHtml();
djr.history.save();
}); });
}; };
......
// player.js // player.js
var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
djr.generateRandomId = function() {
var uuid = [], chars = CHARS, i;
var len = 40, radix = chars.length;
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
return uuid.join('');
};
djr.Player = function(selector) { djr.Player = function(selector) {
this.player = $(selector); this.player = $(selector);
this.curPlaylist = Array(); this.curPlaylist = Array();
...@@ -39,37 +48,43 @@ djr.Player.prototype.clearPlaylist = function() { ...@@ -39,37 +48,43 @@ djr.Player.prototype.clearPlaylist = function() {
}; };
// Set the playlist to a new set of songs. // Set the playlist to a new set of songs.
djr.Player.prototype.setPlaylist = function(hashes) { djr.Player.prototype.setPlaylist = function(uuid, hashes) {
djr.debug('new playlist: ' + hashes.join(', ')); djr.debug('set playlist: ' + uuid + ' - ' + hashes.join(', '));
this.curPlaylistId = uuid;
this.curPlaylist = hashes; this.curPlaylist = hashes;
this.curIdx = -1; this.curIdx = -1;
}; };
// Add songs to the playlist.
djr.Player.prototype.extendPlaylist = function(hashes) {
var i;
for (i = 0; i < hashes.length; i++) {
this.curPlaylist.push(hashes[i]);
}
};
// Save the current playlist. // Save the current playlist.
djr.Player.prototype.savePlaylist = function() { djr.Player.prototype.savePlaylist = function() {
djr.debug('saving playlist ' + this.curPlaylistId);
if (!this.curPlaylistId) {
return;
}
$.ajax({url: '/json/playlist/save', $.ajax({url: '/json/playlist/save',
data: {uuid: this.curPlaylistId, data: {uuid: this.curPlaylistId,
h: this.curPlaylist.join(',')}, h: this.curPlaylist.join(',')},
dataType: 'json', dataType: 'json',
type: 'POST', type: 'POST'
context: this,
success: function(data, status, jqxhr) {
if (data.status && data.uuid != this.curPlaylistId) {
this.curPlaylistId = data.uuid;
djr.debug('created new playlist, UUID=' + data.uuid);
}
}
}); });
}; };
// Load a playlist. // Load a playlist.
djr.Player.prototype.loadPlaylist = function(uuid, callback) { djr.Player.prototype.loadPlaylist = function(uuid, callback) {
djr.debug('loading playlist ' + uuid);
$.ajax({url: '/json/playlist/get/' + uuid, $.ajax({url: '/json/playlist/get/' + uuid,
dataType: 'json', dataType: 'json',
context: this, context: this,
success: function(data, status, jqxhr) { success: function(data, status, jqxhr) {
this.curPlaylistId = uuid; this.setPlaylist(uuid, data.songs);
this.setPlaylist(data.songs);
if (callback) { if (callback) {
callback(); callback();
} }
...@@ -77,6 +92,17 @@ djr.Player.prototype.loadPlaylist = function(uuid, callback) { ...@@ -77,6 +92,17 @@ djr.Player.prototype.loadPlaylist = function(uuid, callback) {
}); });
}; };
// Load search results into the current playlist.
djr.Player.prototype.loadSearchResults = function(hashes, replace) {
if (replace) {
var uuid = djr.generateRandomId();
this.setPlaylist(uuid, hashes);
} else {
this.extendPlaylist(hashes);
}
this.savePlaylist();
};
// Start playing the next song in the playlist. // Start playing the next song in the playlist.
djr.Player.prototype.nextSong = function() { djr.Player.prototype.nextSong = function() {
var next_idx = this.curIdx + 1; var next_idx = this.curIdx + 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment