ElemFillQuestion.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div :class="classes">
  3. <div class="elem-body">
  4. <ul
  5. class="group-item"
  6. v-for="(group, gindex) in questions"
  7. :key="gindex"
  8. :style="gindex !== questions.length - 1 ? groupGapStyles : null"
  9. >
  10. <li
  11. class="question-item"
  12. v-for="(question, qindex) in group"
  13. :key="qindex"
  14. :style="getQuestionGapStyles(group, qindex)"
  15. >
  16. <span
  17. class="option-item"
  18. v-for="(option, oindex) in question"
  19. :key="oindex"
  20. :style="optionGapStyles"
  21. >
  22. <i>{{ option }}</i>
  23. </span>
  24. </li>
  25. </ul>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. export default {
  31. name: "elem-fill-question",
  32. props: {
  33. data: {
  34. type: Object,
  35. },
  36. },
  37. computed: {
  38. classes() {
  39. return [
  40. "elem-fill-question",
  41. `elem-fill-question-${this.data.optionDirection}`,
  42. {
  43. "elem-fill-question-simple":
  44. !this.data.isMultiply && !this.data.isBoolean,
  45. "elem-fill-question-multiply": this.data.isMultiply,
  46. "elem-fill-question-boolean": this.data.isBoolean,
  47. },
  48. ];
  49. },
  50. groupGapStyles() {
  51. return {
  52. marginRight: this.data.groupGap + "px",
  53. };
  54. },
  55. optionGapStyles() {
  56. const styles =
  57. this.data.optionDirection === "vertical"
  58. ? { marginBottom: this.data.optionGap + "px" }
  59. : { marginRight: this.data.optionGap + "px" };
  60. // styles.fontSize = this.data.fontSize;
  61. return styles;
  62. },
  63. },
  64. data() {
  65. return {
  66. questions: [],
  67. };
  68. },
  69. methods: {
  70. parseQuestion(data) {
  71. let questionNo = data.startNumber;
  72. let questions = [];
  73. const choiceList = this.getChoiceList(data);
  74. if (data.questionDirection === "vertical") {
  75. const groupNum = Math.ceil(
  76. data.questionsCount / data.questionCountPerGroup
  77. );
  78. for (let i = 0; i < groupNum; i++) {
  79. questions[i] = [];
  80. const questionCountPerGroup =
  81. i === groupNum - 1
  82. ? data.questionsCount - data.questionCountPerGroup * i
  83. : data.questionCountPerGroup;
  84. for (let j = 0; j < questionCountPerGroup; j++) {
  85. questions[i][j] = [questionNo++, ...choiceList];
  86. }
  87. }
  88. } else {
  89. for (let i = 0; i < data.questionsCount; i++) {
  90. const groupIndex = i % data.groupPerLine;
  91. if (!questions[groupIndex]) questions[groupIndex] = [];
  92. questions[groupIndex].push([questionNo++, ...choiceList]);
  93. }
  94. }
  95. this.questions = questions;
  96. },
  97. getChoiceList(data) {
  98. if (data.isBoolean) {
  99. return ["", ""];
  100. } else {
  101. return "abcdefghijklmnopqrstuv"
  102. .toUpperCase()
  103. .slice(0, data.optionCount)
  104. .split("");
  105. }
  106. },
  107. getQuestionGapStyles(group, qindex) {
  108. const size =
  109. group.length - 1 === qindex
  110. ? 0
  111. : this.data.optionDirection === "vertical"
  112. ? this.data.optionGap
  113. : this.data.questionGap;
  114. return this.data.optionDirection === "vertical"
  115. ? { marginRight: size + "px" }
  116. : { marginBottom: size + "px" };
  117. },
  118. },
  119. watch: {
  120. data: {
  121. immediate: true,
  122. handler(val) {
  123. this.parseQuestion(val);
  124. },
  125. },
  126. },
  127. };
  128. </script>