CardDesign.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <template>
  2. <div class="card-design">
  3. <div class="design-header">
  4. <div class="design-tips">
  5. <i class="el-icon-info"></i>
  6. 请将答题卡里设计的客观题的题目数量和选项个数与试卷题目保持一致
  7. </div>
  8. <div class="design-back" @click="toExit">
  9. 返回<i class="el-icon-arrow-right"></i>
  10. </div>
  11. </div>
  12. <!-- actions -->
  13. <div class="design-action">
  14. <div class="design-logo">
  15. <h1>
  16. <i class="el-icon-d-arrow-left" @click="toExit" title="退出"></i>
  17. 答题卡制作
  18. </h1>
  19. </div>
  20. <div class="action-part">
  21. <div class="action-part-title"><h2>基本设置</h2></div>
  22. <div class="action-part-body">
  23. <page-prop-edit @init-page="initPageData"></page-prop-edit>
  24. </div>
  25. </div>
  26. <div class="action-part">
  27. <div class="action-part-title"><h2>试题配置</h2></div>
  28. <div class="action-part-body">
  29. <div class="type-list">
  30. <div class="type-item" v-for="item in TOPIC_LIST" :key="item.type">
  31. <el-button @click="addNewTopic(item)"
  32. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  33. >
  34. </div>
  35. <div
  36. class="type-item"
  37. v-for="item in NOT_TOPIC_LIST"
  38. :key="item.type"
  39. >
  40. <el-button @click="addNewTopic(item)"
  41. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  42. >
  43. </div>
  44. </div>
  45. <p class="tips-info">提示:点击创建试题</p>
  46. </div>
  47. </div>
  48. <div class="action-part">
  49. <div class="action-part-title"><h2>插入元素</h2></div>
  50. <div class="action-part-body">
  51. <div class="type-list">
  52. <div
  53. class="type-item"
  54. v-for="(item, index) in ELEMENT_LIST"
  55. :key="index"
  56. draggable="true"
  57. @dragstart="dragstart(item)"
  58. >
  59. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  60. </div>
  61. <p class="tips-info">提示:拖动插入元素</p>
  62. </div>
  63. <!-- card config edit -->
  64. <card-config-prop-edit
  65. v-if="isFromCardManage"
  66. class="mt-2"
  67. ></card-config-prop-edit>
  68. </div>
  69. </div>
  70. </div>
  71. <div id="design-main" class="design-main">
  72. <!-- menus -->
  73. <div class="design-control">
  74. <div class="control-left tab-btns">
  75. <el-button
  76. v-for="(page, pageNo) in pages"
  77. :key="pageNo"
  78. :type="curPageNo === pageNo ? 'primary' : 'default'"
  79. @click="swithPage(pageNo)"
  80. >第{{ pageNo + 1 }}页</el-button
  81. >
  82. </div>
  83. <div class="control-right">
  84. <div v-if="hasUnsubmitCont" class="tips-info tips-error">
  85. <i class="el-icon-warning"></i>有未提交数据
  86. </div>
  87. <el-button
  88. type="success"
  89. :loading="isSubmit"
  90. :disabled="!pages.length"
  91. @click="toPreview"
  92. >预览</el-button
  93. >
  94. <!-- <el-button
  95. v-if="showSaveBtn"
  96. type="primary"
  97. :loading="isSubmit"
  98. :disabled="canSave || !pages.length"
  99. @click="toSave"
  100. >暂存</el-button
  101. > -->
  102. <el-button type="primary" :loading="isSubmit" @click="toSubmit"
  103. >提交</el-button
  104. >
  105. </div>
  106. </div>
  107. <!-- edit body -->
  108. <div class="design-body">
  109. <!-- 注意:后台要替换内容,改类名时,要注意 -->
  110. <div
  111. v-for="(page, pageNo) in pages"
  112. :key="pageNo"
  113. :id="`edit-page-box-${pageNo}`"
  114. :class="[
  115. 'page-box',
  116. `page-box-${cardConfig.pageSize}`,
  117. `page-box-${pageNo % 2}`,
  118. { 'page-box-less': pages.length <= 2 },
  119. ]"
  120. >
  121. <!-- locator -->
  122. <div class="page-locator page-locator-top">
  123. <div
  124. v-for="elem in page.locators.top"
  125. :key="elem.id"
  126. :id="elem.id"
  127. class="page-locator-item"
  128. ></div>
  129. </div>
  130. <div class="page-locator page-locator-bottom">
  131. <div
  132. v-for="elem in page.locators.bottom"
  133. :key="elem.id"
  134. :id="elem.id"
  135. class="page-locator-item"
  136. ></div>
  137. </div>
  138. <!-- inner edit area -->
  139. <div class="page-main-inner">
  140. <div
  141. :class="['page-main', `page-main-${page.columns.length}`]"
  142. :style="{ margin: `0 -${page.columnGap / 2}px` }"
  143. >
  144. <div
  145. class="page-column"
  146. v-for="(column, columnNo) in page.columns"
  147. :key="columnNo"
  148. :style="{ padding: `0 ${page.columnGap / 2}px` }"
  149. >
  150. <div
  151. class="page-column-main"
  152. :id="[`column-${pageNo}-${columnNo}`]"
  153. >
  154. <div class="page-column-body" v-if="column.elements.length">
  155. <topic-element-edit
  156. class="page-column-element"
  157. :data-h="element.h"
  158. v-for="element in column.elements"
  159. :key="element.id"
  160. :data="element"
  161. ></topic-element-edit>
  162. </div>
  163. <div class="page-column-body" v-else>
  164. <div
  165. class="page-column-forbid-area"
  166. v-if="cardConfig.showForbidArea"
  167. >
  168. <p>该区域严禁作答</p>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. </div>
  175. <!-- outer edit area -->
  176. <div class="page-main-outer">
  177. <page-number
  178. type="rect"
  179. :total="pages.length"
  180. :current="pageNo + 1"
  181. ></page-number>
  182. <page-number
  183. type="text"
  184. :total="pages.length"
  185. :current="pageNo + 1"
  186. ></page-number>
  187. <elem-undertaking
  188. v-if="cardConfig.undertakingEnable && pageNo === pages.length - 1"
  189. :content="cardConfig.undertakingBody"
  190. :page-size="cardConfig.pageSize"
  191. ></elem-undertaking>
  192. <course-barcode
  193. v-if="pageNo % 2 === 0"
  194. :data="cardConfig"
  195. ></course-barcode>
  196. </div>
  197. </div>
  198. </div>
  199. </div>
  200. <!-- all topics -->
  201. <div v-if="!isSubmit" class="topic-list">
  202. <div :class="['page-box', `page-box-${cardConfig.pageSize}`]">
  203. <div class="page-main-inner">
  204. <div
  205. :class="['page-main', `page-main-${cardConfig.columnNumber}`]"
  206. :style="{ margin: `0 -${cardConfig.columnGap / 2}px` }"
  207. >
  208. <div
  209. class="page-column"
  210. :style="{ padding: `0 ${cardConfig.columnGap / 2}px` }"
  211. >
  212. <div class="page-column-main" id="topic-column">
  213. <div class="page-column-body">
  214. <!-- card-head-sample -->
  215. <card-head-sample
  216. :data="cardHeadSampleData"
  217. id="simple-card-head"
  218. v-if="topics.length && cardHeadSampleData"
  219. ></card-head-sample>
  220. <!-- topic element -->
  221. <topic-element-preview
  222. class="page-column-element"
  223. v-for="element in topics"
  224. :key="element.id"
  225. :data="element"
  226. ></topic-element-preview>
  227. </div>
  228. </div>
  229. </div>
  230. </div>
  231. </div>
  232. </div>
  233. </div>
  234. <!-- element-prop-edit -->
  235. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  236. <!-- right-click-menu -->
  237. <right-click-menu @inset-topic="insetNewTopic"></right-click-menu>
  238. <!-- topic select dialog -->
  239. <topic-select-dialog
  240. ref="TopicSelectDialog"
  241. :topics="topicList"
  242. :insert-topic="preInsertTopic"
  243. @confirm="addNewTopic"
  244. ></topic-select-dialog>
  245. </div>
  246. </template>
  247. <script>
  248. import { mapState, mapMutations, mapActions } from "vuex";
  249. import {
  250. getElementModel,
  251. getCardHeadModel,
  252. ELEMENT_LIST,
  253. TOPIC_LIST,
  254. NOT_TOPIC_LIST,
  255. OTHER_ELEMENT,
  256. } from "../elementModel";
  257. import { CARD_VERSION } from "../enumerate";
  258. import CardConfigPropEdit from "../components/CardConfigPropEdit";
  259. import TopicElementEdit from "../components/TopicElementEdit";
  260. import TopicElementPreview from "../components/TopicElementPreview";
  261. import PagePropEdit from "../components/PagePropEdit";
  262. import ElementPropEdit from "../components/ElementPropEdit";
  263. import RightClickMenu from "../components/RightClickMenu";
  264. import PageNumber from "../components/PageNumber";
  265. import CardHeadSample from "../elements/card-head/CardHead";
  266. import TopicSelectDialog from "../components/TopicSelectDialog";
  267. import ElemUndertaking from "../elements/undertaking/ElemUndertaking.vue";
  268. import CourseBarcode from "./CourseBarcode.vue";
  269. export default {
  270. name: "card-design",
  271. props: {
  272. content: {
  273. type: Object,
  274. default() {
  275. return {
  276. pages: [],
  277. cardConfig: {},
  278. answers: {},
  279. };
  280. },
  281. },
  282. showSaveBtn: {
  283. type: Boolean,
  284. default: true,
  285. },
  286. },
  287. components: {
  288. CardConfigPropEdit,
  289. TopicElementEdit,
  290. TopicElementPreview,
  291. PagePropEdit,
  292. ElementPropEdit,
  293. RightClickMenu,
  294. CardHeadSample,
  295. PageNumber,
  296. TopicSelectDialog,
  297. ElemUndertaking,
  298. CourseBarcode,
  299. },
  300. data() {
  301. return {
  302. ELEMENT_LIST,
  303. TOPIC_LIST,
  304. NOT_TOPIC_LIST,
  305. topicList: [],
  306. columnWidth: 0,
  307. isSubmit: false,
  308. canSave: false,
  309. hasUnsubmitCont: false,
  310. preInsertTopic: {},
  311. };
  312. },
  313. computed: {
  314. ...mapState("card", [
  315. "cardConfig",
  316. "topics",
  317. "pages",
  318. "curElement",
  319. "curPage",
  320. "curPageNo",
  321. "explainHeight",
  322. ]),
  323. cardHeadSampleData() {
  324. if (!this.cardConfig["pageSize"]) return;
  325. const data = getCardHeadModel(this.cardConfig);
  326. data.isSimple = true;
  327. return data;
  328. },
  329. isFromCardManage() {
  330. return this.$route.name === "CardManage";
  331. },
  332. },
  333. mounted() {
  334. this.initCard();
  335. },
  336. beforeDestroy() {
  337. this.initState();
  338. },
  339. methods: {
  340. ...mapMutations("card", [
  341. "addPage",
  342. "setCurPage",
  343. "setCurElement",
  344. "setCardConfig",
  345. "setOpenElementEditDialog",
  346. "setCurDragElement",
  347. "setPages",
  348. "setExplainHeight",
  349. "initState",
  350. ]),
  351. ...mapActions("card", [
  352. "resetTopicSeries",
  353. "removePage",
  354. "addElement",
  355. "modifyCardHead",
  356. "modifyElement",
  357. "rebuildPages",
  358. "initTopicsFromPages",
  359. "scrollToElementPage",
  360. ]),
  361. async initCard() {
  362. const { cardConfig, pages } = this.content;
  363. this.setCardConfig(cardConfig);
  364. if (pages && pages.length) {
  365. this.setPages(pages);
  366. this.initTopicsFromPages();
  367. this.resetTopicSeries();
  368. this.setCurPage(0);
  369. } else {
  370. this.initPageData();
  371. }
  372. this.addWatch();
  373. this.$nextTick(() => {
  374. this.updateExamplainHeight();
  375. this.registSrollEvent();
  376. });
  377. },
  378. initPageData() {
  379. this.modifyCardHead({
  380. ...getCardHeadModel(this.cardConfig),
  381. });
  382. this.$nextTick(() => {
  383. this.rebuildPages();
  384. this.setCurPage(0);
  385. });
  386. },
  387. updateExamplainHeight() {
  388. let columnHeight = document.getElementById("topic-column").offsetHeight;
  389. if (this.cardConfig.subjectiveAttention) columnHeight -= 24;
  390. this.setExplainHeight(Math.floor(columnHeight / 4));
  391. },
  392. addNewTopic(item) {
  393. if (!item.type) {
  394. this.$message.error("试题类型丢失!");
  395. return;
  396. }
  397. let element = getElementModel(item.type);
  398. element.w = document.getElementById("topic-column").offsetWidth;
  399. if (
  400. ["FILL_QUESTION_SIMPLE", "FILL_QUESTION_MULTIPLY"].includes(item.type)
  401. ) {
  402. element.optionCount = this.cardConfig.defaultOptionNumber;
  403. } else if (item.type === "EXPLAIN") {
  404. element.explainHeight = this.explainHeight;
  405. }
  406. if (item.type === "FORBID_AREA") {
  407. const lastTopicElement = this.topics.findLast(
  408. (item) => !OTHER_ELEMENT.includes(item.type)
  409. );
  410. element.sign = lastTopicElement ? lastTopicElement.sign : "objective";
  411. this.addElement(element);
  412. this.setCurElement(element);
  413. this.$nextTick(() => {
  414. this.rebuildPages();
  415. this.$nextTick(() => {
  416. this.scrollToElementPage(element);
  417. });
  418. });
  419. } else {
  420. if (item.insertTopic && item.insertTopic.id) {
  421. element.insertTopic = item.insertTopic;
  422. }
  423. this.setCurElement(element);
  424. this.$refs.ElementPropEdit.open();
  425. // to elementPropEdit/ElementPropEdit open topic edit dialog
  426. }
  427. },
  428. insetNewTopic({ id, type }) {
  429. this.preInsertTopic = { id, type };
  430. this.topicList = this.TOPIC_LIST;
  431. this.$refs.TopicSelectDialog.open();
  432. },
  433. // 元件编辑
  434. dragstart(element) {
  435. this.setCurDragElement(getElementModel(element.type));
  436. },
  437. addWatch() {
  438. this.$watch("cardConfig", (val) => {
  439. const element = getCardHeadModel(val);
  440. this.modifyCardHead(element);
  441. this.$nextTick(() => {
  442. this.rebuildPages();
  443. });
  444. });
  445. },
  446. // 页面切换
  447. swithPage(pindex) {
  448. if (this.curPageNo === pindex) return;
  449. let pageTops = this.pages.map((page, pageNo) => {
  450. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  451. });
  452. pageTops = pageTops.map((item) => item - pageTops[0]);
  453. document.getElementById("design-main").scrollTop = pageTops[pindex];
  454. this.setCurPage(pindex);
  455. this.setCurElement({});
  456. },
  457. registSrollEvent() {
  458. document.getElementById("design-main").addEventListener("scroll", (e) => {
  459. e.preventDefault();
  460. e.stopPropagation();
  461. let pageTops = this.pages.map((page, pageNo) => {
  462. return document.getElementById(`edit-page-box-${pageNo}`).offsetTop;
  463. });
  464. const pageRangeHeight =
  465. document.getElementById(`edit-page-box-0`).offsetHeight * 0.2;
  466. pageTops = pageTops.map((item) => item - pageTops[0] - pageRangeHeight);
  467. const scrollTop = e.target.scrollTop;
  468. // console.log(pageTops, scrollTop);
  469. const targePageTop = pageTops.find((item) => scrollTop < item);
  470. const pageNo = targePageTop
  471. ? pageTops.indexOf(targePageTop) - 1
  472. : pageTops.length - 1;
  473. if (this.curPageNo === pageNo) return;
  474. this.setCurPage(pageNo);
  475. });
  476. },
  477. // save
  478. getCardData(htmlContent = "", model = "") {
  479. const data = {
  480. title: this.cardConfig.cardTitle,
  481. pageSize: this.cardConfig.pageSize,
  482. openAb: this.cardConfig.aOrB,
  483. content: model,
  484. htmlContent,
  485. };
  486. return data;
  487. },
  488. checkElementCovered() {
  489. let elements = [];
  490. this.pages.forEach((page) => {
  491. page.columns.forEach((column) => {
  492. column.elements.forEach((element) => {
  493. if (element.isCovered) {
  494. elements.push(element.id);
  495. }
  496. });
  497. });
  498. });
  499. return elements.length;
  500. },
  501. checkCardValid() {
  502. if (!this.cardConfig.cardTitle) {
  503. this.$message.error("题卡标题不能为空!");
  504. this.setCurPageNo(0);
  505. setTimeout(() => {
  506. document.getElementById("cardTitleInput").focus();
  507. });
  508. return false;
  509. }
  510. if (this.checkElementCovered()) {
  511. this.$message.error("题卡中存在被遮挡的元件,请注意调整!");
  512. return false;
  513. }
  514. return true;
  515. },
  516. getCardJson() {
  517. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  518. return new Promise((resolve) => {
  519. setTimeout(() => {
  520. const data = JSON.stringify(
  521. {
  522. version: CARD_VERSION,
  523. cardType: "STANDARD",
  524. cardConfig: this.cardConfig,
  525. pages: this.pages,
  526. // 来自题库的试卷生成的题卡会有答案信息
  527. answers: this.content.answers || {},
  528. },
  529. (k, v) => (k.startsWith("_") ? undefined : v)
  530. );
  531. resolve(data);
  532. }, 100);
  533. });
  534. },
  535. async toPreview() {
  536. this.$emit("on-preview", {
  537. cardConfig: this.cardConfig,
  538. pages: this.pages,
  539. });
  540. },
  541. async toSave() {
  542. const model = await this.getCardJson();
  543. const datas = this.getCardData("", model);
  544. this.$emit("on-save", datas);
  545. },
  546. toSubmit() {
  547. if (this.isSubmit) return;
  548. if (!this.checkCardValid()) return;
  549. this.$emit("on-submit", {
  550. cardConfig: {
  551. ...this.cardConfig,
  552. courseCodeBarcodeSrc: "",
  553. courseCodeBarcodeName: "",
  554. },
  555. pages: this.pages,
  556. answers: this.content.answers || {},
  557. });
  558. },
  559. toExit() {
  560. this.$emit("on-exit");
  561. },
  562. loading() {
  563. this.isSubmit = true;
  564. },
  565. unloading() {
  566. this.isSubmit = false;
  567. },
  568. updateUnsubmitStatus(status) {
  569. this.hasUnsubmitCont = status;
  570. },
  571. },
  572. };
  573. </script>