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.

83 lines
1.7 KiB

  1. <template>
  2. <v-combobox
  3. :value="annotatedLabels"
  4. :items="labels"
  5. @input="add"
  6. item-text="text"
  7. label="Label"
  8. hide-selected
  9. chips
  10. multiple
  11. >
  12. <template v-slot:selection="{ attrs, item, select, selected }">
  13. <v-chip
  14. v-bind="attrs"
  15. :input-value="selected"
  16. :color="item.background_color"
  17. :text-color="textColor(item.background_color)"
  18. @click="select"
  19. @click:close="remove(item.id)"
  20. close
  21. >
  22. {{ item.text }}
  23. </v-chip>
  24. </template>
  25. </v-combobox>
  26. </template>
  27. <script>
  28. import { idealColor } from '~/plugins/utils'
  29. export default {
  30. props: {
  31. labels: {
  32. type: Array,
  33. default: () => [],
  34. required: true
  35. },
  36. annotations: {
  37. type: Array,
  38. default: () => ([]),
  39. required: true
  40. },
  41. addLabel: {
  42. type: Function,
  43. default: () => ([]),
  44. required: true
  45. },
  46. deleteLabel: {
  47. type: Function,
  48. default: () => ([]),
  49. required: true
  50. }
  51. },
  52. computed: {
  53. annotatedLabels() {
  54. const labelIds = this.annotations.map(item => item.label)
  55. return this.labels.filter(item => labelIds.includes(item.id))
  56. },
  57. labelObject() {
  58. const obj = {}
  59. for (const label of this.labels) {
  60. obj[label.id] = label
  61. }
  62. return obj
  63. }
  64. },
  65. methods: {
  66. textColor(backgroundColor) {
  67. return idealColor(backgroundColor)
  68. },
  69. add(labels) {
  70. const label = labels[labels.length - 1]
  71. this.addLabel(label.id)
  72. },
  73. remove(labelId) {
  74. const annotation = this.annotations.find(item => item.label === labelId)
  75. this.deleteLabel(annotation.id)
  76. }
  77. }
  78. }
  79. </script>