import React, { PropTypes } from 'react' import { connect } from 'react-redux' import { Link } from 'react-router' import { bindActionCreators } from 'redux' import ProgressSlider from './Fields/ProgressSlider' import VolumeControl from './Fields/VolumeControl' import SnapcastVolumeControl from './Fields/SnapcastVolumeControl' import Dater from './Dater' import ArtistSentence from './ArtistSentence' import Thumbnail from './Thumbnail' import Icon from './Icon' import * as helpers from '../helpers' import * as uiActions from '../services/ui/actions' import * as coreActions from '../services/core/actions' import * as mopidyActions from '../services/mopidy/actions' class PlaybackControls extends React.Component{ constructor(props){ super(props) this.stream = null; this.state = { expanded: false } } componentDidMount(){ if (this.props.http_streaming_enabled){ this.playStream(); } } playStream(props = this.props){ if (!this.stream){ this.stream = new Audio(); } else { this.stream.src = null; } if (!props.current_track || !props.http_streaming_enabled || !props.http_streaming_url){ return false; } var url = props.http_streaming_url+"?cache_buster="+props.current_track.uri; console.log("Playing stream: "+url); this.stream.src = url; this.stream.play(); } componentWillReceiveProps(nextProps){ if (!this.props.current_track && nextProps.current_track || this.props.current_track && nextProps.current_track && this.props.current_track.uri !== nextProps.current_track.uri){ this.playStream(nextProps); } } renderPlayButton(){ var button = this.props.mopidyActions.play()}> if (this.props.play_state == 'playing'){ button = this.props.mopidyActions.pause()}> } return button; } renderConsumeButton(){ var button = this.props.mopidyActions.setConsume(true)}>Consume if (this.props.consume){ button = this.props.mopidyActions.setConsume(false)}>Consume } return button; } renderRandomButton(){ var button = this.props.mopidyActions.setRandom(true)}>Shuffle if (this.props.random){ button = this.props.mopidyActions.setRandom(false)}>Shuffle } return button; } renderRepeatButton(){ var button = this.props.mopidyActions.setRepeat(true)}>Repeat if (this.props.repeat){ button = this.props.mopidyActions.setRepeat(false)}>Repeat } return button; } handleThumbnailClick(e){ e.preventDefault(); this.props.uiActions.openModal('kiosk_mode'); } render(){ var images = false if (this.props.current_track && this.props.current_track.images){ images = this.props.current_track.images } return (
{this.props.next_track && this.props.next_track.images ? : null}
this.handleThumbnailClick(e)}>
{ this.props.current_track ? this.props.current_track.name : - }
{ this.props.current_track ? : }
this.props.mopidyActions.previous()}> { this.renderPlayButton() } this.props.mopidyActions.stop()}> this.props.mopidyActions.next()}>
{this.renderConsumeButton()} {this.renderRandomButton()} {this.renderRepeatButton()} {this.props.snapcast_enabled ? : null}
{ this.props.time_position ? : '-' } { this.props.current_track ? : '-' }
this.props.mopidyActions.setVolume(percent)} onMuteChange={mute => this.props.mopidyActions.setMute(mute)} />
this.setState({expanded: !this.state.expanded})}> {this.state.expanded ? : } this.props.uiActions.toggleSidebar()}>
); } } /** * Export our component * * We also integrate our global store, using connect() **/ const mapStateToProps = (state, ownProps) => { return { snapcast_enabled: state.pusher.config.snapcast_enabled, http_streaming_enabled: state.core.http_streaming_enabled, http_streaming_encoding: state.core.http_streaming_encoding, http_streaming_url: state.core.http_streaming_url, current_track: (state.core.current_track && state.core.tracks[state.core.current_track.uri] !== undefined ? state.core.tracks[state.core.current_track.uri] : null), next_track: (state.core.next_track_uri && state.core.tracks[state.core.next_track_uri] !== undefined ? state.core.tracks[state.core.next_track_uri] : null), radio_enabled: (state.ui.radio && state.ui.radio.enabled ? true : false), play_state: state.mopidy.play_state, time_position: state.mopidy.time_position, consume: state.mopidy.consume, repeat: state.mopidy.repeat, random: state.mopidy.random, volume: state.mopidy.volume, mute: state.mopidy.mute, sidebar_open: state.ui.sidebar_open } } const mapDispatchToProps = (dispatch) => { return { coreActions: bindActionCreators(coreActions, dispatch), uiActions: bindActionCreators(uiActions, dispatch), mopidyActions: bindActionCreators(mopidyActions, dispatch) } } export default connect(mapStateToProps, mapDispatchToProps)(PlaybackControls)