diff --git a/server/djrandom/frontend/static/player.js b/server/djrandom/frontend/static/player.js index 8733539178a39f94d105ab2778aeacaedc7ec1c0..855b14f4114eecfbb62d8dab4bd4d5c4b60d68c0 100644 --- a/server/djrandom/frontend/static/player.js +++ b/server/djrandom/frontend/static/player.js @@ -33,6 +33,19 @@ djr.Backend.prototype.search = function(query, callback, ctx) { }); }; +// More like these. +djr.Backend.prototype.moreLikeThese = function(uuids, callback, ctx) { + $.ajax({url: '/json/morelikethese', + data: {'h': uuids.join(',')}, + dataType: 'json', + type: 'POST', + context: ctx || this, + success: function(data, status, jqxhr) { + callback(data.results); + } + }); +}; + // Get information about a specific song. // Read a playlist, calls callback(Playlist). @@ -457,6 +470,36 @@ djr.Player.prototype.streamCurrentPlaylist = function(enable) { // Nothing for now. }; +// Extend the current playlist with suggestions. +djr.Player.prototype.extendCurrentPlaylist = function() { + var player = this; + var cur_songs = this.allSongs(); + + this.backend.moreLikeThese(cur_songs, function(results) { + var new_chunk = player.playlist.createUniqueChunk(results, 'suggestions'); + if (!new_chunk) { + djr.debug('All the results are already in the playlist.'); + return; + } + + // Ok, now, if we have more than 1 chunk in the playlist, we + // merge them. + if (player.playlist.chunks.length > 1) { + player.mergePlaylistChunks(); + } + + // Add the new chunk with the suggestions. + var chunk_id = player.playlist.addChunk(new_chunk); + player.savePlaylist(); + + player.backend.getHtmlForSongs(songs, function(songs_html) { + var chunk_div = 'chunk_' + chunk_id; + player.hideAllChunks(); + player.setChunkHtml(new_chunk, chunk_id, songs_html); + }); + }); +}; + djr.state = { backend: null, @@ -474,6 +517,9 @@ djr.init = function () { $('#playlistStreamBtn').click(function() { djr.state.player.streamCurrentPlaylist(); }); + $('#playlistExtendBtn').click(function() { + djr.state.player.extendCurrentPlaylist(); + }); // Set the album art image to auto-fullscreen on load. $('#albumart_fs').load(function() { diff --git a/server/djrandom/frontend/templates/index.html b/server/djrandom/frontend/templates/index.html index 38f9b19bf654d017b5608c6fbaa0f1dd381c315f..20e3c0159208ac717dc0cbb023ebe1b3f4669004 100644 --- a/server/djrandom/frontend/templates/index.html +++ b/server/djrandom/frontend/templates/index.html @@ -89,7 +89,8 @@ DJ:Random </div> <div id="playlistControls"> - <a id="playlistClearBtn">Clear</a> + <a id="playlistExtendBtn">Extend</a> + | <a id="playlistClearBtn">Clear</a> | <a id="playlistStreamBtn">Stream</a> <span id="playlistStreamUrl"></span> </div>