NestedQuestion.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <div class="nested-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. label-width="100px"
  8. >
  9. <el-form-item prop="quesBody" label="大题题干">
  10. <v-editor
  11. v-model="modalForm.quesBody"
  12. :enable-answer-point="IS_CLOZE"
  13. @answer-point-changed="answerPointsChange"
  14. @change="quesBodyChange"
  15. ></v-editor> </el-form-item
  16. ></el-form>
  17. <div class="tips-info">
  18. <p>小题数:{{ modalForm.subQuestions.length }}</p>
  19. </div>
  20. <el-collapse v-model="activeNames" style="margin-top: 10px">
  21. <el-collapse-item
  22. v-for="(subq, index) in modalForm.subQuestions"
  23. :key="subq.questionKey"
  24. :name="index"
  25. >
  26. <div slot="title" class="subq-header">
  27. <h3>
  28. 第{{ index + 1 }}小题({{ subq.questionType | questionType }})
  29. </h3>
  30. <div>
  31. <el-button
  32. v-if="!deleleDisabled"
  33. class="btn-danger"
  34. size="mini"
  35. type="text"
  36. icon="el-icon-delete"
  37. title="删除"
  38. @click.stop="removeSubQuestion(index)"
  39. ></el-button>
  40. </div>
  41. </div>
  42. <component
  43. :is="getStructTypeComp(subq.questionType)"
  44. ref="QuestionEditDetail"
  45. :question="subq"
  46. :show-ques-body="!IS_CLOZE"
  47. ></component>
  48. </el-collapse-item>
  49. </el-collapse>
  50. <el-popover
  51. v-if="!IS_CLOZE && !IS_LISTENING_QUESTION"
  52. popper-class="nested-subq-popover"
  53. trigger="hover"
  54. >
  55. <div class="nested-subq-list">
  56. <div
  57. v-for="item in BASE_QUESTION_TYPES"
  58. :key="item.code"
  59. class="nested-subq-item"
  60. @click="addSubQuestion(item.code)"
  61. >
  62. {{ item.name }}
  63. </div>
  64. </div>
  65. <el-button
  66. slot="reference"
  67. type="primary"
  68. size="small"
  69. icon="el-icon-circle-plus-outline"
  70. >新增小题</el-button
  71. >
  72. </el-popover>
  73. <!-- 听力题只能添加单选题 -->
  74. <el-button
  75. v-if="IS_LISTENING_QUESTION"
  76. type="primary"
  77. size="small"
  78. icon="el-icon-circle-plus-outline"
  79. @click="addSubQuestion('SINGLE_ANSWER_QUESTION')"
  80. >新增小题</el-button
  81. >
  82. </div>
  83. </template>
  84. <script>
  85. import { isAnEmptyRichText, checkRichTextHasAudio } from "@/utils/utils";
  86. import { randomCode } from "@/plugins/utils";
  87. import {
  88. getInitQuestionModel,
  89. STRUCT_TYPE_COMP_DICT,
  90. } from "../model/questionModel";
  91. import BooleanQuestion from "./BooleanQuestion.vue";
  92. import FillBlankQuestion from "./FillBlankQuestion.vue";
  93. import SelectQuestion from "./SelectQuestion.vue";
  94. import TextAnswerQuestion from "./TextAnswerQuestion.vue";
  95. import { BASE_QUESTION_TYPES } from "@/constants/constants";
  96. export default {
  97. name: "NestedQuestion",
  98. components: {
  99. FillBlankQuestion,
  100. SelectQuestion,
  101. TextAnswerQuestion,
  102. BooleanQuestion,
  103. },
  104. props: {
  105. question: {
  106. type: Object,
  107. default() {
  108. return {};
  109. },
  110. },
  111. },
  112. data() {
  113. return {
  114. modalForm: {},
  115. BASE_QUESTION_TYPES,
  116. activeNames: [],
  117. rules: {
  118. quesBody: [
  119. {
  120. validator: (rule, value, callback) => {
  121. if (!value || isAnEmptyRichText(value)) {
  122. return callback(new Error(`请输入题干`));
  123. }
  124. if (this.IS_LISTENING_QUESTION && !checkRichTextHasAudio(value)) {
  125. return callback(new Error(`题干中需要包含音频`));
  126. }
  127. callback();
  128. },
  129. trigger: "change",
  130. },
  131. ],
  132. },
  133. };
  134. },
  135. computed: {
  136. deleleDisabled() {
  137. return this.IS_CLOZE;
  138. },
  139. IS_CLOZE() {
  140. return this.modalForm.questionType === "CLOZE";
  141. },
  142. IS_LISTENING_QUESTION() {
  143. return this.modalForm.questionType === "LISTENING_QUESTION";
  144. },
  145. },
  146. created() {
  147. this.initData();
  148. },
  149. methods: {
  150. initData() {
  151. this.modalForm = this.$objAssign(
  152. getInitQuestionModel(this.question.questionType),
  153. this.question
  154. );
  155. },
  156. getStructTypeComp(questionType) {
  157. return STRUCT_TYPE_COMP_DICT[questionType];
  158. },
  159. addSubQuestion(questionType, index = null) {
  160. let newQuestion = getInitQuestionModel(questionType);
  161. newQuestion.questionKey = randomCode();
  162. newQuestion.courseId = this.question.courseId;
  163. const nindex =
  164. index === null ? this.modalForm.subQuestions.length : index + 1;
  165. this.modalForm.subQuestions.splice(nindex, 0, newQuestion);
  166. if (!this.activeNames.includes(newQuestion.questionKey) && !this.IS_CLOZE)
  167. this.activeNames.push(newQuestion.questionKey);
  168. },
  169. async removeSubQuestion(index) {
  170. if (this.deleleDisabled) return;
  171. const res = await this.$confirm("确定要删除该小题吗?", "提示", {
  172. type: "warning",
  173. }).catch(() => {});
  174. if (res !== "confirm") return;
  175. const delQuestionKey = this.modalForm.subQuestions[index].questionKey;
  176. this.activeNames = this.activeNames.filter((k) => k !== delQuestionKey);
  177. this.modalForm.subQuestions.splice(index, 1);
  178. },
  179. answerPointsChange(answerPointsChanged) {
  180. let subQuestions = [];
  181. answerPointsChanged.forEach((item) => {
  182. const order = Number(item);
  183. if (order === 0) {
  184. let newQuestion = getInitQuestionModel("SINGLE_ANSWER_QUESTION");
  185. newQuestion.questionKey = randomCode();
  186. newQuestion.courseId = this.question.courseId;
  187. subQuestions.push(newQuestion);
  188. } else {
  189. subQuestions.push(this.modalForm.subQuestions[order - 1]);
  190. }
  191. });
  192. this.modalForm.subQuestions = subQuestions;
  193. },
  194. quesBodyChange() {
  195. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  196. },
  197. async validate() {
  198. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  199. if (!valid) return Promise.reject();
  200. if (!this.modalForm.subQuestions.length) {
  201. this.$message.error("请录入小题");
  202. return Promise.reject();
  203. }
  204. let errorQuestionIndexs = [];
  205. for (let i = 0; i < this.$refs.QuestionEditDetail.length; i++) {
  206. const compInst = this.$refs.QuestionEditDetail[i];
  207. const res = await compInst.validate().catch(() => {});
  208. if (!res) {
  209. errorQuestionIndexs.push(i + 1);
  210. }
  211. }
  212. if (errorQuestionIndexs.length) {
  213. this.$message.error(
  214. `请完成第${errorQuestionIndexs.join()}小题的编辑!`
  215. );
  216. return Promise.reject();
  217. }
  218. return true;
  219. },
  220. getData() {
  221. this.modalForm.subQuestions = this.$refs.QuestionEditDetail.map(
  222. (compInst) => compInst.getData()
  223. );
  224. return this.modalForm;
  225. },
  226. },
  227. };
  228. </script>