index.vue 4.2 KB
Newer Older
何虹's avatar
何虹 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
<template>
  <div class="hsInputBox" :id='elId'>
     <div class="inputTitle" v-if='title'>{{title}}</div>
     <div class="inputCurr">
          <el-input
          :size='size'
          @keyup.enter.native="submitInput"
          v-model="value_inner"
          @input='inputFun'
          @change='changeFun'
          @blur="inputBlur"
          :disabled="isReadOnly"
          :placeholder="placeholder||elInfo.placeholder"></el-input>
     </div>
    <jsoneditor v-model='jsoneditorVisible'
                :elInfo='elInfo'
                :layout='layout'
                :jsoneditorCloseAfter='jsoneditorCloseAfter'
                :jsoneditorOpenAfter='jsoneditorOpenAfter'
                >
    </jsoneditor>
  </div>
</template>
<script>
import ucComponent from '../ucClass/uc_component'
import uuidv1 from 'uuid/v1'
import jsoneditor from '../common/jsoneditor'
export default {
  mixins: [ucComponent],
  components: {
    jsoneditor
  },
  name: 'hsInput',
  props: {
    prop: {},
    formData: {
      type: Object,
      default() {
        return {}
      }
    },
    allSourceData: {
      type: Object,
      default() {
        return {}
      }
    },
    elInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    value: '',
    layout: {},
    jsoneditorCloseAfter: {
      type: Function,
      default() {
        return () => {}
      }
    },
    jsoneditorOpenAfter: {
      type: Function,
      default() {
        return () => {}
      }
    },
    readonly: {
      default: false
    }
  },
  watch: {
    formData: {
      handler: function(newVal, oldVal) {
        if (!this.isInputed) {
          const linkProps = this.configData.linkProps || []
          if (!linkProps.length) {
            this.inputComputed(this.value)
          }
          linkProps.forEach(item => {
            if (this.formData[`_${item}_`]) {
              this.inputComputed(this.value)
            }
          })
        }
      },
      deep: true
    },
    allSourceData: {
      handler: function(newVal, oldVal) {
        const { config } = newVal
        this.initConfig(config)
      },
      deep: true
    },
    value(val, oldVal) {
      this.value_inner = val
      this.valueChange(val)
    },
    value_inner(val) {
      this.$emit('input', val)
    }
  },
  computed: {
    isReadOnly: function() {
      return this.readonly || this.configData.disabled
    }
  },
  data() {
    return {
      elId: '',
      value_inner: '',
      jsoneditorVisible: false,
      title: '',
      placeholder: '',
      size: 'mini',
      configData: {},
      isInputed: false
    }
  },
  created() {
    this.elId = uuidv1() // 获取随机id
  },
  mounted() {
    const { config } = this.allSourceData
    this.initConfig(config)
  },
  methods: {
    inputBlur() {
      this.isInputed = false
      this.formData[`_${this.prop}_`] = false
    },
    execAction(actionName, value) {
      const type = typeof actionName
      if (type === 'function') {
        actionName(value, this.formData)
      }
    },
    valueChange(value) {
      const { changing } = this.configData
      this.execAction(changing, value)
    },
    inputComputed(value) {
      const { computed } = this.configData
      this.execAction(computed, value)
    },
    changeFun(value) {
      const { changeed } = this.configData
      this.execAction(changeed, value)
    },
    inputFun(value) {
      this.isInputed = true
      this.formData[`_${this.prop}_`] = true
      const { inputed } = this.configData
      this.execAction(inputed, value)
    },
    initConfig(config) {
      if (typeof config !== 'object') return
      const { title, placeholder, size } = config
      this.configData = config
      this.title = title
      this.placeholder = placeholder

      if (size) {
        this.size = size
      }
    },
    submitInput(val) {
      const parm = {}
      parm[this.elInfo.prop] = this.value_inner
      this.$emit('enterInput', parm)
    }
  }
}
</script>
<style scoped>
.hsInputBox{
  padding: 1px;
  width:100%;
  height:100%;
  display: flex;
}
.inputTitle{
  flex:1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.inputCurr{
  flex: 3;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>