tiankong.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. `<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]}" />`
  55. );
  56. } else {
  57. result = result.replace(regex,
  58. `<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]}" />`
  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. data() {
  90. return {
  91. myQ: null
  92. }
  93. },
  94. methods: {
  95. updateFlag() {
  96. this.initListener(this.myQ)
  97. },
  98. updateFn(e) {
  99. const str = `popup${e.target.dataset.index}Ref`;
  100. this.$ownerInstance.callMethod('showPopup', str);
  101. },
  102. watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
  103. if (newValue) {
  104. this.myQ = newValue;
  105. this.initListener(newValue)
  106. }
  107. },
  108. initListener(question) {
  109. if (!question) {
  110. return;
  111. }
  112. question.result.forEach((item, index) => {
  113. const dom = document.getElementById(`t_${question.stId}_${index}`)
  114. dom && dom.addEventListener('focus', (e) => this.updateFn(e))
  115. })
  116. },
  117. },
  118. }
  119. </script>