2019-11-24 14:16:24 +01:00
|
|
|
<template>
|
|
|
|
<multiselect
|
2020-09-05 22:35:52 +02:00
|
|
|
:loading="labelService.loading || labelTaskService.loading"
|
2021-01-06 23:36:31 +01:00
|
|
|
placeholder="Type to add a new label..."
|
2020-09-05 22:35:52 +02:00
|
|
|
:multiple="true"
|
2021-01-06 23:36:31 +01:00
|
|
|
@search="findLabel"
|
|
|
|
:search-results="foundLabels"
|
|
|
|
@select="addLabel"
|
2020-09-05 22:35:52 +02:00
|
|
|
label="title"
|
2021-01-06 23:36:31 +01:00
|
|
|
:creatable="true"
|
|
|
|
@create="createAndAddLabel"
|
|
|
|
create-placeholder="Add this as new label"
|
2020-09-05 22:35:52 +02:00
|
|
|
v-model="labels"
|
2019-11-24 14:16:24 +01:00
|
|
|
>
|
2021-01-06 23:36:31 +01:00
|
|
|
<template v-slot:tag="props">
|
2020-09-05 22:35:52 +02:00
|
|
|
<span
|
2021-01-06 23:36:31 +01:00
|
|
|
:style="{'background': props.item.hexColor, 'color': props.item.textColor}"
|
|
|
|
class="tag ml-2 mt-2">
|
|
|
|
<span>{{ props.item.title }}</span>
|
|
|
|
<a @click="removeLabel(props.item)" class="delete is-small"></a>
|
2020-09-05 22:35:52 +02:00
|
|
|
</span>
|
2019-11-24 14:16:24 +01:00
|
|
|
</template>
|
2021-01-14 22:06:22 +01:00
|
|
|
<template v-slot:searchResult="props">
|
|
|
|
<span
|
|
|
|
v-if="typeof props.option === 'string'"
|
|
|
|
class="tag ml-2">
|
|
|
|
<span>{{ props.option }}</span>
|
|
|
|
</span>
|
|
|
|
<span
|
|
|
|
v-else
|
|
|
|
:style="{'background': props.option.hexColor, 'color': props.option.textColor}"
|
|
|
|
class="tag ml-2">
|
|
|
|
<span>{{ props.option.title }}</span>
|
|
|
|
</span>
|
|
|
|
</template>
|
2019-11-24 14:16:24 +01:00
|
|
|
</multiselect>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-11-02 21:47:31 +01:00
|
|
|
import differenceWith from 'lodash/differenceWith'
|
2019-11-24 14:16:24 +01:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
import LabelService from '../../../services/label'
|
|
|
|
import LabelModel from '../../../models/label'
|
|
|
|
import LabelTaskService from '../../../services/labelTask'
|
2021-01-06 23:36:31 +01:00
|
|
|
|
|
|
|
import Multiselect from '@/components/input/multiselect'
|
2019-11-24 14:16:24 +01:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
export default {
|
|
|
|
name: 'edit-labels',
|
|
|
|
props: {
|
|
|
|
value: {
|
|
|
|
default: () => [],
|
|
|
|
type: Array,
|
2019-11-24 14:16:24 +01:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
taskId: {
|
|
|
|
type: Number,
|
|
|
|
required: true,
|
2019-11-24 14:16:24 +01:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
disabled: {
|
|
|
|
default: false,
|
2019-11-24 14:16:24 +01:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
labelService: LabelService,
|
|
|
|
labelTaskService: LabelTaskService,
|
|
|
|
foundLabels: [],
|
|
|
|
labelTimeout: null,
|
|
|
|
labels: [],
|
|
|
|
searchQuery: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
2021-01-06 23:36:31 +01:00
|
|
|
Multiselect,
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value(newLabels) {
|
|
|
|
this.labels = newLabels
|
2019-11-24 14:16:24 +01:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.labelService = new LabelService()
|
|
|
|
this.labelTaskService = new LabelTaskService()
|
|
|
|
this.labels = this.value
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
findLabel(query) {
|
|
|
|
this.searchQuery = query
|
|
|
|
if (query === '') {
|
|
|
|
this.clearAllLabels()
|
|
|
|
return
|
|
|
|
}
|
2019-11-24 14:16:24 +01:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
if (this.labelTimeout !== null) {
|
|
|
|
clearTimeout(this.labelTimeout)
|
|
|
|
}
|
2019-11-24 14:16:24 +01:00
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
// 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
|
2019-11-24 14:16:24 +01:00
|
|
|
})
|
|
|
|
.catch(e => {
|
2020-01-30 22:47:08 +01:00
|
|
|
this.error(e, this)
|
2019-11-24 14:16:24 +01:00
|
|
|
})
|
2020-09-05 22:35:52 +02:00
|
|
|
}, 300)
|
|
|
|
},
|
|
|
|
clearAllLabels() {
|
|
|
|
this.$set(this, 'foundLabels', [])
|
|
|
|
},
|
2020-11-22 17:32:35 +01:00
|
|
|
addLabel(label, showNotification = true) {
|
2020-09-05 22:35:52 +02:00
|
|
|
this.$store.dispatch('tasks/addLabel', {label: label, taskId: this.taskId})
|
|
|
|
.then(() => {
|
|
|
|
this.$emit('input', this.labels)
|
2020-11-22 17:32:35 +01:00
|
|
|
if (showNotification) {
|
|
|
|
this.success({message: 'The label has been added successfully.'}, this)
|
|
|
|
}
|
2020-09-05 22:35:52 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
removeLabel(label) {
|
|
|
|
this.$store.dispatch('tasks/removeLabel', {label: label, taskId: this.taskId})
|
|
|
|
.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)
|
2019-11-24 14:16:24 +01:00
|
|
|
}
|
2020-09-05 22:35:52 +02:00
|
|
|
}
|
|
|
|
this.$emit('input', this.labels)
|
2020-11-22 17:32:35 +01:00
|
|
|
this.success({message: 'The label has been removed successfully.'}, this)
|
2020-09-05 22:35:52 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
2019-11-24 14:16:24 +01:00
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
createAndAddLabel(title) {
|
|
|
|
let newLabel = new LabelModel({title: title})
|
|
|
|
this.labelService.create(newLabel)
|
|
|
|
.then(r => {
|
2020-11-22 17:32:35 +01:00
|
|
|
this.addLabel(r, false)
|
2020-09-05 22:35:52 +02:00
|
|
|
this.labels.push(r)
|
2020-11-22 17:32:35 +01:00
|
|
|
this.success({message: 'The label has been created successfully.'}, this)
|
2020-09-05 22:35:52 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
2019-11-24 14:16:24 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
|
|
</style>
|