NestedQuestion.vue 6.8 KB

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