tiankong.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="ezy-tiankong-box">
  3. <!-- 标题区域 -->
  4. <view class="tiankongn-title"></view>
  5. <!-- 显示填空后的文本 -->
  6. <view class="ezy-shiti-question" v-html="formattedText" :question="question" :myflag="myflag"
  7. :change:myflag="TK.updateFlag" :change:question="TK.watchQuestionChange"></view>
  8. <!-- 提供与占位符数量相匹配的输入框 -->
  9. <template v-for="(placeholder, index) in placeholders" :key="index">
  10. <FillItem :value="question.reply[index]" :ref="`popup${index}Ref`" :index="index" @blur="onBlur"></FillItem>
  11. </template>
  12. </view>
  13. </template>
  14. <script>
  15. import FillItem from "./FillItem.vue";
  16. export default {
  17. components: {
  18. FillItem
  19. },
  20. props: {
  21. placeholders: { // 占位符
  22. type: Array,
  23. required: true
  24. },
  25. question: {
  26. type: Object,
  27. },
  28. showError: {
  29. type: Boolean,
  30. default: false
  31. }
  32. },
  33. data() {
  34. return {
  35. myflag: 0
  36. }
  37. },
  38. computed: {
  39. // 计算属性,用于生成填空后的文本
  40. formattedText() {
  41. let result = this.question.name;
  42. this.placeholders.forEach((placeholder, index) => {
  43. const regex2 = new RegExp(`\\<img `, 'g');
  44. result = result.replace(regex2, '<image ');
  45. // 使用正则表达式全局替换占位符
  46. const regex = new RegExp(`\\${placeholder}`, 'g');
  47. if (this.showError) {
  48. // 答题状态默认错误
  49. let answer_status = 'showError';
  50. // 更新答题状态正确
  51. this.question.reply[index] && this.question.result[index].some(citem => citem === this
  52. .question.reply[index].trim()) && (answer_status = 'active_right');
  53. result = result.replace(regex,
  54. `<view class="tk-input-box"><input class="tiankong-input ${answer_status}" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" /></view>`
  55. );
  56. } else {
  57. result = result.replace(regex,
  58. `<view class="tk-input-box"><input class="tiankong-input" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" /></view>`
  59. );
  60. }
  61. });
  62. this.myflag++;
  63. return result;
  64. },
  65. },
  66. methods: {
  67. setResult(data) {
  68. const {
  69. index,
  70. result
  71. } = data;
  72. this.question.reply[index] = result;
  73. },
  74. onBlur(data) {
  75. const {
  76. index,
  77. result
  78. } = data;
  79. this.setResult(data);
  80. },
  81. showPopup(str) {
  82. this.$refs[str][0].showPopup();
  83. }
  84. },
  85. }
  86. </script>
  87. <script module="TK" lang="renderjs">
  88. export default {
  89. props: {
  90. showError: {
  91. type: Boolean,
  92. default: false
  93. }
  94. },
  95. data() {
  96. return {
  97. myQ: null
  98. }
  99. },
  100. methods: {
  101. updateFlag() {
  102. this.initListener(this.myQ)
  103. },
  104. updateFn(e) {
  105. const str = `popup${e.target.dataset.index}Ref`;
  106. this.$ownerInstance.callMethod('showPopup', str);
  107. },
  108. watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
  109. if (newValue) {
  110. this.myQ = newValue;
  111. this.initListener(newValue)
  112. }
  113. },
  114. initListener(question) {
  115. if (!question) {
  116. return;
  117. }
  118. if (this.showError) {
  119. return;
  120. }
  121. question.result.forEach((item, index) => {
  122. const dom = document.getElementById(`t_${question.stId}_${index}`)
  123. dom && dom.addEventListener('focus', (e) => this.updateFn(e))
  124. })
  125. },
  126. },
  127. }
  128. </script>