ElemFillQuestion.vue 3.3 KB

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