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.

205 lines
4.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <template>
  2. <v-data-table
  3. :headers="headers"
  4. :items="value"
  5. >
  6. <template #top>
  7. <v-dialog
  8. v-model="dialog"
  9. max-width="800px"
  10. >
  11. <template #activator="{ on, attrs }">
  12. <v-btn
  13. color="primary"
  14. dark
  15. class="text-none"
  16. v-bind="attrs"
  17. v-on="on"
  18. >
  19. Add
  20. </v-btn>
  21. </template>
  22. <v-card>
  23. <v-card-title>
  24. <span class="headline">Add a new mapping</span>
  25. </v-card-title>
  26. <v-card-text>
  27. <v-container>
  28. <v-form
  29. ref="form"
  30. v-model="valid"
  31. >
  32. <v-row>
  33. <v-col
  34. cols="12"
  35. sm="12"
  36. class="pa-0"
  37. >
  38. <v-text-field
  39. v-model="editedItem.from"
  40. label="From"
  41. :rules="labelNameRules($t('rules.labelNameRules'))"
  42. outlined
  43. />
  44. </v-col>
  45. <v-col
  46. cols="12"
  47. sm="12"
  48. class="pa-0"
  49. >
  50. <v-select
  51. v-model="editedItem.to"
  52. :items="items"
  53. :rules="labelNameRules($t('rules.labelNameRules'))"
  54. label="To"
  55. outlined
  56. />
  57. </v-col>
  58. </v-row>
  59. </v-form>
  60. </v-container>
  61. </v-card-text>
  62. <v-card-actions>
  63. <v-spacer />
  64. <v-btn
  65. color="blue darken-1"
  66. class="text-capitalize"
  67. text
  68. @click="close"
  69. >
  70. Cancel
  71. </v-btn>
  72. <v-btn
  73. :disabled="!valid"
  74. color="blue darken-1"
  75. class="text-capitalize"
  76. text
  77. @click="save"
  78. >
  79. Save
  80. </v-btn>
  81. </v-card-actions>
  82. </v-card>
  83. </v-dialog>
  84. </template>
  85. <template #[`item.actions`]="{ item }">
  86. <v-icon
  87. small
  88. class="mr-2"
  89. @click="editItem(item)"
  90. >
  91. {{ mdiPencil }}
  92. </v-icon>
  93. <v-icon
  94. small
  95. @click="deleteItem(item)"
  96. >
  97. {{ mdiDelete }}
  98. </v-icon>
  99. </template>
  100. </v-data-table>
  101. </template>
  102. <script lang="ts">
  103. import Vue from 'vue'
  104. import { mdiPencil, mdiDelete } from '@mdi/js'
  105. import { labelNameRules } from '@/rules/index'
  106. export default Vue.extend({
  107. props: {
  108. value: {
  109. type: Array,
  110. default: () => [],
  111. required: true
  112. }
  113. },
  114. data() {
  115. return {
  116. dialog: false,
  117. headers: [
  118. {
  119. text: 'From',
  120. align: 'left',
  121. value: 'from',
  122. sortable: false
  123. },
  124. {
  125. text: 'To',
  126. align: 'left',
  127. value: 'to',
  128. sortable: false
  129. },
  130. {
  131. text: 'Actions',
  132. value: 'actions',
  133. sortable: false
  134. }
  135. ],
  136. valid: false,
  137. editedIndex: -1,
  138. editedItem: {
  139. 'from': '',
  140. 'to': ''
  141. },
  142. defaultItem: {
  143. 'from': '',
  144. 'to': ''
  145. },
  146. items: [] as string[],
  147. labelNameRules,
  148. mdiPencil,
  149. mdiDelete
  150. }
  151. },
  152. async created() {
  153. const project = await this.$services.project.findById(this.$route.params.id)
  154. if (project.projectType.endsWith('Classification')) {
  155. const labels = await this.$services.categoryType.list(this.$route.params.id)
  156. this.items = labels.map(item => item.text)
  157. } else {
  158. const labels = await this.$services.spanType.list(this.$route.params.id)
  159. this.items = labels.map(item => item.text)
  160. }
  161. },
  162. methods: {
  163. editItem(item: {'from': string, 'to': string}) {
  164. this.editedIndex = this.value.indexOf(item)
  165. this.editedItem = Object.assign({}, item)
  166. this.dialog = true
  167. },
  168. deleteItem(item: {'from': string, 'to': string}) {
  169. this.editedIndex = this.value.indexOf(item)
  170. this.editedItem = Object.assign({}, item)
  171. const items = Object.assign([], this.value)
  172. items.splice(this.editedIndex, 1)
  173. this.editedItem = Object.assign({}, this.defaultItem)
  174. this.editedIndex = -1
  175. this.$emit('input', items)
  176. },
  177. close() {
  178. this.dialog = false
  179. this.$nextTick(() => {
  180. this.editedItem = Object.assign({}, this.defaultItem)
  181. this.editedIndex = -1
  182. })
  183. },
  184. save() {
  185. const items = Object.assign([], this.value)
  186. if (this.editedIndex > -1) {
  187. Object.assign(items[this.editedIndex], this.editedItem)
  188. } else {
  189. items.push(this.editedItem)
  190. }
  191. this.$emit('input', items)
  192. this.close()
  193. }
  194. }
  195. })
  196. </script>