2020-06-29 16:49:33 +00:00
|
|
|
<template>
|
2020-11-08 22:35:46 +00:00
|
|
|
<div id="search_tab">
|
2020-09-16 20:22:55 +00:00
|
|
|
<div v-show="!showSearchTab">
|
2020-07-21 09:09:47 +00:00
|
|
|
<h2>{{ $t('search.startSearching') }}</h2>
|
|
|
|
<p>{{ $t('search.description') }}</p>
|
2020-06-29 16:49:33 +00:00
|
|
|
</div>
|
2020-09-16 16:59:25 +00:00
|
|
|
|
2020-09-16 20:22:55 +00:00
|
|
|
<div v-show="showSearchTab">
|
2020-09-16 16:59:25 +00:00
|
|
|
<ul class="section-tabs">
|
|
|
|
<li
|
|
|
|
class="section-tabs__tab"
|
|
|
|
v-for="tab in tabs"
|
|
|
|
:key="tab.name"
|
|
|
|
@click="currentTab = tab"
|
|
|
|
:class="{ active: currentTab.name === tab.name }"
|
|
|
|
>
|
|
|
|
{{ tab.name }}
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
2020-09-17 16:11:41 +00:00
|
|
|
<keep-alive>
|
|
|
|
<component
|
|
|
|
:is="currentTab.component"
|
2020-11-02 11:25:08 +00:00
|
|
|
:viewInfo="getViewInfo()"
|
|
|
|
want-headers
|
2020-09-17 16:11:41 +00:00
|
|
|
@add-to-queue="addToQueue"
|
|
|
|
@change-search-tab="changeSearchTab"
|
|
|
|
></component>
|
|
|
|
</keep-alive>
|
2020-06-29 16:49:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-09-26 19:10:40 +00:00
|
|
|
import BaseLoadingPlaceholder from '@components/globals/BaseLoadingPlaceholder.vue'
|
2020-09-16 16:59:25 +00:00
|
|
|
import ResultsAll from '@components/search/ResultsAll.vue'
|
|
|
|
import ResultsAlbums from '@components/search/ResultsAlbums.vue'
|
|
|
|
import ResultsArtists from '@components/search/ResultsArtists.vue'
|
|
|
|
import ResultsPlaylists from '@components/search/ResultsPlaylists.vue'
|
|
|
|
import ResultsTracks from '@components/search/ResultsTracks.vue'
|
|
|
|
|
2020-07-16 22:11:28 +00:00
|
|
|
import { socket } from '@/utils/socket'
|
2020-09-17 16:11:41 +00:00
|
|
|
import { sendAddToQueue } from '@/utils/downloads'
|
|
|
|
import { numberWithDots, convertDuration } from '@/utils/utils'
|
|
|
|
import EventBus from '@/utils/EventBus'
|
2020-06-29 16:49:33 +00:00
|
|
|
|
2020-11-02 19:00:09 +00:00
|
|
|
import { formatSearchResults, formatSingleTrack, formatAlbums, formatArtist, formatPlaylist } from '@/data/search'
|
2020-11-02 11:25:08 +00:00
|
|
|
|
|
|
|
const resetObj = { data: [], next: 0, total: 0, hasLoaded: false }
|
|
|
|
|
2020-06-29 16:49:33 +00:00
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
BaseLoadingPlaceholder
|
|
|
|
},
|
2020-11-02 11:25:08 +00:00
|
|
|
props: {
|
|
|
|
performScrolledSearch: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
},
|
2020-06-29 16:49:33 +00:00
|
|
|
data() {
|
2020-09-16 16:59:25 +00:00
|
|
|
const $t = this.$t.bind(this)
|
|
|
|
const $tc = this.$tc.bind(this)
|
|
|
|
|
2020-06-29 16:49:33 +00:00
|
|
|
return {
|
2020-09-16 16:59:25 +00:00
|
|
|
currentTab: {
|
|
|
|
name: '',
|
2020-11-02 11:25:08 +00:00
|
|
|
searchType: '',
|
|
|
|
component: {},
|
|
|
|
viewInfo: '',
|
|
|
|
formatFunc: () => {}
|
2020-09-16 16:59:25 +00:00
|
|
|
},
|
|
|
|
tabs: [
|
|
|
|
{
|
|
|
|
name: $t('globals.listTabs.all'),
|
2020-09-16 20:22:55 +00:00
|
|
|
searchType: 'all',
|
2020-11-02 11:25:08 +00:00
|
|
|
component: ResultsAll,
|
|
|
|
viewInfo: 'allTab'
|
2020-09-16 16:59:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: $tc('globals.listTabs.track', 2),
|
2020-09-16 20:22:55 +00:00
|
|
|
searchType: 'track',
|
2020-11-02 11:25:08 +00:00
|
|
|
component: ResultsTracks,
|
|
|
|
viewInfo: 'trackTab',
|
|
|
|
formatFunc: formatSingleTrack
|
2020-09-16 16:59:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: $tc('globals.listTabs.album', 2),
|
2020-09-16 20:22:55 +00:00
|
|
|
searchType: 'album',
|
2020-11-02 11:25:08 +00:00
|
|
|
component: ResultsAlbums,
|
|
|
|
viewInfo: 'albumTab',
|
|
|
|
formatFunc: formatAlbums
|
2020-09-16 16:59:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: $tc('globals.listTabs.artist', 2),
|
2020-09-16 20:22:55 +00:00
|
|
|
searchType: 'artist',
|
2020-11-02 11:25:08 +00:00
|
|
|
component: ResultsArtists,
|
|
|
|
viewInfo: 'artistTab',
|
|
|
|
formatFunc: formatArtist
|
2020-09-16 16:59:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: $tc('globals.listTabs.playlist', 2),
|
2020-09-16 20:22:55 +00:00
|
|
|
searchType: 'playlist',
|
2020-11-02 11:25:08 +00:00
|
|
|
component: ResultsPlaylists,
|
|
|
|
viewInfo: 'playlistTab',
|
|
|
|
formatFunc: formatPlaylist
|
2020-09-16 16:59:25 +00:00
|
|
|
}
|
|
|
|
],
|
2020-06-29 16:49:33 +00:00
|
|
|
results: {
|
|
|
|
query: '',
|
|
|
|
allTab: {
|
|
|
|
ORDER: [],
|
|
|
|
TOP_RESULT: [],
|
2020-11-02 11:25:08 +00:00
|
|
|
ALBUM: {
|
|
|
|
hasLoaded: false
|
|
|
|
},
|
|
|
|
ARTIST: {
|
|
|
|
hasLoaded: false
|
|
|
|
},
|
|
|
|
TRACK: {
|
|
|
|
hasLoaded: false
|
|
|
|
},
|
|
|
|
PLAYLIST: {
|
|
|
|
hasLoaded: false
|
|
|
|
}
|
2020-06-29 16:49:33 +00:00
|
|
|
},
|
2020-11-02 11:25:08 +00:00
|
|
|
trackTab: { ...resetObj },
|
|
|
|
albumTab: { ...resetObj },
|
|
|
|
artistTab: { ...resetObj },
|
|
|
|
playlistTab: { ...resetObj }
|
2020-09-16 16:59:25 +00:00
|
|
|
}
|
2020-06-29 16:49:33 +00:00
|
|
|
}
|
|
|
|
},
|
2020-09-16 20:22:55 +00:00
|
|
|
computed: {
|
|
|
|
showSearchTab() {
|
|
|
|
return this.results.query !== ''
|
2020-09-17 16:11:41 +00:00
|
|
|
},
|
|
|
|
loadedTabs() {
|
2020-11-02 11:25:08 +00:00
|
|
|
const tabsLoaded = []
|
2020-09-17 16:11:41 +00:00
|
|
|
|
|
|
|
for (const resultKey in this.results) {
|
|
|
|
if (this.results.hasOwnProperty(resultKey)) {
|
2020-11-02 11:25:08 +00:00
|
|
|
const currentResult = this.results[resultKey]
|
2020-09-17 16:11:41 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
if (currentResult.hasLoaded) {
|
|
|
|
tabsLoaded.push(resultKey.replace(/Tab/g, ''))
|
2020-09-17 16:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
return tabsLoaded
|
2020-07-06 19:55:28 +00:00
|
|
|
}
|
|
|
|
},
|
2020-09-16 20:22:55 +00:00
|
|
|
created() {
|
|
|
|
this.currentTab = this.tabs[0]
|
|
|
|
},
|
2020-07-06 19:55:28 +00:00
|
|
|
mounted() {
|
2020-11-02 11:25:08 +00:00
|
|
|
this.$root.$on('mainSearch:showNewResults', this.checkIfPerformNewMainSearch)
|
2020-09-24 11:48:37 +00:00
|
|
|
this.$root.$on('mainSearch:updateResults', this.checkIfUpdateResults)
|
2020-07-06 19:55:28 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
socket.on('mainSearch', this.saveMainSearchResult)
|
2020-07-06 19:55:28 +00:00
|
|
|
socket.on('search', this.handleSearch)
|
|
|
|
},
|
2020-06-29 16:49:33 +00:00
|
|
|
methods: {
|
2020-11-02 11:25:08 +00:00
|
|
|
numberWithDots,
|
|
|
|
convertDuration,
|
|
|
|
addToQueue(e) {
|
|
|
|
sendAddToQueue(e.currentTarget.dataset.link)
|
|
|
|
},
|
|
|
|
getViewInfo() {
|
|
|
|
if (this.currentTab.searchType === 'all') {
|
|
|
|
return this.results.allTab
|
|
|
|
}
|
|
|
|
|
2020-11-02 19:00:09 +00:00
|
|
|
return formatSearchResults(this.results[this.currentTab.viewInfo], this.currentTab.formatFunc)
|
2020-11-02 11:25:08 +00:00
|
|
|
},
|
|
|
|
changeSearchTab(tabName) {
|
|
|
|
tabName = tabName.toLowerCase()
|
2020-09-16 20:22:55 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
const newTab = this.tabs.find(tab => {
|
|
|
|
return tab.searchType === tabName
|
2020-09-16 20:22:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!newTab) {
|
2020-11-02 11:25:08 +00:00
|
|
|
console.error(`No tab ${tabName} found`)
|
2020-09-16 20:22:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-17 16:11:41 +00:00
|
|
|
window.scrollTo(0, 0)
|
2020-09-16 20:22:55 +00:00
|
|
|
this.currentTab = newTab
|
|
|
|
},
|
2020-11-02 11:25:08 +00:00
|
|
|
checkIfPerformNewMainSearch(searchTerm) {
|
|
|
|
let needToPerformNewMainSearch = searchTerm !== this.results.query
|
2020-09-24 11:48:37 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
if (needToPerformNewMainSearch) {
|
|
|
|
this.performNewMainSearch(searchTerm)
|
2020-09-24 11:48:37 +00:00
|
|
|
}
|
|
|
|
},
|
2020-11-02 11:25:08 +00:00
|
|
|
performNewMainSearch(term) {
|
2020-09-17 16:11:41 +00:00
|
|
|
socket.emit('mainSearch', { term })
|
2020-09-15 20:44:29 +00:00
|
|
|
|
2020-09-17 16:11:41 +00:00
|
|
|
// Showing loading placeholder
|
|
|
|
this.$root.$emit('updateSearchLoadingState', true)
|
|
|
|
this.currentTab = this.tabs[0]
|
2020-06-29 16:49:33 +00:00
|
|
|
},
|
2020-11-02 11:25:08 +00:00
|
|
|
// ! Updates search only if the search term is the same as before AND we're not in the ALL tab. Wtf
|
|
|
|
checkIfUpdateResults(term) {
|
|
|
|
let needToUpdateSearch = term === this.results.query && this.currentTab.searchType !== 'all'
|
2020-06-29 16:49:33 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
if (needToUpdateSearch) {
|
|
|
|
this.results[this.currentTab.searchType + 'Tab'] = { ...resetObj }
|
|
|
|
this.search(this.currentTab.searchType)
|
|
|
|
}
|
2020-06-29 16:49:33 +00:00
|
|
|
},
|
|
|
|
search(type) {
|
|
|
|
socket.emit('search', {
|
|
|
|
term: this.results.query,
|
2020-09-17 16:31:07 +00:00
|
|
|
type,
|
|
|
|
start: this.results[`${type}Tab`].next,
|
2020-06-29 16:49:33 +00:00
|
|
|
nb: 30
|
|
|
|
})
|
|
|
|
},
|
2020-09-17 16:31:07 +00:00
|
|
|
scrolledSearch() {
|
|
|
|
if (this.currentTab.searchType === 'all') return
|
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
const currentTabKey = `${this.currentTab.searchType}Tab`
|
|
|
|
const needToPerformScrolledSearch = this.results[currentTabKey].next < this.results[currentTabKey].total
|
2020-06-29 16:49:33 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
if (needToPerformScrolledSearch) {
|
2020-09-17 16:31:07 +00:00
|
|
|
this.search(this.currentTab.searchType)
|
2020-06-29 16:49:33 +00:00
|
|
|
}
|
|
|
|
},
|
2020-11-02 11:25:08 +00:00
|
|
|
saveMainSearchResult(searchResult) {
|
|
|
|
// Hide loading placeholder
|
2020-09-15 20:51:50 +00:00
|
|
|
this.$root.$emit('updateSearchLoadingState', false)
|
2020-06-29 16:49:33 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
this.results.query = searchResult.QUERY
|
|
|
|
|
|
|
|
this.results.allTab = searchResult
|
|
|
|
this.results.allTab.TRACK.hasLoaded = true
|
|
|
|
this.results.allTab.ALBUM.hasLoaded = true
|
|
|
|
this.results.allTab.ARTIST.hasLoaded = true
|
|
|
|
this.results.allTab.PLAYLIST.hasLoaded = true
|
2020-06-29 16:49:33 +00:00
|
|
|
|
|
|
|
this.results.trackTab = { ...resetObj }
|
|
|
|
this.results.albumTab = { ...resetObj }
|
|
|
|
this.results.artistTab = { ...resetObj }
|
|
|
|
this.results.playlistTab = { ...resetObj }
|
|
|
|
},
|
|
|
|
handleSearch(result) {
|
2020-11-02 11:25:08 +00:00
|
|
|
const { next: nextResult, total, type, data: newData } = result
|
2020-06-29 16:49:33 +00:00
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
const currentTabKey = type + 'Tab'
|
2020-06-29 16:49:33 +00:00
|
|
|
let next = 0
|
|
|
|
|
|
|
|
if (nextResult) {
|
|
|
|
next = parseInt(nextResult.match(/index=(\d*)/)[1])
|
|
|
|
} else {
|
|
|
|
next = total
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
if (this.results[currentTabKey].total !== total) {
|
|
|
|
this.results[currentTabKey].total = total
|
2020-06-29 16:49:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
if (this.results[currentTabKey].next !== next) {
|
|
|
|
this.results[currentTabKey].next = next
|
|
|
|
this.results[currentTabKey].data = this.results[currentTabKey].data.concat(newData)
|
2020-06-29 16:49:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 11:25:08 +00:00
|
|
|
this.results[currentTabKey].hasLoaded = true
|
2020-09-17 16:11:41 +00:00
|
|
|
},
|
|
|
|
isTabLoaded(tab) {
|
|
|
|
return this.loadedTabs.indexOf(tab.searchType) !== -1 || tab.searchType === 'all'
|
2020-06-29 16:49:33 +00:00
|
|
|
}
|
|
|
|
},
|
2020-07-06 19:55:28 +00:00
|
|
|
watch: {
|
2020-09-17 16:31:07 +00:00
|
|
|
performScrolledSearch(needToSearch) {
|
|
|
|
if (!needToSearch) return
|
2020-06-29 16:49:33 +00:00
|
|
|
|
2020-09-17 16:31:07 +00:00
|
|
|
this.scrolledSearch(needToSearch)
|
2020-09-16 20:22:55 +00:00
|
|
|
},
|
|
|
|
currentTab(newTab) {
|
2020-09-17 16:11:41 +00:00
|
|
|
if (this.isTabLoaded(newTab)) return
|
|
|
|
|
2020-09-16 20:22:55 +00:00
|
|
|
this.search(newTab.searchType)
|
2020-07-06 19:55:28 +00:00
|
|
|
}
|
2020-06-29 16:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
2020-07-20 20:31:54 +00:00
|
|
|
</style>
|