diff --git a/server/djrandom/frontend/static/player.js b/server/djrandom/frontend/static/player.js index 055427dbc3a690265437bf3cf00d82965572d3ed..f7c89bc521ded847d81527082d0a45bc2774e721 100644 --- a/server/djrandom/frontend/static/player.js +++ b/server/djrandom/frontend/static/player.js @@ -48,18 +48,24 @@ djr.Player.prototype.clearPlaylist = function() { }; // Set the playlist to a new set of songs. -djr.Player.prototype.setPlaylist = function(uuid, hashes) { - djr.debug('set playlist: ' + uuid + ' - ' + hashes.join(', ')); - this.curPlaylistId = uuid; +djr.Player.prototype.setPlaylist = function(hashes) { + djr.debug('set playlist: ' + this.curPlaylistId + ' - ' + hashes.join(', ')); this.curPlaylist = hashes; this.curIdx = -1; }; -// Add songs to the playlist. +// Add songs to the playlist, do not add songs already in there. djr.Player.prototype.extendPlaylist = function(hashes) { - var i; + var i, map = {}; + for (i = 0; i < this.curPlaylist.length; i++) { + map[this.curPlaylist[i]] = 1; + } for (i = 0; i < hashes.length; i++) { + if (map[hashes[i]]) { + continue; + } this.curPlaylist.push(hashes[i]); + map[hashes[i]] = 1; } }; @@ -84,7 +90,8 @@ djr.Player.prototype.loadPlaylist = function(uuid, callback) { dataType: 'json', context: this, success: function(data, status, jqxhr) { - this.setPlaylist(uuid, data.songs); + this.curPlaylistId = uuid; + this.setPlaylist(data.songs); if (callback) { callback(); } @@ -94,9 +101,13 @@ djr.Player.prototype.loadPlaylist = function(uuid, callback) { // Load search results into the current playlist. djr.Player.prototype.loadSearchResults = function(hashes, replace) { + var uuid = this.curPlaylistId; + if (replace || !uuid) { + uuid = djr.generateRandomId(); + } + this.curPlaylistId = uuid; if (replace) { - var uuid = djr.generateRandomId(); - this.setPlaylist(uuid, hashes); + this.setPlaylist(hashes); } else { this.extendPlaylist(hashes); }