diff --git a/src/js/components/ContextMenu.js b/src/js/components/ContextMenu.js index 660d4b48..9739386b 100755 --- a/src/js/components/ContextMenu.js +++ b/src/js/components/ContextMenu.js @@ -34,11 +34,25 @@ class ContextMenu extends React.Component{ } componentWillReceiveProps( nextProps ){ + // if we've been given a menu object (ie activated) when we didn't have one prior if (nextProps.menu && !this.props.menu){ this.setState({ submenu_expanded: false }) $('body').addClass('context-menu-open') + var context = this.getContext() + + // if we're able to be in the library, run a check + if (this.props.spotify_authorized && context.source == 'spotify'){ + switch (nextProps.menu.context){ + case 'artist': + case 'album': + case 'playlist': + this.props.spotifyActions.following(nextProps.menu.items[0].uri) + break + } + } + // we DID have one prior, and now we don't } else if (this.props.menu && !nextProps.menu){ $('body').removeClass('context-menu-open') @@ -58,6 +72,81 @@ class ContextMenu extends React.Component{ } } + getContext(){ + var context = { + name: null, + nice_name: 'Unknown' + } + + if (this.props.menu && this.props.menu.context){ + context.name = this.props.menu.context + context.nice_name = this.props.menu.context + + // handle ugly labels + switch (this.props.menu.context){ + case 'playlist': + case 'editable-playlist': + context.nice_name = 'playlist' + break + + case 'track': + case 'queue-track': + case 'playlist-track': + case 'editable-playlist-track': + context.nice_name = 'track' + break + } + + // Consider the object(s) themselves + // We can only really accommodate the first item. The only instances where + // there is multiple is tracklists, when they're all of the same source (except search?) + if (this.props.menu.items && this.props.menu.items.length > 0){ + var item = this.props.menu.items[0] + context.item = item + context.source = helpers.uriSource(item.uri) + context.type = helpers.uriType(item.uri) + context.in_library = this.inLibrary(item) + } + } + + return context + } + + inLibrary(item = null){ + if (!item){ + return false + } + + switch (helpers.uriType(item.uri)){ + case 'artist': + return (this.props.library_artists && this.props.library_artists.indexOf(item.uri) > -1) + break + case 'album': + return (this.props.library_albums && this.props.library_albums.indexOf(item.uri) > -1) + break + case 'playlist': + return (this.props.library_playlists && this.props.library_playlists.indexOf(item.uri) > -1) + break + } + return false + } + + canBeInLibrary(){ + if (!this.props.spotify_authorized){ + return false + } + return (helpers.uriSource(this.props.menu.items[0].uri) == 'spotify') + } + + toggleInLibrary(in_library){ + this.props.uiActions.hideContextMenu() + if (in_library){ + this.props.spotifyActions.following(this.props.menu.items[0].uri, 'DELETE') + } else { + this.props.spotifyActions.following(this.props.menu.items[0].uri, 'PUT') + } + } + playQueueItem(){ this.props.uiActions.hideContextMenu() var tracks = this.props.menu.items; @@ -84,10 +173,6 @@ class ContextMenu extends React.Component{ this.props.mopidyActions.enqueueURIsNext(this.props.menu.uris, this.props.menu.tracklist_uri) } - addToPlaylist(){ - this.setState({ submenu_expanded: !this.state.submenu_expanded }) - } - addToQueue(){ this.props.uiActions.hideContextMenu() this.props.mopidyActions.enqueueURIs(this.props.menu.uris, this.props.menu.tracklist_uri) @@ -139,6 +224,7 @@ class ContextMenu extends React.Component{ closeAndDeselectTracks(){ this.props.uiActions.hideContextMenu(); + // TODO } renderPlaylistSubmenu(){ @@ -175,100 +261,30 @@ class ContextMenu extends React.Component{ ) } - getItems(){ - switch (this.props.menu.context) { - - case 'album': - var items = [ - { handleClick: 'playURIs', label: 'Play' }, - { handleClick: 'playURIsNext', label: 'Play next' }, - { handleClick: 'addToQueue', label: 'Add to queue' }, - { handleClick: 'goToArtist', label: 'Go to artist' }, - // { handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO - { handleClick: 'copyURIs', label: 'Copy URI' } - ] - break - - case 'artist': - var items = [ - //{ handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO - { handleClick: 'startRadio', label: 'Start radio' }, - { handleClick: 'copyURIs', label: 'Copy URI' } - ] - break - - case 'playlist': - var items = [ - { handleClick: 'playURIs', label: 'Play' }, - { handleClick: 'goToUser', label: 'Go to user' }, - // { handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO - { handleClick: 'copyURIs', label: 'Copy URI' } - ] - break - - case 'queue': - var items = [ - { handleClick: 'playQueueItem', label: 'Play' }, - { handleClick: 'addToPlaylist', label: 'Add to playlist' }, - { handleClick: 'copyURIs', label: 'Copy URIs' }, - { handleClick: 'removeFromQueue', label: 'Remove' } - ] - break - - case 'editable-playlist-track': - var items = [ - { handleClick: 'playURIs', label: 'Play' }, - { handleClick: 'playURIsNext', label: 'Play next' }, - { handleClick: 'addToQueue', label: 'Add to queue' }, - { handleClick: 'addToPlaylist', label: 'Add to playlist' }, - { handleClick: 'startRadio', label: 'Start radio' }, - { handleClick: 'copyURIs', label: 'Copy URIs' }, - { handleClick: 'removeFromPlaylist', label: 'Remove' } - ] - break - - default: - var items = [ - { handleClick: 'playURIs', label: 'Play' }, - { handleClick: 'playURIsNext', label: 'Play next' }, - { handleClick: 'addToQueue', label: 'Add to queue' }, - { handleClick: 'addToPlaylist', label: 'Add to playlist' }, - { handleClick: 'startRadio', label: 'Start radio' }, - { handleClick: 'copyURIs', label: 'Copy URIs' } - ] - break - } - - return items - } - renderTitle(){ - if (!this.props.menu.items || this.props.menu.items.length <= 0){ - return null - } + var context = this.getContext() - switch (this.props.menu.context){ + switch (context.type){ case 'artist': case 'album': case 'playlist': - var item = this.props.menu.items[0] var style = null - if (item && item.images){ + if (context.item && context.item.images){ style = { - backgroundImage: 'url('+helpers.sizedImages(item.images).medium+')' + backgroundImage: 'url('+helpers.sizedImages(context.item.images).medium+')' } } return ( - + {style ?
: null}