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

292 lines
8.4 KiB
JavaScript
Raw Normal View History

2016-10-27 10:02:12 +13:00
import React, { PropTypes } from 'react'
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'
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) {
super(props);
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
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)
}
handleScroll(){
if (this.props.menu) this.props.uiActions.hideContextMenu()
2016-12-14 16:41:05 +13:00
}
handleClick(){
if (this.props.menu) this.props.uiActions.hideContextMenu()
2016-12-14 16:41:05 +13:00
}
playQueueItem(){
var tracks = this.props.menu.items;
this.props.mopidyActions.changeTrack( tracks[0].tlid );
this.props.uiActions.hideContextMenu();
}
removeFromQueue(){
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 );
this.props.uiActions.hideContextMenu();
}
playURIs(){
2017-02-14 12:13:07 +13:00
this.props.mopidyActions.playURIs(this.props.menu.uris, this.props.menu.tracklist_uri);
this.props.uiActions.hideContextMenu();
}
playURIsNext(){
2017-02-14 12:13:07 +13:00
this.props.mopidyActions.enqueueURIsNext(this.props.menu.uris, this.props.menu.tracklist_uri);
this.props.uiActions.hideContextMenu();
}
addToPlaylist(){
this.props.uiActions.openModal('add_to_playlist', { tracks_uris: this.props.menu.uris })
this.props.uiActions.hideContextMenu();
}
addToQueue(){
2017-02-14 12:13:07 +13:00
this.props.mopidyActions.enqueueURIs(this.props.menu.uris, this.props.menu.tracklist_uri)
this.props.uiActions.hideContextMenu()
}
addTracksToPlaylist(playlist_uri){
this.props.uiActions.addTracksToPlaylist(playlist_uri, this.props.menu.uris)
this.props.uiActions.hideContextMenu();
}
removeFromPlaylist(){
this.props.uiActions.removeTracksFromPlaylist(this.props.menu.tracklist_uri, this.props.menu.indexes)
this.props.uiActions.hideContextMenu();
2016-10-27 10:02:12 +13:00
}
startRadio(){
this.props.pusherActions.startRadio(this.props.menu.uris)
this.props.uiActions.hideContextMenu();
}
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();
}
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 (
<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>
)
}
getItems(){
switch (this.props.menu.context) {
2016-10-27 10:02:12 +13:00
case 'album':
var items = [
{ handleClick: 'playURIs', label: 'Play' },
{ handleClick: 'playURIsNext', label: 'Play next' },
{ handleClick: 'addToQueue', label: 'Add to queue' },
// { handleClick: 'toggleFollow', label: 'Follow/unfollow' }, TODO
{ handleClick: 'copyURIs', label: 'Copy URI' }
]
break
2016-10-27 10:02:12 +13:00
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: 'toggleFollow', label: 'Follow/unfollow' }, TODO
{ handleClick: 'copyURIs', label: 'Copy URI' }
]
break
case 'queue':
2017-01-27 16:13:11 +13:00
var items = [
{ 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
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
2016-10-27 10:02:12 +13:00
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
2016-10-27 10:02:12 +13:00
}
return items
}
2017-01-27 16:13:11 +13:00
renderTitle(){
var item = this.props.menu.item
var style = null
2017-01-27 16:13:11 +13:00
if (item && item.images){
style = {
backgroundImage: 'url('+helpers.sizedImages(item.images).medium+')'
}
2017-01-27 16:13:11 +13:00
}
return (
<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
)
}
renderItems(){
var items = this.getItems()
2016-10-27 10:02:12 +13:00
return (
<div>
{
items.map((item, index) => {
if (item.handleClick == 'addToPlaylist'){
2016-11-11 23:21:51 +13:00
return (
<span key={item.handleClick} className="menu-item-wrapper has-submenu">
<a className="menu-item" onClick={e => this[item.handleClick](e)}>
<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{
return (
<span className="menu-item-wrapper" key={item.handleClick}>
<a className="menu-item" onClick={e => this[item.handleClick](e)}>
<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-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 items = this.getItems()
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
2017-01-29 16:34:17 +13:00
var className = "context-menu"
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-01-29 16:34:17 +13:00
<div className={className} style={style}>
{this.props.menu.item ? this.renderTitle() : null}
{this.renderItems()}
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,
playlists: state.ui.playlists
}
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)