v-form-designer新增两个API方法:getWidgetRef、getSelectedWidgetRef。

master
vdpAdmin 2022-04-12 19:29:04 +08:00
parent 72a7f402b3
commit 1d85321f85
15 changed files with 139 additions and 27 deletions

View File

@ -98,7 +98,8 @@ export function createDesigner(vueInstance) {
getImportTemplate() { getImportTemplate() {
return { return {
widgetList: [], widgetList: [],
formConfig: deepClone(this.formConfig) // formConfig: deepClone(this.formConfig)
formConfig: deepClone(defaultFormConfig)
} }
}, },
@ -131,7 +132,7 @@ export function createDesigner(vueInstance) {
} }
}, },
updateSelectedWidgetNameAndRef(selectedWidget, newName, newLabel) { updateSelectedWidgetNameAndLabel(selectedWidget, newName, newLabel) {
this.selectedWidgetName = newName this.selectedWidgetName = newName
//selectedWidget.options.name = newName //此行多余 //selectedWidget.options.name = newName //此行多余
if (!!newLabel && (Object.keys(selectedWidget.options).indexOf('label') > -1)) { if (!!newLabel && (Object.keys(selectedWidget.options).indexOf('label') > -1)) {
@ -637,6 +638,33 @@ export function createDesigner(vueInstance) {
return Object.keys(originalWidget.options).indexOf(configName) > -1 return Object.keys(originalWidget.options).indexOf(configName) > -1
}, },
upgradeWidgetConfig(oldWidget) {
let newWidget = null
if (!!oldWidget.category) {
newWidget = this.getContainerByType(oldWidget.type)
} else {
newWidget = this.getFieldWidgetByType(oldWidget.type)
}
if (!newWidget || !newWidget.options) {
return
}
Object.keys(newWidget.options).forEach(ck => {
if (!oldWidget.hasOwnProperty(ck)) {
oldWidget.options[ck] = deepClone(newWidget.options[ck])
}
})
},
upgradeFormConfig(oldFormConfig) {
Object.keys(this.formConfig).forEach(fc => {
if (!oldFormConfig.hasOwnProperty(fc)) {
oldFormConfig[fc] = deepClone(this.formConfig[fc])
}
})
},
cloneGridCol(widget, parentWidget) { cloneGridCol(widget, parentWidget) {
let newGridCol = deepClone(this.getContainerByType('grid-col')) let newGridCol = deepClone(this.getContainerByType('grid-col'))
newGridCol.options.span = widget.options.span newGridCol.options.span = widget.options.span

View File

@ -41,11 +41,13 @@
import Draggable from 'vuedraggable' import Draggable from 'vuedraggable'
import i18n from "@/utils/i18n"; import i18n from "@/utils/i18n";
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index' import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
import refMixinDesign from "@/components/form-designer/refMixinDesign"
export default { export default {
name: "GridColWidget", name: "GridColWidget",
componentName: "GridColWidget", componentName: "GridColWidget",
mixins: [i18n], mixins: [i18n, refMixinDesign],
inject: ['refList'],
components: { components: {
Draggable, Draggable,
...FieldComponents, ...FieldComponents,
@ -168,6 +170,7 @@
}, },
created() { created() {
this.initRefList()
this.initLayoutProps() this.initLayoutProps()
}, },
methods: { methods: {

View File

@ -28,13 +28,15 @@
<script> <script>
import i18n from "@/utils/i18n" import i18n from "@/utils/i18n"
import GridColWidget from "@/components/form-designer/form-widget/container-widget/grid-col-widget" import GridColWidget from "@/components/form-designer/form-widget/container-widget/grid-col-widget"
import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin"; import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin"
import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper"; import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper"
import refMixinDesign from "@/components/form-designer/refMixinDesign"
export default { export default {
name: "grid-widget", name: "grid-widget",
componentName: 'ContainerWidget', componentName: 'ContainerWidget',
mixins: [i18n, containerMixin], mixins: [i18n, containerMixin, refMixinDesign],
inject: ['refList'],
components: { components: {
ContainerWrapper, ContainerWrapper,
GridColWidget GridColWidget
@ -59,12 +61,14 @@
watch: { watch: {
// //
}, },
created() {
this.initRefList()
},
mounted() { mounted() {
// //
}, },
methods: { methods: {
} }
} }
</script> </script>

View File

@ -49,11 +49,13 @@
import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin" import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin"
import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper" import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper"
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index' import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
import refMixinDesign from "@/components/form-designer/refMixinDesign"
export default { export default {
name: "tab-widget", name: "tab-widget",
componentName: 'ContainerWidget', componentName: 'ContainerWidget',
mixins: [i18n, containerMixin], mixins: [i18n, containerMixin, refMixinDesign],
inject: ['refList'],
components: { components: {
ContainerWrapper, ContainerWrapper,
Draggable, Draggable,
@ -86,6 +88,9 @@
watch: { watch: {
// //
}, },
created() {
this.initRefList()
},
mounted() { mounted() {
// //
}, },

View File

@ -55,11 +55,13 @@
import Draggable from 'vuedraggable' import Draggable from 'vuedraggable'
import i18n from "@/utils/i18n" import i18n from "@/utils/i18n"
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index' import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
import refMixinDesign from "@/components/form-designer/refMixinDesign"
export default { export default {
name: "TableCellWidget", name: "TableCellWidget",
componentName: "TableCellWidget", componentName: "TableCellWidget",
mixins: [i18n], mixins: [i18n, refMixinDesign],
inject: ['refList'],
components: { components: {
Draggable, Draggable,
...FieldComponents, ...FieldComponents,
@ -140,6 +142,9 @@
watch: { watch: {
// //
}, },
created() {
this.initRefList()
},
methods: { methods: {
selectWidget(widget) { selectWidget(widget) {
this.designer.setSelected(widget) this.designer.setSelected(widget)

View File

@ -38,11 +38,13 @@
import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin" import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin"
import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper" import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper"
import TableCellWidget from "@/components/form-designer/form-widget/container-widget/table-cell-widget" import TableCellWidget from "@/components/form-designer/form-widget/container-widget/table-cell-widget"
import refMixinDesign from "@/components/form-designer/refMixinDesign"
export default { export default {
name: "table-widget", name: "table-widget",
componentName: 'ContainerWidget', componentName: 'ContainerWidget',
mixins: [i18n, containerMixin], mixins: [i18n, containerMixin, refMixinDesign],
inject: ['refList'],
components: { components: {
ContainerWrapper, ContainerWrapper,
TableCellWidget, TableCellWidget,
@ -67,6 +69,9 @@
watch: { watch: {
// //
}, },
created() {
this.initRefList()
},
mounted() { mounted() {
// //
}, },

View File

@ -12,7 +12,7 @@
:automatic-dropdown="field.options.automaticDropdown" :automatic-dropdown="field.options.automaticDropdown"
:multiple="field.options.multiple" :multiple-limit="field.options.multipleLimit" :multiple="field.options.multiple" :multiple-limit="field.options.multipleLimit"
:placeholder="field.options.placeholder || i18nt('render.hint.selectPlaceholder')" :placeholder="field.options.placeholder || i18nt('render.hint.selectPlaceholder')"
:remote="this.field.options.remote" :remote-method="remoteQuery" :remote="field.options.remote" :remote-method="remoteQuery"
@focus="handleFocusCustomEvent" @blur="handleBlurCustomEvent" @focus="handleFocusCustomEvent" @blur="handleBlurCustomEvent"
@change="handleChangeEvent"> @change="handleChangeEvent">
<el-option v-for="item in field.options.optionItems" :key="item.value" :label="item.label" <el-option v-for="item in field.options.optionItems" :key="item.value" :label="item.label"

View File

@ -159,14 +159,19 @@
return this.formModel return this.formModel
}, },
getWidgetRef(widgetName, showError) { getWidgetRef(widgetName, showError = false) {
let foundRef = this.widgetRefList[widgetName] let foundRef = this.widgetRefList[widgetName]
if (!foundRef && !!showError) { if (!foundRef && !!showError) {
this.$message.error(this.i18nt('designer.hint.refNotFound') + widgetName) this.$message.error(this.i18nt('render.hint.refNotFound') + widgetName)
} }
return foundRef return foundRef
}, },
getSelectedWidgetRef() {
let wName = this.designer.selectedWidgetName
return this.getWidgetRef(wName)
},
} }
} }
</script> </script>

View File

@ -45,7 +45,7 @@
</el-header> </el-header>
<el-main class="form-widget-main"> <el-main class="form-widget-main">
<el-scrollbar class="container-scroll-bar" :style="{height: scrollerHeight}"> <el-scrollbar class="container-scroll-bar" :style="{height: scrollerHeight}">
<v-form-widget :designer="designer" :form-config="designer.formConfig"> <v-form-widget :designer="designer" :form-config="designer.formConfig" ref="formRef">
</v-form-widget> </v-form-widget>
</el-scrollbar> </el-scrollbar>
</el-main> </el-main>
@ -70,7 +70,7 @@
deepClone, deepClone,
getAllContainerWidgets, getAllContainerWidgets,
getAllFieldWidgets, getAllFieldWidgets,
getQueryParam getQueryParam, traverseAllWidgets
} from "@/utils/util" } from "@/utils/util"
import {MOCK_CASE_URL, VARIANT_FORM_VERSION} from "@/utils/config" import {MOCK_CASE_URL, VARIANT_FORM_VERSION} from "@/utils/config"
import i18n, { changeLocale } from "@/utils/i18n" import i18n, { changeLocale } from "@/utils/i18n"
@ -357,6 +357,32 @@
return !!widgetList ? getAllContainerWidgets(widgetList) : getAllContainerWidgets(this.designer.widgetList) return !!widgetList ? getAllContainerWidgets(widgetList) : getAllContainerWidgets(this.designer.widgetList)
}, },
/**
* 升级表单json以补充最新的组件属性
* @param formJson
*/
upgradeFormJson(formJson) {
if (!formJson.widgetList || !formJson.formConfig) {
this.$message.error('Invalid form json!')
return
}
traverseAllWidgets(formJson.widgetList, (w) => {
this.designer.upgradeWidgetConfig(w)
})
this.designer.upgradeFormConfig(formJson.formConfig)
return formJson
},
getWidgetRef(widgetName, showError = false) {
return this.$refs['formRef'].getWidgetRef(widgetName, showError)
},
getSelectedWidgetRef() {
return this.$refs['formRef'].getSelectedWidgetRef()
},
//TODO: //TODO:
} }
@ -365,6 +391,8 @@
<style lang="scss" scoped> <style lang="scss" scoped>
.el-container.main-container { .el-container.main-container {
background: #fff;
::v-deep aside { /* 防止aside样式被外部样式覆盖 */ ::v-deep aside { /* 防止aside样式被外部样式覆盖 */
margin: 0; margin: 0;
padding: 0; padding: 0;

View File

@ -0,0 +1,28 @@
export default {
methods: {
initRefList() {
if ((this.refList !== null) && !!this.widget.options.name) {
this.refList[this.widget.options.name] = this
}
},
getWidgetRef(widgetName, showError = false) {
let foundRef = this.refList[widgetName]
if (!foundRef && !!showError) {
this.$message.error(this.i18nt('render.hint.refNotFound') + widgetName)
}
return foundRef
},
/* 该方法用于组件重名检查!! */
registerToRefList(oldRefName) {
if ((this.refList !== null) && !!this.widget.options.name) {
if (!!oldRefName) {
delete this.refList[oldRefName]
}
this.refList[this.widget.options.name] = this
}
},
}
}

View File

@ -54,19 +54,18 @@
} }
if (!!this.designer.formWidget) { if (!!this.designer.formWidget) {
//newName let foundRef = this.designer.formWidget.getWidgetRef(newName) // newName
let foundRef = this.designer.formWidget.getWidgetRef(newName)
if (!!foundRef) { if (!!foundRef) {
this.selectedWidget.options.name = oldName this.selectedWidget.options.name = oldName
this.$message.info(this.i18nt('designer.hint.duplicateName') + newName) this.$message.info(this.i18nt('designer.hint.duplicateName') + newName)
return return
} }
let fieldWidget = this.designer.formWidget.getWidgetRef(oldName) let widgetInDesign = this.designer.formWidget.getWidgetRef(oldName)
if (!!fieldWidget && !!fieldWidget.registerToRefList) { if (!!widgetInDesign && !!widgetInDesign.registerToRefList) {
fieldWidget.registerToRefList(oldName) //refref widgetInDesign.registerToRefList(oldName) //refref
let newLabel = this.getLabelByFieldName(newName) let newLabel = this.getLabelByFieldName(newName)
this.designer.updateSelectedWidgetNameAndRef(this.selectedWidget, newName, newLabel) this.designer.updateSelectedWidgetNameAndLabel(this.selectedWidget, newName, newLabel)
} }
} }
}, },

View File

@ -32,6 +32,7 @@
</template> </template>
<script> <script>
import emitter from '@/utils/emitter'
import i18n from "../../../utils/i18n" import i18n from "../../../utils/i18n"
import refMixin from "../../../components/form-render/refMixin" import refMixin from "../../../components/form-render/refMixin"
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index' import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
@ -39,7 +40,7 @@
export default { export default {
name: "GridColItem", name: "GridColItem",
componentName: 'ContainerItem', componentName: 'ContainerItem',
mixins: [i18n, refMixin], mixins: [emitter, i18n, refMixin],
components: { components: {
...FieldComponents, ...FieldComponents,
}, },

View File

@ -26,6 +26,7 @@
</template> </template>
<script> <script>
import emitter from '@/utils/emitter'
import i18n from "../../../utils/i18n" import i18n from "../../../utils/i18n"
import refMixin from "../../../components/form-render/refMixin" import refMixin from "../../../components/form-render/refMixin"
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index' import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
@ -33,7 +34,7 @@
export default { export default {
name: "TableCellItem", name: "TableCellItem",
componentName: "ContainerItem", componentName: "ContainerItem",
mixins: [i18n, refMixin], mixins: [emitter, i18n, refMixin],
components: { components: {
...FieldComponents, ...FieldComponents,
}, },

View File

@ -6,7 +6,7 @@ export default {
} }
}, },
getWidgetRef(widgetName, showError) { getWidgetRef(widgetName, showError = false) {
let foundRef = this.refList[widgetName] let foundRef = this.refList[widgetName]
if (!foundRef && !!showError) { if (!foundRef && !!showError) {
this.$message.error(this.i18nt('render.hint.refNotFound') + widgetName) this.$message.error(this.i18nt('render.hint.refNotFound') + widgetName)

View File

@ -154,9 +154,9 @@ export default {
nodeTreeTitle: 'Tree View Of Component Hierarchy', nodeTreeTitle: 'Tree View Of Component Hierarchy',
clear: 'Clear', clear: 'Clear',
preview: 'Preview', preview: 'Preview',
importJson: 'Import JSON', importJson: 'Import',
exportJson: 'Export JSON', exportJson: 'Export',
exportCode: 'Export Code', exportCode: 'Codes',
generateCode: 'Generate Code', generateCode: 'Generate Code',
generateSFC: 'Generate SFC', generateSFC: 'Generate SFC',
}, },