Files
Iris/src/js/components/ContextMenu.js

546 lines
14 KiB
JavaScript
Raw Normal View History

2016-10-27 10:02:12 +13:00
import React, { PropTypes } from 'react'
2017-02-27 08:44:02 +13:00
import { Link, hashHistory } from 'react-router'
2016-10-27 10:02:12 +13:00
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
2016-11-12 16:36:36 +13:00
import FontAwesome from 'react-fontawesome'
2016-10-27 10:02:12 +13:00
import TrackList from './TrackList'
import * as helpers from '../helpers'
2016-10-27 10:02:12 +13:00
import * as uiActions from '../services/ui/actions'
import * as pusherActions from '../services/pusher/actions'
2016-10-27 10:02:12 +13:00
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
class ContextMenu extends React.Component{
constructor(props) {
2017-02-28 20:58:15 +13:00
super(props)
this.state = {
submenu_expanded: false
2017-02-28 20:58:15 +13:00
}
2016-12-14 16:41:05 +13:00
this.handleScroll = this.handleScroll.bind(this)
this.handleClick = this.handleClick.bind(this)
}
2016-12-14 16:41:05 +13:00
2017-02-28 20:58:15 +13:00
componentDidMount(){
2016-12-14 16:41:05 +13:00
window.addEventListener("scroll", this.handleScroll, false)
window.addEventListener("click", this.handleClick, false)
}
componentWillUnmount(){
2016-12-14 16:41:05 +13:00
window.removeEventListener("scroll", this.handleScroll, false)
window.removeEventListener("click", this.handleClick, false)
}
componentWillReceiveProps( nextProps ){
2017-03-10 22:35:11 +13:00
2017-02-28 20:58:15 +13:00
// 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 })
2017-03-13 05:39:12 +13:00
$('body').addClass('context-menu-open')
var context = this.getContext()
2017-02-28 20:58:15 +13:00
// if we're able to be in the library, run a check
2017-03-13 05:39:12 +13:00
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
}
}
2017-02-28 20:58:15 +13:00
// we DID have one prior, and now we don't
} else if (this.props.menu && !nextProps.menu){
$('body').removeClass('context-menu-open')
}
}
2017-02-27 08:44:02 +13:00
handleScroll(e){
if (this.props.menu){
this.props.uiActions.hideContextMenu()
}
2016-12-14 16:41:05 +13:00
}
2017-02-27 08:44:02 +13:00
handleClick(e){
// if we click outside of the context menu or context menu trigger, kill it
if ($(e.target).closest('.context-menu').length <= 0 && $(e.target).closest('.context-menu-trigger').length <= 0){
2017-02-27 08:44:02 +13:00
this.props.uiActions.hideContextMenu()
}
2016-12-14 16:41:05 +13:00
}
2017-03-13 05:39:12 +13:00
getContext(){
var context = {
name: null,
2017-03-13 05:39:12 +13:00
nice_name: 'Unknown'
}
if (this.props.menu && this.props.menu.context){
context.name = this.props.menu.context
2017-03-13 05:39:12 +13:00
context.nice_name = this.props.menu.context
2017-03-13 05:39:12 +13:00
// handle ugly labels
switch (this.props.menu.context){
case 'playlist':
case 'editable-playlist':
2017-03-13 05:39:12 +13:00
context.nice_name = 'playlist'
break
case 'track':
case 'queue-track':
case 'playlist-track':
case 'editable-playlist-track':
2017-03-13 05:39:12 +13:00
context.nice_name = 'track'
break
}
2017-03-13 05:39:12 +13:00
// 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
2017-03-13 13:17:11 +13:00
context.items_count = this.props.menu.items.length
2017-03-13 05:39:12 +13:00
context.source = helpers.uriSource(item.uri)
context.type = helpers.uriType(item.uri)
context.in_library = this.inLibrary(item)
}
}
return context
}
2017-03-13 05:39:12 +13:00
inLibrary(item = null){
if (!item){
2017-03-10 22:35:11 +13:00
return false
}
2017-03-13 05:39:12 +13:00
switch (helpers.uriType(item.uri)){
2017-03-10 22:35:11 +13:00
case 'artist':
2017-03-13 05:39:12 +13:00
return (this.props.library_artists && this.props.library_artists.indexOf(item.uri) > -1)
2017-03-10 22:35:11 +13:00
break
case 'album':
2017-03-13 05:39:12 +13:00
return (this.props.library_albums && this.props.library_albums.indexOf(item.uri) > -1)
2017-03-10 22:35:11 +13:00
break
case 'playlist':
2017-03-13 05:39:12 +13:00
return (this.props.library_playlists && this.props.library_playlists.indexOf(item.uri) > -1)
2017-03-10 22:35:11 +13:00
break
}
return false
}
canBeInLibrary(){
if (!this.props.spotify_authorized){
return false
}
return (helpers.uriSource(this.props.menu.items[0].uri) == 'spotify')
}
2017-03-10 22:35:11 +13:00
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;
this.props.mopidyActions.changeTrack( tracks[0].tlid )
}
removeFromQueue(){
this.props.uiActions.hideContextMenu()
var tracks = this.props.menu.items;
var tracks_tlids = [];
for( var i = 0; i < tracks.length; i++ ){
tracks_tlids.push( tracks[i].tlid );
}
this.props.mopidyActions.removeTracks( tracks_tlids );
}
playURIs(){
this.props.uiActions.hideContextMenu()
this.props.mopidyActions.playURIs(this.props.menu.uris, this.props.menu.tracklist_uri)
}
playURIsNext(){
this.props.uiActions.hideContextMenu()
this.props.mopidyActions.enqueueURIsNext(this.props.menu.uris, this.props.menu.tracklist_uri)
}
addToQueue(){
this.props.uiActions.hideContextMenu()
this.props.mopidyActions.enqueueURIs(this.props.menu.uris, this.props.menu.tracklist_uri)
}
addTracksToPlaylist(playlist_uri){
this.props.uiActions.hideContextMenu()
this.props.uiActions.addTracksToPlaylist(playlist_uri, this.props.menu.uris)
}
removeFromPlaylist(){
this.props.uiActions.hideContextMenu()
this.props.uiActions.removeTracksFromPlaylist(this.props.menu.tracklist_uri, this.props.menu.indexes)
2016-10-27 10:02:12 +13:00
}
startRadio(){
this.props.uiActions.hideContextMenu()
this.props.pusherActions.startRadio(this.props.menu.uris)
}
goToArtist(){
if (!this.props.menu.items || this.props.menu.items.length <= 0 || !this.props.menu.items[0].artists || this.props.menu.items[0].artists.length <= 0){
return null
} else {
this.props.uiActions.hideContextMenu()
hashHistory.push( global.baseURL +'artist/'+ this.props.menu.items[0].artists[0].uri )
}
}
2017-02-27 08:44:02 +13:00
goToUser(){
if (!this.props.menu.items || this.props.menu.items.length <= 0){
return null
} else {
this.props.uiActions.hideContextMenu()
hashHistory.push( global.baseURL +'user/'+ this.props.menu.items[0].owner.uri )
}
2017-02-27 08:44:02 +13:00
}
copyURIs(e){
var temp = $("<input>");
$("body").append(temp);
temp.val(this.props.menu.uris.join(',')).select();
document.execCommand("copy");
temp.remove();
this.props.uiActions.createNotification( "Copied "+this.props.menu.uris.length+" URIs" )
this.props.uiActions.hideContextMenu()
}
closeAndDeselectTracks(){
this.props.uiActions.hideContextMenu();
2017-03-13 05:39:12 +13:00
// TODO
}
2016-11-11 23:21:51 +13:00
renderPlaylistSubmenu(){
var playlists = []
2017-01-16 21:49:04 +13:00
for (var uri in this.props.playlists){
if (this.props.playlists[uri].can_edit) playlists.push( this.props.playlists[uri])
2016-11-11 23:21:51 +13:00
}
2017-01-16 21:49:04 +13:00
playlists = helpers.sortItems(playlists, 'name')
2016-11-11 23:21:51 +13:00
return (
2017-03-02 08:08:27 +13:00
<div className={this.state.submenu_expanded ? 'submenu expanded' : 'submenu'}>
2017-03-01 08:28:10 +13:00
<span className="menu-item-wrapper">
<a className="menu-item close-submenu" onClick={e => this.setState({submenu_expanded: false})}>
<span className="label">
2017-03-02 08:08:27 +13:00
<FontAwesome name='caret-left' />
&nbsp;&nbsp;
2017-03-02 08:13:04 +13:00
Back
2017-03-01 08:28:10 +13:00
</span>
</a>
</span>
2016-11-11 23:21:51 +13:00
{
playlists.map( playlist => {
return (
<span className="menu-item-wrapper" key={playlist.uri} >
<a className="menu-item" onClick={ () => this.addTracksToPlaylist(playlist.uri) }>
<span className="label">{ playlist.name }</span>
</a>
</span>
2016-11-11 23:21:51 +13:00
)
})
}
</div>
)
}
2017-01-27 16:13:11 +13:00
renderTitle(){
2017-03-13 05:39:12 +13:00
var context = this.getContext()
2017-03-13 13:17:11 +13:00
if (context.name == 'custom'){
return null
// TODO: make this the page title
}
2017-03-13 05:39:12 +13:00
switch (context.type){
case 'artist':
case 'album':
case 'playlist':
var style = null
2017-03-13 05:39:12 +13:00
if (context.item && context.item.images){
style = {
2017-03-13 05:39:12 +13:00
backgroundImage: 'url('+helpers.sizedImages(context.item.images).medium+')'
}
}
return (
2017-03-13 05:39:12 +13:00
<Link className="title" to={global.baseURL+context.type+'/'+context.item.uri}>
{style ? <div className="background" style={style}></div> : null}
<div className="type">
2017-03-13 05:39:12 +13:00
{context.source}
&nbsp;
2017-03-13 05:39:12 +13:00
{context.nice_name}
</div>
2017-03-13 05:39:12 +13:00
<div className="text">{context.item.name}</div>
</Link>
)
break
default:
return (
<span className="title">
<div className="type">
2017-03-13 05:39:12 +13:00
{context.source}
&nbsp;
2017-03-13 05:39:12 +13:00
{context.nice_name}s
</div>
<div className="text">
2017-03-13 13:17:11 +13:00
{context.items_count} items
</div>
</span>
)
break
2017-01-27 16:13:11 +13:00
}
}
renderItems(){
2017-03-13 05:39:12 +13:00
var context = this.getContext()
var play_uris = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.playURIs(e)}>
<span className="label">Play</span>
</a>
</span>
)
var play_queue_item = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.playQueueItem(e)}>
<span className="label">Play</span>
</a>
</span>
)
var play_uris_next = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.playURIsNext(e)}>
<span className="label">Play next</span>
</a>
</span>
)
var add_to_queue = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.addToQueue(e)}>
<span className="label">Add to queue</span>
</a>
</span>
)
var add_to_playlist = (
<span className="menu-item-wrapper has-submenu">
<a className="menu-item" onClick={e => this.setState({ submenu_expanded: !this.state.submenu_expanded })}>
<span className="label">Add to playlist</span>
<FontAwesome className="submenu-icon" name='caret-right' />
</a>
{this.renderPlaylistSubmenu()}
</span>
)
var toggle_in_library = (
<span className="menu-item-wrapper">
2017-03-13 05:39:12 +13:00
<a className="menu-item" onClick={e => this.toggleInLibrary(context.in_library)}>
<span className="label">
2017-03-13 05:39:12 +13:00
{context.in_library ? 'Remove from library' : 'Add to library'}
</span>
</a>
</span>
)
var go_to_artist = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.goToArtist(e)}>
<span className="label">Go to artist</span>
</a>
</span>
)
var go_to_user = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.goToUser(e)}>
<span className="label">Go to user</span>
</a>
</span>
)
var start_radio = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.startRadio(e)}>
2017-03-13 05:39:12 +13:00
<span className="label">Start {this.context.nice_name} radio</span>
</a>
</span>
)
var remove_from_queue = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.removeFromQueue(e)}>
<span className="label">Remove</span>
</a>
</span>
)
var remove_from_playlist = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.removeFromPlaylist(e)}>
<span className="label">Remove</span>
</a>
</span>
)
var copy_uris = (
<span className="menu-item-wrapper">
<a className="menu-item" onClick={e => this.copyURIs(e)}>
<span className="label">Copy URIs</span>
</a>
</span>
)
2017-03-13 05:39:12 +13:00
switch (context.name){
case 'album':
return (
<div>
{play_uris}
{play_uris_next}
{add_to_queue}
{this.canBeInLibrary() ? toggle_in_library : null}
{go_to_artist}
{copy_uris}
</div>
)
break
case 'artist':
return (
<div>
{start_radio}
{this.canBeInLibrary() ? toggle_in_library : null}
{copy_uris}
</div>
)
break
case 'playlist':
return (
<div>
{play_uris}
{this.canBeInLibrary() ? toggle_in_library : null}
2017-03-13 05:39:12 +13:00
{context.source == 'spotify' ? go_to_user : null}
{copy_uris}
</div>
)
break
case 'queue':
return (
<div>
{play_queue_item}
{add_to_playlist}
{copy_uris}
{remove_from_queue}
</div>
)
break
case 'editable-playlist-track':
return (
<div>
{play_uris}
{play_uris_next}
{add_to_queue}
{add_to_playlist}
{start_radio}
{copy_uris}
{remove_from_playlist}
</div>
)
break
default:
return (
<div>
{play_uris}
{play_uris_next}
{add_to_queue}
{add_to_playlist}
{start_radio}
{copy_uris}
</div>
)
break
}
2016-10-27 10:02:12 +13:00
}
render(){
if (!this.props.menu) return null;
2016-10-27 10:02:12 +13:00
var style = {
left: this.props.menu.position_x,
top: this.props.menu.position_y,
2016-10-27 10:02:12 +13:00
}
var height = 0 // TODO: use jquery to detect height
2017-03-01 08:28:10 +13:00
var className = "context-menu "+this.props.menu.context
if (this.state.submenu_expanded) className += ' submenu-expanded'
if (this.props.menu.position_x > (window.innerWidth - 154)) className += ' right-align'
if (this.props.menu.position_x > (window.innerWidth - 308)) className += ' right-align-submenu'
if (this.props.menu.position_y > (window.innerHeight - height)){
style.top = style.top - height
className += ' bottom-align-submenu'
}
2016-10-27 10:02:12 +13:00
return (
2017-03-06 09:03:20 +13:00
<div id="context-menu" className={className} style={style}>
2017-02-27 08:44:02 +13:00
<div className="liner">
{this.renderTitle()}
2017-03-06 09:03:20 +13:00
{this.props.menu.context == 'custom' ? this.props.menu.options : this.renderItems()}
2017-02-27 08:44:02 +13:00
</div>
<div className="background" onClick={e => this.props.uiActions.hideContextMenu()}></div>
2016-10-27 10:02:12 +13:00
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
return {
menu: state.ui.context_menu,
current_track: state.ui.current_track,
2016-11-11 23:21:51 +13:00
current_tracklist: state.ui.current_tracklist,
2017-03-10 22:35:11 +13:00
library_artists: state.ui.library_artists,
library_albums: state.ui.library_albums,
library_playlists: state.ui.library_playlists,
playlists: state.ui.playlists,
spotify_authorized: state.spotify.authorized
}
2016-10-27 10:02:12 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch),
pusherActions: bindActionCreators(pusherActions, dispatch),
2016-10-27 10:02:12 +13:00
spotifyActions: bindActionCreators(spotifyActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(ContextMenu)