Newer
Older
<div class="ui vertical stripe segment" v-title="'Radio Builder'">
<h2 class="ui header"><i18next path="Builder"/></h2>
<i18next tag="p" path="You can use this interface to build your own custom radio, which will play tracks according to your criteria"/>
<div class="ui form">
<div class="inline fields">
<div class="field">
<input id="name" type="text" v-model="radioName" placeholder="My awesome radio" />
</div>
<div class="field">
<input id="public" type="checkbox" v-model="isPublic" />
<button :disabled="!canSave" @click="save" class="ui green button"><i18ext path="Save"/></button>
<radio-button v-if="id" type="custom" :custom-radio-id="id"></radio-button>
</div>
</div>
<div class="ui form">
<div class="inline field">
<select class="ui dropdown" v-model="currentFilterType">
<option v-for="f in availableFilters" :value="f.type">{{ f.label }}</option>
</select>
<button :disabled="!currentFilterType" @click="add" class="ui button"><i18next path="Add filter"/></button>
</div>
<p v-if="currentFilter">
{{ currentFilter.help_text }}
</p>
</div>
<table class="ui table">
<thead>
<tr>
<i18next tag="th" class="two wide" path="Filter name"/>
<i18next tag="th" class="one wide" path="Exclude"/>
<i18next tag="th" class="six wide" path="Config"/>
<i18next tag="th" class="five wide" path="Candidates"/>
<i18next tag="th" class="two wide" path="Actions"/>
</tr>
</thead>
<tbody>
<builder-filter
v-for="(f, index) in filters"
:key="(f, index, f.hash)"
:index="index"
@update-config="updateConfig"
@delete="deleteFilter"
:config="f.config"
:filter="f.filter">
</builder-filter>
</tbody>
</table>
<template v-if="checkResult">
<i18next tag="h3" class="ui header" path="{%0%} tracks matching combined filters">
{{ checkResult.candidates.count }}
</i18next>
<track-table v-if="checkResult.candidates.sample" :tracks="checkResult.candidates.sample"></track-table>
</template>
</div>
</div>
</div>
</template>
<script>
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import $ from 'jquery'
import _ from 'lodash'
import BuilderFilter from './Filter'
import TrackTable from '@/components/audio/track/Table'
import RadioButton from '@/components/radios/Button'
export default {
props: {
id: {required: false}
},
components: {
BuilderFilter,
TrackTable,
RadioButton
},
data: function () {
return {
availableFilters: [],
currentFilterType: null,
filters: [],
checkResult: null,
radioName: '',
isPublic: true
}
},
created: function () {
let self = this
this.fetchFilters().then(() => {
if (self.id) {
self.fetch()
}
})
},
mounted () {
$('.ui.dropdown').dropdown()
},
methods: {
fetchFilters: function () {
let self = this
let url = 'radios/radios/filters/'
return axios.get(url).then((response) => {
self.availableFilters = response.data
})
},
add () {
this.filters.push({
config: {},
filter: this.currentFilter,
hash: +new Date()
})
this.fetchCandidates()
},
updateConfig (index, field, value) {
this.filters[index].config[field] = value
this.fetchCandidates()
},
deleteFilter (index) {
this.filters.splice(index, 1)
this.fetchCandidates()
},
fetch: function () {
let self = this
let url = 'radios/radios/' + this.id + '/'
axios.get(url).then((response) => {
self.filters = response.data.config.map(f => {
return {
config: f,
filter: this.availableFilters.filter(e => { return e.type === f.type })[0],
hash: +new Date()
}
})
self.radioName = response.data.name
self.isPublic = response.data.is_public
})
},
fetchCandidates: function () {
let self = this
let final = this.filters.map(f => {
let c = _.clone(f.config)
c.type = f.filter.type
return c
})
final = {
'filters': [
{'type': 'group', filters: final}
]
}
axios.post(url, final).then((response) => {
self.checkResult = response.data.filters[0]
})
},
save: function () {
let self = this
let final = this.filters.map(f => {
let c = _.clone(f.config)
c.type = f.filter.type
return c
})
final = {
'name': this.radioName,
'is_public': this.isPublic,
'config': final
}
if (this.id) {
let url = 'radios/radios/' + this.id + '/'
axios.put(url, final).then((response) => {
let url = 'radios/radios/'
axios.post(url, final).then((response) => {
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
params: {
id: response.data.id
}
})
})
}
}
},
computed: {
canSave: function () {
return (
this.radioName.length > 0 && this.checkErrors.length === 0
)
},
checkErrors: function () {
if (!this.checkResult) {
return []
}
let errors = this.checkResult.errors
return errors
},
currentFilter: function () {
let self = this
return this.availableFilters.filter(e => {
return e.type === self.currentFilterType
})[0]
}
},
watch: {
filters: {
handler: function () {
this.fetchCandidates()
},
deep: true
}
}
}
</script>