Files
Iris/src/js/views/App.js

173 lines
4.7 KiB
JavaScript
Raw Normal View History

2016-10-03 14:44:27 +13:00
import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { createStore, bindActionCreators } from 'redux'
import { hashHistory, Link } from 'react-router'
2016-10-03 14:44:27 +13:00
import { connect } from 'react-redux'
import Sidebar from '../components/Sidebar'
import MiniPlayer from '../components/MiniPlayer'
2016-11-21 21:46:53 +13:00
import SidebarToggleButton from '../components/SidebarToggleButton'
2016-10-27 10:02:12 +13:00
import ContextMenu from '../components/ContextMenu'
import TouchContextMenu from '../components/TouchContextMenu'
2016-11-10 16:17:10 +13:00
import Dragger from '../components/Dragger'
2016-11-14 17:32:36 +13:00
import Modal from '../components/Modal/Modal'
2016-12-13 11:08:13 +13:00
import Notifications from '../components/Notifications'
2017-01-29 12:54:06 +13:00
import DebugInfo from '../components/DebugInfo'
import * as helpers from '../helpers'
2016-10-27 10:02:12 +13:00
import * as uiActions from '../services/ui/actions'
2016-10-27 16:58:11 +13:00
import * as pusherActions from '../services/pusher/actions'
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
2016-10-03 14:44:27 +13:00
2016-10-14 18:15:34 +13:00
/**
* Root level application
**/
2016-10-03 14:44:27 +13:00
class App extends React.Component{
constructor(props){
super(props);
2016-10-27 13:02:10 +13:00
this.handleKeyUp = this.handleKeyUp.bind(this);
2016-10-27 13:56:47 +13:00
this.handleKeyDown = this.handleKeyDown.bind(this);
2016-10-03 14:44:27 +13:00
}
2016-10-27 13:02:10 +13:00
componentWillMount(){
window.addEventListener("keyup", this.handleKeyUp, false);
window.addEventListener("keydown", this.handleKeyDown, false);
}
componentDidMount(){
2016-10-27 16:58:11 +13:00
this.props.pusherActions.connect();
this.props.mopidyActions.connect();
2016-11-06 15:47:37 +13:00
this.props.spotifyActions.connect();
2016-11-11 16:47:32 +13:00
if( this.props.spotify_authorized ) this.props.spotifyActions.getAllLibraryPlaylists();
// when we navigate to a new route
hashHistory.listen( location => {
// hide our sidebar
this.props.uiActions.toggleSidebar( false )
});
}
2016-10-27 16:58:11 +13:00
componentWillReceiveProps(nextProps){
// mopidy comes online
if( !this.props.mopidy_connected && nextProps.mopidy_connected ){
this.props.mopidyActions.getLibraryPlaylists();
}
// spotify authorized
if( !this.props.spotify_authorized && nextProps.spotify_authorized ){
this.props.spotifyActions.getAllLibraryPlaylists();
}
// spotify un-authorized
if( this.props.spotify_authorized && !nextProps.spotify_authorized ){
// TODO: flush out playlists and then re-fetch mopidy
}
2016-10-27 13:02:10 +13:00
}
componentWillUnmount(){
window.removeEventListener("keyup", this.handleKeyUp, false);
2016-10-27 13:56:47 +13:00
window.removeEventListener("keydown", this.handleKeyDown, false);
}
shouldTriggerShortcut(e){
var ignoreNodes = ['INPUT', 'TEXTAREA'];
var keyCodes = [32,27];
2016-10-27 13:56:47 +13:00
if( ignoreNodes.indexOf(e.target.nodeName) > -1 ){
return false;
}
if( keyCodes.indexOf(e.keyCode) > -1 ){
e.preventDefault();
return true;
}
}
handleKeyDown(e){
this.shouldTriggerShortcut(e);
2016-10-27 13:02:10 +13:00
}
handleKeyUp(e){
2016-10-27 13:56:47 +13:00
if( !this.shouldTriggerShortcut(e) ) return;
switch(e.keyCode){
2016-10-27 13:02:10 +13:00
case 32: // spacebar
if( this.props.play_state == 'playing' ){
2016-10-27 13:02:10 +13:00
this.props.mopidyActions.pause();
}else{
this.props.mopidyActions.play();
}
break;
case 27: // esc
if( this.props.dragger && this.props.dragger.dragging ){
this.props.uiActions.dragCancel();
}
if( this.props.modal ){
this.props.uiActions.closeModal();
}
2016-10-27 13:02:10 +13:00
break;
}
}
2016-10-03 14:44:27 +13:00
render(){
2016-11-10 16:17:10 +13:00
var className = '';
if (this.props.dragger && this.props.dragger.active) className += ' dragging'
if (this.props.sidebar_open) className += ' sidebar-open'
if (this.props.modal) className += ' modal-open'
if (helpers.isTouchDevice() || this.props.emulate_touch) className += ' can-touch'
2016-11-10 16:17:10 +13:00
2016-10-03 14:44:27 +13:00
return (
2016-11-10 16:17:10 +13:00
<div className={className}>
<Sidebar />
<MiniPlayer />
<main>
{this.props.children}
</main>
<ContextMenu />
<TouchContextMenu />
2016-11-10 16:17:10 +13:00
<Dragger />
2016-11-11 23:21:51 +13:00
<Modal />
2016-12-13 11:08:13 +13:00
<Notifications />
2017-01-29 12:54:06 +13:00
{this.props.debug_info ? <DebugInfo /> : null}
2016-10-03 14:44:27 +13:00
</div>
);
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return {
mopidy_connected: state.mopidy.connected,
spotify_authorized: state.spotify.authorized,
play_state: state.mopidy.play_state,
2016-11-21 21:46:53 +13:00
sidebar_open: state.ui.sidebar_open,
2016-11-10 16:17:10 +13:00
dragger: state.ui.dragger,
modal: state.ui.modal,
2017-01-29 12:54:06 +13:00
context_menu: state.ui.context_menu,
debug_info: state.ui.debug_info
}
2016-10-03 14:44:27 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
2016-10-27 10:02:12 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
2016-10-27 16:58:11 +13:00
pusherActions: bindActionCreators(pusherActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
2016-10-03 14:44:27 +13:00
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App)