Filter.vue 4.68 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<template>
  <tr>
    <td>{{ filter.label }}</td>
    <td>
      <div class="ui toggle checkbox">
        <input name="public" type="checkbox" v-model="exclude" @change="$emit('update-config', index, 'not', exclude)">
        <label></label>
      </div>
    </td>
    <td>
      <div
        v-for="(f, index) in filter.fields"
        class="ui field"
        :key="(f.name, index)"
        :ref="f.name">
Agate's avatar
Agate committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
        <div :class="['ui', 'search', 'selection', 'dropdown', {'autocomplete': f.autocomplete}, {'multiple': f.type === 'list'}]">
          <i class="dropdown icon"></i>
          <div class="default text">{{ f.placeholder }}</div>
          <input v-if="f.type === 'list' && config[f.name]" :value="config[f.name].join(',')" type="hidden">
          <div v-if="config[f.name]" class="ui menu">
            <div
              v-if="f.type === 'list'"
              v-for="(v, index) in config[f.name]"
              class="ui item"
              :data-value="v">
                <template v-if="config.names">
                  {{ config.names[index] }}
                </template>
                <template v-else>{{ v }}</template>
Eliot Berriot's avatar
Eliot Berriot committed
30
31
32
            </div>
          </div>
        </div>
Agate's avatar
Agate committed
33
      </div>
Eliot Berriot's avatar
Eliot Berriot committed
34
35
36
37
38
    </td>
    <td>
      <span
        @click="showCandidadesModal = !showCandidadesModal"
        v-if="checkResult"
Agate's avatar
Agate committed
39
        :class="['ui', {'success': checkResult.candidates.count > 10}, 'label']">
Eliot Berriot's avatar
Eliot Berriot committed
40
41
42
43
        {{ checkResult.candidates.count }} tracks matching filter
      </span>
      <modal v-if="checkResult" :show.sync="showCandidadesModal">
        <div class="header">
44
          <translate translate-context="Popup/Radio/Title/Noun">Tracks matching filter</translate>
Eliot Berriot's avatar
Eliot Berriot committed
45
46
47
48
49
50
51
        </div>
        <div class="content">
          <div class="description">
            <track-table v-if="checkResult.candidates.count > 0" :tracks="checkResult.candidates.sample"></track-table>
          </div>
        </div>
        <div class="actions">
Agate's avatar
Agate committed
52
          <div class="ui basic deny button">
jovuit's avatar
jovuit committed
53
            <translate translate-context="*/*/Button.Label/Verb">Cancel</translate>
Eliot Berriot's avatar
Eliot Berriot committed
54
55
56
57
58
          </div>
        </div>
      </modal>
    </td>
    <td>
Agate's avatar
Agate committed
59
      <button @click="$emit('delete', index)" class="ui basic danger button"><translate translate-context="Content/Radio/Button.Label/Verb">Remove</translate></button>
Eliot Berriot's avatar
Eliot Berriot committed
60
61
62
63
    </td>
  </tr>
</template>
<script>
Eliot Berriot's avatar
Eliot Berriot committed
64
import axios from 'axios'
Eliot Berriot's avatar
Eliot Berriot committed
65
import $ from 'jquery'
66
import _ from '@/lodash'
Eliot Berriot's avatar
Eliot Berriot committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

import Modal from '@/components/semantic/Modal'
import TrackTable from '@/components/audio/track/Table'
import BuilderFilter from './Filter'

export default {
  components: {
    BuilderFilter,
    TrackTable,
    Modal
  },
  props: {
    filter: {type: Object},
    config: {type: Object},
    index: {type: Number}
  },
  data: function () {
    return {
      checkResult: null,
      showCandidadesModal: false,
87
      exclude: this.config.not
Eliot Berriot's avatar
Eliot Berriot committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    }
  },
  mounted: function () {
    let self = this
    this.filter.fields.forEach(f => {
      let selector = ['.dropdown']
      let settings = {
        onChange: function (value, text, $choice) {
          value = $(this).dropdown('get value').split(',')
          if (f.type === 'list' && f.subtype === 'number') {
            value = value.map(e => {
              return parseInt(e)
            })
          }
          self.value = value
          self.$emit('update-config', self.index, f.name, value)
          self.fetchCandidates()
        }
      }
      if (f.type === 'list') {
        selector.push('.multiple')
      }
      if (f.autocomplete) {
        selector.push('.autocomplete')
        settings.fields = f.autocomplete_fields
        settings.minCharacters = 1
        settings.apiSettings = {
115
          url: self.$store.getters['instance/absoluteUrl'](f.autocomplete + '?' + f.autocomplete_qs),
Eliot Berriot's avatar
Eliot Berriot committed
116
          beforeXHR: function (xhrObject) {
117
118
119
            if (self.$store.state.auth.oauth.accessToken) {
              xhrObject.setRequestHeader('Authorization', self.$store.getters['auth/header'])
            }
Eliot Berriot's avatar
Eliot Berriot committed
120
121
122
123
124
125
            return xhrObject
          },
          onResponse: function (initialResponse) {
            if (settings.fields.remoteValues) {
              return initialResponse
            }
126
            return {results: initialResponse.results}
Eliot Berriot's avatar
Eliot Berriot committed
127
128
129
130
131
132
133
134
135
          }
        }
      }
      $(self.$el).find(selector.join('')).dropdown(settings)
    })
  },
  methods: {
    fetchCandidates: function () {
      let self = this
Eliot Berriot's avatar
Eliot Berriot committed
136
      let url = 'radios/radios/validate/'
Eliot Berriot's avatar
Eliot Berriot committed
137
138
139
      let final = _.clone(this.config)
      final.type = this.filter.type
      final = {'filters': [final]}
Eliot Berriot's avatar
Eliot Berriot committed
140
      axios.post(url, final).then((response) => {
Eliot Berriot's avatar
Eliot Berriot committed
141
142
143
144
145
146
147
148
149
150
151
        self.checkResult = response.data.filters[0]
      })
    }
  },
  watch: {
    exclude: function () {
      this.fetchCandidates()
    }
  }
}
</script>