Skip to content

Commit

Permalink
ModelRelations
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Oct 22, 2020
1 parent 0464f9e commit 7e151cd
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 14 deletions.
35 changes: 35 additions & 0 deletions src/components/form/widgets/Percent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<template>
<span class="el-input-group" style="width:6rem;">
<el-input-number v-model="number" :controls="false" style="width:6rem;"
@change="onChange"></el-input-number><span class="el-input-group__append">%</span>
</span>
</template>
<script>
import {options_without_time} from '../../../utils/date_picker_options'
import dateUtil from 'element-ui/src/utils/date'
export default{
props: {
value: String,
field: Object
},
data () {
return {
number: this.value * 100
}
},
components: {},
created () {
this.number = this.value * 100
},
methods: {
onChange(v) {
this.$emit('input', v / 100)
}
},
watch: {
value (v) {
this.number = this.value * 100
}
}
}
</script>
5 changes: 4 additions & 1 deletion src/components/model/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,12 @@
this.model.data = val
this.$emit("input", val)
},
items (val) {
items () {
this.normalizeItems()
},
defaults () {
this.normalizeItems()
}
}
}
</script>
42 changes: 41 additions & 1 deletion src/components/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,47 @@ export default function (appModel, defaults, eventor) {
return axios.all([this.loadOptions(), this.loadViewsConfig()]).then(axios.spread((restOptions, config) => {
return [restOptions, config]
}))
}
},
parentMultipleRelationField (parent) {
if (parent) {
let pfs = Object.values(parent.fieldConfigs)
let f = pfs.find(a => a.model === this.appModel)
if (f && f.multiple === true) {
return f
}
}
return null
},
getParentQueries(parent, pct_id) {
let r = {}
if (parent) {
let f = this.parentMultipleRelationField(parent)
if (f) {
let ids = parent.data[f.name]
r['id__in'] = ids.length > 0 && ids || [0]
} else {
let am = parent.appModel
let pid = parent.id
let fs = Object.values(this.fieldConfigs)
let f = fs.find(a => a.model === am)
if (f && f.multiple !== true) {
r[f.name] = pid
} else {
let popt = this.options
if (popt.generic_foreign_key) {
let {ct_field, fk_field} = popt.generic_foreign_key
r[ct_field] = pct_id
if (!this.fieldConfigs[fk_field]) {
throw Error(`genric foreign key id_field:${id_field} not found.`)
}
r[fk_field] = pid || undefined
}
}

}
}
return r
},
}
m.init()
return m
Expand Down
8 changes: 2 additions & 6 deletions src/components/model/MultiCreator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import Model from './Model'
import {debounce} from 'lodash'
import Qs from 'qs'
import arrayNormalize from '../../utils/array_normalize'
export default{
model: {
event: "change"
Expand Down Expand Up @@ -123,12 +124,7 @@
})
}, 2000),
normalizeItems() {
this.fieldItems = (this.items || this.viewConfig.items).map((a) => {
if (typeof a == 'string') {
return this.model.fieldConfigs[a]
}
return a
})
this.fieldItems = arrayNormalize((this.items || this.viewConfig.items), this.model.fieldConfigs)
},
onEdit () {
this.edit = true
Expand Down
42 changes: 37 additions & 5 deletions src/components/model/Relations.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<template>
<el-tabs type="border-card" v-if="modelItems.length>0">
<el-tab-pane lazy v-for="m in modelItems" :key="m.name">
<el-tabs type="border-card" v-if="modelItems.length>0 && !loading" v-model="index">
<el-tab-pane lazy v-for="m,i in modelItems" :key="m.key || m.name" style="min-height: 10rem;">
<template slot="label"><i :class="`fa fa-${m.icon}`"></i>{{m.label}}</template>
<model-table :appModel="m.name" :baseQueries="m.baseQueries" :items="m.items" :parent="parent"></model-table>
<component :is="m.view" v-bind="[$props, m]" v-if="m.view"></component>
<model-table v-else :appModel="m.name" v-bind="[$props, m]"></model-table>
</el-tab-pane>
</el-tabs>
</template>
Expand All @@ -17,7 +18,9 @@
},
data () {
return {
modelItems: []
modelItems: [],
index: 0,
loading: false
}
},
components: {ModelTable},
Expand All @@ -27,15 +30,44 @@
methods: {
normalizeItems () {
let items = this.items || this.parent.viewsConfig.relations || []
this.modelItems = arrayNormalize(items, {}, (a) => {
this.modelItems = arrayNormalize(items, {}, (a, i) => {
let m = a.model = Model(a.name)
a.icon = a.icon || m.config.icon
a.label = a.label || m.config.verbose_name
return a
})
this.modelItems.forEach((m, i) => {
this.checkComponent(i)
})
},
checkComponent(i) {
let m = this.modelItems[i]
let v = m.view
if (!v) {
return null
}
if (typeof v == 'string') {
let p = `${m.name.replace('.', '/')}/${m.view}`
this.loading = true
return import('@/views/' + p + '.vue').then(module => {
let v = module.default
this.modelItems[i] = {...this.modelItems[i], view: v}
this.loading = false
return v
}).catch(() => {
console.error(`can't import ${m.name}.${m.view}`)
})
} else {
return m.view
}
}
},
computed: {
},
watch: {
index (v) {
// this.checkComponent(v)
},
items () {
this.normalizeItems()
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/model/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<model-table :appModel="appModel" :options="{remoteTable:{table:{topActions:[], rowActions:[]}}}"
:batchActions="[{name:'add', label:`添加到${parent.title()}`, type:'primary', confirm:false, do:addToParent}]"
v-if="parentMultipleRelationField"></model-table>
<component :is="creator" :appModel="appModel" :defaults="parentQueries" v-else
<component :is="creator" :appModel="appModel" :defaults="createDefaults" v-else
:topActions="['saveAndAnother']"></component>

</slot>
Expand Down Expand Up @@ -384,6 +384,9 @@
d[a.name]={name: a.name, label: a.label}
})
return d
},
createDefaults () {
return {...this.baseQueries, ...this.parentQueries}
}
},
watch: {
Expand Down

0 comments on commit 7e151cd

Please sign in to comment.