CardDesign.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <template>
  2. <div class="card-design">
  3. <div class="design-header">
  4. <div class="design-steps">
  5. <div class="step-item" v-for="(step, index) in steps" :key="index">
  6. <i>{{ index + 1 }}</i>
  7. <span>{{ step }}</span>
  8. </div>
  9. </div>
  10. </div>
  11. <!-- actions -->
  12. <div class="design-action">
  13. <div class="design-logo">
  14. <h1>
  15. <i class="el-icon-d-arrow-left" @click="toExit" title="退出"></i>
  16. 答题卡制作
  17. </h1>
  18. </div>
  19. <div class="action-part">
  20. <div class="action-part-title"><h2>基本设置</h2></div>
  21. <div class="action-part-body">
  22. <page-prop-edit @init-page="initPageData"></page-prop-edit>
  23. </div>
  24. </div>
  25. <div class="action-part">
  26. <div class="action-part-title"><h2>试题配置</h2></div>
  27. <div class="action-part-body">
  28. <div class="type-list">
  29. <div
  30. class="type-item"
  31. v-for="(item, index) in TOPIC_LIST"
  32. :key="index"
  33. >
  34. <el-button @click="addNewTopic(item)"
  35. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  36. >
  37. </div>
  38. </div>
  39. <p class="tips-info">提示:点击创建试题</p>
  40. </div>
  41. </div>
  42. <div class="action-part">
  43. <div class="action-part-title"><h2>插入元素</h2></div>
  44. <div class="action-part-body">
  45. <div class="type-list">
  46. <div
  47. class="type-item"
  48. v-for="(item, index) in ELEMENT_LIST"
  49. :key="index"
  50. draggable="true"
  51. @dragstart="dragstart(item)"
  52. >
  53. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  54. </div>
  55. <p class="tips-info">提示:拖动插入元素</p>
  56. </div>
  57. <!-- Develop btns -->
  58. <!-- <card-config-prop-edit></card-config-prop-edit> -->
  59. </div>
  60. <!-- <br /><br /> -->
  61. <!-- <el-button @click="initCard">新建页面</el-button> -->
  62. </div>
  63. <!-- <div class="action-part">
  64. <div class="action-part-title"><h2>阅卷参数</h2></div>
  65. <div class="action-part-body">
  66. <el-button type="primary" @click="modifyParams"
  67. >上传阅卷参数<span class="color-danger"
  68. >({{ paperParams["pageSumScore"] || 0 }}分)</span
  69. ></el-button
  70. >
  71. </div>
  72. </div> -->
  73. </div>
  74. <div class="design-main">
  75. <!-- menus -->
  76. <div class="design-control">
  77. <div class="control-left tab-btns">
  78. <el-button
  79. v-for="(page, pageNo) in pages"
  80. :key="pageNo"
  81. :type="curPageNo === pageNo ? 'primary' : 'default'"
  82. @click="swithPage(pageNo)"
  83. >第{{ pageNo + 1 }}页</el-button
  84. >
  85. </div>
  86. <div class="control-right">
  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. :class="[
  112. 'page-box',
  113. `page-box-${cardConfig.pageSize}`,
  114. `page-box-${curPageNo % 2}`,
  115. { 'page-box-less': pages.length <= 2 }
  116. ]"
  117. v-if="curPage.locators"
  118. >
  119. <!-- locator -->
  120. <div class="page-locator page-locator-top">
  121. <div
  122. v-for="elem in curPage.locators.top"
  123. :key="elem.id"
  124. :id="elem.id"
  125. class="page-locator-item"
  126. ></div>
  127. </div>
  128. <div class="page-locator page-locator-bottom">
  129. <div
  130. v-for="elem in curPage.locators.bottom"
  131. :key="elem.id"
  132. :id="elem.id"
  133. class="page-locator-item"
  134. ></div>
  135. </div>
  136. <!-- inner edit area -->
  137. <div class="page-main-inner">
  138. <div
  139. :class="['page-main', `page-main-${curPage.columns.length}`]"
  140. :style="{ margin: `0 -${curPage.columnGap / 2}px` }"
  141. >
  142. <div
  143. class="page-column"
  144. v-for="(column, columnNo) in curPage.columns"
  145. :key="columnNo"
  146. :style="{ padding: `0 ${curPage.columnGap / 2}px` }"
  147. >
  148. <div
  149. class="page-column-main"
  150. :id="[`column-${curPageNo}-${columnNo}`]"
  151. >
  152. <div class="page-column-body" v-if="column.elements.length">
  153. <topic-element-edit
  154. class="page-column-element"
  155. :data-h="element.h"
  156. v-for="element in column.elements"
  157. :key="element.id"
  158. :data="element"
  159. ></topic-element-edit>
  160. </div>
  161. <div class="page-column-body" v-else>
  162. <div
  163. class="page-column-forbid-area"
  164. v-if="cardConfig.showForbidArea"
  165. >
  166. <p>该区域严禁作答</p>
  167. </div>
  168. </div>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. <!-- outer edit area -->
  174. <div class="page-main-outer">
  175. <page-number
  176. type="rect"
  177. :total="pages.length"
  178. :current="curPageNo + 1"
  179. ></page-number>
  180. <page-number
  181. type="text"
  182. :total="pages.length"
  183. :current="curPageNo + 1"
  184. ></page-number>
  185. </div>
  186. </div>
  187. </div>
  188. </div>
  189. <!-- all topics -->
  190. <div class="topic-list">
  191. <div :class="['page-box', `page-box-${cardConfig.pageSize}`]">
  192. <div class="page-main-inner">
  193. <div
  194. :class="['page-main', `page-main-${cardConfig.columnNumber}`]"
  195. :style="{ margin: `0 -${cardConfig.columnGap / 2}px` }"
  196. >
  197. <div
  198. class="page-column"
  199. :style="{ padding: `0 ${cardConfig.columnGap / 2}px` }"
  200. >
  201. <div class="page-column-main" id="topic-column">
  202. <div class="page-column-body">
  203. <!-- card-head-sample -->
  204. <card-head-sample
  205. :data="cardHeadSampleData"
  206. id="simple-card-head"
  207. v-if="topics.length && cardHeadSampleData"
  208. ></card-head-sample>
  209. <!-- topic element -->
  210. <topic-element-preview
  211. class="page-column-element"
  212. v-for="element in topics"
  213. :key="element.id"
  214. :data="element"
  215. ></topic-element-preview>
  216. </div>
  217. </div>
  218. </div>
  219. </div>
  220. </div>
  221. </div>
  222. </div>
  223. <!-- element-prop-edit -->
  224. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  225. <!-- right-click-menu -->
  226. <right-click-menu @inset-topic="insetNewTopic"></right-click-menu>
  227. <!-- paper-params -->
  228. <paper-params
  229. :pages="pages"
  230. :paper-params="paperParams"
  231. @confirm="paperParamsModified"
  232. ref="PaperParams"
  233. ></paper-params>
  234. <!-- topic select dialog -->
  235. <topic-select-dialog
  236. ref="TopicSelectDialog"
  237. :topics="topicList"
  238. @confirm="addNewTopic"
  239. ></topic-select-dialog>
  240. </div>
  241. </template>
  242. <script>
  243. import { mapState, mapMutations, mapActions } from "vuex";
  244. import {
  245. getElementModel,
  246. getCardHeadModel,
  247. ELEMENT_LIST,
  248. TOPIC_LIST
  249. } from "../elementModel";
  250. import { CARD_VERSION } from "../enumerate";
  251. // import CardConfigPropEdit from "../components/CardConfigPropEdit";
  252. import TopicElementEdit from "../components/TopicElementEdit";
  253. import TopicElementPreview from "../components/TopicElementPreview";
  254. import PagePropEdit from "../components/PagePropEdit";
  255. import ElementPropEdit from "../components/ElementPropEdit";
  256. import RightClickMenu from "../components/RightClickMenu";
  257. import PageNumber from "../components/PageNumber";
  258. import PaperParams from "../components/PaperParams";
  259. import CardHeadSample from "../elements/card-head/CardHead";
  260. import TopicSelectDialog from "../components/TopicSelectDialog";
  261. export default {
  262. name: "card-design",
  263. props: {
  264. content: {
  265. type: Object,
  266. default() {
  267. return {
  268. pages: [],
  269. cardConfig: {},
  270. paperParams: {}
  271. };
  272. }
  273. },
  274. showSaveBtn: {
  275. type: Boolean,
  276. default: true
  277. }
  278. },
  279. components: {
  280. // CardConfigPropEdit,
  281. TopicElementEdit,
  282. TopicElementPreview,
  283. PagePropEdit,
  284. ElementPropEdit,
  285. RightClickMenu,
  286. CardHeadSample,
  287. PageNumber,
  288. PaperParams,
  289. TopicSelectDialog
  290. },
  291. data() {
  292. return {
  293. ELEMENT_LIST,
  294. TOPIC_LIST,
  295. topicList: [],
  296. steps: ["添加标题", "基本设置", "试题配置", "预览生成"],
  297. columnWidth: 0,
  298. isSubmit: false,
  299. canSave: false
  300. };
  301. },
  302. computed: {
  303. ...mapState("card", [
  304. "cardConfig",
  305. "topics",
  306. "pages",
  307. "paperParams",
  308. "curElement",
  309. "curPage",
  310. "curPageNo"
  311. ]),
  312. cardHeadSampleData() {
  313. if (!this.cardConfig["pageSize"]) return;
  314. const data = getCardHeadModel(this.cardConfig);
  315. data.isSimple = true;
  316. return data;
  317. }
  318. },
  319. mounted() {
  320. this.initCard();
  321. },
  322. methods: {
  323. ...mapMutations("card", [
  324. "addPage",
  325. "setCurPage",
  326. "setCurElement",
  327. "setCardConfig",
  328. "setOpenElementEditDialog",
  329. "setCurDragElement",
  330. "setPages",
  331. "setPaperParams",
  332. "setInsetTarget",
  333. "initState"
  334. ]),
  335. ...mapActions("card", [
  336. "resetTopicSeries",
  337. "removePage",
  338. "addElement",
  339. "modifyCardHead",
  340. "modifyElement",
  341. "rebuildPages",
  342. "initTopicsFromPages"
  343. ]),
  344. async initCard() {
  345. const { cardConfig, pages, paperParams } = this.content;
  346. this.setCardConfig(cardConfig);
  347. this.setPaperParams(paperParams);
  348. if (pages && pages.length) {
  349. this.setPages(pages);
  350. this.initTopicsFromPages();
  351. this.resetTopicSeries();
  352. this.setCurPage(0);
  353. } else {
  354. this.initPageData();
  355. }
  356. this.addWatch();
  357. },
  358. initPageData() {
  359. this.modifyCardHead({
  360. ...getCardHeadModel(this.cardConfig)
  361. });
  362. this.$nextTick(() => {
  363. this.rebuildPages();
  364. this.setCurPage(0);
  365. });
  366. },
  367. addNewTopic(item) {
  368. let element = getElementModel(item.type);
  369. element.w = document.getElementById("topic-column").offsetWidth;
  370. this.setCurElement(element);
  371. this.$refs.ElementPropEdit.open();
  372. // to elementPropEdit/ElementPropEdit open topic edit dialog
  373. },
  374. insetNewTopic({ id, type }) {
  375. console.log(id, type);
  376. this.setInsetTarget({ id, type });
  377. if (type === "FILL_QUESTION") {
  378. this.topicList = this.TOPIC_LIST;
  379. } else {
  380. this.topicList = this.TOPIC_LIST.filter(
  381. item => item.type !== "FILL_QUESTION"
  382. );
  383. }
  384. this.$refs.TopicSelectDialog.open();
  385. },
  386. // 元件编辑
  387. dragstart(element) {
  388. this.setCurDragElement(getElementModel(element.type));
  389. },
  390. addWatch() {
  391. this.$watch("cardConfig", val => {
  392. const element = getCardHeadModel(val);
  393. this.modifyCardHead(element);
  394. this.$nextTick(() => {
  395. this.rebuildPages();
  396. });
  397. });
  398. },
  399. swithPage(pindex) {
  400. if (this.curPageNo === pindex) return;
  401. this.setCurPage(pindex);
  402. this.setCurElement({});
  403. },
  404. // paper-params
  405. modifyParams() {
  406. this.$refs.PaperParams.open();
  407. },
  408. paperParamsModified(paperParams) {
  409. this.setPaperParams(paperParams);
  410. },
  411. // save
  412. getCardData(htmlContent = "", model = "") {
  413. const data = {
  414. title: this.cardConfig.cardTitle,
  415. content: model,
  416. htmlContent
  417. };
  418. return data;
  419. },
  420. checkElementCovered() {
  421. let elements = [];
  422. this.pages.forEach(page => {
  423. page.columns.forEach(column => {
  424. column.elements.forEach(element => {
  425. if (element.isCovered) {
  426. elements.push(element.id);
  427. }
  428. });
  429. });
  430. });
  431. return elements.length;
  432. },
  433. checkCardValid() {
  434. if (!this.cardConfig.cardTitle) {
  435. this.$message.error("题卡标题不能为空!");
  436. this.setCurPageNo(0);
  437. setTimeout(() => {
  438. document.getElementById("cardTitleInput").focus();
  439. });
  440. return false;
  441. }
  442. // if (!this.cardConfig.cardDesc) {
  443. // this.$message.error("题卡描述信息不能为空!");
  444. // this.setCurPage(0);
  445. // setTimeout(() => {
  446. // document.getElementById("cardDescInput").focus();
  447. // });
  448. // return false;
  449. // }
  450. if (this.checkElementCovered()) {
  451. this.$message.error("题卡中存在被遮挡的元件,请注意调整!");
  452. return false;
  453. }
  454. return true;
  455. },
  456. getCardJson() {
  457. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  458. return new Promise(resolve => {
  459. setTimeout(() => {
  460. const data = JSON.stringify(
  461. {
  462. version: CARD_VERSION,
  463. cardType: "STANDARD",
  464. cardConfig: this.cardConfig,
  465. paperParams: this.paperParams,
  466. pages: this.pages
  467. },
  468. (k, v) => (k.startsWith("_") ? undefined : v)
  469. );
  470. resolve(data);
  471. }, 100);
  472. });
  473. },
  474. async toPreview() {
  475. if (this.isSubmit) return;
  476. this.isSubmit = true;
  477. const model = await this.getCardJson();
  478. const datas = this.getCardData("", model);
  479. this.$emit("on-preview", datas);
  480. },
  481. async toSave() {
  482. if (!this.checkCardValid()) return;
  483. if (this.isSubmit) return;
  484. this.isSubmit = true;
  485. const model = await this.getCardJson();
  486. const datas = this.getCardData("", model);
  487. this.$emit("on-save", datas);
  488. },
  489. toSubmit() {
  490. if (this.isSubmit) return;
  491. if (!this.checkCardValid()) return;
  492. this.$emit("on-submit", {
  493. cardConfig: this.cardConfig,
  494. pages: this.pages,
  495. paperParams: this.paperParams
  496. });
  497. },
  498. toExit() {
  499. this.$emit("on-exit");
  500. },
  501. loading() {
  502. this.isSubmit = true;
  503. },
  504. unloading() {
  505. this.isSubmit = false;
  506. }
  507. },
  508. beforeDestroy() {
  509. this.initState();
  510. }
  511. };
  512. </script>