MatchQuestion.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="match-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. label-width="100px"
  8. >
  9. <el-form-item v-if="IS_PARAGRAPH_MATCHING" prop="quesBody" label="题干">
  10. <v-editor
  11. v-model="modalForm.quesBody"
  12. @change="quesBodyChange"
  13. ></v-editor>
  14. </el-form-item>
  15. <el-form-item prop="quesAnswer" label="答案">
  16. <el-radio-group v-model="quesAnswer" @change="answerChange">
  17. <el-radio-button
  18. v-for="option in parentQuestion.quesOptions"
  19. :key="option.number"
  20. :label="option.number"
  21. >
  22. {{ (option.number - 1) | optionOrderWordFilter }}
  23. </el-radio-button>
  24. </el-radio-group>
  25. </el-form-item>
  26. <el-form-item label="答案解析">
  27. <v-editor
  28. v-model="modalForm.answerAnalysis"
  29. :enable-audio="false"
  30. ></v-editor>
  31. </el-form-item>
  32. </el-form>
  33. <question-info-edit
  34. v-if="canEditQuestionInfo"
  35. ref="QuestionInfoEdit"
  36. :question="modalForm"
  37. @change="questionInfoChange"
  38. ></question-info-edit>
  39. </div>
  40. </template>
  41. <script>
  42. import { isAnEmptyRichText } from "@/utils/utils";
  43. import QuestionInfoEdit from "../QuestionInfoEdit.vue";
  44. import { getMatchQuestionModel } from "./questionModel";
  45. export default {
  46. name: "MatchQuestion",
  47. components: { QuestionInfoEdit },
  48. props: {
  49. question: {
  50. type: Object,
  51. default() {
  52. return {};
  53. },
  54. },
  55. parentQuestion: {
  56. type: Object,
  57. default() {
  58. return {
  59. quesOptions: [],
  60. questionType: "",
  61. };
  62. },
  63. },
  64. canEditQuestionInfo: {
  65. type: Boolean,
  66. default: true,
  67. },
  68. },
  69. data() {
  70. return {
  71. modalForm: {},
  72. quesAnswer: null,
  73. rules: {
  74. quesBody: [
  75. {
  76. validator: (rule, value, callback) => {
  77. if (!value || isAnEmptyRichText(value)) {
  78. return callback(new Error(`请输入题干`));
  79. }
  80. callback();
  81. },
  82. trigger: "change",
  83. },
  84. ],
  85. quesAnswer: [
  86. {
  87. validator: (rule, value, callback) => {
  88. if (!value || !value.length) {
  89. return callback(new Error(`请设置答案`));
  90. }
  91. callback();
  92. },
  93. trigger: "change",
  94. },
  95. ],
  96. },
  97. };
  98. },
  99. computed: {
  100. IS_PARAGRAPH_MATCHING() {
  101. return this.parentQuestion.questionType === "PARAGRAPH_MATCHING";
  102. },
  103. },
  104. created() {
  105. this.initData();
  106. },
  107. methods: {
  108. initData() {
  109. this.modalForm = this.$objAssign(getMatchQuestionModel(), this.question);
  110. this.modalForm.courseId = this.parentQuestion.courseId;
  111. this.modalForm.quesAnswer = this.question.quesAnswer
  112. ? JSON.parse(this.question.quesAnswer)
  113. : [];
  114. this.quesAnswer = this.modalForm.quesAnswer[0] || null;
  115. },
  116. quesBodyChange() {
  117. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  118. },
  119. answerChange() {
  120. this.modalForm.quesAnswer =
  121. this.quesAnswer || this.quesAnswer === 0 ? [this.quesAnswer] : [];
  122. this.$refs.modalFormComp.validateField(`quesAnswer`, () => {});
  123. },
  124. optionChange() {
  125. const optionCount = this.parentQuestion.quesOptions.length;
  126. if (!this.quesAnswer || this.quesAnswer <= optionCount) return;
  127. this.quesAnswer = null;
  128. this.answerChange();
  129. },
  130. questionInfoChange(questionInfo) {
  131. this.modalForm = Object.assign({}, this.modalForm, questionInfo);
  132. },
  133. validate() {
  134. return this.$refs.modalFormComp.validate();
  135. },
  136. getData() {
  137. let data = { ...this.modalForm };
  138. data.quesAnswer = JSON.stringify(data.quesAnswer);
  139. return data;
  140. },
  141. },
  142. };
  143. </script>