CreateQuestionForm.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="CreateQuestionForm">
  3. <entity-edit v-if="mounted"
  4. ref="entityEdit"
  5. v-model:value="inputs"
  6. title="ایجاد سوال جدید"
  7. :api="api"
  8. :loaded-data="loadedData"
  9. :entity-id-key="entityIdKey"
  10. :entity-param-key="entityParamKey"
  11. :show-route-name="showRouteName"
  12. :show-close-button="false"
  13. :show-edit-button="false"
  14. :show-expand-button="false"
  15. :show-save-button="false"
  16. :show-reload-button="false"
  17. :redirect-after-edit="false"
  18. :after-load-input-data="afterLoadInputData">
  19. <template #toolbar>
  20. <q-btn v-close-popup
  21. icon="close"
  22. flat />
  23. </template>
  24. </entity-edit>
  25. </div>
  26. </template>
  27. <script>
  28. import { shallowRef } from 'vue'
  29. import { EntityEdit } from 'quasar-crud'
  30. import { Question } from 'src/models/Question.js'
  31. import { APIGateway } from 'src/api/APIGateway.js'
  32. import BtnControl from 'src/components/Control/btn.vue'
  33. import { FormBuilderAssist } from 'quasar-form-builder'
  34. import QuestionChoices from 'src/components/FormBuilderCustumComponents/QuestionChoices/QuestionChoices.vue'
  35. const BtnControlComp = shallowRef(BtnControl)
  36. const QuestionChoicesComp = shallowRef(QuestionChoices)
  37. export default {
  38. name: 'CreateQuestionForm',
  39. components: {
  40. EntityEdit
  41. },
  42. props: {
  43. testId: {
  44. type: [Number, String, Boolean],
  45. default: null
  46. },
  47. testSetId: {
  48. type: [Number, String, Boolean],
  49. default: null
  50. },
  51. questionType: {
  52. default: 'QUESTION_BANK',
  53. type: String
  54. }
  55. },
  56. emits: ['onQuestionCreated'],
  57. data () {
  58. return {
  59. mounted: false,
  60. entityLoading: true,
  61. api: null,
  62. entityIdKey: 'id',
  63. entityParamKey: 'id',
  64. showRouteName: 'Admin.Unit.Questions.Show',
  65. loadedData: new Question(),
  66. inputs: [
  67. { type: 'input', name: 'text', responseKey: 'text', label: 'سوال', placeholder: ' ', col: 'col-12' },
  68. { type: 'inputEditor', name: 'correct_answer', responseKey: 'correct_answer', label: 'پاسخ', placeholder: ' ', col: 'col-12' },
  69. { type: 'input', name: 'mark', responseKey: 'mark', label: 'بارم پیشنهادی سوال', placeholder: ' ', col: 'col-md-3 col-12' },
  70. { type: 'select', name: 'level', responseKey: 'level', label: 'سطح سوال', placeholder: ' ', options: (new Question()).levelEnums, col: 'col-md-3 col-12' },
  71. { type: QuestionChoicesComp, name: 'choices', responseKey: 'choices_info', correctChoiceIndex: 0, col: 'col-12 flex justify-end' },
  72. { type: BtnControlComp, name: 'btn', label: 'ذخیره سوال', placeholder: ' ', atClick: () => {}, col: 'col-12 flex justify-end' },
  73. { type: 'hidden', name: 'correct_choice_index', responseKey: 'correct_choice_index' },
  74. { type: 'hidden', name: 'type', responseKey: 'type', value: 'QUESTION_BANK' }
  75. ]
  76. }
  77. },
  78. watch: {
  79. questionType: {
  80. handler (newValue) {
  81. console.log(newValue)
  82. FormBuilderAssist.setAttributeByName(this.inputs, 'type', 'value', newValue)
  83. },
  84. immediate: true
  85. }
  86. },
  87. created() {
  88. if (this.testId) {
  89. this.api = APIGateway.test.APIAdresses.appendQuestion(this.testId)
  90. } else if (this.testSetId) {
  91. this.api = APIGateway.testSet.APIAdresses.appendQuestion(this.testSetId)
  92. }
  93. },
  94. mounted() {
  95. this.setInputs()
  96. this.mounted = true
  97. this.$bus.on('onChangeCorrectChoiceIndexInQuestionChoicesInput', (correctChoiceIndex) => {
  98. FormBuilderAssist.setAttributeByName(this.inputs, 'correct_choice_index', 'value', correctChoiceIndex)
  99. })
  100. },
  101. methods: {
  102. setInputs () {
  103. FormBuilderAssist.setAttributeByName(this.inputs, 'btn', 'atClick', this.edit)
  104. if (this.questionType === 'QUESTION_BANK') {
  105. FormBuilderAssist.setAttributeByName(this.inputs, 'choices', 'type', 'hidden')
  106. FormBuilderAssist.setAttributeByName(this.inputs, 'choices', 'ignoreValue', true)
  107. }
  108. if (this.questionType === 'EVENT') {
  109. FormBuilderAssist.setAttributeByName(this.inputs, 'mark', 'type', 'hidden')
  110. FormBuilderAssist.setAttributeByName(this.inputs, 'mark', 'ignoreValue', true)
  111. FormBuilderAssist.setAttributeByName(this.inputs, 'level', 'type', 'hidden')
  112. FormBuilderAssist.setAttributeByName(this.inputs, 'level', 'ignoreValue', true)
  113. }
  114. },
  115. afterLoadInputData () {
  116. this.entityLoading = false
  117. },
  118. edit() {
  119. // console.log(this.$refs.entityEdit.getFormData())
  120. FormBuilderAssist.setAttributeByName(this.inputs, 'type', 'value', this.questionType)
  121. // console.log(this.$refs.entityEdit.getFormData())
  122. this.entityLoading = true
  123. this.$refs.entityEdit.editEntity(false)
  124. .then((response) => {
  125. this.$emit('onQuestionCreated', response.data)
  126. this.entityLoading = false
  127. })
  128. .catch(() => {
  129. this.entityLoading = false
  130. })
  131. }
  132. }
  133. }
  134. </script>
  135. <style scoped lang="scss">
  136. .CreateQuestionForm {
  137. .entity-edit {
  138. height: 100vh;
  139. overflow: auto;
  140. }
  141. }
  142. </style>