Blame view

src/components/JsonEditors/index.vue 4.55 KB
xiaomiao committed
1 2
<template>
  <div>
xiaomiao committed
3 4
    <i class="el-icon-s-management icon" :title="title" @click="openDialog" />
    <el-dialog
xiaomiao committed
5
      class="configuration"
xiaomiao committed
6 7 8 9 10 11 12
      :key="key"
      :title="title"
      :inner-dialog="true"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      append-to-body
xiaomiao committed
13
      @cancel="cancel">
xiaomiao committed
14 15 16 17 18 19 20
      <vue-json-editor
        id="minejson"
        v-model="resultInfo"
        :mode="'code'"
        lang="zh"
        @json-change="onJsonChange"
        @json-save="onJsonSave"
xiaomiao committed
21
        @has-error="onError" />
xiaomiao committed
22 23 24 25 26
      <el-tooltip
        content="全屏缩放"
        effect="dark"
        placement="bottom"
        fullscreen
xiaomiao committed
27
        class="fullScreen">
xiaomiao committed
28 29 30 31
        <i class="el-icon-full-screen" @click="enLarge" />
      </el-tooltip>
      <template slot="footer">
        <div class="dialog-footer flex flex-pack-center">
xiaomiao committed
32 33 34
          <el-button
            type="primary"
            class="confirmBtn"
xiaomiao committed
35
            @click="onJsonSave">保存</el-button>
xiaomiao committed
36 37 38
          <el-button
            type="primary"
            class="cancelBtn"
xiaomiao committed
39
            @click="cancel">关闭</el-button>
xiaomiao committed
40 41 42 43 44 45
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
xiaomiao committed
46 47 48 49
  import vueJsonEditor from 'vue-json-editor'
  export default {
    components: {
      vueJsonEditor
xiaomiao committed
50
    },
xiaomiao committed
51 52 53 54
    props: {
      title: {
        type: String,
        default: '配置参数'
xiaomiao committed
55
      },
xiaomiao committed
56 57 58
      resultInfos: {
        type: String,
        default: ''
xiaomiao committed
59 60
      }
    },
xiaomiao committed
61 62 63 64 65 66 67 68 69 70
    data () {
      return {
        activeNames: [],
        resultInfo: {},
        tmpResultInfo: {},
        dialogVisible: false,
        hasJsonFlag: true,
        key: 0,
        isEnlarge: false
      }
xiaomiao committed
71
    },
xiaomiao committed
72 73 74 75 76 77 78 79 80 81 82
    watch: {
      resultInfos: {
        handler: function (val) {
          ++this.key
          this.resultInfo =
            this.resultInfos === '' ? {} : JSON.parse(this.resultInfos)
          this.tmpResultInfo = this.resultInfo
        },
        deep: true,
        immediate: true
      }
xiaomiao committed
83
    },
xiaomiao committed
84 85 86 87

    mounted () {
      this.resultInfo =
        this.resultInfos === '' ? {} : JSON.parse(this.resultInfos)
xiaomiao committed
88
    },
xiaomiao committed
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

    methods: {
      onJsonChange (value) {
        // 只有在格式正确的时候进入此事件
        this.hasJsonFlag = true
      },
      onJsonSave () {
        const value = this.resultInfo
        console.log(this.resultInfo, 'resultInfo')
        if (this.hasJsonFlag === false) {
          this.$message.error({ message: 'json格式验证失败', showClose: true })
          // alert("json验证失败")
          return false
        } else {
          this.dialogVisible = false
          this.$emit('getJsonString', JSON.stringify(value))
          return true
        }
      },
      onError (value) {
        this.hasJsonFlag = false
      },
      openDialog () {
        this.dialogVisible = true
      },
      cancel () {
        console.log(this.tmpResultInfo, 'tmpResultInfo')
        this.resultInfo = this.tmpResultInfo
        this.dialogVisible = false
      },
      // 放大
      enLarge () {
        const fullarea = document.getElementById('minejson')
        if (fullarea.requestFullscreen) {
          fullarea.requestFullscreen()
        } else if (fullarea.webkitRequestFullScreen) {
          fullarea.webkitRequestFullScreen() // webkit内核(chrome、safari、Opera等)
        } else if (fullarea.mozRequestFullScreen) {
          fullarea.mozRequestFullScreen() // moz内核(firefox)
        } else if (fullarea.msRequestFullscreen) {
          fullarea.msRequestFullscreen() // IE11、edge
        }
        this.isEnlarge = true
xiaomiao committed
132 133 134 135 136 137
      }
    }
  }
</script>

<style scoped lang="scss">
xiaomiao committed
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
  /* jsoneditor右上角默认有一个链接,加css去掉了 */
  .icon {
    color: #349af3;
  }
  /deep/ .jsoneditor-vue {
    height: 100%;
  }
  .fullScreen {
    position: absolute;
    right: 5%;
    top: 20%;
    cursor: pointer;
    color: #fff;
  }
  /deep/ .jsoneditor-modes {
    display: none !important;
  }
  /deep/.jsoneditor-poweredBy {
    display: none !important;
  }
  .jsoneditor-menu {
    background-color: #9c9e9f !important;
    border-bottom: 1px solid #9c9e9f !important;
  }
  .jsoneditor {
    border: 1px solid #9c9e9f !important;
  }
  .el-collapse {
    border: 0;
  }
  .el-collapse-item__header {
    height: 44px;
  }
  .configuration {
    color: white;
    margin-top: 6vh;
    /deep/.el-dialog {
      background-color: #031a46 !important;
      border: 1px solid #5f82c7;
      .el-dialog__header {
        .el-dialog__title {
          color: white !important;
        }
        .el-dialog__headerbtn {
          top: 20px;
        }
      }
    }
  }
xiaomiao committed
187
</style>