Files
Iris/src/js/views/Search.js

399 lines
13 KiB
JavaScript
Raw Normal View History

2016-11-03 08:57:16 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { Link, hashHistory } from 'react-router'
2016-11-03 08:57:16 +13:00
import { bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
2017-01-04 15:24:13 +13:00
import ReactGA from 'react-ga'
2016-11-03 08:57:16 +13:00
import Header from '../components/Header'
import DropdownField from '../components/DropdownField'
2016-11-03 08:57:16 +13:00
import TrackList from '../components/TrackList'
import ArtistGrid from '../components/ArtistGrid'
import AlbumGrid from '../components/AlbumGrid'
import PlaylistGrid from '../components/PlaylistGrid'
2016-11-06 15:47:37 +13:00
import LazyLoadListener from '../components/LazyLoadListener'
2017-06-20 19:55:47 +12:00
import SearchForm from '../components/SearchForm'
import URILink from '../components/URILink'
2016-11-03 08:57:16 +13:00
import * as helpers from '../helpers'
import * as coreActions from '../services/core/actions'
2016-11-03 08:57:16 +13:00
import * as uiActions from '../services/ui/actions'
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
class Search extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
super(props);
2016-11-03 08:57:16 +13:00
}
componentDidMount(){
2018-01-05 21:31:59 +13:00
var context = "all";
var term = null;
if (this.props.params && this.props.params.query && this.props.params.query !== ''){
2018-01-05 21:31:59 +13:00
context = helpers.getFromUri("searchcontext",this.props.params.query);
term = helpers.getFromUri("searchterm",this.props.params.query);
}
2017-06-20 19:55:47 +12:00
// Auto-focus on the input field
2018-01-05 21:31:59 +13:00
$(document).find('.search-form input').focus();
2018-01-05 21:31:59 +13:00
if (context && term){
if (this.props.mopidy_connected && this.props.search_uri_schemes){
2018-01-05 21:31:59 +13:00
this.props.mopidyActions.getSearchResults(context, term)
2017-09-05 08:26:04 +12:00
}
if (this.props.spotify_connected && this.props.search_uri_schemes && this.props.search_uri_schemes.includes('spotify:')){
2018-01-05 21:31:59 +13:00
this.props.spotifyActions.getSearchResults(context, term)
2017-09-05 08:26:04 +12:00
}
}
2016-11-03 08:57:16 +13:00
}
componentWillReceiveProps(newProps){
2018-01-05 21:31:59 +13:00
if (this.props.params && this.props.params.query && this.props.params.query !== ''){
var old_context = helpers.getFromUri("searchcontext",this.props.params.query);
var old_term = helpers.getFromUri("searchterm",this.props.params.query);
} else {
2018-01-05 21:31:59 +13:00
var old_context = "all";
var old_term = null;
}
if (newProps.params && newProps.params.query && newProps.params.query !== ''){
2018-01-05 21:31:59 +13:00
var context = helpers.getFromUri("searchcontext",newProps.params.query);
var term = helpers.getFromUri("searchterm",newProps.params.query);
} else {
2018-01-05 21:31:59 +13:00
var context = "all";
var term = null;
}
2016-11-09 15:49:25 +13:00
2018-01-05 21:31:59 +13:00
if (term && !this.props.mopidy_connected && newProps.mopidy_connected){
this.props.mopidyActions.getSearchResults(context, term);
}
2018-01-05 21:31:59 +13:00
if (term && !this.props.spotify_connected && newProps.spotify_connected && newProps.search_uri_schemes.includes('spotify:')){
this.props.spotifyActions.getSearchResults(context, term);
2017-03-23 19:20:41 +13:00
}
2017-09-05 08:26:04 +12:00
// Search changed
2018-01-05 21:31:59 +13:00
if (term && context && term !== old_term){
2017-09-05 08:26:04 +12:00
this.props.mopidyActions.clearSearchResults();
this.props.spotifyActions.clearSearchResults();
2017-09-05 08:26:04 +12:00
if (this.props.mopidy_connected && this.props.search_uri_schemes){
2018-01-05 21:31:59 +13:00
this.props.mopidyActions.getSearchResults(context, term)
2017-09-05 08:26:04 +12:00
}
if (this.props.mopidy_connected && this.props.search_uri_schemes && this.props.search_uri_schemes.includes('spotify:')){
2018-01-05 21:31:59 +13:00
this.props.spotifyActions.getSearchResults(context, term)
2017-09-05 08:26:04 +12:00
}
}
2016-11-03 08:57:16 +13:00
}
2016-11-06 15:47:37 +13:00
loadMore(type){
alert('load more: '+type)
2017-10-10 13:05:28 +13:00
//this.props.spotifyActions.getURL(this.props['spotify_'+type+'_more'], 'SPOTIFY_SEARCH_RESULTS_LOADED_MORE_'+type.toUpperCase());
2016-11-03 08:57:16 +13:00
}
2017-12-04 08:43:09 +13:00
setSort(value){
var reverse = false
if (this.props.sort == value ) reverse = !this.props.sort_reverse
var data = {
search_results_sort_reverse: reverse,
search_results_sort: value
}
this.props.uiActions.set(data)
}
2016-11-06 15:47:37 +13:00
renderResults(){
2018-01-05 21:31:59 +13:00
var context = helpers.getFromUri("searchcontext",this.props.params.query);
var term = helpers.getFromUri("searchterm",this.props.params.query);
if (!context){
context = "all";
}
var spotify_search_enabled = (this.props.search_settings && this.props.search_settings.spotify);
2017-03-24 23:05:43 +13:00
if (this.props.sort == 'uri'){
var sort_map = this.props.search_uri_schemes;
} else {
var sort_map = null;
}
var artists = [];
if (this.props.mopidy_search_results.artists){
artists = [...artists, ...helpers.getIndexedRecords(this.props.artists,this.props.mopidy_search_results.artists)];
2017-01-16 18:31:06 +13:00
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.artists){
artists = [...artists, ...helpers.getIndexedRecords(this.props.artists,this.props.spotify_search_results.artists)];
2017-09-05 15:47:35 +12:00
}
artists = helpers.sortItems(artists, this.props.sort, this.props.sort_reverse, sort_map);
2017-01-16 18:31:06 +13:00
var albums = [];
if (this.props.mopidy_search_results.albums){
2017-12-04 08:43:09 +13:00
albums = [...albums, ...helpers.getIndexedRecords(this.props.albums,this.props.mopidy_search_results.albums)];
2017-01-16 18:31:06 +13:00
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.albums){
albums = [...albums, ...helpers.getIndexedRecords(this.props.albums,this.props.spotify_search_results.albums)]
2017-12-04 08:43:09 +13:00
}
albums = helpers.sortItems(albums, this.props.sort, this.props.sort_reverse, sort_map);
2017-01-16 18:31:06 +13:00
var playlists = []
if (this.props.mopidy_search_results.playlists){
playlists = [...playlists, ...helpers.getIndexedRecords(this.props.playlists,this.props.mopidy_search_results.playlists)];
2017-01-16 18:31:06 +13:00
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.playlists){
playlists = [...playlists, ...helpers.getIndexedRecords(this.props.playlists,this.props.spotify_search_results.playlists)];
2017-09-05 15:47:35 +12:00
}
playlists = helpers.sortItems(playlists, this.props.sort, this.props.sort_reverse, sort_map);
2017-01-16 18:31:06 +13:00
var tracks = [];
if (this.props.mopidy_search_results.tracks){
tracks = [...tracks, ...this.props.mopidy_search_results.tracks];
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.tracks){
tracks = [...tracks, ...this.props.spotify_search_results.tracks];
2017-09-05 15:47:35 +12:00
}
tracks = helpers.sortItems(tracks, this.props.sort, this.props.sort_reverse, sort_map);
2018-01-05 21:31:59 +13:00
switch (context){
2016-11-06 15:47:37 +13:00
case 'artist':
2016-11-06 15:47:37 +13:00
return (
<div>
<h4>
2018-01-05 21:31:59 +13:00
<URILink unencoded type="search" uri={"search:all:"+term}>
Search
</URILink>
2018-01-05 21:31:59 +13:00
&nbsp; <FontAwesome name="angle-right" />&nbsp;
Artists
</h4>
2016-11-06 15:47:37 +13:00
<section className="grid-wrapper">
2017-03-27 08:29:18 +13:00
<ArtistGrid artists={artists} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['artists_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('artists') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'album':
2016-11-06 15:47:37 +13:00
return (
<div>
<h4>
2018-01-05 21:31:59 +13:00
<URILink unencoded type="search" uri={"search:all:"+term}>
Search
</URILink>
2018-01-05 21:31:59 +13:00
&nbsp; <FontAwesome name="angle-right" />&nbsp;
Albums
</h4>
2016-11-06 15:47:37 +13:00
<section className="grid-wrapper">
2017-03-27 08:29:18 +13:00
<AlbumGrid albums={albums} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['albums_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('albums') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'playlist':
2016-11-06 15:47:37 +13:00
return (
<div>
<h4>
2018-01-05 21:31:59 +13:00
<URILink unencoded type="search" uri={"search:all:"+term}>
Search
</URILink>
2018-01-05 21:31:59 +13:00
&nbsp; <FontAwesome name="angle-right" />&nbsp;
Playlists
</h4>
2016-11-06 15:47:37 +13:00
<section className="grid-wrapper">
2017-03-27 08:29:18 +13:00
<PlaylistGrid playlists={playlists} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['playlists_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('playlists') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'track':
2016-11-06 15:47:37 +13:00
return (
<div>
<h4>
2018-01-05 21:31:59 +13:00
<URILink unencoded type="search" uri={"search:all:"+term}>
Search
</URILink>
2018-01-05 21:31:59 +13:00
&nbsp; <FontAwesome name="angle-right" />&nbsp;
Tracks
</h4>
2016-11-06 15:47:37 +13:00
<section className="list-wrapper">
2018-01-05 21:31:59 +13:00
<TrackList tracks={tracks} uri={this.props.params.query} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['tracks_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('tracks') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'all':
2016-11-06 15:47:37 +13:00
default:
2017-03-27 08:29:18 +13:00
if (artists.length > 0){
var artists_section = (
<section>
2018-01-05 21:31:59 +13:00
<div className="inner">
<URILink unencoded type="search" uri={"search:artist:"+term}>
<h4>Artists</h4>
</URILink>
<ArtistGrid show_source_icon artists={artists.slice(0,5)} />
{artists.length > 4 ? <URILink unencoded type="search" uri={"search:artist:"+term} className="button grey">
All artists ({artists.length})
</URILink> : null}
2017-03-27 08:29:18 +13:00
</div>
</section>
)
} else {
var artists_section = null;
2017-03-27 08:29:18 +13:00
}
if (albums.length > 0){
var albums_section = (
<section>
2018-01-05 21:31:59 +13:00
<div className="inner">
<URILink unencoded type="search" uri={"search:album:"+term}>
<h4>Albums</h4>
</URILink>
<AlbumGrid show_source_icon albums={albums.slice(0,5)} />
{albums.length > 4 ? <URILink unencoded type="search" uri={"search:album:"+term} className="button grey">
All albums ({albums.length})
</URILink> : null}
2017-03-27 08:29:18 +13:00
</div>
</section>
)
} else {
var albums_section = null;
2017-03-27 08:29:18 +13:00
}
if (playlists.length > 0){
var playlists_section = (
<section>
2018-01-05 21:31:59 +13:00
<div className="inner">
<URILink unencoded type="search" uri={"search:playlist:"+term}>
<h4>Playlists</h4>
</URILink>
<PlaylistGrid show_source_icon playlists={playlists.slice(0,5)} />
{playlists.length > 4 ? <URILink unencoded type="search" uri={"search:playlist:"+term} className="button grey">
All playlists ({playlists.length})
</URILink> : null}
2017-03-27 08:29:18 +13:00
</div>
</section>
)
} else {
var playlists_section = null;
2017-03-27 08:29:18 +13:00
}
2017-09-05 08:26:04 +12:00
if (tracks.length > 0){
var tracks_section = (
<section className="list-wrapper">
2018-01-05 21:31:59 +13:00
<TrackList tracks={tracks} uri={this.props.params.query} show_source_icon />
2017-09-05 08:26:04 +12:00
<LazyLoadListener loading={this.props['tracks_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('tracks') }/>
</section>
)
} else {
var tracks_section = null;
2017-09-05 08:26:04 +12:00
}
2016-11-06 15:47:37 +13:00
return (
<div>
<div className="search-result-sections cf">
2017-03-27 08:29:18 +13:00
{artists_section}
{albums_section}
{playlists_section}
</div>
2017-09-05 08:26:04 +12:00
{tracks_section}
2016-11-06 15:47:37 +13:00
</div>
)
}
}
2016-11-06 15:47:37 +13:00
render(){
2017-12-04 08:43:09 +13:00
var sort_options = [
{
value: 'name',
label: 'Name'
},
{
value: 'artists.name',
label: 'Artist'
},
{
value: 'duration',
label: 'Duration'
},
{
value: 'uri',
2017-12-04 08:43:09 +13:00
label: 'Source'
}
];
2017-12-04 08:43:09 +13:00
var options = (
<span>
<DropdownField
icon="sort"
name="Sort"
value={this.props.sort}
options={sort_options}
reverse={this.props.sort_reverse}
handleChange={val => {this.setSort(val); this.props.uiActions.hideContextMenu() }} />
<button className="no-hover" onClick={e => this.props.uiActions.openModal('search_uri_schemes', {query: this.props.params.query})}>
<FontAwesome name="wrench" />&nbsp;
2017-03-25 10:21:10 +13:00
Sources
</button>
</span>
)
2016-11-06 15:47:37 +13:00
return (
<div className="view search-view">
<Header
icon="search"
options={options}
uiActions={this.props.uiActions} />
<SearchForm
query={(this.props.params.query ? this.props.params.query : '')}
view={(this.props.params.view ? this.props.params.view : 'all')} />
2017-06-18 05:47:36 +12:00
<div className="content-wrapper">
{ this.renderResults() }
</div>
2016-11-03 08:57:16 +13:00
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
2016-11-09 15:49:25 +13:00
return {
mopidy_connected: state.mopidy.connected,
spotify_connected: state.spotify.connected,
albums: (state.core.albums ? state.core.albums : []),
artists: (state.core.artists ? state.core.artists : []),
playlists: (state.core.playlists ? state.core.playlists : []),
tracks: (state.core.tracks ? state.core.tracks : []),
search_uri_schemes: (state.ui.search_uri_schemes ? state.ui.search_uri_schemes : []),
mopidy_search_results: (state.mopidy.search_results ? state.mopidy.search_results : {}),
2017-12-04 08:43:09 +13:00
spotify_search_results: (state.spotify.search_results ? state.spotify.search_results : {}),
sort: (state.ui.search_results_sort ? state.ui.search_results_sort : 'name'),
sort_reverse: (state.ui.search_results_sort_reverse ? true : false),
2016-11-09 15:49:25 +13:00
}
2016-11-03 08:57:16 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
coreActions: bindActionCreators(coreActions, dispatch),
2016-11-03 08:57:16 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Search)