CardDesign.vue 15 KB

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