ElemFillQuestion.vue 3.2 KB

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