This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
frontend/src/components/tasks/partials/editLabels.vue

161 lines
3.8 KiB
Vue
Raw Normal View History

2019-11-24 13:16:24 +00: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"
:disabled="disabled"
2019-11-24 13:16:24 +00:00
>
2019-12-15 20:42:40 +00:00
<template slot="tag" slot-scope="{ option }">
2019-11-24 13:16:24 +00:00
<span class="tag"
:style="{'background': option.hexColor, 'color': option.textColor}">
2019-11-24 13:16:24 +00: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 13:16:24 +00:00
import LabelService from '../../../services/label'
import LabelModel from '../../../models/label'
import LabelTaskService from '../../../services/labelTask'
2020-07-27 17:53:19 +00:00
import LoadingComponent from '../../misc/loading'
import ErrorComponent from '../../misc/error'
2019-11-24 13:16:24 +00:00
export default {
name: 'edit-labels',
props: {
value: {
2019-11-24 13:16:24 +00:00
default: () => [],
type: Array,
},
taskId: {
2019-11-24 13:16:24 +00:00
type: Number,
required: true,
},
disabled: {
default: false,
},
2019-11-24 13:16:24 +00:00
},
data() {
return {
labelService: LabelService,
labelTaskService: LabelTaskService,
foundLabels: [],
labelTimeout: null,
labels: [],
searchQuery: '',
}
},
components: {
2020-07-27 17:53:19 +00:00
multiselect: () => ({
component: import(/* webpackPrefetch: true *//* webpackChunkName: "multiselect" */ 'vue-multiselect'),
loading: LoadingComponent,
error: ErrorComponent,
timeout: 60000,
}),
2019-11-24 13:16:24 +00:00
},
watch: {
value(newLabels) {
2019-11-24 13:16:24 +00:00
this.labels = newLabels
}
},
created() {
this.labelService = new LabelService()
this.labelTaskService = new LabelTaskService()
this.labels = this.value
2019-11-24 13:16:24 +00: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 13:16:24 +00:00
})
}, 300)
},
clearAllLabels() {
this.$set(this, 'foundLabels', [])
},
addLabel(label) {
this.$store.dispatch('tasks/addLabel', {label: label, taskId: this.taskId})
2019-11-24 13:16:24 +00:00
.then(() => {
this.$emit('input', this.labels)
2019-11-24 13:16:24 +00:00
})
.catch(e => {
this.error(e, this)
2019-11-24 13:16:24 +00:00
})
},
removeLabel(label) {
this.$store.dispatch('tasks/removeLabel', {label: label, taskId: this.taskId})
2019-11-24 13:16:24 +00:00
.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.$emit('input', this.labels)
2019-11-24 13:16:24 +00:00
})
.catch(e => {
this.error(e, this)
2019-11-24 13:16:24 +00: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 13:16:24 +00:00
})
},
},
}
</script>
<style scoped>
</style>