avue-custom-form.vue 7.2 KB
Newer Older
llz's avatar
llz committed
1 2
<template>
  <div v-if="formOption!=null" class="app-custom-form">
3
    <avue-form :option="formOption" v-model="formVal"></avue-form>
llz's avatar
llz committed
4 5 6 7
  </div>
</template>

<script lang="ts">
8 9 10 11 12 13 14 15
import {
  Vue,
  Component,
  Prop,
  Model,
  Emit,
  Watch,
} from "vue-property-decorator";
ShineKOT's avatar
ShineKOT committed
16

17
import { Subject, Subscription } from "rxjs";
llz's avatar
llz committed
18 19 20 21 22
@Component({})
export default class AvueCustomForm extends Vue {
  /**
   * 编辑器参数传入组件配置
   *
23
   * @type {*}
llz's avatar
llz committed
24 25
   * @memberof AvueCustomForm
   */
26
  public options: any;
llz's avatar
llz committed
27 28 29 30 31 32 33

  /**
   * 是否需要转换为string类型
   *
   * @type {boolean}
   * @memberof AvueCustomForm
   */
34
  @Prop() public isParseString?: boolean;
llz's avatar
llz committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  /**
   * 远端地址
   *
   * @type {string}
   * @memberof AvueCustomForm
   */
  @Prop() public url?: string;

  /**
   * 表单项值
   *
   * @type {any}
   * @memberof AvueCustomForm
   */
50
  @Model("change") public value: any;
llz's avatar
llz committed
51 52 53 54 55 56 57 58 59

  /**
   * 是否将表单数据通过组件配置带入组件中
   *
   * @type {boolean}
   * @memberof AvueCustomForm
   */
  @Prop() public isFormData?: boolean;

60 61 62 63 64 65 66 67
  /**
   * 是否为子表单
   *
   * @type {boolean}
   * @memberof AvueCustomForm
   */
  @Prop() public isSubForm?: boolean;

llz's avatar
llz committed
68 69 70
  /**
   * 表单数据
   *
71
   * @type {*}
llz's avatar
llz committed
72 73
   * @memberof AvueCustomForm
   */
74
  @Prop()
75
  public formData?: any;
llz's avatar
llz committed
76 77 78 79 80 81 82 83 84

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

85 86 87 88 89 90 91 92
  /**
   * 获取组件值
   *
   * @return {any}
   * @memberof AvueCustomForm
   */
  get formVal() {
    let obj: any = {};
93 94 95
    if (this.value) {
      if (this.isParseString) obj = JSON.parse(this.value);
      else obj = this.value;
96 97
      if (this.isSubForm && obj instanceof Array)
        obj = this.loadSubFormData(obj);
98
    }
99 100 101 102 103 104 105 106 107 108 109 110 111
    return obj;
  }

  /**
   * 设置组件值
   *
   * @param value
   * @memberof AvueCustomForm
   */
  set formVal(value: any) {
    this.setValue(value);
  }

llz's avatar
llz committed
112
  /**
llz's avatar
llz committed
113
   * 订阅对象
llz's avatar
llz committed
114 115 116 117 118 119 120 121 122 123
   *
   * @protected
   * @type {(Subscription | undefined)}
   * @memberof AvueCustomForm
   */
  protected formStateEvent: Subscription | undefined;

  /**
   * 当前组件配置设置属性
   *
124
   * @type {*}
llz's avatar
llz committed
125 126 127 128 129 130 131
   * @memberof AvueCustomForm
   */
  public formOption: any = null;

  /**
   * avue-form默认配置
   *
132
   * @type {*}
llz's avatar
llz committed
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
   * @memberof AvueCustomForm
   */
  public defaultOptions: any = {
    column: [
      {
        type: "dynamic",
        label: "子表单",
        span: 24,
        display: true,
        children: {
          align: "center",
          headerAlign: "center",
          index: false,
          addBtn: true,
          delBtn: true,
          column: [
            {
              type: "input",
              label: "属性",
              span: 24,
              display: true,
              prop: "property",
            },
            {
              type: "input",
              label: "值",
              span: 24,
              display: true,
              prop: "value",
            },
          ],
        },
        prop: "1599476281048_17916",
      },
    ],
    labelPosition: "left",
    labelSuffix: ":",
    labelWidth: 120,
    gutter: 0,
    menuBtn: false,
    submitBtn: false,
    submitText: "提交",
    emptyBtn: false,
    emptyText: "清空",
    menuPosition: "center",
  };

  /**
   * vue生命周期
   *
   * @memberof AvueCustomForm
   */
  public mounted() {
    let that: any = this;
    if (this.formState) {
      this.formStateEvent = this.formState.subscribe(({ type, data }) => {
        if (Object.is("load", type)) that.load();
      });
    }
  }

  /**
   * 加载表单配置,配置优先级,依次按优先级加载(表单项值  >   远端请求  >   默认值)
   *
   * @memberof AvueCustomForm
   */
199
  public async load() {
llz's avatar
llz committed
200 201 202 203
    let that: any = this;
    if (!this.options && this.options == null) {
      if (this.url && this.options == null) {
        const get: Promise<any> = this.$http.get(this.url);
204
        await get.then((response: any) => {
205
          if (response && response.data) {
206 207 208 209
            let options: any = response.data;
            this.transitionDicUrlCondition(options);
            that.formOption = options;
            if (that.isFormData) that.getFormData();
llz's avatar
llz committed
210 211 212
          }
        });
      } else {
213
        this.transitionDicUrlCondition(this.defaultOptions);
llz's avatar
llz committed
214 215 216 217
        this.formOption = this.defaultOptions;
        if (this.isFormData) that.getFormData();
      }
    } else {
218
      this.transitionDicUrlCondition(this.options);
llz's avatar
llz committed
219 220 221 222 223 224 225 226 227 228 229 230 231
      this.formOption = this.options;
      if (this.isFormData) that.getFormData();
    }
  }

  /**
   * 当组件配置中的属性与表单属性重复时,表单值塞入组件中
   *
   * @memberof AvueCustomForm
   */
  public getFormData() {
    let that: any = this;
    let obj: any;
232
    if (this.formVal) obj = JSON.parse(JSON.stringify(this.formVal));
llz's avatar
llz committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    else obj = {};
    let recursionOption: any = function (group: any) {
      group.column.forEach((gItem: any) => {
        if (group.column && group.column.length > 0) {
          if (that.formData[gItem.prop])
            obj[gItem.prop] = that.formData[gItem.prop];
        }
      });
      if (group.group && group.group.length > 0)
        group.group.forEach((gItem: any) => {
          recursionOption(gItem);
        });
    };
    recursionOption(this.formOption);
    this.setValue(obj);
  }

  /**
   * 设置表单值
   *
   * @param {*} value
   * @memberof AvueCustomForm
   */
  public setValue(value: any) {
257
    if (this.isSubForm) value = this.getSubFormData(value);
llz's avatar
llz committed
258 259 260
    if (this.isParseString) this.$emit("change", JSON.stringify(value));
    else this.$emit("change", value);
  }
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  /**
   * 提取第一个属性值
   *
   * @memberof AvueCustomForm
   * @return {Array<any>}
   */
  public getSubFormData(value: any): Array<any> {
    let arr: Array<any> = [];
    for (let val in value) {
      arr = value[val];
      break;
    }
    return arr;
  }

  /**
   * 加载子表单值
   *
   * @memberof AvueCustomForm
   * @return {*}
   */
  public loadSubFormData(arr: Array<any>): any {
    let value: any = {};
    value[this.formOption.column[0].prop] = arr;
    return value;
  }

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  /**
   * 配置的下拉列表转换符号支持动态配置
   *
   * @memberof AvueCustomForm
   * @param {*}
   */
  public transitionDicUrlCondition(options: any) {
    let that: any = this;
    let recursive: any = function (obj: any) {
      if (obj.column && obj.column.length > 0) {
        obj.column.forEach((col: any) => {
          if (col.dicUrl && col.dicUrl.indexOf("$") > 0) {
            let g = /\${[^+]+}/;
            let dicGroup = col.dicUrl.match(g);
            dicGroup.forEach((dic: any) => {
              col.dicUrl = col.dicUrl.replace(
                dic,
                that.formData[dic.substring(2, dic.length - 1)]
              );
            });
          }
          if (col.children) recursive(col.children);
          if (col.group) recursive(col.group);
        });
      }
      if (obj.children) recursive(obj.children);
      if (obj.group) recursive(obj.group);
    };
    recursive(options);
  }

320 321 322 323 324 325
  /**
   * 销毁组件(vue生命周期)
   *
   * @type {Subject<any>}
   * @memberof AvueCustomForm
   */
326 327 328
  public destroy() {
    if (this.formStateEvent) {
      this.formStateEvent.unsubscribe();
329 330
    }
  }
llz's avatar
llz committed
331 332
}
</script>