app-json-schema.vue 5.0 KB
Newer Older
1 2 3 4 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 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 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
<template>
    <div
        ref="json_schema"
        style="
            text-align: center;
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 20px;
        "
    ></div>
</template>
<script lang="ts">
import {
    Vue,
    Component,
    Prop,
    Model,
    Emit,
    Watch,
} from "vue-property-decorator";

@Component({})
export default class AppJsonSchema extends Vue {
    /**
     *值
     */
    @Model("change") readonly value?: any;

    /**
     * 只读
     */
    @Prop({default:false}) public readonly!: boolean;

    /**
     * 禁用
     */
    @Prop({default:false}) public disabled!: boolean;

    /**
     * name
     */
    @Prop() public name!: string;

    /**
     * 是否开启高级配置,默认为true--开启
     */
    @Prop({ default: true }) public enableAdvancedSetting?: boolean;

    /** 
     * 状态管理
     */
    public state: boolean = this.readonly || this.disabled;

    /**
     * json 编辑对象
     */
    public amisScoped: any;

    @Watch("value", { immediate: true })
    onValueWatch() {
        if (this.amisScoped) {
            const tempcom = this.amisScoped.getComponentByName("page.form");
            if (tempcom) {
                if (typeof this.value == 'object' && this.value !== null) {
                    tempcom.setData({ schema: this.value });
                } else {
                    tempcom.setData({ schema: { type: 'object' } });
                }
            }
        }
    }
    @Watch("disabled")
    onDisabledWatch() {
        this.state = this.readonly || this.disabled;
        this.initJsonSchema();
    }

    @Watch("readonly")
    onReadonlyWatch() {
        this.state = this.readonly || this.disabled;
        this.initJsonSchema();
    }

    

    public initJsonSchema() {
        let amis = (window as any).amisRequire("amis/embed");
        let that = this;
        this.amisScoped = amis.embed(
            this.$refs.json_schema,
            {
                type: "page",
                name: "page",
                body: {
                    type: "form",
                    name: "form",
                    title: "属性结构",
                    mode: "horizontal",
                    actions: [
                        {
                            type: "submit",
                            label: "确定",
                            disabled: that.state,
                        },
                    ],
                    body: [
                        {
                            type: "json-schema-editor",
                            name: "schema",
                            label: "字段类型",
                            disabled: that.state,
                            enableAdvancedSetting: that.enableAdvancedSetting,
                            advancedSettings: {
                                string: [
                                    {
                                        type: "input-text",
                                        name: "maxLength",
                                        label: "Max Length",
                                    },
                                ],
                                number: [
                                    {
                                        type: "input-number",
                                        name: "max",
                                        label: "Max",
                                    },
                                ],
                            },
                        },
                    ],
                },
            },
            {},
            {
                tracker: (eventTrack: any, props: any) => {
                    const { eventType } = eventTrack;
                    if (eventType == "submit") {
                        //抛出数据
                        if (props && props.data && props.data.schema) {
                            const obj = props.data.schema;
                            if (obj && (!that.disabled || !that.readonly)) {
                                that.$emit("change", obj);
                            }
                        }
                    }
                },
            }
        );
    }

    /** 
     * 设置值
     */
    public setJsonData() {
        if (this.amisScoped && this.amisScoped.getComponentByName) {
            const tempcom = this.amisScoped.getComponentByName('page.form');
            if (tempcom) {
                if (typeof this.value == 'object' && this.value !== null) {
                    tempcom.setData({ schema: this.value });
                } else {
                    tempcom.setData({ schema: { type: 'object' } });
                }
            }
        }
    }

    /**
     * 生命周期mounted
     */
    mounted() {
        this.initJsonSchema();
        setTimeout(() => {
            this.setJsonData();
        }, 500);
    }
}
</script>