SelectQuestion.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <!-- 选择题 -->
  2. <template>
  3. <QuestionBase>
  4. <template #content>
  5. <div class="stem">
  6. <RichText v-model="data.stem" :font-size="18" placeholder="输入题干" />
  7. <RichText
  8. v-if="isEnable(data.property.is_enable_description)"
  9. v-model="data.description"
  10. placeholder="输入提示"
  11. />
  12. </div>
  13. <div class="content">
  14. <ul>
  15. <li v-for="(item, i) in data.option_list" :key="i" class="content-item">
  16. <span class="question-number" title="双击切换序号类型" @dblclick="changeOptionType(data)">
  17. {{ computedQuestionNumber(i, data.option_number_show_mode) }}
  18. </span>
  19. <div v-if="isEnable(data.property.is_option_subdivision)" class="option-list">
  20. <div v-for="li in item.data_list" :key="li.mark" class="option-content">
  21. <span
  22. v-if="data.property.select_type === selectTypeList[0].value"
  23. :class="['selectionbox', { active: isAnswer(li.mark, item.mark) }]"
  24. @click="selectAnswer(li.mark, item.mark)"
  25. ></span>
  26. <span
  27. v-else
  28. :class="['checkbox', { active: isAnswer(li.mark, item.mark) }]"
  29. @click="selectAnswer(li.mark, item.mark)"
  30. >
  31. <SvgIcon icon-class="check-mark" width="10" height="7" />
  32. </span>
  33. <RichText v-model="li.content" placeholder="输入内容" :inline="true" />
  34. </div>
  35. </div>
  36. <div v-else class="option-content">
  37. <span
  38. v-if="data.property.select_type === selectTypeList[0].value"
  39. :class="['selectionbox', { active: isAnswer(item.mark) }]"
  40. @click="selectAnswer(item.mark)"
  41. ></span>
  42. <span v-else :class="['checkbox', { active: isAnswer(item.mark) }]" @click="selectAnswer(item.mark)">
  43. <SvgIcon icon-class="check-mark" width="10" height="7" />
  44. </span>
  45. <RichText v-model="item.content" placeholder="输入内容" :inline="true" />
  46. </div>
  47. <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i)" />
  48. </li>
  49. </ul>
  50. </div>
  51. <div class="footer">
  52. <span class="add-option" @click="addOption">
  53. <SvgIcon icon-class="add-circle" size="14" /> <span>增加选项</span>
  54. </span>
  55. </div>
  56. </template>
  57. <template #property>
  58. <el-form :model="data.property" label-width="72px" label-position="left">
  59. <el-form-item label="题号">
  60. <el-input v-model="data.property.question_number" />
  61. </el-form-item>
  62. <el-form-item>
  63. <el-radio
  64. v-for="{ value, label } in questionNumberTypeList"
  65. :key="value"
  66. v-model="data.other.question_number_type"
  67. :label="value"
  68. >
  69. {{ label }}
  70. </el-radio>
  71. </el-form-item>
  72. <el-form-item label="题干题号">
  73. <el-select v-model="data.property.stem_question_number_font_size">
  74. <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
  75. </el-select>
  76. </el-form-item>
  77. <el-form-item label="选项题号">
  78. <el-select v-model="data.property.option_question_number_font_size">
  79. <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
  80. </el-select>
  81. </el-form-item>
  82. <el-form-item label="提示">
  83. <el-radio
  84. v-for="{ value, label } in switchOption"
  85. :key="value"
  86. v-model="data.property.is_enable_description"
  87. :label="value"
  88. >
  89. {{ label }}
  90. </el-radio>
  91. </el-form-item>
  92. <el-form-item label="选项">
  93. <el-radio
  94. v-for="{ value, label } in selectTypeList"
  95. :key="value"
  96. v-model="data.property.select_type"
  97. :label="value"
  98. @input="changeSelectType"
  99. >
  100. {{ label }}
  101. </el-radio>
  102. </el-form-item>
  103. <el-form-item label="选项细分">
  104. <el-radio
  105. v-for="{ value, label } in switchOption"
  106. :key="value"
  107. v-model="data.property.is_option_subdivision"
  108. :label="value"
  109. @input="changeOptionSubdivision"
  110. >
  111. {{ label }}
  112. </el-radio>
  113. </el-form-item>
  114. <el-form-item>
  115. <el-input-number
  116. v-model="data.property.option_number"
  117. :min="2"
  118. :max="5"
  119. :step="1"
  120. :disabled="!isEnable(data.property.is_option_subdivision)"
  121. @change="changeOptionNumber"
  122. />
  123. </el-form-item>
  124. <el-form-item label="分值">
  125. <el-radio
  126. v-for="{ value, label } in scoreTypeList"
  127. :key="value"
  128. v-model="data.property.score_type"
  129. :label="value"
  130. :disabled="true"
  131. >
  132. {{ label }}
  133. </el-radio>
  134. </el-form-item>
  135. <el-form-item>
  136. <el-input-number
  137. v-model="data.property.score"
  138. :min="0"
  139. :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
  140. />
  141. </el-form-item>
  142. </el-form>
  143. </template>
  144. </QuestionBase>
  145. </template>
  146. <script>
  147. import QuestionMixin from '../common/QuestionMixin.js';
  148. import { selectTypeList, scoreTypeList, changeOptionType, isEnable } from '@/views/exercise_questions/data/common';
  149. import { getSelectData, getOption, getSubdivisionOption } from '@/views/exercise_questions/data/select';
  150. export default {
  151. name: 'SelectQuestion',
  152. mixins: [QuestionMixin],
  153. data() {
  154. return {
  155. selectTypeList,
  156. changeOptionType,
  157. data: getSelectData(),
  158. };
  159. },
  160. watch: {
  161. 'data.property.is_option_subdivision': {
  162. handler(val) {
  163. this.data.answer.is_option_subdivision = val;
  164. },
  165. immediate: true,
  166. },
  167. },
  168. methods: {
  169. /**
  170. * 智能识别
  171. * @param {String} text 识别数据
  172. */
  173. recognition(text) {
  174. let arr = text
  175. .split(/[\r\n]/)
  176. .map((item) => item.trim())
  177. .filter((item) => item);
  178. if (arr.length > 0) {
  179. let sliceLength = 0;
  180. if (arr[0].substring(0, 3) === '题干:') {
  181. this.data.stem = arr[0].substring(3);
  182. sliceLength += 1;
  183. }
  184. if (arr.slice(sliceLength)[0] && arr.slice(sliceLength)[0].substring(0, 3) === '提示:') {
  185. this.data.description = arr.slice(sliceLength)[0].substring(3);
  186. this.data.property.is_enable_description = 'true';
  187. sliceLength += 1;
  188. } else {
  189. this.data.description = '';
  190. this.data.property.is_enable_description = 'false';
  191. }
  192. this.data.option_list = arr.slice(sliceLength).map((content) => getOption(content));
  193. }
  194. },
  195. changeSelectType(val) {
  196. if (val === selectTypeList[0].value && this.data.answer.answer_list.length > 1) {
  197. if (isEnable(this.data.property.is_option_subdivision)) {
  198. this.data.answer.answer_list = [];
  199. } else {
  200. this.data.answer.answer_list = [this.data.answer.answer_list[0]];
  201. }
  202. }
  203. },
  204. isAnswer(mark, parent_mark) {
  205. if (isEnable(this.data.property.is_option_subdivision)) {
  206. return this.data.answer.answer_list.find((item) => item.mark === parent_mark)?.value_list?.includes(mark);
  207. }
  208. return this.data.answer.answer_list.includes(mark);
  209. },
  210. selectAnswer(mark, parent_mark) {
  211. let is_option_subdivision = isEnable(this.data.property.is_option_subdivision);
  212. let answer_list = this.data.answer.answer_list;
  213. let index = is_option_subdivision
  214. ? answer_list.findIndex((item) => item.mark === parent_mark)
  215. : answer_list.indexOf(mark);
  216. if (this.data.property.select_type === selectTypeList[0].value) {
  217. if (is_option_subdivision) {
  218. if (index === -1) {
  219. answer_list.push({ mark: parent_mark, value_list: [mark] });
  220. } else {
  221. this.$set(this.data.answer.answer_list[index], 'value_list', [mark]);
  222. }
  223. } else {
  224. this.$set(this.data.answer, 'answer_list', [mark]);
  225. }
  226. }
  227. if (this.data.property.select_type === selectTypeList[1].value) {
  228. if (is_option_subdivision) {
  229. if (index === -1) {
  230. answer_list.push({ mark: parent_mark, value_list: [mark] });
  231. } else {
  232. let mIndex = answer_list[index].value_list.findIndex((item) => item.mark === mark);
  233. if (mIndex === -1) {
  234. answer_list[index].value_list.push(mark);
  235. } else {
  236. answer_list[index].value_list.splice(mIndex, 1);
  237. }
  238. }
  239. return;
  240. }
  241. if (index === -1) {
  242. answer_list.push(mark);
  243. } else {
  244. answer_list.splice(index, 1);
  245. }
  246. }
  247. },
  248. addOption() {
  249. this.data.option_list.push(
  250. isEnable(this.data.property.is_option_subdivision)
  251. ? getSubdivisionOption(this.data.property.option_number)
  252. : getOption(),
  253. );
  254. },
  255. /**
  256. * 修改是否选项细分
  257. * @param {'true'|'false'} val
  258. */
  259. changeOptionSubdivision(val) {
  260. // 现在,选项细分开启分值类型为细分,关闭为总分
  261. this.data.property.score_type = scoreTypeList[isEnable(val) ? 1 : 0].value;
  262. if (isEnable(val)) {
  263. // 创建与当前 option_list 相同数量的选项,选项内的细分数量与 property.option_number 数量一样
  264. this.data.answer.answer_list = [];
  265. this.data.option_list = Array.from({ length: this.data.option_list.length }, () => {
  266. return getSubdivisionOption(this.data.property.option_number);
  267. });
  268. } else {
  269. this.data.answer.answer_list = [];
  270. this.data.option_list = Array.from({ length: this.data.option_list.length }, getOption);
  271. }
  272. },
  273. /**
  274. * 修改选项细分数量,并尽量保留以前的数据
  275. * @param {Number} val
  276. */
  277. changeOptionNumber(val) {
  278. this.data.answer.answer_list = [];
  279. this.data.option_list = this.data.option_list.map(({ data_list, mark }) => {
  280. if (data_list.length < val) {
  281. return { mark, data_list: [...data_list, ...Array.from({ length: val - data_list.length }, getOption)] };
  282. }
  283. return { mark, data_list: data_list.slice(0, val) };
  284. });
  285. },
  286. },
  287. };
  288. </script>
  289. <style lang="scss" scoped>
  290. .content {
  291. .option-list {
  292. display: flex;
  293. flex: 1;
  294. column-gap: 4px;
  295. }
  296. .option-content {
  297. .selectionbox {
  298. display: inline-flex;
  299. align-items: center;
  300. justify-content: center;
  301. width: 16px;
  302. height: 16px;
  303. margin-right: 8px;
  304. cursor: pointer;
  305. border: 1px solid #333;
  306. border-radius: 50%;
  307. &.active {
  308. &::before {
  309. display: inline-block;
  310. width: 6px;
  311. height: 6px;
  312. content: '';
  313. background-color: #333;
  314. border-radius: 50%;
  315. }
  316. }
  317. }
  318. .checkbox {
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. width: 16px;
  323. height: 16px;
  324. margin-right: 8px;
  325. cursor: pointer;
  326. background-color: #fff;
  327. border: 1px solid #dcdfe6;
  328. border-radius: 2px;
  329. .svg-icon {
  330. display: none;
  331. }
  332. &.active {
  333. color: #fff;
  334. background-color: $light-main-color;
  335. .svg-icon {
  336. display: block;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. </style>