ContentCategorySelector.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <q-card>
  3. <q-card-section>
  4. <span v-if="categoryType === 'content'">
  5. انتخاب دسته محتوا
  6. </span>
  7. <span v-else-if="categoryType === 'store'">
  8. انتخاب دسته فروشگاه
  9. </span>
  10. </q-card-section>
  11. <q-separator />
  12. <q-card-section>
  13. <div class="ContentCategorySelector row q-col-gutter-md">
  14. <div class="col-md-4 col-12">
  15. <div>
  16. دسته بندی اصلی
  17. </div>
  18. <q-select v-model="mainCategory"
  19. :options="mainCategoryOptions"
  20. :loading="categories.laoding" />
  21. </div>
  22. <div class="col-md-4 col-12">
  23. <div>
  24. دسته بندی جزیی
  25. </div>
  26. <q-select v-model="subCategory"
  27. :options="subCategoryOptions"
  28. :loading="categories.laoding" />
  29. </div>
  30. <div class="col-md-4 col-12">
  31. <div>
  32. بخش
  33. </div>
  34. <q-select v-model="bakhshCategory"
  35. :options="bakhshCategoryOptions"
  36. :loading="categories.laoding" />
  37. </div>
  38. </div>
  39. </q-card-section>
  40. </q-card>
  41. </template>
  42. <script>
  43. import { APIGateway } from 'src/api/APIGateway.js'
  44. import { StoreCategoryList } from 'src/models/StoreCategory.js'
  45. import { ContentCategoryList } from 'src/models/ContentCategory.js'
  46. export default {
  47. name: 'ContentCategorySelector',
  48. props: {
  49. value: {
  50. default: () => [],
  51. type: [Array, String, Number, Boolean]
  52. },
  53. disable: {
  54. default: false,
  55. type: Boolean
  56. },
  57. categoryType: {
  58. type: String,
  59. default: 'content'
  60. }
  61. },
  62. emits: ['update:value'],
  63. data () {
  64. return {
  65. mainCategory: null,
  66. subCategory: null,
  67. bakhshCategory: null,
  68. mainCategoryOptions: [],
  69. subCategoryOptions: [],
  70. bakhshCategoryOptions: [],
  71. categories: this.categoryType === 'content' ? new ContentCategoryList() : new StoreCategoryList()
  72. }
  73. },
  74. watch: {
  75. value () {
  76. if (this.value?.id) {
  77. this.getCategories()
  78. } else {
  79. this.inputData = this.value
  80. }
  81. },
  82. mainCategory (newValue) {
  83. this.subCategory = null
  84. this.subCategoryOptions = []
  85. if (!newValue.item.children) {
  86. this.$emit('update:value', newValue.value)
  87. return
  88. }
  89. this.subCategoryOptions = newValue.item.children.map(item => {
  90. return {
  91. item,
  92. label: item.title,
  93. value: item.id
  94. }
  95. })
  96. },
  97. subCategory (newValue) {
  98. this.bakhshCategory = null
  99. this.bakhshCategoryOptions = []
  100. if (!newValue.item.children) {
  101. this.$emit('update:value', newValue.value)
  102. return
  103. }
  104. this.bakhshCategoryOptions = newValue.item.children.map(item => {
  105. return {
  106. item,
  107. label: item.title,
  108. value: item.id
  109. }
  110. })
  111. },
  112. bakhshCategory (newValue) {
  113. const categoryId = newValue ? newValue.value : null
  114. this.$emit('update:value', categoryId)
  115. }
  116. },
  117. mounted() {
  118. this.getCategories()
  119. },
  120. methods: {
  121. setMainCategory () {
  122. if (!this.value?.parent?.parent?.id) {
  123. return
  124. }
  125. this.mainCategory = this.mainCategoryOptions.find(item => item.value === this.value.parent.parent.id)
  126. },
  127. setSubCategory () {
  128. if (!this.value?.parent?.id) {
  129. return
  130. }
  131. this.subCategory = this.subCategoryOptions.find(item => item.value === this.value.parent.id)
  132. },
  133. setBakhshCategory () {
  134. if (!this.value?.id) {
  135. return
  136. }
  137. this.bakhshCategory = this.bakhshCategoryOptions.find(item => item.value === this.value.id)
  138. },
  139. getCategories () {
  140. this.categories.laoding = true
  141. const apiGateway = this.categoryType === 'content' ? APIGateway.contentCategory.index({ parent__isnull: 'true', per_page: 99999 })
  142. : APIGateway.storeCategory.index({ parent__isnull: 'true', per_page: 99999 })
  143. apiGateway
  144. .then(categories => {
  145. if (this.categoryType === 'content') {
  146. this.categories = new ContentCategoryList(categories.list)
  147. } else {
  148. this.categories = new StoreCategoryList(categories.list)
  149. }
  150. this.categories.laoding = false
  151. this.mainCategoryOptions = this.categories.list.map(item => {
  152. return {
  153. item,
  154. label: item.title,
  155. value: item.id
  156. }
  157. })
  158. if (this.value?.parent?.parent?.id && this.value.id) {
  159. this.$nextTick(() => {
  160. this.setMainCategory()
  161. this.$nextTick(() => {
  162. this.setSubCategory()
  163. this.$nextTick(() => {
  164. this.setBakhshCategory()
  165. this.$emit('update:value', this.value.id)
  166. })
  167. })
  168. })
  169. }
  170. })
  171. .catch(() => {
  172. this.categories.laoding = false
  173. })
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. .ContentCategorySelector {
  180. }
  181. </style>