CardFreeDesign.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <div class="card-design card-free-design">
  3. <div class="design-header">
  4. <el-button
  5. class="btn-help"
  6. icon="el-icon-question"
  7. type="text"
  8. @click="showHelp"
  9. ></el-button>
  10. <div class="design-back" @click="toExit">
  11. <i class="el-icon-d-arrow-left" title="退出"></i>
  12. 返回
  13. </div>
  14. </div>
  15. <!-- actions -->
  16. <div class="design-action">
  17. <div class="design-logo">
  18. <h1>
  19. <i class="el-icon-d-arrow-left" @click="toExit" title="退出"></i>
  20. 答题卡制作
  21. </h1>
  22. </div>
  23. <div class="action-part">
  24. <div class="action-part-title"><h2>基本设置</h2></div>
  25. <div class="action-part-body">
  26. <page-prop-edit></page-prop-edit>
  27. </div>
  28. </div>
  29. <div class="action-part">
  30. <div class="action-part-title"><h2>当前页面设置</h2></div>
  31. <div class="action-part-body">
  32. <edit-page></edit-page>
  33. </div>
  34. </div>
  35. <div class="action-part">
  36. <div class="action-part-title"><h2>复杂元素</h2></div>
  37. <div class="action-part-body">
  38. <div class="type-list">
  39. <div
  40. class="type-item"
  41. v-for="(item, index) in TOPIC_LIST"
  42. :key="index"
  43. draggable="true"
  44. @dragstart="dragstart(item)"
  45. >
  46. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  47. </div>
  48. </div>
  49. <p class="tips-info">提示:拖动插入元素</p>
  50. </div>
  51. </div>
  52. <div class="action-part">
  53. <div class="action-part-title"><h2>简单元素</h2></div>
  54. <div class="action-part-body">
  55. <div class="type-list">
  56. <div
  57. class="type-item"
  58. v-for="(item, index) in ELEMENT_LIST"
  59. :key="index"
  60. draggable="true"
  61. @dragstart="dragstart(item)"
  62. >
  63. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  64. </div>
  65. <p class="tips-info">提示:拖动插入元素</p>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="action-part">
  70. <div class="action-part-title"><h2>元素面板</h2></div>
  71. <div class="action-part-body">
  72. <!-- element-tier-edit -->
  73. <element-tier-edit ref="ElementTierEdit"></element-tier-edit>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="design-main">
  78. <!-- menus -->
  79. <div class="design-control">
  80. <div class="control-left tab-btns">
  81. <el-button
  82. v-for="(page, pageNo) in pages"
  83. :key="pageNo"
  84. :type="curPageNo === pageNo ? 'primary' : 'default'"
  85. @click="swithPage(pageNo)"
  86. >
  87. <span>第{{ pageNo + 1 }}页</span>
  88. <span class="page-delete" @click.stop="toDeletePage(page)"
  89. ><i class="el-icon-error"></i
  90. ></span>
  91. </el-button>
  92. <el-button icon="el-icon-plus" @click="toAddPage"></el-button>
  93. </div>
  94. <div class="control-right">
  95. <div v-if="hasUnsubmitCont" class="tips-info tips-error">
  96. <i class="el-icon-warning"></i>有未提交数据
  97. </div>
  98. <el-button
  99. type="success"
  100. :loading="isSubmit"
  101. :disabled="!pages.length"
  102. @click="toPreview"
  103. >预览</el-button
  104. >
  105. <!-- <el-button
  106. v-if="showSaveBtn"
  107. type="primary"
  108. :loading="isSubmit"
  109. :disabled="canSave || !pages.length"
  110. @click="toSave"
  111. >暂存</el-button
  112. > -->
  113. <el-button type="primary" :loading="isSubmit" @click="toSubmit"
  114. >提交</el-button
  115. >
  116. </div>
  117. </div>
  118. <!-- edit body -->
  119. <div class="design-body">
  120. <div
  121. :class="[
  122. 'page-box',
  123. `page-box-${curPage.pageSize}`,
  124. `page-box-${curPageNo % 2}`,
  125. { 'page-box-less': pages.length <= 2 },
  126. ]"
  127. v-if="curPage.id"
  128. >
  129. <!-- locator -->
  130. <div class="page-locator page-locator-top">
  131. <div
  132. v-for="elem in curPage.locators.top"
  133. :key="elem.id"
  134. :id="elem.id"
  135. class="page-locator-item"
  136. ></div>
  137. </div>
  138. <div class="page-locator page-locator-bottom">
  139. <div
  140. v-for="elem in curPage.locators.bottom"
  141. :key="elem.id"
  142. :id="elem.id"
  143. class="page-locator-item"
  144. ></div>
  145. </div>
  146. <!-- inner edit area -->
  147. <div class="page-main-inner">
  148. <div
  149. :class="['page-main', `page-main-${curPage.columns.length}`]"
  150. :style="{ margin: `0 -${curPage.columnGap / 2}px` }"
  151. >
  152. <div
  153. class="page-column"
  154. v-for="(column, columnNo) in curPage.columns"
  155. :key="columnNo"
  156. :style="{ padding: `0 ${curPage.columnGap / 2}px` }"
  157. >
  158. <topic-column-edit
  159. class="page-column-main"
  160. :data="column"
  161. ></topic-column-edit>
  162. </div>
  163. </div>
  164. </div>
  165. <!-- outer edit area -->
  166. <div class="page-main-outer">
  167. <page-number
  168. type="rect"
  169. :total="pages.length"
  170. :current="curPageNo + 1"
  171. ></page-number>
  172. <page-number
  173. type="text"
  174. :total="pages.length"
  175. :current="curPageNo + 1"
  176. ></page-number>
  177. <course-barcode
  178. v-if="curPageNo % 2 === 0"
  179. :data="cardConfig"
  180. ></course-barcode>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. <!-- element-prop-edit -->
  186. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  187. <!-- right-click-menu -->
  188. <right-click-menu></right-click-menu>
  189. <!-- shortcut-key -->
  190. <shortcut-key
  191. ref="ShortcutKey"
  192. @sk-save="skSave"
  193. @sk-submit="skSubmit"
  194. @sk-preview="skPreview"
  195. ></shortcut-key>
  196. <!-- help-dialog -->
  197. <help-dialog
  198. ref="HelpDialog"
  199. @on-enable="shortcutEnableChange"
  200. ></help-dialog>
  201. </div>
  202. </template>
  203. <script>
  204. import { mapState, mapMutations, mapActions } from "vuex";
  205. import { getElementModel, ELEMENT_LIST, TOPIC_LIST } from "../elements/model";
  206. import { getModel as getPageModel } from "../../../elements/page/model";
  207. import { CARD_VERSION } from "../../../enumerate";
  208. import TopicColumnEdit from "../components/TopicColumnEdit";
  209. import ElementPropEdit from "../components/ElementPropEdit";
  210. import ElementTierEdit from "../components/ElementTierEdit";
  211. import PagePropEdit from "../components/PagePropEdit";
  212. import RightClickMenu from "../components/RightClickMenu";
  213. import ShortcutKey from "../components/ShortcutKey";
  214. import HelpDialog from "../components/HelpDialog";
  215. import EditPage from "../../../elements/page/EditPage";
  216. import PageNumber from "../../../components/PageNumber";
  217. import CourseBarcode from "../../../components/CourseBarcode.vue";
  218. export default {
  219. name: "card-free-design",
  220. props: {
  221. content: {
  222. type: Object,
  223. default() {
  224. return {
  225. pages: [],
  226. cardConfig: {},
  227. };
  228. },
  229. },
  230. showSaveBtn: {
  231. type: Boolean,
  232. default: true,
  233. },
  234. },
  235. components: {
  236. TopicColumnEdit,
  237. PagePropEdit,
  238. EditPage,
  239. ElementPropEdit,
  240. ElementTierEdit,
  241. RightClickMenu,
  242. ShortcutKey,
  243. HelpDialog,
  244. PageNumber,
  245. CourseBarcode,
  246. },
  247. data() {
  248. return {
  249. ELEMENT_LIST,
  250. TOPIC_LIST,
  251. topicList: [],
  252. columnWidth: 0,
  253. isSubmit: false,
  254. canSave: false,
  255. hasUnsubmitCont: false,
  256. };
  257. },
  258. computed: {
  259. ...mapState("free", [
  260. "cardConfig",
  261. "pages",
  262. "curElement",
  263. "curPage",
  264. "curPageNo",
  265. "curDragElement",
  266. "curColumnId",
  267. ]),
  268. },
  269. mounted() {
  270. this.initCard();
  271. },
  272. beforeDestroy() {
  273. this.initState();
  274. },
  275. methods: {
  276. ...mapMutations("free", [
  277. "setCurPageNo",
  278. "setCurElement",
  279. "setCardConfig",
  280. "setOpenElementEditDialog",
  281. "setCurDragElement",
  282. "setPages",
  283. "initState",
  284. ]),
  285. ...mapActions("free", [
  286. "addPage",
  287. "removePage",
  288. "addElement",
  289. "modifyElement",
  290. ]),
  291. async initCard() {
  292. const { cardConfig, pages } = this.content;
  293. this.setCardConfig(cardConfig);
  294. if (pages && pages.length) {
  295. this.setPages(pages);
  296. this.setCurPageNo(0);
  297. } else {
  298. this.initPageData();
  299. }
  300. },
  301. initPageData() {
  302. const page = getPageModel(this.cardConfig);
  303. this.addPage(page);
  304. this.setCurPageNo(0);
  305. },
  306. toAddPage() {
  307. const page = getPageModel(this.cardConfig);
  308. this.addPage(page);
  309. },
  310. addNewTopic(item) {
  311. let element = getElementModel(item.type);
  312. this.setCurElement(element);
  313. this.$refs.ElementPropEdit.open();
  314. // to elementPropEdit/ElementPropEdit open topic edit dialog
  315. },
  316. // 元件编辑
  317. dragstart(element) {
  318. this.setCurDragElement(getElementModel(element.type));
  319. },
  320. // 操作
  321. swithPage(pindex) {
  322. if (this.curPageNo === pindex) return;
  323. this.setCurPageNo(pindex);
  324. this.setCurElement({});
  325. },
  326. toDeletePage(page) {
  327. if (this.pages.length === 1) {
  328. this.$message.error("只剩最后一页,不能再删除了");
  329. return;
  330. }
  331. this.removePage(page);
  332. },
  333. // save
  334. getCardData(htmlContent = "", model = "") {
  335. let data = {
  336. title: this.cardConfig.cardTitle,
  337. pageSize: this.cardConfig.pageSize,
  338. content: model,
  339. htmlContent,
  340. };
  341. return data;
  342. },
  343. getCardJson() {
  344. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  345. return new Promise((resolve) => {
  346. setTimeout(() => {
  347. const data = JSON.stringify(
  348. {
  349. version: CARD_VERSION,
  350. cardType: "FREE",
  351. cardConfig: this.cardConfig,
  352. pages: this.pages,
  353. },
  354. (k, v) => (k.startsWith("_") ? undefined : v)
  355. );
  356. resolve(data);
  357. }, 100);
  358. });
  359. },
  360. skSave() {
  361. this.toSave();
  362. },
  363. skSubmit() {
  364. this.toSubmit();
  365. },
  366. skPreview() {
  367. this.toPreview();
  368. },
  369. shortcutEnableChange(enable) {
  370. if (enable) {
  371. this.$refs.ShortcutKey.registShortcutKey();
  372. } else {
  373. this.$refs.ShortcutKey.removeShortcutKey();
  374. }
  375. },
  376. toPreview() {
  377. this.$emit("on-preview", {
  378. cardConfig: this.cardConfig,
  379. pages: this.pages,
  380. });
  381. },
  382. async toSave() {
  383. const model = await this.getCardJson();
  384. const datas = this.getCardData("", model);
  385. this.$emit("on-save", datas);
  386. },
  387. toSubmit() {
  388. if (this.isSubmit) return;
  389. if (this.pages.length % 2) {
  390. this.$message.error("请确保题卡页数是偶数");
  391. return;
  392. }
  393. this.$emit("on-submit", {
  394. cardConfig: {
  395. ...this.cardConfig,
  396. courseCodeBarcodeSrc: "",
  397. courseCodeBarcodeName: "",
  398. },
  399. pages: this.pages,
  400. });
  401. },
  402. toExit() {
  403. this.$emit("on-exit");
  404. },
  405. showHelp() {
  406. this.$refs.HelpDialog.open();
  407. },
  408. loading() {
  409. this.isSubmit = true;
  410. },
  411. unloading() {
  412. this.isSubmit = false;
  413. },
  414. updateUnsubmitStatus(status) {
  415. this.hasUnsubmitCont = status;
  416. },
  417. },
  418. };
  419. </script>