Skip to content
Snippets Groups Projects
Commit 5f6cded0 authored by ale's avatar ale
Browse files

merge old chunks when searching

parent a4f33312
No related branches found
No related tags found
No related merge requests found
......@@ -40,7 +40,7 @@ djr.Backend.prototype.getPlaylist = function(uuid, callback, ctx) {
type: 'GET',
context: ctx || this,
success: function(data, status, jqxhr) {
callback(new djr.PlaylistChunk(data.songs));
callback(new djr.PlaylistChunk(data.songs, uuid));
}
});
};
......@@ -74,8 +74,9 @@ djr.Backend.prototype.nowPlaying = function(song) {
// A Playlist chunk (basically, an array of songs).
djr.PlaylistChunk = function(songs) {
djr.PlaylistChunk = function(songs, title) {
this.songs = songs || [];
this.title = title;
};
djr.PlaylistChunk.prototype.hasSong = function(sha1) {
......@@ -108,12 +109,8 @@ djr.Playlist.prototype.allSongs = function() {
return songs;
};
// Add a new chunk (only adds unique songs).
// Returns the chunk ID, or -1 if no songs were added.
djr.Playlist.prototype.addChunk = function(playlist_chunk) {
djr.debug('adding chunk to playlist ' + this.uuid);
var chunk_id = this.next_chunk_id++;
// Creates a new chunk with only unique songs.
djr.Playlist.prototype.createUniqueChunk = function(songs, title) {
var songs = [], i;
for (i = 0; i < playlist_chunk.songs.length; i++) {
var song = playlist_chunk.songs[i];
......@@ -121,16 +118,26 @@ djr.Playlist.prototype.addChunk = function(playlist_chunk) {
continue;
}
songs.push(song);
this.song_map[song] = chunk_id;
}
if (songs.length == 0) {
return -1;
if (songs.length > 0) {
return new djr.PlaylistChunk(songs, title);
} else {
this.chunk_map[chunk_id] = new djr.PlaylistChunk(songs);
return null;
}
};
// Add a new chunk (only adds unique songs).
// Returns the chunk ID, or -1 if no songs were added.
djr.Playlist.prototype.addChunk = function(playlist_chunk) {
djr.debug('adding chunk to playlist ' + this.uuid);
var i, chunk_id = this.next_chunk_id++;
for (i = 0; i < playlist_chunk.songs.length; i++) {
var song = playlist_chunk.songs[i];
this.song_map[song] = chunk_id;
}
this.chunk_map[chunk_id] = playlist_chunk;
this.chunks.push(chunk_id);
return chunk_id;
}
};
// Return the songs for a specific chunk.
......@@ -150,9 +157,15 @@ djr.Playlist.prototype.removeChunk = function(chunk_id) {
// Return a new Playlist with all the current chunks merged.
djr.Playlist.prototype.merge = function() {
var new_title_parts = [], i;
for (i = 0; i < this.chunks.length; i++) {
new_title_parts.push(this.chunk_map[this.chunks[i]].title);
}
var new_title = new_title_parts.join(' + ');
var new_playlist = new djr.Playlist();
new_playlist.uuid = this.uuid;
new_playlist.addChunk(this.allSongs());
new_playlist.addChunk(
new djr.PlaylistChunk(this.allSongs(), new_title));
return new_playlist;
};
......@@ -201,7 +214,7 @@ djr.Player = function(backend, selector) {
};
djr.Player.prototype.hideAllChunks = function() {
$('.chunk .inner').hide();
$('.chunk .chunk_inner').hide();
};
// Callback for removechunk click.
......@@ -211,6 +224,10 @@ djr.Player.prototype.removeChunkCallback = function() {
$(this).remove();
};
djr.Player.prototype.mergePlaylistChunks = function() {
this.playlist = this.playlist.merge();
};
// Search!
djr.Player.prototype.search = function(query) {
var player = this;
......@@ -225,12 +242,22 @@ djr.Player.prototype.search = function(query) {
return;
}
var chunk_id = player.playlist.addChunk(new djr.PlaylistChunk(songs));
if (chunk_id < 0) {
// Create a chunk of unique new songs.
var new_chunk = player.playlist.createUniqueChunk(songs, query);
if (!new_chunk) {
djr.debug('All the results are already in the playlist.');
return;
}
songs = player.playlist.getChunkSongs(chunk_id);
songs = new_chunk.songs;
// Ok, now, if we have more than 1 chunk in the playlist, we
// merge them.
if (player.playlist.chunks.length > 1) {
player.mergePlaylistChunks();
}
// Now add the new chunks with the search results.
var chunk_id = player.playlist.addChunk(new_chunk);
djr.debug('search: new chunk ' + chunk_id + ', ' + songs.length + ' songs');
player.backend.getHtmlForSongs(songs, function(songs_html) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment