From 81403f04910c83a5e7162a205fdbf15fc096aab7 Mon Sep 17 00:00:00 2001 From: Ryan Nauman Date: Mon, 19 Oct 2015 07:34:20 -0400 Subject: [PATCH] artist top tracks to playlist feature, closes #60 --- rdio-enhancer.css | 17 +++++++++ rdio-enhancer.ts | 94 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 5 deletions(-) diff --git a/rdio-enhancer.css b/rdio-enhancer.css index b85e10d..696dbec 100644 --- a/rdio-enhancer.css +++ b/rdio-enhancer.css @@ -162,3 +162,20 @@ .Menu li.share.copy_link { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAMAAABFNRROAAAABGdBTUEAALGPC/xhBQAAAIpQTFRFuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJuMPJAAAAuMPJkvC3mwAAAC10Uk5T9mM8pbFOrqKZZsMVVPAzVznb4QN7YPPG3kKE+fzn5Ku0Gx4MvVF4JAYJdY0A/S76QAAAAHtJREFUCNctzFcWwjAMRFHRO6Gmh1Q7sa3Z//YQEfq7R+cNsV4Xdn5iUmQxgONTdYqx2qQ4qNw6Z15gP8tsE+tcAv+TlYaWA9Je9ELhvRnwGWXToohq+QqY2rI8u3BB3UlDBg3zGNp5m26orlmFt4oHadBMf/Xhcc8V/AVvFxghUQadAAAAAABJRU5ErkJggg==); } + +.artist_tracks_to_playlist { + float: right; + margin-right: 10px; + background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAUCAMAAAC+oj0CAAAABGdBTUEAALGPC/xhBQAAAD9QTFRFlJmclJiclJmclJmclJmdlJmclJiclJmclJmclJmclJiclJmclJmclJmclJmclJmcAAAAlJmdlZmdlJiclJmcCxAmtwAAABF0Uk5TmhBwEJkP9O7075n1ynEGywDZOPyuAAAAd0lEQVQY05WO4RKDIAyDcc6pQy2SvP+zruDJUXV3ml/la0jjQhYZKoFuH2rMHTNe4z9uXOJDNhzBlUXFjfTKC51w8+SjgrDuaN3LPH2G6XUI6UQLxVTJ1Z+/pb/BPUCc8ZiR7laD3xtWanArW7I0BmtB8V7mJfwAFT8ct1xKBKUAAAAASUVORK5CYII=); + height: 28px; + width: 28px; + border-radius: 50%; + border: 1px solid #c9cccd; + background-size: auto 10px; + background-position: center center; + background-repeat: no-repeat; + cursor: pointer; +} +.artist_tracks_to_playlist:hover { + border-color: #94999c; +} diff --git a/rdio-enhancer.ts b/rdio-enhancer.ts index 810ad11..20ee353 100644 --- a/rdio-enhancer.ts +++ b/rdio-enhancer.ts @@ -250,11 +250,6 @@ function injectedJs() { return parent_get_attributes; } } - if(a == "Dialog.EditPlaylistDialog") { - } - if(a == "TrackList") { - - } if(a == "ActionMenu") { b.orig_getMenuOptions = b.getMenuOptions; @@ -746,6 +741,95 @@ function injectedJs() { } } + // Create playlists based on an artist's top tracks + if(a == "Catalog.Artist.Songs") { + b.orig_events = b.events || null; + b.events = function() { + var local_events = b.orig_events ? b.orig_events.call(this) : {}; + local_events["click .artist_tracks_to_playlist"] = "onArtistTracksToPlaylistClicked"; + return local_events; + }; + + b.onArtistTracksToPlaylistClicked = function(event) { + this.artistTracksToPlaylistMenu.open(); + R.Utils.stopEvent(event); + }; + + b.onArtistTracksToPlaylistOptionSelected = function(menuItem) { + var count = menuItem.get('value'), + artistKey = this.model.get('key'), + maxArtistTracks = this.model.get('length'), + playlistName = this.model.get('name') + ' Top Tracks', + tracksToAdd, + getTracks, + createPlaylist; + createPlaylist = (tracksToAdd) => { + return R.Api.request({ + method: "createPlaylist", + content: { name: playlistName, description: playlistName, tracks: tracksToAdd }, + success: function() { + R.enhancer.show_message('Playlist created.', true); + }, + error: function() { + R.enhancer.show_message('There was an error creating an artist playlist.', true); + } + }); + } + R.Api.request({ + method: "getTracksForArtist", + content: { artist: artistKey, count: count, extras: ['-*', 'key'] }, + success: (response) => { + if(response.status != 'ok') { + R.enhancer.show_message('There was an error getting tracks for this artist.', true); + return; + } + tracksToAdd = _.pluck(response.result.items, 'key'); + createPlaylist(tracksToAdd); + }, + error: () => { + R.enhancer.show_message('There was an error getting tracks for this artist.', true); + } + }); + }; + + b.getMenuOptions = (maxTracks) => { + var sizeChoices = _.filter([10, 100, 250, 500, 1000], (num) => { return num <= maxTracks }), + menuOptions = new Backbone.Collection([]), + cur, + createMenuItem; + createMenuItem = (val) => { + return { + label: 'Add Top ' + val, + value: val, + callback: b.onArtistTracksToPlaylistOptionSelected, + extraClassNames: 'playlist' + }; + }; + while (sizeChoices.length) { + cur = sizeChoices.shift(); + menuOptions.push(createMenuItem(cur)); + } + menuOptions.push(createMenuItem(maxTracks)); + return menuOptions; + }; + + b.orig_onRendered = b.onRendered || R.doNothing; + b.onRendered = function() { + b.orig_onRendered.call(this); + this.$(".SortControls").append('
'); + var $artistTracksToPlaylistMenuEl = this.$(".artist_tracks_to_playlist"), + artistMaxTracks = this.model.get('length'), + menuOptionsModel = b.getMenuOptions(artistMaxTracks); + + this.artistTracksToPlaylistMenu = this.addChild(new R.Components.Menu({ + positionOverEl: $artistTracksToPlaylistMenuEl, + positionUnder: false, + model: menuOptionsModel, + defaultContext: this + })); + }; + } + return R.Component.orig_create.call(this, a,b,c); }; },