app-form-json.vue 3.5 KB
Newer Older
sq3536's avatar
sq3536 committed
1
<template>
llz's avatar
llz committed
2
  <div class="app-form-json">
3 4
    <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="CurrentVal">
    </el-input>
sq3536's avatar
sq3536 committed
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
    <div id='editor_holder_json'></div>
  </div>
</template>
<script lang="ts">
import { Component, Vue, Prop, Model, Watch } from "vue-property-decorator";
import { VNode, CreateElement } from "vue";
import { interval, Subject, Subscription } from "rxjs";
import JSONEditor from "@json-editor/json-editor";
import BootstrapVue from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";

import "bootstrap-vue/dist/bootstrap-vue.css";
import "../app-form-json/app-form-json.less";

@Component({})
export default class AppFormJson extends Vue {
  /**
   * 双向绑定值
   *
   * @type {*}
   * @memberof AppFormJson
   */
  @Model("change") itemValue?: any;

  /**
   * 表单数据
   *
   * @type {*}
   * @memberof AppFormJson
   */
  @Prop() public data!: any;

sq3536's avatar
sq3536 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  /**
   * 数据格式
   *
   * @type {*}
   * @memberof AppFormJson
   */
  @Prop() public schema?: any;

  /**
   * 格式选项
   *
   * @type {*}
   * @memberof AppFormJson
   */
  @Prop() public options?: any;

sq3536's avatar
sq3536 committed
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
  /**
   * 当前值
   *
   * @return {*}
   * @memberof AppFormJson
   */
  get CurrentVal() {
    return this.itemValue;
  }

  /**
   * 设置值
   *
   * @param {*} [value]
   * @memberof AppFormJson
   */
  set CurrentVal(val: any) {
    this.$emit("change", val);
  }

  /**
   * 表单状态
   *
   * @type {Subject<any>}
   * @memberof AppFormJson
   */
  @Prop() public formState?: Subject<any>;

  /**
   * 表单状态事件
   *
   * @private
   * @type {(Unsubscribable | undefined)}
   * @memberof AppFormJson
   */
  private formStateEvent: Subscription | undefined;

  /**
   * Vue生命周期(实例销毁后)
   *
   * @memberof AppFormJson
   */
  public destroyed() {
    if (this.editor) this.editor = null;
  }

  /**
   * Vue生命周期(实例挂载后)
   *
   * @memberof AppFormJson
   */
  public mounted() {
llz's avatar
llz committed
105 106 107 108 109 110 111 112
    if (!this.formState) {
      return;
    }
    this.formStateEvent = this.formState.subscribe(($event: any) => {
      if (Object.is($event.type, "load")) {
        this.renderJsoneditor();
      }
    });
sq3536's avatar
sq3536 committed
113 114 115 116
  }

  /**
   * 编辑器对象
llz's avatar
llz committed
117 118
   *
   * @type {*}
sq3536's avatar
sq3536 committed
119 120 121 122
   * @memberof AppFormJson
   */
  public editor: any;

sq3536's avatar
sq3536 committed
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

  public getSchema(): any{
    if(this.schema)
      return this.schema;
    else
      return {};
  }

  public getOptions(): any{
    let _options={
      theme: "bootstrap3",
      iconlib: "fontawesome4",
      disable_edit_json: true,
      display_required_only: true,
      disable_collapse: true,
      disable_array_delete_last_row: true,
      ajax: true,
    };

    _options["schema"]=this.getSchema();

    if (this.CurrentVal) {
      _options["startval"] = JSON.parse(this.CurrentVal);
    }

    if(this.options){
      return Object.assign({},_options,this.options)
    }
    return _options;
  }

sq3536's avatar
sq3536 committed
154 155
  /**
   * 编辑器生成
llz's avatar
llz committed
156
   *
sq3536's avatar
sq3536 committed
157 158 159 160 161 162 163 164 165 166
   * @memberof AppFormJson
   */
  public renderJsoneditor() {
    var _this = this;
    var element = document.getElementById("editor_holder_json");
    if (this.editor) {
      this.editor.destroy();
    }


sq3536's avatar
sq3536 committed
167
    this.editor = new JSONEditor(element, _this.getOptions());
sq3536's avatar
sq3536 committed
168 169 170 171 172 173 174

    this.editor.on("change", () => {
      let value = _this.editor.getValue();
      _this.CurrentVal = JSON.stringify(value);
    });
  }

llz's avatar
llz committed
175 176 177 178 179 180
  /**
   * 设置编辑器值
   *
   * @param {*} [val]
   * @memberof AppFormJson
   */
sq3536's avatar
sq3536 committed
181 182 183 184 185 186 187 188 189
  public setEditValue(val: any): void {
    if (val) {
      this.editor.setValue(val);
    } else {
      this.editor.setValue({});
    }
  }
}
</script>