Files
Iris/src/js/views/Album.js

192 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-10-03 14:44:27 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import Header from '../components/Header'
import TrackList from '../components/TrackList'
2016-10-31 09:25:15 +13:00
import Thumbnail from '../components/Thumbnail'
import Parallax from '../components/Parallax'
import ArtistSentence from '../components/ArtistSentence'
2016-10-31 16:49:42 +13:00
import ArtistGrid from '../components/ArtistGrid'
import FollowButton from '../components/Fields/FollowButton'
2016-10-31 16:49:42 +13:00
import Dater from '../components/Dater'
2016-11-07 21:21:15 +13:00
import LazyLoadListener from '../components/LazyLoadListener'
import ContextMenuTrigger from '../components/ContextMenuTrigger'
2018-05-21 06:01:53 +12:00
import Icon from '../components/Icon'
import * as helpers from '../helpers'
2018-08-13 19:58:36 +12:00
import * as coreActions from '../services/core/actions'
import * as uiActions from '../services/ui/actions'
2016-10-19 14:09:34 +13:00
import * as mopidyActions from '../services/mopidy/actions'
2016-11-07 21:21:15 +13:00
import * as spotifyActions from '../services/spotify/actions'
2016-10-03 14:44:27 +13:00
class Album extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2016-10-03 14:44:27 +13:00
super(props);
}
componentDidMount(){
this.setWindowTitle();
2018-08-13 19:58:36 +12:00
this.props.coreActions.loadAlbum(this.props.params.uri);
2016-10-03 14:44:27 +13:00
}
handleContextMenu(e){
e.preventDefault()
var data = { uris: [this.props.params.uri] }
2017-10-10 13:05:28 +13:00
this.props.uiActions.showContextMenu(e, data, 'album', 'click' )
}
2017-10-10 13:05:28 +13:00
componentWillReceiveProps(nextProps){
// if our URI has changed, fetch new album
2017-10-10 13:05:28 +13:00
if (nextProps.params.uri != this.props.params.uri){
2018-08-13 19:58:36 +12:00
this.props.coreActions.loadAlbum(nextProps.params.uri);
// if mopidy has just connected AND we're a local album, go get
2018-08-13 19:58:36 +12:00
} else if (!this.props.mopidy_connected && nextProps.mopidy_connected){
if (helpers.uriSource(nextProps.params.uri) != 'spotify'){
this.props.coreActions.loadAlbum(nextProps.params.uri);
2016-11-01 13:19:51 +13:00
}
}
if (!this.props.album && nextProps.album){
this.setWindowTitle(nextProps.album);
}
}
2018-08-13 19:58:36 +12:00
setWindowTitle(album = this.props.album){
if (album){
var artists = "";
2018-08-29 21:13:54 +12:00
if (album.artists_uris && this.props.artists){
for (var i = 0; i < album.artists_uris.length; i++){
var uri = album.artists_uris[i]
if (this.props.artists.hasOwnProperty(uri)){
if (artists != ""){
artists += ", ";
}
artists += this.props.artists[uri].name;
}
}
}
this.props.uiActions.setWindowTitle(album.name+" by "+artists+" (album)");
} else{
this.props.uiActions.setWindowTitle("Album");
}
2016-11-01 13:19:51 +13:00
}
2017-02-27 08:44:02 +13:00
handleContextMenu(e){
var data = {
e: e,
context: 'album',
items: [this.props.album],
2017-02-27 08:44:02 +13:00
uris: [this.props.params.uri]
}
this.props.uiActions.showContextMenu(data);
2017-02-27 08:44:02 +13:00
}
2016-11-07 21:21:15 +13:00
loadMore(){
this.props.spotifyActions.getMore(
this.props.album.tracks_more,
{
parent_type: 'album',
parent_key: this.props.album.uri,
records_type: 'track'
}
);
2016-11-07 21:21:15 +13:00
}
play(){
this.props.mopidyActions.playURIs([this.props.params.uri], this.props.params.uri)
}
inLibrary(){
var library = helpers.uriSource(this.props.params.uri)+'_library_albums'
return (this.props[library] && this.props[library].indexOf(this.props.params.uri) > -1)
}
2016-11-07 21:21:15 +13:00
render(){
2018-08-13 19:58:36 +12:00
if (!this.props.album){
if (helpers.isLoading(this.props.load_queue,['spotify_albums/'+helpers.getFromUri('albumid',this.props.params.uri)])){
return (
<div className="body-loader loading">
<div className="loader"></div>
</div>
)
} else {
return null;
}
2017-05-22 04:19:44 +12:00
}
2018-08-29 21:13:54 +12:00
var album = helpers.collateObject(this.props.album, {tracks: this.props.tracks, artists: this.props.artists});
2017-11-08 21:13:32 +13:00
2018-08-29 21:13:54 +12:00
if (album.tracks && album.tracks.length <= 0 && helpers.isLoading(this.props.load_queue,['spotify_albums/'+helpers.getFromUri('albumid',this.props.params.uri)])){
2018-04-14 13:48:03 +12:00
var is_loading_tracks = true;
} else {
var is_loading_tracks = false;
}
2017-05-22 04:19:44 +12:00
return (
2017-06-18 05:47:36 +12:00
<div className="view album-view content-wrapper">
<div className="thumbnail-wrapper">
2018-08-29 21:13:54 +12:00
<Thumbnail size="large" canZoom images={album.images} />
</div>
2017-05-22 04:19:44 +12:00
<div className="title">
2018-08-29 21:13:54 +12:00
<h1>{album.name}</h1>
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
<ul className="details">
2018-08-29 21:13:54 +12:00
{!this.props.slim_mode ? <li className="has-tooltip"><Icon type="fontawesome" name={helpers.sourceIcon(this.props.params.uri )} /><span className="tooltip">{helpers.uriSource(this.props.params.uri )} {album.type ? album.type : 'album'}</span></li> : null}
{!this.props.slim_mode && album.artists.length > 0 ? <li><ArtistSentence artists={album.artists} /></li> : null}
{album.release_date ? <li><Dater type="date" data={album.release_date} /></li> : null}
2017-05-22 04:19:44 +12:00
<li>
2018-08-29 21:13:54 +12:00
{album.tracks ? <span>{album.tracks.length} tracks, <Dater type="total-time" data={album.tracks} /></span> : '0 tracks, 0 mins' }
2017-05-22 04:19:44 +12:00
</li>
</ul>
2016-11-01 13:19:51 +13:00
</div>
2017-05-22 04:19:44 +12:00
<div className="actions">
<button className="primary" onClick={e => this.play()}>Play</button>
{ helpers.uriSource(this.props.params.uri) == 'spotify' ? <FollowButton className="secondary" uri={this.props.params.uri} addText="Add to library" removeText="Remove from library" is_following={this.inLibrary()} /> : null }
2017-10-21 21:58:53 +13:00
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
2016-10-03 14:44:27 +13:00
</div>
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
<section className="list-wrapper">
2018-08-29 21:13:54 +12:00
<TrackList className="album-track-list" tracks={album.tracks} uri={this.props.params.uri} />
<LazyLoadListener loading={album.tracks_more} forceLoader={is_loading_tracks} loadMore={() => this.loadMore()}/>
2017-05-22 04:19:44 +12:00
</section>
</div>
)
2016-10-03 14:44:27 +13:00
}
}
const mapStateToProps = (state, ownProps) => {
var uri = ownProps.params.uri;
return {
2017-08-23 07:39:33 +12:00
slim_mode: state.ui.slim_mode,
2017-05-22 04:19:44 +12:00
load_queue: state.ui.load_queue,
2017-11-08 21:13:32 +13:00
tracks: state.core.tracks,
artists: state.core.artists,
album: (state.core.albums && state.core.albums[uri] !== undefined ? state.core.albums[uri] : false ),
albums: state.core.albums,
spotify_library_albums: state.spotify.library_albums,
local_library_albums: state.mopidy.library_albums,
spotify_authorized: state.spotify.authorization,
mopidy_connected: state.mopidy.connected
};
2016-10-03 14:44:27 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
2018-08-13 19:58:36 +12:00
coreActions: bindActionCreators(coreActions, dispatch),
uiActions: bindActionCreators(uiActions, dispatch),
2016-10-19 14:09:34 +13:00
mopidyActions: bindActionCreators(mopidyActions, dispatch),
2016-10-16 13:28:21 +13:00
spotifyActions: bindActionCreators(spotifyActions, dispatch)
2016-10-03 14:44:27 +13:00
}
}
2018-08-13 19:58:36 +12:00
export default connect(mapStateToProps, mapDispatchToProps)(Album)