InputXml.vue 2.59 KB
Newer Older
liuxin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<template>
    <el-input
        :id="id"
        ref="form-item"
        v-model="value"
        type="textarea"
        v-bind="$attrs"
        v-on="$listeners"
        v-bind:disabled="isDisable"
        :rows="maxRows"
        resize="none"
        @change="onChange"
    />
</template>

<script>
export default {
    data: function () {
        return {
            value: "",
            isXml: false,
        };
    },
    props: {
        maxRows: {
            type: Number,
            default: 3,
        },
        maxCols: {
            type: Number,
            default: 4,
        },
        disabled: {
            type: Boolean,
            default: false,
        },
        id: {
            type: String,
            default: undefined,
        },
        model: {
            type: Object,
            default: undefined,
        },
    },
    computed: {
        // model: {
        //     get() {
        //         let value = this.format(this.value);
        //         return value;
        //     },
        //     set(newVal) {
        //         this.$emit("input", newVal);
        //     },
        // },
潘际乾 committed
56 57 58
        mode () {
            return this.$store.state.Status.mode
        },
liuxin committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        isDisable: {
            get() {
                return this.mode === "display" || this.disabled;
            },
        },
    },
    methods: {
        show: function (rows) {
            let strs = rows.length > 0 ? rows[0] : "";
            if (strs.startsWith("<?xml")) {
                this.isXml = true;
                var pattern = /<tdfmt\s[^>]*>(.*)<\/tdfmt>/;
                var temp = pattern.exec(strs);
                if (temp != null) {
73
                    strs = temp[1];
liuxin committed
74 75
                }
            }
76
            return strs.replace(/<br\/>/g, "\n");
liuxin committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        },
        format: function (value) {
            if(this.isXml){
                var len = value.length;
                value = value.replace(/\n/g, "<br/>")
                var head = "<?xml version='1.0'?><tdfmt sel-start='" + len + "'>";
                var tail = "</tdfmt>";
                return head + value + tail;
            }
            return value;
        },
        onChange() {    
            if (this.model && this.model.rows) {
                this.model["rows"][0] = this.format(this.value);
                this.$emit("change", this.value);
            }
        },
    },
    watch: {
        model: function () {
            let rows = this.model["rows"] || [];
            this.value = this.show(rows);
        },
    },
};
</script>

<style>
/* .el-input.highlight .el-input__inner{
  border-color: red;
} */
</style>