SelectorComponent.vue 5.6 KB

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