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.

196 lines
4.5 KiB

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 v-slot:top>
  7. <v-dialog
  8. v-model="dialog"
  9. max-width="800px"
  10. >
  11. <template v-slot: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 v-slot:[`item.actions`]="{ item }">
  86. <v-icon
  87. small
  88. class="mr-2"
  89. @click="editItem(item)"
  90. >
  91. mdi-pencil
  92. </v-icon>
  93. <v-icon
  94. small
  95. @click="deleteItem(item)"
  96. >
  97. mdi-delete
  98. </v-icon>
  99. </template>
  100. </v-data-table>
  101. </template>
  102. <script lang="ts">
  103. import Vue from 'vue'
  104. import { labelNameRules } from '@/rules/index'
  105. export default Vue.extend({
  106. props: {
  107. value: {
  108. type: Array,
  109. default: () => [],
  110. required: true
  111. }
  112. },
  113. data() {
  114. return {
  115. dialog: false,
  116. headers: [
  117. {
  118. text: 'From',
  119. align: 'left',
  120. value: 'from',
  121. sortable: false
  122. },
  123. {
  124. text: 'To',
  125. align: 'left',
  126. value: 'to',
  127. sortable: false
  128. },
  129. {
  130. text: 'Actions',
  131. value: 'actions',
  132. sortable: false
  133. }
  134. ],
  135. valid: false,
  136. editedIndex: -1,
  137. editedItem: {
  138. 'from': '',
  139. 'to': ''
  140. },
  141. defaultItem: {
  142. 'from': '',
  143. 'to': ''
  144. },
  145. items: [] as string[],
  146. labelNameRules
  147. }
  148. },
  149. async created() {
  150. const labels = await this.$services.label.list(this.$route.params.id)
  151. this.items = labels.map(item => item.text)
  152. },
  153. methods: {
  154. editItem(item: {'from': string, 'to': string}) {
  155. this.editedIndex = this.value.indexOf(item)
  156. this.editedItem = Object.assign({}, item)
  157. this.dialog = true
  158. },
  159. deleteItem(item: {'from': string, 'to': string}) {
  160. this.editedIndex = this.value.indexOf(item)
  161. this.editedItem = Object.assign({}, item)
  162. const items = Object.assign([], this.value)
  163. items.splice(this.editedIndex, 1)
  164. this.editedItem = Object.assign({}, this.defaultItem)
  165. this.editedIndex = -1
  166. this.$emit('input', items)
  167. },
  168. close() {
  169. this.dialog = false
  170. this.$nextTick(() => {
  171. this.editedItem = Object.assign({}, this.defaultItem)
  172. this.editedIndex = -1
  173. })
  174. },
  175. save() {
  176. const items = Object.assign([], this.value)
  177. if (this.editedIndex > -1) {
  178. Object.assign(items[this.editedIndex], this.editedItem)
  179. } else {
  180. items.push(this.editedItem)
  181. }
  182. this.$emit('input', items)
  183. this.close()
  184. }
  185. }
  186. })
  187. </script>