ElemFillQuestion.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. },
  47. groupGapStyles() {
  48. return {
  49. marginRight: this.data.groupGap + "px"
  50. };
  51. },
  52. questionGapStyles() {
  53. return this.data.optionDirection === "vertical"
  54. ? { marginRight: this.data.questionGap + "px" }
  55. : { marginBottom: this.data.questionGap + "px" };
  56. },
  57. optionGapStyles() {
  58. const styles =
  59. this.data.optionDirection === "vertical"
  60. ? { marginBottom: this.data.optionGap + "px" }
  61. : { marginRight: this.data.optionGap + "px" };
  62. // styles.fontSize = this.data.fontSize;
  63. return styles;
  64. }
  65. },
  66. data() {
  67. return {
  68. questions: []
  69. };
  70. },
  71. methods: {
  72. parseQuestion(data) {
  73. let questionNo = data.startNumber;
  74. let questions = [];
  75. const choiceList = this.getChoiceList(data);
  76. if (data.questionDirection === "vertical") {
  77. const groupNum = Math.ceil(
  78. data.questionsCount / data.questionCountPerGroup
  79. );
  80. for (let i = 0; i < groupNum; i++) {
  81. questions[i] = [];
  82. const questionCountPerGroup =
  83. i === groupNum - 1
  84. ? data.questionsCount - data.questionCountPerGroup * i
  85. : data.questionCountPerGroup;
  86. for (let j = 0; j < questionCountPerGroup; j++) {
  87. questions[i][j] = [questionNo++, ...choiceList];
  88. }
  89. }
  90. } else {
  91. for (let i = 0; i < data.questionsCount; i++) {
  92. const groupIndex = i % data.groupPerLine;
  93. if (!questions[groupIndex]) questions[groupIndex] = [];
  94. questions[groupIndex].push([questionNo++, ...choiceList]);
  95. }
  96. }
  97. this.questions = questions;
  98. },
  99. getChoiceList(data) {
  100. if (data.isBoolean) {
  101. return data.booleanType.split(",");
  102. } else {
  103. return "abcdefghijklmnopqrstuv"
  104. .toUpperCase()
  105. .slice(0, data.optionCount)
  106. .split("");
  107. }
  108. }
  109. },
  110. watch: {
  111. data: {
  112. immediate: true,
  113. handler(val) {
  114. this.parseQuestion(val);
  115. }
  116. }
  117. }
  118. };
  119. </script>