From 17cb61d40132ec32c6fc32d27bbbb3092f85451a Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 23 Jul 2011 10:44:17 +0100
Subject: [PATCH] handle the initial case when playlist id = null

---
 server/djrandom/frontend/static/player.js | 27 ++++++++++++++++-------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/server/djrandom/frontend/static/player.js b/server/djrandom/frontend/static/player.js
index 055427d..f7c89bc 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);
   }
-- 
GitLab