app-form-json.vue 7.3 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 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
    <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;

  /**
   * 当前值
   *
   * @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
89 90 91 92 93 94 95 96
    if (!this.formState) {
      return;
    }
    this.formStateEvent = this.formState.subscribe(($event: any) => {
      if (Object.is($event.type, "load")) {
        this.renderJsoneditor();
      }
    });
sq3536's avatar
sq3536 committed
97 98 99 100
  }

  /**
   * 编辑器对象
llz's avatar
llz committed
101 102
   *
   * @type {*}
sq3536's avatar
sq3536 committed
103 104 105 106 107 108
   * @memberof AppFormJson
   */
  public editor: any;

  /**
   * 编辑器生成
llz's avatar
llz committed
109
   *
sq3536's avatar
sq3536 committed
110 111 112 113 114 115 116 117 118 119
   * @memberof AppFormJson
   */
  public renderJsoneditor() {
    var _this = this;
    var element = document.getElementById("editor_holder_json");
    if (this.editor) {
      this.editor.destroy();
    }

    let _schema = {
llz's avatar
llz committed
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      $ref: "#/definitions/model",
      definitions: {
        model: {
          title: "模型",
          headerTemplate: "{{ self.dataModelName }}",
          type: "object",
          format: "grid",
          properties: {
            dataModelName: {
              title: "模型名",
              type: "string",
              options: {
                grid_columns: 4,
              },
              propertyOrder: 1,
            },
            objectProperties: {
              title: "对象参数集合",
              type: "array",
              format: "tabs",
              uniqueItems: true,
              items: {
                title: "对象",
                headerTemplate: "{{ self.propertyName }}",
                type: "object",
                properties: {
                  propertyName: {
                    title: "对象",
                    type: "string",
                    options: {
                      grid_columns: 4,
                    },
                    propertyOrder: 1,
                  },
                  system: {
                    title: "系统",
                    type: "string",
                    options: {
                      grid_columns: 4,
                    },
                    propertyOrder: 2,
                  },
                  propertyEntity: {
                    title: "结构实体",
                    type: "string",
                    options: {
                      grid_columns: 4,
                    },
                    propertyOrder: 3,
                  },
                  propertyMappings: {
                    title: "对象关系映射",
                    type: "array",
                    format: "table",
                    uniqueItems: true,
                    items: {
                      title: "映射",
                      type: "object",
                      properties: {
                        selfPropertyColumn: {
                          title: "列",
                          type: "string",
                          options: {
                            grid_columns: 4,
                          },
                          propertyOrder: 1,
                        },
                        joinPropertyName: {
                          title: "关联对象",
                          type: "string",
                          options: {
                            grid_columns: 4,
                          },
                          propertyOrder: 2,
                        },
                        joinPropertyColumn: {
                          title: "关联列",
                          type: "string",
                          options: {
                            grid_columns: 4,
                          },
                          propertyOrder: 3,
                        },
                      },
                    },
                    propertyOrder: 4,
                  },
                },
              },
              propertyOrder: 2,
            },
            nestedDataModels: {
              title: "嵌套子模型集合",
              type: "array",
              format: "tabs",
              uniqueItems: true,
              items: {
                title: "子模型",
                $ref: "#/definitions/model",
              },
              propertyOrder: 3,
            },
            layerMappings: {
              title: "模型上下级映射",
              type: "array",
              format: "table",
              uniqueItems: true,
              items: {
                title: "映射",
                type: "object",
                properties: {
                  selfPropertyColumn: {
                    title: "列",
                    type: "string",
                    options: {
                      grid_columns: 4,
                    },
                    propertyOrder: 1,
                  },
                  parentPropertyColumn: {
                    title: "父模型列",
                    type: "string",
                    options: {
                      grid_columns: 4,
                    },
                    propertyOrder: 2,
                  },
                },
              },
              propertyOrder: 4,
            },
          },
        },
      },
    };
sq3536's avatar
sq3536 committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    let opt: any = {
      schema: _schema,
      theme: "bootstrap3",
      iconlib: "fontawesome4",
      disable_edit_json: true,
      display_required_only: true,
      disable_collapse: true,
      disable_array_delete_last_row: true,
      ajax: true,
    };
    if (this.CurrentVal) {
      opt["startval"] = JSON.parse(this.CurrentVal);
    }

    this.editor = new JSONEditor(element, opt);

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

llz's avatar
llz committed
277 278 279 280 281 282
  /**
   * 设置编辑器值
   *
   * @param {*} [val]
   * @memberof AppFormJson
   */
sq3536's avatar
sq3536 committed
283 284 285 286 287 288 289 290 291
  public setEditValue(val: any): void {
    if (val) {
      this.editor.setValue(val);
    } else {
      this.editor.setValue({});
    }
  }
}
</script>