diff --git a/server/djrandom/frontend/static/djrandom.js b/server/djrandom/frontend/static/djrandom.js index 91aefffdfe85c0c2b4c8875b185235cbe1a4c8dc..20830cf48473ac006ab2b361ddeded084c501053 100644 --- a/server/djrandom/frontend/static/djrandom.js +++ b/server/djrandom/frontend/static/djrandom.js @@ -46,13 +46,9 @@ djr.history.restore = function() { djr.state.player.loadPlaylist(newPlaylistID, function() { djr.loadPlaylistHtml(); }); - djr.setQuery(hash_info.pl); } }; - -// State-related functions - djr.getQuery = function() { return $('#queryField').val(); }; @@ -87,9 +83,7 @@ djr.doSearch = function() { $('#loading').show(); $.getJSON('/json/search', {'q': djr.getQuery()}, 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. var hashes = Array(); $.each(data.results, function(idx, item) { @@ -97,14 +91,17 @@ djr.doSearch = function() { hashes.push(item.sha1); } }); + // Clear the current playlist and save a new one. - djr.state.player.clearPlaylist(); - djr.state.player.setPlaylist(hashes); - djr.state.player.savePlaylist(); + if (hashes.length > 0) { + djr.state.player.loadSearchResults(hashes, false); + } // Load the HTML rendering of the playlist. djr.loadPlaylistHtml(); - }); + + djr.history.save(); + }); }; diff --git a/server/djrandom/frontend/static/player.js b/server/djrandom/frontend/static/player.js index b5d5bffdf95d9b0c54c94f38ad8d40730aa7e484..055427dbc3a690265437bf3cf00d82965572d3ed 100644 --- a/server/djrandom/frontend/static/player.js +++ b/server/djrandom/frontend/static/player.js @@ -1,5 +1,14 @@ // 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) { this.player = $(selector); this.curPlaylist = Array(); @@ -39,37 +48,43 @@ djr.Player.prototype.clearPlaylist = function() { }; // Set the playlist to a new set of songs. -djr.Player.prototype.setPlaylist = function(hashes) { - djr.debug('new playlist: ' + hashes.join(', ')); +djr.Player.prototype.setPlaylist = function(uuid, hashes) { + djr.debug('set playlist: ' + uuid + ' - ' + hashes.join(', ')); + this.curPlaylistId = uuid; this.curPlaylist = hashes; 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. djr.Player.prototype.savePlaylist = function() { + djr.debug('saving playlist ' + this.curPlaylistId); + if (!this.curPlaylistId) { + return; + } $.ajax({url: '/json/playlist/save', data: {uuid: this.curPlaylistId, h: this.curPlaylist.join(',')}, dataType: 'json', - 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); - } - } + type: 'POST' }); }; // Load a playlist. djr.Player.prototype.loadPlaylist = function(uuid, callback) { + djr.debug('loading playlist ' + uuid); $.ajax({url: '/json/playlist/get/' + uuid, dataType: 'json', context: this, success: function(data, status, jqxhr) { - this.curPlaylistId = uuid; - this.setPlaylist(data.songs); + this.setPlaylist(uuid, data.songs); if (callback) { 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. djr.Player.prototype.nextSong = function() { var next_idx = this.curIdx + 1;