model.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { getElementId, randomCode } from "../../plugins/utils";
  2. import { BOOLEAN_TYPE } from "../../enumerate";
  3. const MODEL = {
  4. type: "FILL_QUESTION",
  5. x: 0,
  6. y: 0,
  7. w: 0,
  8. h: 114,
  9. minHeight: 114,
  10. sign: "objective",
  11. topicName: "",
  12. topicNo: null,
  13. startNumber: 1,
  14. questionsCount: 10,
  15. optionCount: 4,
  16. questionCountPerGroup: 5,
  17. groupPerLine: 4, // 小题纵向排列时,表示每行组数。小题横向排列时,表示每行小题数。
  18. optionDirection: "horizontal",
  19. questionDirection: "vertical",
  20. questionGap: 8,
  21. groupGap: 30,
  22. optionGap: 6,
  23. isBoolean: false, // 是否是判断题
  24. booleanType: BOOLEAN_TYPE[0],
  25. isMultiply: false, // 是否是多选题
  26. isCovered: false,
  27. fontSize: "14px",
  28. };
  29. const getModel = (preSetData = {}) => {
  30. const model = Object.assign({}, MODEL, preSetData);
  31. return {
  32. id: getElementId(),
  33. key: randomCode(),
  34. ...model,
  35. };
  36. };
  37. const getOptionStructInfo = (pageSize, columnNumber, model) => {
  38. // 选项纵向排列
  39. if (model.optionDirection === "vertical") {
  40. return { groupGap: 23, groupPerLine: 5 };
  41. }
  42. // 选项横向排列
  43. // 不同栏数,不同选项个数,每一行对应的组数
  44. // 以一行4题,每题5选项为标准展示效果
  45. const numberPerChildren = {
  46. A3: {
  47. 2: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
  48. 3: [0, 0, 5, 4, 3, 2, 2, 2, 1],
  49. 4: [0, 0, 3, 3, 2, 2, 1],
  50. },
  51. A4: {
  52. 1: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
  53. 2: [0, 0, 3, 3, 2, 2, 1],
  54. },
  55. };
  56. const groupGapSet = {
  57. A3: {
  58. 2: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
  59. 3: [0, 0, 27, 27, 40, 40, 40, 40, 30],
  60. 4: [0, 0, 33, 27, 40, 30],
  61. },
  62. A4: {
  63. 1: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
  64. 2: [0, 0, 33, 27, 40, 30],
  65. },
  66. };
  67. const groupGapList = groupGapSet[pageSize][columnNumber];
  68. const groupGap =
  69. model.optionCount >= groupGapList.length
  70. ? groupGapList.pop()
  71. : groupGapList[model.optionCount];
  72. const numList = numberPerChildren[pageSize][columnNumber];
  73. const groupPerLine =
  74. model.optionCount >= numList.length
  75. ? numList.pop()
  76. : numList[model.optionCount];
  77. return { groupGap, groupPerLine };
  78. };
  79. const getFullModel = (model, { pageSize, columnNumber }) => {
  80. const parent = { ...model };
  81. const { groupGap, groupPerLine } = getOptionStructInfo(
  82. pageSize,
  83. columnNumber,
  84. model
  85. );
  86. const numPerLine = groupPerLine * model.questionCountPerGroup;
  87. const total = Math.ceil(model.questionsCount / numPerLine);
  88. let elements = [];
  89. for (let i = 0; i < total; i++) {
  90. let child = Object.assign({}, parent, {
  91. id: getElementId(),
  92. key: randomCode(),
  93. groupPerLine,
  94. groupGap,
  95. startNumber: model.startNumber + i * numPerLine,
  96. questionsCount:
  97. i === total - 1 ? model.questionsCount - numPerLine * i : numPerLine,
  98. parent,
  99. isLast: i === total - 1,
  100. });
  101. if (model.optionDirection === "vertical") {
  102. const optionCount = model.optionCount + 1;
  103. const optionsHeight =
  104. 10 * optionCount + (optionCount - 1) * model.optionGap + 20;
  105. child.h = i ? optionsHeight : optionsHeight + 34;
  106. } else {
  107. const optionCount =
  108. model.questionDirection === "vertical"
  109. ? Math.min(child.questionsCount, model.questionCountPerGroup)
  110. : Math.ceil(child.questionsCount / groupPerLine);
  111. const optionsHeight =
  112. 10 * optionCount + (optionCount - 1) * model.questionGap + 20;
  113. child.h = i ? optionsHeight : optionsHeight + 34;
  114. }
  115. child.minHeight = child.h;
  116. elements[i] = child;
  117. }
  118. return elements;
  119. };
  120. export { MODEL, getModel, getFullModel };