import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import VolumeControl from './VolumeControl'
import Icon from '../Icon'
import * as helpers from '../../helpers'
import * as coreActions from '../../services/core/actions'
import * as pusherActions from '../../services/pusher/actions'
class OutputControl extends React.Component{
constructor(props){
super(props);
this.state = {
expanded: false
}
this.handleClick = this.handleClick.bind(this);
}
handleClick(e){
if ($(e.target).closest('.output-control').length <= 0){
this.setExpanded(false);
}
}
setExpanded(expanded = !this.state.expanded){
if (expanded){
this.setState({expanded: expanded});
window.addEventListener("click", this.handleClick, false);
// Re-check our snapcast clients
// TODO: Once we have push events, remove this as it'll (marginally)
// slow down the reveal/render
if (this.props.pusher_connected && this.props.snapcast_enabled){
this.props.pusherActions.getSnapcast();
}
} else {
this.setState({expanded: expanded});
window.removeEventListener("click", this.handleClick, false);
}
}
renderOutputs(){
var clients = [];
for (var key in this.props.snapcast_clients){
if (this.props.snapcast_clients.hasOwnProperty(key)){
var client = this.props.snapcast_clients[key];
if (client.connected){
clients.push(client);
}
}
}
return (
{this.props.http_streaming_enabled ?
this.props.coreActions.cachebustHttpStream()}>
Local browser
this.props.coreActions.set({http_streaming_volume: percent})}
onMuteChange={mute => this.props.coreActions.set({http_streaming_mute: mute})}
/>
: null}
{
clients.map(client => {
var name = client.config.name ? client.config.name : client.host.name;
return (
{name}
this.props.pusherActions.setSnapcastClientVolume(client.id, percent)}
onMuteChange={mute => this.props.pusherActions.setSnapcastClientMute(client.id, mute)}
/>
)
})
}
);
}
render(){
if (this.state.expanded){
return (
this.setExpanded()}>
{this.renderOutputs()}
);
} else {
// No customisable outputs
if (!this.props.http_streaming_enabled && !this.props.snapcast_enabled){
return (
);
} else {
return (
this.setExpanded()}>
);
}
}
}
}
const mapStateToProps = (state, ownProps) => {
return {
http_streaming_enabled: state.core.http_streaming_enabled,
http_streaming_volume: state.core.http_streaming_volume,
http_streaming_mute: state.core.http_streaming_mute,
snapcast_enabled: (state.pusher.config ? state.pusher.config.snapcast_enabled : null),
pusher_connected: state.pusher.connected,
snapcast_clients: state.pusher.snapcast_clients
}
}
const mapDispatchToProps = (dispatch) => {
return {
coreActions: bindActionCreators(coreActions, dispatch),
pusherActions: bindActionCreators(pusherActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(OutputControl)