Skip to content
Snippets Groups Projects
Search.vue 2.63 KiB
Newer Older
    <h2><translate>Search for some music</translate></h2>
    <div :class="['ui', {'loading': isLoading }, 'search']">
      <div class="ui icon big input">
        <i class="search icon"></i>
Eliot Berriot's avatar
Eliot Berriot committed
        <input ref="search" class="prompt" :placeholder="labels.searchPlaceholder" v-model.trim="query" type="text" />
      </div>
    </div>
    <template v-if="query.length > 0">
      <h3 class="ui title"><translate>Artists</translate></h3>
      <div v-if="results.artists.length > 0">
        <div class="ui cards">
          <artist-card :key="artist.id" v-for="artist in results.artists" :artist="artist" ></artist-card>
Allan Nordhøy's avatar
Allan Nordhøy committed
      <p v-else><translate>No artist matched your query</translate></p>
    </template>
    <template v-if="query.length > 0">
      <h3 class="ui title"><translate>Albums</translate></h3>
      <div v-if="results.albums.length > 0" class="ui stackable three column grid">
        <div class="column" :key="album.id" v-for="album in results.albums">
          <album-card class="fluid" :album="album" ></album-card>
        </div>
      </div>
Allan Nordhøy's avatar
Allan Nordhøy committed
      <p v-else><translate>No album matched your query</translate></p>
import _ from '@/lodash'
import axios from 'axios'
import logger from '@/logging'
import AlbumCard from '@/components/audio/album/Card'
import ArtistCard from '@/components/audio/artist/Card'

export default {
  components: {
    AlbumCard,
    ArtistCard
  },
  props: {
    autofocus: {type: Boolean, default: false}
  },
  data () {
    return {
      query: '',
      results: {
        albums: [],
        artists: []
      },
    }
  },
  mounted () {
    if (this.autofocus) {
      this.$refs.search.focus()
    }
    this.search()
  },
Eliot Berriot's avatar
Eliot Berriot committed
  computed: {
    labels () {
      return {
Allan Nordhøy's avatar
Allan Nordhøy committed
        searchPlaceholder: this.$gettext('Artist, album, track…')
    search: _.debounce(function () {
      if (this.query.length < 1) {
        return
      }
      var self = this
      self.isLoading = true
      logger.default.debug('Searching track matching "' + this.query + '"')
      let params = {
        query: this.query
      }
      axios.get('search', {
        params: params
      }).then((response) => {
        self.results = self.castResults(response.data)
        self.isLoading = false
      })
        albums: results.albums,
        artists: results.artists
      }
    }
  },
  watch: {
    query () {
      this.search()
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>