You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1 KiB

4 years ago
  1. <template>
  2. <v-combobox
  3. v-model="annotatedLabels"
  4. :items="labels"
  5. item-text="text"
  6. :label="$t('labels.labels')"
  7. hide-selected
  8. chips
  9. multiple
  10. :search-input.sync="search"
  11. @change="search=''"
  12. >
  13. <template v-slot:selection="{ attrs, item, select, selected }">
  14. <v-chip
  15. v-bind="attrs"
  16. :input-value="selected"
  17. :color="item.background_color"
  18. :text-color="textColor(item.background_color)"
  19. close
  20. @click="select"
  21. @click:close="remove(item.id)"
  22. >
  23. {{ item.text }}
  24. </v-chip>
  25. </template>
  26. </v-combobox>
  27. </template>
  28. <script>
  29. import { idealColor } from '~/plugins/utils'
  30. export default {
  31. props: {
  32. labels: {
  33. type: Array,
  34. default: () => [],
  35. required: true
  36. },
  37. annotations: {
  38. type: Array,
  39. default: () => ([]),
  40. required: true
  41. },
  42. addLabel: {
  43. type: Function,
  44. default: () => ([]),
  45. required: true
  46. },
  47. deleteLabel: {
  48. type: Function,
  49. default: () => ([]),
  50. required: true
  51. }
  52. },
  53. data() {
  54. return {
  55. search: ''
  56. }
  57. },
  58. computed: {
  59. annotatedLabels: {
  60. get() {
  61. const labelIds = this.annotations.map(item => item.label)
  62. return this.labels.filter(item => labelIds.includes(item.id))
  63. },
  64. set(newValue) {
  65. if (newValue.length > this.annotations.length) {
  66. const label = newValue[newValue.length - 1]
  67. if (typeof label === 'object') {
  68. this.add(label)
  69. } else {
  70. newValue.pop()
  71. }
  72. } else {
  73. const label = this.annotatedLabels.find(x => !newValue.some(y => y.id === x.id))
  74. if (typeof label === 'object') {
  75. this.remove(label.id)
  76. }
  77. }
  78. }
  79. }
  80. },
  81. methods: {
  82. textColor(backgroundColor) {
  83. return idealColor(backgroundColor)
  84. },
  85. add(label) {
  86. this.addLabel(label.id)
  87. },
  88. remove(labelId) {
  89. const annotation = this.annotations.find(item => item.label === labelId)
  90. this.deleteLabel(annotation.id)
  91. }
  92. }
  93. }
  94. </script>