Blame view

src/views/system/menus/JsonEditors/index.vue 3.45 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
          <btn nativeType="cx" @click="onJsonSave">保存</btn>
          <btn nativeType="cx" @click="cancel">关闭</btn>

xiaomiao committed
35 36 37 38 39 40
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
xiaomiao committed
41 42 43 44
  import vueJsonEditor from 'vue-json-editor'
  export default {
    components: {
      vueJsonEditor
xiaomiao committed
45
    },
xiaomiao committed
46 47 48 49
    props: {
      title: {
        type: String,
        default: '配置参数'
xiaomiao committed
50
      },
xiaomiao committed
51 52 53
      resultInfos: {
        type: String,
        default: ''
xiaomiao committed
54 55
      }
    },
xiaomiao committed
56 57 58 59 60 61 62 63 64 65
    data () {
      return {
        activeNames: [],
        resultInfo: {},
        tmpResultInfo: {},
        dialogVisible: false,
        hasJsonFlag: true,
        key: 0,
        isEnlarge: false
      }
xiaomiao committed
66
    },
xiaomiao committed
67 68 69 70 71 72 73 74 75 76 77
    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
78
    },
xiaomiao committed
79 80 81 82

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

    methods: {
      onJsonChange (value) {
        // 只有在格式正确的时候进入此事件
        this.hasJsonFlag = true
      },
      onJsonSave () {
        const value = this.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 () {
        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
125 126 127 128 129 130
      }
    }
  }
</script>

<style scoped lang="scss">
xiaomiao committed
131 132 133 134
  /* jsoneditor右上角默认有一个链接,加css去掉了 */
  .icon {
    color: #349af3;
  }
xiaomiao committed
135
</style>