ElemFillQuestion.vue 3.6 KB

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