model.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { getElementId } from "../../plugins/utils";
  2. const MODEL = {
  3. type: "FILL_LINE",
  4. x: 0,
  5. y: 0,
  6. w: 0,
  7. h: 40,
  8. minHeight: 40,
  9. sign: "subjective",
  10. topicName: "",
  11. topicNo: null,
  12. startNumber: 1,
  13. questionsCount: 4,
  14. questionNumberPerLine: 2,
  15. lineNumberPerQuestion: 1,
  16. lineSpacing: 40,
  17. questionDirection: "horizontal",
  18. questionLineType: "norm",
  19. questionLineNums: [],
  20. numberPre: "",
  21. isCovered: false
  22. };
  23. const getModel = () => {
  24. return {
  25. id: getElementId(),
  26. ...MODEL
  27. };
  28. };
  29. const getFullModel = model => {
  30. const parent = { ...model };
  31. const numPerLine = model.questionNumberPerLine;
  32. let elements = [];
  33. let questionLineNums = model.questionLineNums;
  34. if (model.questionLineType === "norm") {
  35. questionLineNums = [];
  36. for (
  37. let j = model.startNumber;
  38. j < model.startNumber + model.questionsCount;
  39. j++
  40. ) {
  41. questionLineNums.push({
  42. no: j,
  43. count: model.lineNumberPerQuestion
  44. });
  45. }
  46. }
  47. if (model.questionDirection === "vertical") {
  48. const total = Math.ceil(model.questionsCount / numPerLine);
  49. for (let i = 0; i < total; i++) {
  50. const childQuestionLineNums = questionLineNums.slice(
  51. i * numPerLine,
  52. numPerLine
  53. );
  54. const maxLineNumberPerQuestion = Math.max.apply(
  55. null,
  56. childQuestionLineNums.map(item => item.count)
  57. );
  58. const questionHeight = model.lineSpacing * maxLineNumberPerQuestion;
  59. let child = Object.assign({}, parent, {
  60. id: getElementId(),
  61. h: i ? questionHeight : questionHeight + 34,
  62. startNumber: model.startNumber + i * numPerLine,
  63. questionsCount:
  64. i === total - 1 ? model.questionsCount - numPerLine * i : numPerLine,
  65. parent: parent,
  66. isLast: i === total - 1,
  67. questionLineNums: childQuestionLineNums
  68. });
  69. child.minHeight = child.h;
  70. elements[i] = child;
  71. }
  72. } else {
  73. for (let i = 0; i < model.questionsCount; i++) {
  74. const childQuestionLineNums = questionLineNums[i];
  75. const maxLineNumberPerQuestion = Math.ceil(
  76. childQuestionLineNums.count / numPerLine
  77. );
  78. const questionHeight = model.lineSpacing * maxLineNumberPerQuestion;
  79. let child = Object.assign({}, parent, {
  80. id: getElementId(),
  81. h: i ? questionHeight : questionHeight + 34,
  82. startNumber: model.startNumber + i,
  83. questionsCount: 1,
  84. parent: parent,
  85. isLast: i === model.questionsCount - 1,
  86. questionLineNums: [childQuestionLineNums]
  87. });
  88. child.minHeight = child.h;
  89. elements[i] = child;
  90. }
  91. }
  92. return elements;
  93. };
  94. export { MODEL, getModel, getFullModel };