model.js 2.7 KB

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