exchange.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import { CARD_VERSION } from "../enumerate";
  2. import { deepCopy } from "../plugins/utils";
  3. const initIndex = {
  4. question: 1,
  5. absent: 1,
  6. paperType: 1,
  7. examNumber: 1,
  8. selective: 1,
  9. pageNumber: 1
  10. };
  11. export default {
  12. data() {
  13. return {
  14. fillAreaIndex: {
  15. ...initIndex
  16. },
  17. VALID_ELEMENTS_FOR_EXTERNAL: [
  18. "LOCATOR",
  19. "BARCODE",
  20. "CARD_HEAD",
  21. "FILL_QUESTION",
  22. "FILL_LINE",
  23. "EXPLAIN",
  24. "COMPOSITION"
  25. ]
  26. };
  27. },
  28. methods: {
  29. getFillAreaIndex(type) {
  30. return this.fillAreaIndex[type]++;
  31. },
  32. getElementHumpName(cont) {
  33. return cont
  34. .split("_")
  35. .map(item => item[0] + item.substr(1).toLowerCase())
  36. .join("");
  37. },
  38. getPreviewElementById(id) {
  39. return document.getElementById(`preview-${id}`);
  40. },
  41. parsePageExchange(pages) {
  42. const npages = deepCopy(pages);
  43. const pageNumberInfo = this.getPageNumberInfo();
  44. npages.forEach((page, pindex) => {
  45. let exchange = {
  46. locator: this.getLocatorInfo(page.locators),
  47. barcode: [],
  48. info_area: [],
  49. fill_area: [],
  50. answer_area: []
  51. };
  52. const elements = [
  53. page.globals,
  54. ...page.columns.map(column => column.elements)
  55. ];
  56. elements.forEach(elemGroup => {
  57. elemGroup.forEach(element => {
  58. if (this.VALID_ELEMENTS_FOR_EXTERNAL.includes(element.type)) {
  59. const funcName = this.getElementHumpName(element.type);
  60. console.log(funcName);
  61. const info = this[`get${funcName}Info`](element);
  62. Object.entries(info).forEach(([key, vals]) => {
  63. exchange[key] = exchange[key].concat(vals);
  64. });
  65. }
  66. });
  67. });
  68. if (!(pindex % 2)) {
  69. let pnoInfo = deepCopy(pageNumberInfo);
  70. pnoInfo[0].index = this.getFillAreaIndex("pageNumber");
  71. exchange.fill_area = exchange.fill_area.concat(pnoInfo);
  72. }
  73. page.exchange = exchange;
  74. });
  75. this.fillAreaIndex = { ...initIndex };
  76. return npages;
  77. },
  78. getPageNumberInfo() {
  79. const dom = document.querySelector(".page-box-0");
  80. let options = [];
  81. dom
  82. .querySelector(".page-number-rect-list")
  83. .childNodes.forEach((item, index) => {
  84. options[index] = this.getOffsetInfo(item);
  85. });
  86. return [
  87. {
  88. field: "pageNumber",
  89. index: 1,
  90. single: true,
  91. horizontal: true,
  92. items: [
  93. {
  94. main_number: null,
  95. sub_number: null,
  96. options
  97. }
  98. ]
  99. }
  100. ];
  101. },
  102. getLocatorInfo(locators) {
  103. return locators.map(locatorGroup => {
  104. const locatorInfos = locatorGroup.map(locator => {
  105. return this.getOffsetInfo(document.getElementById(locator.id));
  106. });
  107. return {
  108. top: locatorInfos[0],
  109. bottom: locatorInfos[1]
  110. };
  111. });
  112. },
  113. getCardHeadInfo(element) {
  114. const dom = this.getPreviewElementById(element.id);
  115. const headArea = this.getOffsetInfo(dom);
  116. let fill_area = [];
  117. let barcode = [];
  118. // 学生考号
  119. if (element.examNumberStyle === "fill") {
  120. // fill_area
  121. let listInfos = [];
  122. dom
  123. .querySelectorAll(".stdno-fill-list")
  124. .forEach((questionItem, questionIndex) => {
  125. let options = [];
  126. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  127. options[optionIndex] = this.getOffsetInfo(optionItem);
  128. });
  129. listInfos[questionIndex] = {
  130. main_number: null,
  131. sub_number: null,
  132. options
  133. };
  134. });
  135. fill_area.push({
  136. field: "examNumber",
  137. index: this.getFillAreaIndex("examNumber"),
  138. single: true,
  139. horizontal: false,
  140. items: listInfos
  141. });
  142. } else {
  143. // barcode
  144. const stdnoDom =
  145. element.columnNumber <= 2
  146. ? dom.querySelector(".head-stdno").parentNode
  147. : dom.querySelector(".head-stdno");
  148. barcode.push({
  149. field: "examNumber",
  150. area: this.getOffsetInfo(stdnoDom)
  151. });
  152. }
  153. // 缺考涂填
  154. if (element.missAndFill && !element.isSimple) {
  155. fill_area.push({
  156. field: "absent",
  157. index: this.getFillAreaIndex("absent"),
  158. single: true,
  159. horizontal: true,
  160. items: [
  161. {
  162. main_number: null,
  163. sub_number: null,
  164. options: [
  165. this.getOffsetInfo(document.getElementById("dynamic-miss-area"))
  166. ]
  167. }
  168. ]
  169. });
  170. }
  171. // A/B卷类型
  172. if (element.aOrB && !element.isSimple) {
  173. if (element.aOrBType === "auto") {
  174. // barcode
  175. barcode.push({
  176. field: "paperType",
  177. area: this.getOffsetInfo(
  178. document.getElementById("dynamic-aorb-barcode")
  179. )
  180. });
  181. } else {
  182. // fill_area
  183. let options = [];
  184. document
  185. .getElementById("head-dynamic-aorb")
  186. .querySelectorAll(".head-dynamic-rect")
  187. .forEach((optionItem, optionIndex) => {
  188. options[optionIndex] = this.getOffsetInfo(optionItem);
  189. });
  190. fill_area.push({
  191. field: "paperType",
  192. index: this.getFillAreaIndex("paperType"),
  193. single: true,
  194. horizontal: true,
  195. items: [
  196. {
  197. main_number: null,
  198. sub_number: null,
  199. options
  200. }
  201. ]
  202. });
  203. }
  204. }
  205. return {
  206. info_area: [headArea],
  207. fill_area,
  208. barcode
  209. };
  210. },
  211. getFillQuestionInfo(element) {
  212. const dom = this.getPreviewElementById(element.id);
  213. const single = !element.isMultiply;
  214. const horizontal = element.optionDirection === "horizontal";
  215. let fillAreas = [];
  216. dom.querySelectorAll(".group-item").forEach(groupItem => {
  217. let listInfos = [];
  218. groupItem
  219. .querySelectorAll(".question-item")
  220. .forEach((questionItem, questionIndex) => {
  221. let options = [];
  222. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  223. if (optionIndex)
  224. options[optionIndex - 1] = this.getOffsetInfo(optionItem);
  225. });
  226. listInfos[questionIndex] = {
  227. main_number: element.topicNo,
  228. sub_number: questionItem.firstChild.textContent,
  229. options
  230. };
  231. });
  232. fillAreas.push({
  233. field: "question",
  234. index: this.getFillAreaIndex("question"),
  235. single,
  236. horizontal,
  237. items: listInfos
  238. });
  239. });
  240. return {
  241. fill_area: fillAreas
  242. };
  243. },
  244. getFillLineInfo(element) {
  245. const dom = this.getPreviewElementById(element.id);
  246. return {
  247. answer_area: [
  248. {
  249. main_number: element.topicNo,
  250. sub_numbers: [
  251. element.startNumber,
  252. element.startNumber + element.questionsCount - 1
  253. ],
  254. area: this.getOffsetInfo(dom)
  255. }
  256. ]
  257. };
  258. },
  259. getExplainInfo(element) {
  260. const dom = this.getPreviewElementById(element.id);
  261. return {
  262. answer_area: [
  263. {
  264. main_number: element.topicNo,
  265. sub_numbers: [element.serialNumber],
  266. area: this.getOffsetInfo(dom)
  267. }
  268. ]
  269. };
  270. },
  271. getCompositionInfo(element) {
  272. const dom = this.getPreviewElementById(element.id);
  273. return {
  274. answer_area: [
  275. {
  276. main_number: element.topicNo,
  277. sub_numbers: [],
  278. area: this.getOffsetInfo(dom)
  279. }
  280. ]
  281. };
  282. },
  283. getOffsetInfo(dom) {
  284. let { offsetTop, offsetLeft } = dom;
  285. let parentNode = dom.offsetParent;
  286. while (parentNode.className.indexOf("page-box") === -1) {
  287. offsetTop += parentNode.offsetTop;
  288. offsetLeft += parentNode.offsetLeft;
  289. parentNode = parentNode.offsetParent;
  290. }
  291. const pw = parentNode.offsetWidth;
  292. const ph = parentNode.offsetHeight;
  293. const infos = [
  294. offsetLeft / pw,
  295. offsetTop / ph,
  296. dom.offsetWidth / pw,
  297. dom.offsetHeight / ph
  298. ];
  299. return infos.map(num => num.toFixed(10) * 1);
  300. },
  301. getPageModel({ cardConfig, paperParams, pages }) {
  302. return JSON.stringify(
  303. {
  304. version: CARD_VERSION,
  305. cardConfig,
  306. paperParams,
  307. pages: this.parsePageExchange(pages)
  308. },
  309. (k, v) => (k.startsWith("_") ? undefined : v)
  310. );
  311. }
  312. }
  313. };