ElementPropEdit.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <el-dialog
  3. class="element-prop-edit edit-dialog"
  4. :visible.sync="openElementEditDialog"
  5. :title="title"
  6. top="10vh"
  7. width="640px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. :before-close="cancel"
  11. append-to-body
  12. >
  13. <component
  14. v-if="openElementEditDialog"
  15. ref="ElementPropEditComp"
  16. :is="curEditComponent"
  17. :instance="curElement"
  18. :key="curElement.id"
  19. @modified="modified"
  20. ></component>
  21. <div slot="footer">
  22. <el-button type="primary" :disabled="loading" @click="submit"
  23. >确认</el-button
  24. >
  25. <el-button @click="cancel">取消</el-button>
  26. </div>
  27. </el-dialog>
  28. </template>
  29. <script>
  30. import { mapState, mapMutations, mapActions } from "vuex";
  31. import { getElementName } from "../elementModel";
  32. import EditComposition from "../elements/composition/EditComposition";
  33. import EditExplain from "../elements/explain/EditExplain";
  34. import EditFillLine from "../elements/fill-line/EditFillLine";
  35. import EditFillQuestion from "../elements/fill-question/EditFillQuestion";
  36. import EditText from "../elements/text/EditText";
  37. import EditImage from "../elements/image/EditImage";
  38. import EditLine from "../elements/line/EditLine";
  39. import EditLines from "../elements/lines/EditLines";
  40. import EditGrids from "../elements/grids/EditGrids";
  41. export default {
  42. name: "element-prop-edit",
  43. components: {
  44. EditComposition,
  45. EditExplain,
  46. EditFillLine,
  47. EditFillQuestion,
  48. EditText,
  49. EditImage,
  50. EditLine,
  51. EditLines,
  52. EditGrids,
  53. },
  54. data() {
  55. return { loading: false };
  56. },
  57. computed: {
  58. ...mapState("card", ["curElement", "topics", "openElementEditDialog"]),
  59. title() {
  60. return this.curElement.type
  61. ? getElementName(this.curElement.type)
  62. : "属性编辑";
  63. },
  64. curEditComponent() {
  65. if (!this.curElement.type) return;
  66. let type = this.curElement.type.toLowerCase().replace("_", "-");
  67. if (type.indexOf("line-") === 0) type = "line";
  68. return `edit-${type}`;
  69. },
  70. },
  71. methods: {
  72. ...mapMutations("card", ["setOpenElementEditDialog"]),
  73. ...mapActions("card", [
  74. "addElement",
  75. "modifyElement",
  76. "modifyElementChild",
  77. "rebuildPages",
  78. "scrollToElementPage",
  79. ]),
  80. cancel() {
  81. this.setOpenElementEditDialog(false);
  82. },
  83. open() {
  84. this.setOpenElementEditDialog(true);
  85. },
  86. submit() {
  87. if (this.loading) return;
  88. this.loading = true;
  89. setTimeout(() => {
  90. this.loading = false;
  91. }, 500);
  92. this.$refs.ElementPropEditComp.submit();
  93. },
  94. equalTopicType(topic1, topic2) {
  95. if (topic1.type === topic2.type) {
  96. if (topic1.type !== "FILL_QUESTION") {
  97. return true;
  98. }
  99. return (
  100. topic1.isBoolean === topic2.isBoolean &&
  101. topic1.isMultiply === topic2.isMultiply
  102. );
  103. }
  104. return false;
  105. },
  106. checkTopicType(element) {
  107. const relateTopics = this.topics.filter(
  108. (item) =>
  109. item.topicNo === element.topicNo &&
  110. item.parent &&
  111. item.parent.id !== element.id
  112. );
  113. if (!relateTopics.length) return true;
  114. return !relateTopics.some((topic) => this.equalTopicType(element, topic));
  115. },
  116. checkTopicNo(element) {
  117. if (element.type === "COMPOSITION") return true;
  118. const relateTopics = this.topics.filter(
  119. (item) =>
  120. item.topicNo === element.topicNo &&
  121. item.parent &&
  122. item.parent.id !== element.id
  123. );
  124. if (!relateTopics.length) return true;
  125. let er = [
  126. element.startNumber,
  127. element.startNumber + element.questionsCount - 1,
  128. ];
  129. const unvalid = relateTopics.some((item) => {
  130. const topic = item.type === "EXPLAIN" ? item.parent : item;
  131. const tr = [
  132. topic.startNumber,
  133. topic.startNumber + topic.questionsCount - 1,
  134. ];
  135. return (
  136. (er[0] <= tr[0] && er[1] >= tr[0]) ||
  137. (er[0] <= tr[1] && er[1] >= tr[1]) ||
  138. (er[0] >= tr[0] && er[1] <= tr[1])
  139. );
  140. });
  141. return !unvalid;
  142. },
  143. modified(element) {
  144. if (!element["container"]) {
  145. if (!this.checkTopicType(element)) {
  146. this.$message.error("同一大题号的所有试题题型必须相同");
  147. return;
  148. }
  149. // 在不校验大题号重复的情况下,需要校验小题号重复
  150. if (!this.checkTopicNo(element)) {
  151. this.$message.error("小题号重复,请重新设置小题号");
  152. return;
  153. }
  154. }
  155. // 编辑试题
  156. // 属性存在的条件:parent:大题的小题,container:题目内的子元素
  157. if (this.curElement["_edit"]) {
  158. if (element["container"]) {
  159. this.modifyElementChild(element);
  160. } else {
  161. this.modifyElement(element);
  162. }
  163. } else {
  164. this.addElement(element);
  165. }
  166. this.cancel();
  167. this.$nextTick(() => {
  168. this.rebuildPages();
  169. this.$nextTick(() => {
  170. this.scrollToElementPage(element);
  171. });
  172. });
  173. },
  174. },
  175. };
  176. </script>