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.

91 lines
2.0 KiB

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