import React, { PropTypes } from 'react' import { Link, hashHistory } from 'react-router' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import FontAwesome from 'react-fontawesome' import TrackList from './TrackList' import * as helpers from '../helpers' import * as uiActions from '../services/ui/actions' import * as pusherActions from '../services/pusher/actions' import * as mopidyActions from '../services/mopidy/actions' import * as spotifyActions from '../services/spotify/actions' class ContextMenu extends React.Component{ constructor(props) { super(props) this.state = { submenu_expanded: false } this.handleScroll = this.handleScroll.bind(this) this.handleClick = this.handleClick.bind(this) } componentDidMount(){ window.addEventListener("scroll", this.handleScroll, false) window.addEventListener("click", this.handleClick, false) } componentWillUnmount(){ window.removeEventListener("scroll", this.handleScroll, false) window.removeEventListener("click", this.handleClick, false) } 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') // we DID have one prior, and now we don't } else if (this.props.menu && !nextProps.menu){ $('body').removeClass('context-menu-open') } } handleScroll(e){ if (this.props.menu){ this.props.uiActions.hideContextMenu() } } handleClick(e){ // if we click outside of the context menu, kill it if ($(e.target).closest('.context-menu').length <= 0){ this.props.uiActions.hideContextMenu() } } 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) } 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) } 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) } 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 ) } } 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 ) } } copyURIs(e){ var temp = $(""); $("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(); } renderPlaylistSubmenu(){ var playlists = [] for (var uri in this.props.playlists){ if (this.props.playlists[uri].can_edit) playlists.push( this.props.playlists[uri]) } playlists = helpers.sortItems(playlists, 'name') return (