From 9fe1d9b437e60dc73090454b57ddcd0be3f9afe9 Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Mon, 13 Mar 2017 05:39:12 +1300 Subject: [PATCH] getContext() --- src/js/components/ContextMenu.js | 81 ++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/src/js/components/ContextMenu.js b/src/js/components/ContextMenu.js index ca270d58..9739386b 100755 --- a/src/js/components/ContextMenu.js +++ b/src/js/components/ContextMenu.js @@ -38,10 +38,12 @@ class ContextMenu extends React.Component{ // 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') + $('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 && this.contextType().source == 'spotify'){ + if (this.props.spotify_authorized && context.source == 'spotify'){ switch (nextProps.menu.context){ case 'artist': case 'album': @@ -70,54 +72,62 @@ class ContextMenu extends React.Component{ } } - contextType(){ + getContext(){ var context = { name: null, - label: 'Unknown' + nice_name: 'Unknown' } if (this.props.menu && this.props.menu.context){ context.name = this.props.menu.context - context.label = 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.label = 'playlist' + context.nice_name = 'playlist' break case 'track': case 'queue-track': case 'playlist-track': case 'editable-playlist-track': - context.label = '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(){ - if (!this.props.menu || !this.props.menu.context){ + inLibrary(item = null){ + if (!item){ return false } - switch (this.props.menu.context){ + switch (helpers.uriType(item.uri)){ case 'artist': - return (this.props.library_artists && this.props.library_artists.indexOf(this.props.menu.items[0].uri) > -1) + 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(this.props.menu.items[0].uri) > -1) + 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(this.props.menu.items[0].uri) > -1) + return (this.props.library_playlists && this.props.library_playlists.indexOf(item.uri) > -1) break } - return false } @@ -125,7 +135,6 @@ class ContextMenu extends React.Component{ if (!this.props.spotify_authorized){ return false } - return (helpers.uriSource(this.props.menu.items[0].uri) == 'spotify') } @@ -215,6 +224,7 @@ class ContextMenu extends React.Component{ closeAndDeselectTracks(){ this.props.uiActions.hideContextMenu(); + // TODO } renderPlaylistSubmenu(){ @@ -252,32 +262,29 @@ class ContextMenu extends React.Component{ } renderTitle(){ - if (!this.props.menu.items || this.props.menu.items.length <= 0){ - return null - } + var context = this.getContext() - switch (this.contextType().name){ + 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}
- {helpers.uriSource(item.uri)} + {context.source}   - {this.contextType().label} + {context.nice_name}
-
{item.name}
+
{context.item.name}
) break @@ -286,9 +293,9 @@ class ContextMenu extends React.Component{ return (
- {helpers.uriSource(this.props.menu.items[0].uri)} + {context.source}   - {this.contextType().label}s + {context.nice_name}s
{this.props.menu.items.length} items @@ -301,6 +308,8 @@ class ContextMenu extends React.Component{ } renderItems(){ + var context = this.getContext() + var play_uris = ( this.playURIs(e)}> @@ -345,9 +354,9 @@ class ContextMenu extends React.Component{ var toggle_in_library = ( - this.toggleInLibrary(this.inLibrary())}> + this.toggleInLibrary(context.in_library)}> - {this.inLibrary() ? 'Remove from library' : 'Add to library'} + {context.in_library ? 'Remove from library' : 'Add to library'} @@ -372,7 +381,7 @@ class ContextMenu extends React.Component{ var start_radio = ( this.startRadio(e)}> - Start {this.contextType().label} radio + Start {this.context.nice_name} radio ) @@ -401,7 +410,7 @@ class ContextMenu extends React.Component{ ) - switch (this.contextType().name){ + switch (context.name){ case 'album': return ( @@ -431,7 +440,7 @@ class ContextMenu extends React.Component{
{play_uris} {this.canBeInLibrary() ? toggle_in_library : null} - {go_to_user} + {context.source == 'spotify' ? go_to_user : null} {copy_uris}
)