vikunja-frontend/src/components/tasks/reusable/editLabels.vue

156 lines
3.8 KiB
Vue
Raw Normal View History

2019-11-24 14:16:24 +01:00
<template>
<multiselect
:multiple="true"
:close-on-select="false"
:clear-on-select="true"
:options-limit="300"
:hide-selected="true"
v-model="labels"
:options="foundLabels"
:searchable="true"
:loading="labelService.loading || labelTaskService.loading"
:internal-search="true"
@search-change="findLabel"
@select="addLabel"
placeholder="Type to add a new label..."
label="title"
track-by="id"
:taggable="true"
:showNoOptions="false"
@tag="createAndAddLabel"
tag-placeholder="Add this as new label"
>
2019-12-15 21:42:40 +01:00
<template slot="tag" slot-scope="{ option }">
2019-11-24 14:16:24 +01:00
<span class="tag"
:style="{'background': option.hexColor, 'color': option.textColor}">
2019-11-24 14:16:24 +01:00
<span>{{ option.title }}</span>
<a class="delete is-small" @click="removeLabel(option)"></a>
</span>
</template>
<template slot="clear" slot-scope="props">
<div class="multiselect__clear" v-if="labels.length"
@mousedown.prevent.stop="clearAllLabels(props.search)"></div>
</template>
</multiselect>
</template>
<script>
import { differenceWith } from 'lodash'
2019-11-24 14:16:24 +01:00
import multiselect from 'vue-multiselect'
import LabelService from '../../../services/label'
import LabelModel from '../../../models/label'
import LabelTaskService from '../../../services/labelTask'
import LabelTaskModel from '../../../models/labelTask'
export default {
name: 'edit-labels',
props: {
value: {
2019-11-24 14:16:24 +01:00
default: () => [],
type: Array,
},
taskId: {
2019-11-24 14:16:24 +01:00
type: Number,
required: true,
},
},
data() {
return {
labelService: LabelService,
labelTaskService: LabelTaskService,
foundLabels: [],
labelTimeout: null,
labels: [],
searchQuery: '',
}
},
components: {
multiselect,
},
watch: {
value(newLabels) {
2019-11-24 14:16:24 +01:00
this.labels = newLabels
}
},
created() {
this.labelService = new LabelService()
this.labelTaskService = new LabelTaskService()
this.labels = this.value
2019-11-24 14:16:24 +01:00
},
methods: {
findLabel(query) {
this.searchQuery = query
if (query === '') {
this.clearAllLabels()
return
}
if (this.labelTimeout !== null) {
clearTimeout(this.labelTimeout)
}
// Delay the search 300ms to not send a request on every keystroke
this.labelTimeout = setTimeout(() => {
this.labelService.getAll({}, {s: query})
.then(response => {
this.$set(this, 'foundLabels', differenceWith(response, this.labels, (first, second) => {
return first.id === second.id
}))
this.labelTimeout = null
})
.catch(e => {
this.error(e, this)
2019-11-24 14:16:24 +01:00
})
}, 300)
},
clearAllLabels() {
this.$set(this, 'foundLabels', [])
},
addLabel(label) {
let labelTask = new LabelTaskModel({taskId: this.taskId, labelId: label.id})
2019-11-24 14:16:24 +01:00
this.labelTaskService.create(labelTask)
.then(() => {
this.success({message: 'The label was successfully added.'}, this)
this.$emit('input', this.labels)
2019-11-24 14:16:24 +01:00
})
.catch(e => {
this.error(e, this)
2019-11-24 14:16:24 +01:00
})
},
removeLabel(label) {
let labelTask = new LabelTaskModel({taskId: this.taskId, labelId: label.id})
2019-11-24 14:16:24 +01:00
this.labelTaskService.delete(labelTask)
.then(() => {
// Remove the label from the list
for (const l in this.labels) {
if (this.labels[l].id === label.id) {
this.labels.splice(l, 1)
}
}
this.success({message: 'The label was successfully removed.'}, this)
this.$emit('input', this.labels)
2019-11-24 14:16:24 +01:00
})
.catch(e => {
this.error(e, this)
2019-11-24 14:16:24 +01:00
})
},
createAndAddLabel(title) {
let newLabel = new LabelModel({title: title})
this.labelService.create(newLabel)
.then(r => {
this.addLabel(r)
this.labels.push(r)
})
.catch(e => {
this.error(e, this)
2019-11-24 14:16:24 +01:00
})
},
},
}
</script>
<style scoped>
</style>