NestedQuestion.vue 6.3 KB

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