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.

MultiselectTag.vue 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!--
  2. - @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  3. -
  4. - @author Julius Härtl <jus@bitgrid.net>
  5. -
  6. - @license GNU AGPL version 3 or any later version
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <Multiselect v-model="inputValObjects"
  24. :options="tags"
  25. :options-limit="5"
  26. :placeholder="label"
  27. track-by="id"
  28. :custom-label="tagLabel"
  29. class="multiselect-vue"
  30. :multiple="multiple"
  31. :close-on-select="false"
  32. :tag-width="60"
  33. :disabled="disabled"
  34. @input="update">
  35. <span slot="noResult">{{ t('core', 'No results') }}</span>
  36. <template #option="scope">
  37. {{ tagLabel(scope.option) }}
  38. </template>
  39. </multiselect>
  40. </template>
  41. <script>
  42. import { Multiselect } from 'nextcloud-vue/dist/Components/Multiselect'
  43. import { searchTags } from './api'
  44. let uuid = 0
  45. export default {
  46. name: 'MultiselectTag',
  47. components: {
  48. Multiselect
  49. },
  50. props: {
  51. label: {
  52. type: String,
  53. required: true
  54. },
  55. value: {
  56. type: Array,
  57. default() {
  58. return []
  59. }
  60. },
  61. disabled: {
  62. type: Boolean,
  63. default: false
  64. },
  65. multiple: {
  66. type: Boolean,
  67. default: true
  68. }
  69. },
  70. data() {
  71. return {
  72. inputValObjects: [],
  73. tags: []
  74. }
  75. },
  76. computed: {
  77. id() {
  78. return 'settings-input-text-' + this.uuid
  79. }
  80. },
  81. watch: {
  82. value(newVal) {
  83. this.inputValObjects = this.getValueObject()
  84. }
  85. },
  86. beforeCreate: function() {
  87. this.uuid = uuid.toString()
  88. uuid += 1
  89. searchTags().then((result) => {
  90. this.tags = result
  91. this.inputValObjects = this.getValueObject()
  92. }).catch(console.error.bind(this))
  93. },
  94. methods: {
  95. getValueObject() {
  96. if (this.tags.length === 0) {
  97. return []
  98. }
  99. if (this.multiple) {
  100. return this.value.filter((tag) => tag !== '').map(
  101. (id) => this.tags.find((tag2) => tag2.id === id)
  102. )
  103. } else {
  104. return this.tags.find((tag) => tag.id === this.value)
  105. }
  106. },
  107. update() {
  108. if (this.multiple) {
  109. this.$emit('input', this.inputValObjects.map((element) => element.id))
  110. } else {
  111. if (this.inputValObjects === null) {
  112. this.$emit('input', '')
  113. } else {
  114. this.$emit('input', this.inputValObjects.id)
  115. }
  116. }
  117. },
  118. tagLabel({ displayName, userVisible, userAssignable }) {
  119. if (userVisible === false) {
  120. return t('systemtags', '%s (invisible)').replace('%s', displayName)
  121. }
  122. if (userAssignable === false) {
  123. return t('systemtags', '%s (restricted)').replace('%s', displayName)
  124. }
  125. return displayName
  126. }
  127. }
  128. }
  129. </script>