2016-11-03 08:57:16 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import { connect } from 'react-redux'
|
2017-03-14 08:55:10 +13:00
|
|
|
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'
|
2017-03-14 08:55:10 +13:00
|
|
|
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'
|
2016-11-03 08:57:16 +13:00
|
|
|
|
2017-03-23 21:20:25 +13:00
|
|
|
import * as helpers from '../helpers'
|
2017-08-03 15:26:54 +12:00
|
|
|
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){
|
2017-06-21 09:16:26 +12:00
|
|
|
super(props)
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
2017-06-20 19:55:47 +12:00
|
|
|
|
2017-06-22 09:01:50 +12:00
|
|
|
// Auto-focus on the input field
|
|
|
|
|
$(document).find('.search-form input').focus();
|
2017-09-04 11:25:58 +12:00
|
|
|
|
2017-09-05 08:26:04 +12:00
|
|
|
if (this.props.params.query && this.props.params.query !== ''){
|
2017-09-09 21:06:15 +12:00
|
|
|
if (this.props.mopidy_connected && this.props.search_uri_schemes){
|
2017-09-05 08:26:04 +12:00
|
|
|
this.props.mopidyActions.getSearchResults(this.props.view, this.props.params.query)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-09 21:06:15 +12:00
|
|
|
if (this.props.spotify_connected && this.props.search_uri_schemes && this.props.search_uri_schemes.includes('spotify:')){
|
2017-09-05 08:26:04 +12:00
|
|
|
this.props.spotifyActions.getSearchResults(this.props.view, this.props.params.query)
|
|
|
|
|
}
|
2017-09-04 11:25:58 +12:00
|
|
|
}
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps){
|
2017-09-05 08:26:04 +12:00
|
|
|
if (!this.props.mopidy_connected && newProps.mopidy_connected && newProps.params.query){
|
2017-09-04 11:25:58 +12:00
|
|
|
this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query)
|
|
|
|
|
}
|
2016-11-09 15:49:25 +13:00
|
|
|
|
2017-09-09 21:06:15 +12:00
|
|
|
if (!this.props.spotify_connected && newProps.spotify_connected && newProps.params.query && newProps.search_uri_schemes.includes('spotify:')){
|
2017-09-04 11:25:58 +12:00
|
|
|
this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query)
|
2017-03-23 19:20:41 +13:00
|
|
|
}
|
2017-09-05 08:26:04 +12:00
|
|
|
|
|
|
|
|
// Search changed
|
|
|
|
|
if (this.props.params.query !== newProps.params.query || this.props.view !== newProps.view){
|
|
|
|
|
|
|
|
|
|
this.props.mopidyActions.clearSearchResults()
|
|
|
|
|
this.props.spotifyActions.clearSearchResults()
|
|
|
|
|
|
2017-09-09 21:06:15 +12:00
|
|
|
if (this.props.mopidy_connected && this.props.search_uri_schemes){
|
2017-09-05 08:26:04 +12:00
|
|
|
this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-09 21:06:15 +12:00
|
|
|
if (this.props.mopidy_connected && this.props.search_uri_schemes && this.props.search_uri_schemes.includes('spotify:')){
|
2017-09-05 08:26:04 +12:00
|
|
|
this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
loadMore(type){
|
2017-09-04 11:25:58 +12:00
|
|
|
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(){
|
2017-03-24 23:05:43 +13:00
|
|
|
var spotify_search_enabled = (this.props.search_settings && this.props.search_settings.spotify)
|
|
|
|
|
|
2017-01-16 18:31:06 +13:00
|
|
|
var artists = []
|
2017-08-31 21:13:10 +12:00
|
|
|
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-12-04 08:43:09 +13:00
|
|
|
artists = helpers.sortItems(artists, this.props.sort, this.props.sort_reverse);
|
2017-01-16 18:31:06 +13:00
|
|
|
|
|
|
|
|
var albums = []
|
2017-08-31 21:13:10 +12:00
|
|
|
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);
|
2017-01-16 18:31:06 +13:00
|
|
|
|
|
|
|
|
var playlists = []
|
2017-08-31 21:13:10 +12:00
|
|
|
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-12-04 08:43:09 +13:00
|
|
|
playlists = helpers.sortItems(playlists, this.props.sort, this.props.sort_reverse);
|
2017-01-16 18:31:06 +13:00
|
|
|
|
2017-03-23 21:20:25 +13:00
|
|
|
var tracks = []
|
2017-08-31 21:13:10 +12:00
|
|
|
if (this.props.mopidy_search_results.tracks){
|
|
|
|
|
tracks = [...tracks, ...this.props.mopidy_search_results.tracks]
|
2017-03-23 21:20:25 +13:00
|
|
|
}
|
2017-09-05 15:47:35 +12:00
|
|
|
if (this.props.spotify_search_results.tracks){
|
|
|
|
|
tracks = [...tracks, ...this.props.spotify_search_results.tracks]
|
|
|
|
|
}
|
2017-12-04 08:43:09 +13:00
|
|
|
tracks = helpers.sortItems(tracks, this.props.sort, this.props.sort_reverse);
|
2017-03-23 21:20:25 +13:00
|
|
|
|
2017-09-03 13:53:11 +12:00
|
|
|
switch (this.props.view){
|
2016-11-06 15:47:37 +13:00
|
|
|
|
|
|
|
|
case 'artists':
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<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
|
|
|
|
|
|
|
|
|
|
case 'albums':
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<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
|
|
|
|
|
|
|
|
|
|
case 'playlists':
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<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
|
|
|
|
|
|
|
|
|
|
case 'tracks':
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<section className="list-wrapper">
|
2017-09-03 13:53:11 +12:00
|
|
|
<TrackList tracks={tracks} uri={'iris:search:'+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
|
|
|
|
|
|
|
|
|
|
default:
|
2017-03-27 08:29:18 +13:00
|
|
|
|
|
|
|
|
if (artists.length > 0){
|
|
|
|
|
var artists_section = (
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-09-03 13:53:11 +12:00
|
|
|
<h4>Artists</h4>
|
2017-09-03 16:10:32 +12:00
|
|
|
<ArtistGrid show_source_icon single_row artists={artists.slice(0,5)} />
|
2017-03-27 08:29:18 +13:00
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var artists_section = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (albums.length > 0){
|
|
|
|
|
var albums_section = (
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-09-03 13:53:11 +12:00
|
|
|
<h4>Albums</h4>
|
2017-09-03 16:10:32 +12:00
|
|
|
<AlbumGrid show_source_icon single_row albums={albums.slice(0,5)} />
|
2017-03-27 08:29:18 +13:00
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var albums_section = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (playlists.length > 0){
|
|
|
|
|
var playlists_section = (
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-09-03 13:53:11 +12:00
|
|
|
<h4>Playlists</h4>
|
2017-09-03 16:10:32 +12:00
|
|
|
<PlaylistGrid show_source_icon single_row playlists={playlists.slice(0,5)} />
|
2017-03-27 08:29:18 +13:00
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var playlists_section = null
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 08:26:04 +12:00
|
|
|
if (tracks.length > 0){
|
|
|
|
|
var tracks_section = (
|
|
|
|
|
<section className="list-wrapper">
|
|
|
|
|
<h4>Tracks</h4>
|
|
|
|
|
<TrackList tracks={tracks} uri={'iris:search:'+this.props.params.query} show_source_icon />
|
|
|
|
|
<LazyLoadListener loading={this.props['tracks_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('tracks') }/>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var tracks_section = null
|
|
|
|
|
}
|
|
|
|
|
|
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}
|
2016-11-04 09:35:39 +13:00
|
|
|
</div>
|
2017-09-05 08:26:04 +12:00
|
|
|
{tracks_section}
|
2016-11-06 15:47:37 +13:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-04 09:35:39 +13:00
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
render(){
|
2017-09-03 13:53:11 +12:00
|
|
|
var view_options = [
|
2017-03-14 08:55:10 +13:00
|
|
|
{
|
2017-03-23 19:20:41 +13:00
|
|
|
value: 'all',
|
2017-03-14 08:55:10 +13:00
|
|
|
label: 'All'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'artists',
|
|
|
|
|
label: 'Artists'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'albums',
|
|
|
|
|
label: 'Albums'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'playlists',
|
|
|
|
|
label: 'Playlists'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'tracks',
|
|
|
|
|
label: 'Tracks'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
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: 'source',
|
|
|
|
|
label: 'Source'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
2017-03-14 08:55:10 +13:00
|
|
|
var options = (
|
|
|
|
|
<span>
|
2017-12-04 08:43:09 +13:00
|
|
|
<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() }} />
|
2017-09-03 13:53:11 +12:00
|
|
|
<DropdownField icon="eye" name="View" value={this.props.view} options={view_options} handleChange={val => {this.props.uiActions.set({ search_view: val }); this.props.uiActions.hideContextMenu() }} />
|
2017-09-09 21:06:15 +12:00
|
|
|
<button className="no-hover" onClick={e => this.props.uiActions.openModal('search_uri_schemes', {query: this.props.params.query})}>
|
2017-03-24 08:46:20 +13:00
|
|
|
<FontAwesome name="wrench" />
|
2017-03-25 10:21:10 +13:00
|
|
|
Sources
|
2017-03-24 08:46:20 +13:00
|
|
|
</button>
|
2017-03-14 08:55:10 +13:00
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
return (
|
|
|
|
|
<div className="view search-view">
|
2017-06-22 09:01:50 +12:00
|
|
|
<Header icon="search" options={options} uiActions={this.props.uiActions} />
|
|
|
|
|
<SearchForm query={this.props.params.query} />
|
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 {
|
2017-09-03 13:53:11 +12:00
|
|
|
view: (state.ui.search_view ? state.ui.search_view : 'all'),
|
2016-11-09 15:49:25 +13:00
|
|
|
mopidy_connected: state.mopidy.connected,
|
2017-08-31 21:13:10 +12:00
|
|
|
spotify_connected: state.spotify.connected,
|
2017-08-02 10:53:30 +12:00
|
|
|
albums: (state.core.albums ? state.core.albums : []),
|
2017-08-31 21:13:10 +12:00
|
|
|
artists: (state.core.artists ? state.core.artists : []),
|
2017-08-02 10:53:30 +12:00
|
|
|
playlists: (state.core.playlists ? state.core.playlists : []),
|
2017-08-31 21:13:10 +12:00
|
|
|
tracks: (state.core.tracks ? state.core.tracks : []),
|
2017-09-09 21:06:15 +12:00
|
|
|
search_uri_schemes: (state.ui.search_uri_schemes ? state.ui.search_uri_schemes : []),
|
2017-08-31 21:13:10 +12:00
|
|
|
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 {
|
2017-08-03 15:26:54 +12:00
|
|
|
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)
|