2016-10-27 10:02:12 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
2017-01-29 11:56:58 +13:00
|
|
|
import { Link } 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'
|
2016-12-09 10:06:00 +13:00
|
|
|
import * as helpers from '../helpers'
|
2016-10-27 10:02:12 +13:00
|
|
|
import * as uiActions from '../services/ui/actions'
|
2017-01-29 11:56:58 +13:00
|
|
|
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) {
|
|
|
|
|
super(props);
|
2016-12-14 16:41:05 +13:00
|
|
|
this.handleScroll = this.handleScroll.bind(this)
|
|
|
|
|
this.handleClick = this.handleClick.bind(this)
|
2016-11-13 14:41:08 +13:00
|
|
|
}
|
2016-12-14 16:41:05 +13:00
|
|
|
|
2016-11-13 14:41:08 +13:00
|
|
|
componentDidMount(){
|
2016-12-14 16:41:05 +13:00
|
|
|
window.addEventListener("scroll", this.handleScroll, false)
|
|
|
|
|
window.addEventListener("click", this.handleClick, false)
|
2016-11-13 14:41:08 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(){
|
2016-12-14 16:41:05 +13:00
|
|
|
window.removeEventListener("scroll", this.handleScroll, false)
|
|
|
|
|
window.removeEventListener("click", this.handleClick, false)
|
2016-11-13 14:41:08 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleScroll(){
|
2017-01-29 16:25:05 +13:00
|
|
|
if (this.props.menu) this.props.uiActions.hideContextMenu()
|
2016-12-14 16:41:05 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleClick(){
|
2017-01-29 16:25:05 +13:00
|
|
|
if (this.props.menu) this.props.uiActions.hideContextMenu()
|
2016-12-14 16:41:05 +13:00
|
|
|
}
|
2016-11-13 14:41:08 +13:00
|
|
|
|
|
|
|
|
playQueueItem(){
|
2017-01-29 16:25:05 +13:00
|
|
|
var tracks = this.props.menu.items;
|
2017-01-27 09:55:19 +13:00
|
|
|
this.props.mopidyActions.changeTrack( tracks[0].tlid );
|
2016-11-13 14:41:08 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeFromQueue(){
|
2017-01-29 16:25:05 +13:00
|
|
|
var tracks = this.props.menu.items;
|
2017-01-27 09:55:19 +13:00
|
|
|
var tracks_tlids = [];
|
|
|
|
|
for( var i = 0; i < tracks.length; i++ ){
|
|
|
|
|
tracks_tlids.push( tracks[i].tlid );
|
2016-11-13 14:41:08 +13:00
|
|
|
}
|
2017-01-27 09:55:19 +13:00
|
|
|
this.props.mopidyActions.removeTracks( tracks_tlids );
|
2016-11-13 14:41:08 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 09:55:19 +13:00
|
|
|
playURIs(){
|
2017-02-14 12:13:07 +13:00
|
|
|
this.props.mopidyActions.playURIs(this.props.menu.uris, this.props.menu.tracklist_uri);
|
2016-11-13 14:41:08 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 09:55:19 +13:00
|
|
|
playURIsNext(){
|
2017-02-14 12:13:07 +13:00
|
|
|
this.props.mopidyActions.enqueueURIsNext(this.props.menu.uris, this.props.menu.tracklist_uri);
|
2016-11-13 14:41:08 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 10:06:00 +13:00
|
|
|
addToPlaylist(){
|
2017-01-29 16:25:05 +13:00
|
|
|
this.props.uiActions.openModal('add_to_playlist', { tracks_uris: this.props.menu.uris })
|
2016-12-09 10:06:00 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 14:41:08 +13:00
|
|
|
addToQueue(){
|
2017-02-14 12:13:07 +13:00
|
|
|
this.props.mopidyActions.enqueueURIs(this.props.menu.uris, this.props.menu.tracklist_uri)
|
2017-01-27 09:55:19 +13:00
|
|
|
this.props.uiActions.hideContextMenu()
|
2016-11-13 14:41:08 +13:00
|
|
|
}
|
|
|
|
|
|
2017-01-29 16:25:05 +13:00
|
|
|
addTracksToPlaylist(playlist_uri){
|
|
|
|
|
this.props.uiActions.addTracksToPlaylist(playlist_uri, this.props.menu.uris)
|
2016-12-09 10:06:00 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 14:41:08 +13:00
|
|
|
removeFromPlaylist(){
|
2017-01-29 16:25:05 +13:00
|
|
|
this.props.uiActions.removeTracksFromPlaylist(this.props.menu.tracklist_uri, this.props.menu.indexes)
|
2016-11-13 14:41:08 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
2016-10-27 10:02:12 +13:00
|
|
|
}
|
|
|
|
|
|
2017-01-29 11:56:58 +13:00
|
|
|
startRadio(){
|
2017-01-29 16:25:05 +13:00
|
|
|
this.props.pusherActions.startRadio(this.props.menu.uris)
|
2017-01-29 11:56:58 +13:00
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 09:55:19 +13:00
|
|
|
copyURIs(e){
|
|
|
|
|
var temp = $("<input>");
|
|
|
|
|
$("body").append(temp);
|
2017-01-29 16:25:05 +13:00
|
|
|
temp.val(this.props.menu.uris.join(',')).select();
|
2017-01-27 09:55:19 +13:00
|
|
|
document.execCommand("copy");
|
|
|
|
|
temp.remove();
|
|
|
|
|
|
2017-01-29 16:25:05 +13:00
|
|
|
this.props.uiActions.createNotification( "Copied "+this.props.menu.uris.length+" URIs" )
|
2016-11-19 08:44:25 +13:00
|
|
|
this.props.uiActions.hideContextMenu()
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 07:56:42 +13:00
|
|
|
closeAndDeselectTracks(){
|
|
|
|
|
this.props.uiActions.hideContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
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 (
|
|
|
|
|
<div className="submenu">
|
|
|
|
|
{
|
|
|
|
|
playlists.map( playlist => {
|
|
|
|
|
return (
|
2016-12-09 10:06:00 +13:00
|
|
|
<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-29 11:56:58 +13:00
|
|
|
getItems(){
|
2017-01-29 16:25:05 +13:00
|
|
|
switch (this.props.menu.context) {
|
2016-10-27 10:02:12 +13:00
|
|
|
|
2017-01-29 16:25:05 +13:00
|
|
|
case 'album':
|
2016-12-21 10:22:43 +13:00
|
|
|
var items = [
|
2017-01-29 16:25:05 +13:00
|
|
|
{ handleClick: 'playURIs', label: 'Play' },
|
|
|
|
|
{ handleClick: 'playURIsNext', label: 'Play next' },
|
|
|
|
|
{ handleClick: 'addToQueue', label: 'Add to queue' },
|
2017-02-02 09:06:15 +13:00
|
|
|
// { handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO
|
2017-01-29 16:25:05 +13:00
|
|
|
{ handleClick: 'copyURIs', label: 'Copy URI' }
|
2016-12-20 07:56:42 +13:00
|
|
|
]
|
|
|
|
|
break
|
2016-10-27 10:02:12 +13:00
|
|
|
|
2017-01-29 16:25:05 +13:00
|
|
|
case 'artist':
|
2017-01-27 09:55:19 +13:00
|
|
|
var items = [
|
2017-02-02 09:06:15 +13:00
|
|
|
//{ handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO
|
2017-01-29 16:25:05 +13:00
|
|
|
{ handleClick: 'startRadio', label: 'Start radio' },
|
|
|
|
|
{ handleClick: 'copyURIs', label: 'Copy URI' }
|
2017-01-27 13:24:36 +13:00
|
|
|
]
|
|
|
|
|
break
|
|
|
|
|
|
2017-01-29 16:25:05 +13:00
|
|
|
case 'playlist':
|
2017-01-27 13:24:36 +13:00
|
|
|
var items = [
|
2017-01-29 16:25:05 +13:00
|
|
|
{ handleClick: 'playURIs', label: 'Play' },
|
2017-02-02 09:06:15 +13:00
|
|
|
// { handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO
|
2017-01-29 16:25:05 +13:00
|
|
|
{ handleClick: 'copyURIs', label: 'Copy URI' }
|
2017-01-27 09:55:19 +13:00
|
|
|
]
|
|
|
|
|
break
|
|
|
|
|
|
2017-02-14 20:24:35 +13:00
|
|
|
case 'queue':
|
2017-01-27 16:13:11 +13:00
|
|
|
var items = [
|
2017-01-29 16:25:05 +13:00
|
|
|
{ handleClick: 'playQueueItem', label: 'Play' },
|
|
|
|
|
{ handleClick: 'addToPlaylist', label: 'Add to playlist' },
|
|
|
|
|
{ handleClick: 'copyURIs', label: 'Copy URIs' },
|
|
|
|
|
{ handleClick: 'removeFromQueue', label: 'Remove' }
|
2017-01-27 16:13:11 +13:00
|
|
|
]
|
|
|
|
|
break
|
|
|
|
|
|
2017-01-29 11:56:58 +13:00
|
|
|
case 'editable-playlist-track':
|
|
|
|
|
var items = [
|
2017-01-29 16:25:05 +13:00
|
|
|
{ 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' }
|
2017-01-29 11:56:58 +13:00
|
|
|
]
|
|
|
|
|
break
|
|
|
|
|
|
2016-10-27 10:02:12 +13:00
|
|
|
default:
|
2016-12-21 10:22:43 +13:00
|
|
|
var items = [
|
2017-01-29 16:25:05 +13:00
|
|
|
{ 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' }
|
2016-12-20 07:56:42 +13:00
|
|
|
]
|
|
|
|
|
break
|
2016-10-27 10:02:12 +13:00
|
|
|
}
|
|
|
|
|
|
2016-12-21 10:22:43 +13:00
|
|
|
return items
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 16:13:11 +13:00
|
|
|
renderTitle(){
|
2017-01-29 16:25:05 +13:00
|
|
|
var item = this.props.menu.item
|
2017-01-29 11:56:58 +13:00
|
|
|
var style = null
|
2017-01-27 16:13:11 +13:00
|
|
|
if (item && item.images){
|
2017-01-29 11:56:58 +13:00
|
|
|
style = {
|
|
|
|
|
backgroundImage: 'url('+helpers.sizedImages(item.images).medium+')'
|
|
|
|
|
}
|
2017-01-27 16:13:11 +13:00
|
|
|
}
|
|
|
|
|
return (
|
2017-01-29 11:56:58 +13:00
|
|
|
<Link className="title" to={global.baseURL+helpers.uriType(item.uri)+'/'+item.uri}>
|
|
|
|
|
{style ? <div className="background" style={style}></div> : null}
|
|
|
|
|
<div className="text">{item.name}</div>
|
|
|
|
|
</Link>
|
2017-01-27 16:13:11 +13:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 11:56:58 +13:00
|
|
|
renderItems(){
|
|
|
|
|
var items = this.getItems()
|
2016-12-20 07:56:42 +13:00
|
|
|
|
2016-10-27 10:02:12 +13:00
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{
|
|
|
|
|
items.map((item, index) => {
|
2017-01-29 16:25:05 +13:00
|
|
|
if (item.handleClick == 'addToPlaylist'){
|
2016-11-11 23:21:51 +13:00
|
|
|
return (
|
2016-12-09 10:06:00 +13:00
|
|
|
<span key={item.handleClick} className="menu-item-wrapper has-submenu">
|
2017-01-29 11:56:58 +13:00
|
|
|
<a className="menu-item" onClick={e => this[item.handleClick](e)}>
|
2016-12-09 10:06:00 +13:00
|
|
|
<span className="label">{ item.label }</span>
|
|
|
|
|
<FontAwesome className="submenu-icon" name="caret-right" />
|
|
|
|
|
</a>
|
2016-11-11 23:21:51 +13:00
|
|
|
{ this.renderPlaylistSubmenu() }
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
}else{
|
2016-12-09 10:06:00 +13:00
|
|
|
return (
|
|
|
|
|
<span className="menu-item-wrapper" key={item.handleClick}>
|
2017-01-29 11:56:58 +13:00
|
|
|
<a className="menu-item" onClick={e => this[item.handleClick](e)}>
|
2016-12-09 10:06:00 +13:00
|
|
|
<span className="label">{ item.label }</span>
|
|
|
|
|
</a>
|
|
|
|
|
</span>
|
|
|
|
|
)
|
2016-11-11 23:21:51 +13:00
|
|
|
}
|
2016-10-27 10:02:12 +13:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</div>
|
2016-12-20 07:56:42 +13:00
|
|
|
)
|
2016-10-27 10:02:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render(){
|
2017-01-29 16:25:05 +13:00
|
|
|
if (!this.props.menu) return null;
|
2016-10-27 10:02:12 +13:00
|
|
|
|
|
|
|
|
var style = {
|
2017-01-29 16:25:05 +13:00
|
|
|
left: this.props.menu.position_x,
|
|
|
|
|
top: this.props.menu.position_y,
|
2016-10-27 10:02:12 +13:00
|
|
|
}
|
|
|
|
|
|
2017-01-29 16:25:05 +13:00
|
|
|
var items = this.getItems()
|
2016-12-21 10:22:43 +13:00
|
|
|
var height = 0
|
2016-12-21 15:24:07 +13:00
|
|
|
if (items) height = items.length * 34 // this is an approximation of how tall each menu item is
|
2016-12-21 10:22:43 +13:00
|
|
|
|
2017-01-29 16:34:17 +13:00
|
|
|
var className = "context-menu"
|
2017-01-29 16:25:05 +13:00
|
|
|
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)){
|
2016-12-21 10:22:43 +13:00
|
|
|
style.top = style.top - height
|
|
|
|
|
className += ' bottom-align-submenu'
|
|
|
|
|
}
|
2016-12-20 13:41:58 +13:00
|
|
|
|
2016-10-27 10:02:12 +13:00
|
|
|
return (
|
2017-01-29 16:34:17 +13:00
|
|
|
<div className={className} style={style}>
|
2017-01-29 16:25:05 +13:00
|
|
|
{this.props.menu.item ? this.renderTitle() : null}
|
|
|
|
|
{this.renderItems()}
|
2016-10-27 10:02:12 +13:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2016-11-09 13:18:03 +13:00
|
|
|
return {
|
2017-01-29 16:25:05 +13:00
|
|
|
menu: state.ui.context_menu,
|
2016-11-09 13:18:03 +13:00
|
|
|
current_track: state.ui.current_track,
|
2016-11-11 23:21:51 +13:00
|
|
|
current_tracklist: state.ui.current_tracklist,
|
|
|
|
|
playlists: state.ui.playlists
|
2016-11-09 13:18:03 +13:00
|
|
|
}
|
2016-10-27 10:02:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
|
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
2017-01-29 11:56:58 +13:00
|
|
|
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)
|