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

306 lines
9.3 KiB
JavaScript
Raw Normal View History

2016-10-27 10:02:12 +13:00
import React, { PropTypes } from 'react'
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 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(){
2016-12-14 16:41:05 +13:00
if( this.props.context_menu.show && this.props.context_menu.trigger == 'click' ){
this.props.uiActions.hideContextMenu();
}
}
handleClick(){
if( this.props.context_menu.show && this.props.context_menu.trigger == 'click' ){
this.props.uiActions.hideContextMenu();
}
2016-12-14 16:41:05 +13:00
}
playQueueItem(){
var tracks = this.props.context_menu.data.items;
this.props.mopidyActions.changeTrack( tracks[0].tlid );
this.props.uiActions.hideContextMenu();
}
removeFromQueue(){
var tracks = this.props.context_menu.data.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(){
this.props.mopidyActions.playURIs(this.props.context_menu.data.uris);
this.props.uiActions.hideContextMenu();
}
playURIsNext(){
this.props.mopidyActions.enqueueURIsNext(this.props.context_menu.data.uris);
this.props.uiActions.hideContextMenu();
}
addToPlaylist(){
this.props.uiActions.openModal( 'add_to_playlist', { tracks_uris: this.props.context_menu.data.uris } )
this.props.uiActions.hideContextMenu();
}
addToQueue(){
this.props.mopidyActions.enqueueURIs(this.props.context_menu.data.uris)
this.props.uiActions.hideContextMenu()
}
addTracksToPlaylist( playlist_uri ){
this.props.uiActions.addTracksToPlaylist( playlist_uri, this.props.context_menu.data.uris )
this.props.uiActions.hideContextMenu();
}
removeFromPlaylist(){
this.props.uiActions.removeTracksFromPlaylist( this.props.playlist.uri, this.props.context_menu.data.indexes )
this.props.uiActions.hideContextMenu();
2016-10-27 10:02:12 +13:00
}
copyURIs(e){
var temp = $("<input>");
$("body").append(temp);
temp.val(this.props.context_menu.data.uris.join(',')).select();
document.execCommand("copy");
temp.remove();
this.props.uiActions.createNotification( "Copied "+this.props.context_menu.data.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(trigger){
switch (this.props.context_menu.context) {
2016-10-27 10:02:12 +13:00
case 'queue':
var items = [
{ handleClick: 'playQueueItem', label: 'Play', icon: 'play' },
{ handleClick: 'addToPlaylist', label: 'Add to playlist', icon: 'plus', playlists: true },
{ handleClick: 'copyURIs', label: 'Copy URIs', icon: 'copy' },
{ handleClick: 'removeFromQueue', label: 'Remove', icon: 'trash' }
]
break
2016-10-27 10:02:12 +13:00
case 'editable-playlist':
var items = [
{ handleClick: 'playURIs', label: 'Play', icon: 'play' },
{ handleClick: 'playURIsNext', label: 'Play next', icon: 'play' },
{ handleClick: 'addToQueue', label: 'Add to queue', icon: 'plus' },
{ handleClick: 'addToPlaylist', label: 'Add to playlist', icon: 'plus', playlists: true },
{ handleClick: 'copyURIs', label: 'Copy URIs', icon: 'copy' },
{ handleClick: 'removeFromPlaylist', label: 'Remove', icon: 'trash' }
]
break
2016-10-27 10:02:12 +13:00
case 'album':
var items = [
{ handleClick: 'playURIs', label: 'Play', icon: 'play' },
{ handleClick: 'playURIsNext', label: 'Play next', icon: 'play' },
{ handleClick: 'addToQueue', label: 'Add to queue', icon: 'plus' },
{ handleClick: 'copyURIs', label: 'Copy URI', icon: 'copy' }
]
break
case 'artist':
var items = [
{ handleClick: 'startRadio', label: 'Start radio', icon: 'spotify' },
{ handleClick: 'toggleFollow', label: 'Follow/unfollow', icon: 'toggle-on' },
{ handleClick: 'copyURIs', label: 'Copy URI', icon: 'copy' }
]
break
2017-01-27 16:13:11 +13:00
case 'playlist':
var items = [
{ handleClick: 'playURIs', label: 'Play', icon: 'play' },
{ handleClick: 'toggleFollow', label: 'Follow/unfollow', icon: 'toggle-on' },
{ handleClick: 'copyURIs', label: 'Copy URI', icon: 'copy' }
]
break
2016-10-27 10:02:12 +13:00
default:
var items = [
{ handleClick: 'playURIs', label: 'Play', icon: 'play' },
{ handleClick: 'playURIsNext', label: 'Play next', icon: 'play' },
{ handleClick: 'addToQueue', label: 'Add to queue', icon: 'plus' },
{ handleClick: 'addToPlaylist', label: 'Add to playlist', icon: 'plus', playlists: true },
{ handleClick: 'copyURIs', label: 'Copy URIs', icon: 'copy' }
]
break
2016-10-27 10:02:12 +13:00
}
return items
}
2017-01-27 16:13:11 +13:00
renderTitle(){
var style = {}
var item = this.props.context_menu.data.item
var image = null
if (item && item.images){
image = helpers.sizedImages(item.images).medium
}
return (
<div className="title" style={style}>
{image ? <img src={image} /> : null}
{this.props.context_menu.data.item.name}
</div>
)
}
renderItems(trigger){
var items = this.getItems(trigger)
var closeItem = (
<span className="menu-item-wrapper cancel">
<a className="menu-item" onClick={ (e) => this.closeAndDeselectTracks(e) }>
<FontAwesome className="icon" fixedWidth name='close' />
<span className="label">Cancel</span>
</a>
</span>
)
2016-10-27 10:02:12 +13:00
return (
<div>
{
items.map((item, index) => {
2016-11-11 23:21:51 +13:00
if( item.playlists ){
return (
<span key={item.handleClick} className="menu-item-wrapper has-submenu">
<a className="menu-item" onClick={ (e) => this[item.handleClick](e) }>
<FontAwesome className="icon" fixedWidth name={item.icon} />
<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) }>
<FontAwesome className="icon" fixedWidth name={item.icon} />
<span className="label">{ item.label }</span>
</a>
</span>
)
2016-11-11 23:21:51 +13:00
}
2016-10-27 10:02:12 +13:00
})
}
{ trigger == 'touch' ? closeItem : null }
2016-10-27 10:02:12 +13:00
</div>
)
2016-10-27 10:02:12 +13:00
}
render(){
if( !this.props.context_menu.show ) return null;
2016-10-27 10:02:12 +13:00
var style = {
left: this.props.context_menu.position_x,
top: this.props.context_menu.position_y,
2016-10-27 10:02:12 +13:00
}
var className = 'context-menu'
if (this.props.emulate_touch) {
className += ' touch'
2016-12-22 08:21:19 +13:00
var trigger = 'touch'
} else {
className += ' '+this.props.context_menu.trigger
2016-12-22 08:21:19 +13:00
var trigger = this.props.context_menu.trigger
}
var items = this.getItems(trigger)
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
if (this.props.context_menu.position_x > (window.innerWidth - 154)) className += ' right-align'
if (this.props.context_menu.position_x > (window.innerWidth - 308)) className += ' right-align-submenu'
if (this.props.context_menu.position_y > (window.innerHeight - height)){
style.top = style.top - height
className += ' bottom-align-submenu'
}
2016-10-27 10:02:12 +13:00
return (
<div className={className} style={style}>
2017-01-27 16:13:11 +13:00
{this.renderTitle()}
{this.renderItems(trigger)}
2016-10-27 10:02:12 +13:00
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
return {
emulate_touch: state.ui.emulate_touch,
context_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,
playlist: state.ui.playlist,
2016-11-11 23:21:51 +13:00
playlists: state.ui.playlists
}
2016-10-27 10:02:12 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(ContextMenu)