123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <template>
- <view class="ezy-tiankong-box">
- <!-- 标题区域 -->
- <view class="tiankongn-title"></view>
- <!-- 显示填空后的文本 -->
- <view class="ezy-shiti-question" v-html="formattedText" :question="question" :myflag="myflag"
- :change:myflag="TK.updateFlag" :change:question="TK.watchQuestionChange"></view>
- <!-- 提供与占位符数量相匹配的输入框 -->
- <template v-for="(placeholder, index) in placeholders" :key="index">
- <FillItem :value="question.reply[index]" :ref="`popup${index}Ref`" :index="index" @blur="onBlur"></FillItem>
- </template>
- </view>
- </template>
- <script>
- import FillItem from "./FillItem.vue";
- export default {
- components: {
- FillItem
- },
- props: {
- placeholders: { // 占位符
- type: Array,
- required: true
- },
- question: {
- type: Object,
- },
- showError: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- myflag: 0
- }
- },
- computed: {
- // 计算属性,用于生成填空后的文本
- formattedText() {
- let result = this.question.name;
- this.placeholders.forEach((placeholder, index) => {
- const regex2 = new RegExp(`\\<img `, 'g');
- result = result.replace(regex2, '<image ');
- // 使用正则表达式全局替换占位符
- const regex = new RegExp(`\\${placeholder}`, 'g');
- if (this.showError) {
- // 答题状态默认错误
- let answer_status = 'showError';
- // 更新答题状态正确
- this.question.reply[index] && this.question.result[index].some(citem => citem === this
- .question.reply[index].trim()) && (answer_status = 'active_right');
- result = result.replace(regex,
- `<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]}" />`
- );
- } else {
- result = result.replace(regex,
- `<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]}" />`
- );
- }
- });
- this.myflag++;
- return result;
- },
- },
- methods: {
- setResult(data) {
- const {
- index,
- result
- } = data;
- this.question.reply[index] = result;
- },
- onBlur(data) {
- const {
- index,
- result
- } = data;
- this.setResult(data);
- },
- showPopup(str) {
- this.$refs[str][0].showPopup();
- }
- },
- }
- </script>
- <script module="TK" lang="renderjs">
- export default {
- data() {
- return {
- myQ: null
- }
- },
- methods: {
- updateFlag() {
- this.initListener(this.myQ)
- },
- updateFn(e) {
- const str = `popup${e.target.dataset.index}Ref`;
- this.$ownerInstance.callMethod('showPopup', str);
- },
- watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
- if (newValue) {
- this.myQ = newValue;
- this.initListener(newValue)
- }
- },
- initListener(question) {
- if (!question) {
- return;
- }
- question.result.forEach((item, index) => {
- const dom = document.getElementById(`t_${question.stId}_${index}`)
- dom && dom.addEventListener('focus', (e) => this.updateFn(e))
- })
- },
- },
- }
- </script>
|