index.vue 4.46 KB
Newer Older
何虹's avatar
何虹 committed
1
<template>
何虹's avatar
何虹 committed
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
  <div
    class="hsInputBox"
    :id='elId'
  >
    <div
      class="inputTitle"
      v-if='configData.title'
    >{{configData.title}}</div>
    <div class="inputCurr">
      <el-input
        :clearable='clearable'
        :size='configData.size'
        @clear='clear'
        @keyup.enter.native="submitInput"
        v-model="value_inner"
        @input='inputFun'
        @change='changeFun'
        @blur="inputBlur"
        :disabled="isReadOnly"
        :placeholder="configData.placeholder"
      ></el-input>
    </div>
    <jsoneditor
      v-model='jsoneditorVisible'
      :elInfo='elInfo'
      :layout='layout'
      :jsoneditorCloseAfter='jsoneditorCloseAfter'
      :jsoneditorOpenAfter='jsoneditorOpenAfter'
    >
何虹's avatar
何虹 committed
31 32 33 34
    </jsoneditor>
  </div>
</template>
<script>
何虹's avatar
何虹 committed
35 36 37 38
import ucComponent from "../ucClass/uc_component";
import uuidv1 from "uuid/v1";
import jsoneditor from "../common/jsoneditor";
import _ from "lodash";
何虹's avatar
何虹 committed
39 40 41 42 43
export default {
  mixins: [ucComponent],
  components: {
    jsoneditor
  },
何虹's avatar
何虹 committed
44
  name: "hsInput",
何虹's avatar
何虹 committed
45 46 47 48 49
  props: {
    prop: {},
    formData: {
      type: Object,
      default() {
何虹's avatar
何虹 committed
50
        return {};
何虹's avatar
何虹 committed
51 52 53 54 55
      }
    },
    allSourceData: {
      type: Object,
      default() {
何虹's avatar
何虹 committed
56
        return {};
何虹's avatar
何虹 committed
57 58 59 60 61
      }
    },
    elInfo: {
      type: Object,
      default() {
何虹's avatar
何虹 committed
62
        return {};
何虹's avatar
何虹 committed
63 64
      }
    },
何虹's avatar
何虹 committed
65
    value: "",
何虹's avatar
何虹 committed
66 67 68 69
    layout: {},
    jsoneditorCloseAfter: {
      type: Function,
      default() {
何虹's avatar
何虹 committed
70
        return () => {};
何虹's avatar
何虹 committed
71 72 73 74 75
      }
    },
    jsoneditorOpenAfter: {
      type: Function,
      default() {
何虹's avatar
何虹 committed
76
        return () => {};
何虹's avatar
何虹 committed
77 78 79 80 81 82 83 84 85 86
      }
    },
    readonly: {
      default: false
    }
  },
  watch: {
    formData: {
      handler: function(newVal, oldVal) {
        if (!this.isInputed) {
何虹's avatar
何虹 committed
87
          const linkProps = this.configData.linkProps || [];
何虹's avatar
何虹 committed
88
          if (!linkProps.length) {
何虹's avatar
何虹 committed
89
            this.inputComputed(this.value);
何虹's avatar
何虹 committed
90 91 92
          }
          linkProps.forEach(item => {
            if (this.formData[`_${item}_`]) {
何虹's avatar
何虹 committed
93
              this.inputComputed(this.value);
何虹's avatar
何虹 committed
94
            }
何虹's avatar
何虹 committed
95
          });
何虹's avatar
何虹 committed
96 97 98 99 100 101
        }
      },
      deep: true
    },
    allSourceData: {
      handler: function(newVal, oldVal) {
何虹's avatar
何虹 committed
102 103
        const { config } = newVal;
        this.initConfig(config);
何虹's avatar
何虹 committed
104 105 106 107
      },
      deep: true
    },
    value(val, oldVal) {
何虹's avatar
何虹 committed
108 109
      this.value_inner = val;
      this.valueChange(val);
何虹's avatar
何虹 committed
110 111
    },
    value_inner(val) {
何虹's avatar
何虹 committed
112
      this.$emit("input", val);
何虹's avatar
何虹 committed
113 114 115 116
    }
  },
  computed: {
    isReadOnly: function() {
何虹's avatar
何虹 committed
117
      return this.readonly || this.configData.disabled;
何虹's avatar
何虹 committed
118 119 120 121
    }
  },
  data() {
    return {
何虹's avatar
何虹 committed
122 123
      elId: "",
      value_inner: "",
何虹's avatar
何虹 committed
124
      jsoneditorVisible: false,
何虹's avatar
何虹 committed
125 126 127 128 129 130 131 132 133 134
      isInputed: false,
      clearable: true,
      configData: {
        title: "",
        placeholder: "",
        size: "mini",
        clearable: true,
        placeholder: ""
      }
    };
何虹's avatar
何虹 committed
135 136
  },
  created() {
何虹's avatar
何虹 committed
137
    this.elId = uuidv1(); // 获取随机id
何虹's avatar
何虹 committed
138 139
  },
  mounted() {
何虹's avatar
何虹 committed
140 141 142
    const { config } = this.allSourceData;

    this.initConfig(config);
何虹's avatar
何虹 committed
143 144 145
  },
  methods: {
    inputBlur() {
何虹's avatar
何虹 committed
146 147
      this.isInputed = false;
      this.formData[`_${this.prop}_`] = false;
何虹's avatar
何虹 committed
148 149
    },
    execAction(actionName, value) {
何虹's avatar
何虹 committed
150 151 152
      const type = typeof actionName;
      if (type === "function") {
        actionName(value, this.formData);
何虹's avatar
何虹 committed
153 154 155
      }
    },
    valueChange(value) {
何虹's avatar
何虹 committed
156 157 158 159 160 161
      const { changing } = this.configData;
      this.execAction(changing, value);
    },
    clear(value) {
      const { clear } = this.configData;
      this.execAction(clear, value);
何虹's avatar
何虹 committed
162 163
    },
    inputComputed(value) {
何虹's avatar
何虹 committed
164 165
      const { computed } = this.configData;
      this.execAction(computed, value);
何虹's avatar
何虹 committed
166 167
    },
    changeFun(value) {
何虹's avatar
何虹 committed
168 169
      const { changeed } = this.configData;
      this.execAction(changeed, value);
何虹's avatar
何虹 committed
170 171
    },
    inputFun(value) {
何虹's avatar
何虹 committed
172 173 174 175
      this.isInputed = true;
      this.formData[`_${this.prop}_`] = true;
      const { inputed } = this.configData;
      this.execAction(inputed, value);
何虹's avatar
何虹 committed
176 177
    },
    initConfig(config) {
何虹's avatar
何虹 committed
178 179 180
      if (typeof config !== "object") return;
      const config_ = _.cloneDeep(config);
      this.configData = Object.assign(this.configData, config_);
何虹's avatar
何虹 committed
181 182
    },
    submitInput(val) {
何虹's avatar
何虹 committed
183 184 185
      const { entered } = this.configData;
      this.execAction(entered, value, this.elInfo);
      this.$emit("enterInput", this.value_inner, this.elInfo);
何虹's avatar
何虹 committed
186 187
    }
  }
何虹's avatar
何虹 committed
188
};
何虹's avatar
何虹 committed
189 190
</script>
<style scoped>
何虹's avatar
何虹 committed
191
.hsInputBox {
何虹's avatar
何虹 committed
192
  padding: 1px;
何虹's avatar
何虹 committed
193 194
  width: 100%;
  height: 100%;
何虹's avatar
何虹 committed
195 196
  display: flex;
}
何虹's avatar
何虹 committed
197 198
.inputTitle {
  flex: 1;
何虹's avatar
何虹 committed
199 200 201 202
  display: flex;
  justify-content: center;
  align-items: center;
}
何虹's avatar
何虹 committed
203
.inputCurr {
何虹's avatar
何虹 committed
204 205 206 207 208 209
  flex: 3;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>