Skip to content

Commit

Permalink
auth gen edit_page, list_page
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Sep 6, 2019
1 parent c8e8385 commit 06dc43c
Show file tree
Hide file tree
Showing 18 changed files with 358 additions and 181 deletions.
18 changes: 14 additions & 4 deletions src/components/form/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<el-alert :title="errors.non_field_errors" type="error" v-if="errors.non_field_errors"
:closable="false"></el-alert>
<el-row>
<template v-for="f in formItems">
<template v-for="f in formItems" v-if="canEdit(f)">
<el-col :xs="f.span.xs" :sm="f.span.sm" :md="f.span.md" :lg="f.span.lg" :xl="f.span.xl"
:key="f.name" v-if="!elOptions.inline && !elOptions.oneColumn && f.widget !== 'hidden'">
<form-item :field="f" v-model="formValue" :options="options" :error="errors[f.name]"></form-item>
Expand Down Expand Up @@ -60,7 +60,8 @@
submit: Function,
submitName: {
type: String, default: '提交'
}
},
successInfo: String
},
components: {FormItem, Actions},
data () {
Expand All @@ -87,11 +88,19 @@
return data
})
}
},
canEdit(f) {
return ! (f.widget instanceof Function)
},
genValuesWithFunctionWidget () {
let d = this.formValue
this.formItems.filter(a => a.widget instanceof Function).forEach(a => {
d[a.name] = a.widget(d)
})
},
onPosted(data){
this.loading = false
this.$message({message: `${this.submitName}成功`, type: 'success'})
this.$message({message: this.successInfo || `${this.submitName}成功`, type: 'success'})
this.$emit("form-posted", data)
return data
},
Expand All @@ -101,6 +110,7 @@
}
},
onSubmit () {
this.genValuesWithFunctionWidget()
return this.$refs.form.validate().then(this.onValidated).then(this.doSubmit).then(this.onPosted).catch(e => {
if (e === false) {
this.$message({message: '表单检验未通过,请按提示修改', type: 'error'})
Expand Down
30 changes: 13 additions & 17 deletions src/components/layout/Actions.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<template>
<div>
<el-button-group v-if="showActions.length>0">
<template v-for="a in showActions">
<el-button :type="a.type" :title="a.title" size="small" @click="handleCommand(a.do)"
v-if="!a.show || a.show()" :key="a.name">
<i :class="`fa fa-${a.icon}`"></i>{{a.label}}
</el-button>
</template>
</el-button-group>
<el-dropdown v-if="dropdownActions.length>0" @command="handleCommand" size="small" :trigger="trigger">
<el-button-group v-if="showActions.length>0">
<template v-for="a in showActions">
<el-button :type="a.type" :title="a.title" :size="size" @click="handleCommand(a.do)"
v-if="!a.show || a.show()" :key="a.name">
<i :class="`fa fa-${a.icon}`"></i>{{a.label}}
</el-button>
</template>
<el-dropdown v-if="dropdownActions.length>0" @command="handleCommand" :size="size" :trigger="trigger">
<span>
<i class="el-icon-arrow-down el-icon--right" style="margin-right: 1rem"></i>
<i class="el-icon-arrow-down el-icon--right" style="margin: 0 0.5rem;"></i>
</span>
<el-dropdown-menu slot="dropdown">
<template v-for="a in dropdownActions">
Expand All @@ -21,7 +19,7 @@
</template>
</el-dropdown-menu>
</el-dropdown>
</div>
</el-button-group>
</template>
<script>
export default{
Expand All @@ -33,7 +31,8 @@
type: Object, default: () => {
return {}
}
}
},
size: {type: String, default: "small"}
},
data () {
return {}
Expand All @@ -47,7 +46,7 @@
computed: {
_items (){
return this.items.map(a => {
if(typeof a === 'string'){
if (typeof a === 'string') {
a = this.map[a]
}
return a
Expand All @@ -68,6 +67,3 @@
}
}
</script>
<style scoped>
</style>
64 changes: 33 additions & 31 deletions src/components/layout/BatchActions.vue
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
<template>
<span>
<el-button plain :icon="a.icon" v-for="a in items" @click="onCommand(a.name)" :title="a.notice"
:disabled="count===0"
<el-button plain :icon="a.icon" v-for="a in items" @click="onCommand(a)" :title="a.notice"
:disabled="count===0" :type="a.type"
:key="a.name">{{a.label}}
</el-button>
</span>
</template>
<script>
export default{
props:{
items: Array,
count: {type:Number, default: 0}
},
data () {
return {}
},
components: {},
methods: {
onCommand(name){
let rc = this.count
if (rc == 0) {
this.$message('请先勾选至少一条记录')
return
}
let action = this.items.find((a) => a.name == name)
if (action && action.do) {
this.$confirm(action.notice, `确定要对勾选中的${rc}条记录执行"${action.label}"操作吗?`, {type: 'warning'}).then(() => {
action.do(name).then(({data}) => {
this.$message(`操作成功 ${data.rows}`)
this.$emit("done", data)
export default{
props: {
items: Array,
context: Object,
count: {type: Number, default: 0}
},
data () {
return {}
},
components: {},
methods: {
onCommand(action){
let rc = this.count
if (rc == 0) {
this.$message('请先勾选至少一条记录')
return
}
if (action && action.do) {
let confirm = action.confirm === false ? () => Promise.resolve() : () => this.$confirm(action.notice, `确定要对勾选中的${rc}条记录执行"${action.label}"操作吗?`, {type: 'warning'})
confirm().then(() => {
action.do({action, ...this.context}).then(({data}) => {
let countInfo = data.rows >= 0 ? `${data.rows}条记录` : ''
this.$message({message: `操作成功${countInfo}.`, type: 'success'})
this.$emit("done", data)
})
}).catch(this.onServerResponseError).catch(() => {
})
}).catch(this.onServerResponseError).catch(()=>{})
}
this.$emit("command", name)
}
this.$emit("command", name)
}
},
computed: {}
}
},
computed: {}
}
</script>
<style scoped></style>
23 changes: 23 additions & 0 deletions src/components/model/EditPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<model-form :appModel="appModel" v-model="data" ref="form">
<template v-slot:bottom="{model}">
<model-relations v-if="model.data.id" :items="model.config.relations"
:parent="model"></model-relations>
</template>
</model-form>
</template>
<script>
import edit_mixin from 'vue-django/src/components/model/edit_mixin'
export default{
mixins: [edit_mixin],
data () {
return {
appModel: ''
}
},
created () {
let ps = this.$route.path.split('/')
this.appModel = `${ps[1]}.${ps[2]}`
}
}
</script>
28 changes: 19 additions & 9 deletions src/components/model/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<actions :items="_topActions"></actions>
</el-col>
</el-row>
<x-form :url="url" :items="formItems" v-model="formValue" ref="form" :options="options.form"
<x-form :url="url" :items="formItems" v-model="formValue" ref="form" :options="options.form" :successInfo="successInfo"
:method="method" @form-posted="onPosted" :submit="submit">
<span slot="submit" v-if="!options.inline"></span>
</x-form>
Expand All @@ -19,10 +19,12 @@
import {Register} from '../../utils/app_model'
import XForm from '../form/Form.vue'
import Model from './Model'
import server_response from '../../mixins/server_response'
import array_normalize from '../../utils/array_normalize'
import Actions from '../layout/Actions.vue'
import RelatedSelect from './Select.vue'
export default{
mixins: [server_response],
components: {XForm, Actions},
props: {
appModel: String,
Expand Down Expand Up @@ -50,6 +52,8 @@
mid : undefined,
formItems: [],
formValue: {},
intent: '',
successInfo: undefined,
model: Model(this.appModel, this.defaults, this.$store.state.bus),
avairableActions: {
'save': {
Expand Down Expand Up @@ -97,7 +101,7 @@
this.formValue = Object.assign({},this.model.data)
this.normalizeItems()
this.$emit('loaded', this.model)
})
}).catch(this.onServerResponseError)
},
getId(){
let id = this.value && this.value.id ||
Expand All @@ -118,6 +122,8 @@
}
return import(`@/views${this.model.getListUrl()}config.js`).then(m => {
return m.default.formItems
}).catch(() => {}).then( items => {
return items || this.model.config.formItems || Object.values(this.model.options.actions.POST).filter(a => a.read_only !== true)
})
},
Expand All @@ -139,7 +145,7 @@
},
onPosted(data)
{
let payLoad = {model: this.model.config, data}
let payLoad = {model: this.model.config, data, intent: this.intent}
this.$emit("form-posted", payLoad)
},
toDelete(){
Expand All @@ -150,17 +156,21 @@
this.mid = this.model.id = undefined
this.formValue = Object.assign({},this.model.data)
},
onDelete(){
this.$confirm('确定要删除吗?', {type: 'warning'}).then(() => {
return this.model.destroy()
}).catch(this.onServerResponseError)
},
onSubmit (context) {
return this.$refs.form.onSubmit().then((data) => {
if (data && data.id) {
this.$router.replace(this.model.getDetailUrl(data.id))
}
})
this.intent = 'save'
this.successInfo = '保存成功.'
return this.$refs.form.onSubmit()
},
saveAndAnother(){
this.intent = 'saveAndAnother'
this.successInfo = '保存成功, 继续添加下一个.'
this.$refs.form.onSubmit().then((data) => {
if (data && data.id) {
this.$message({message: '继续创建下一个'})
this.clear()
}
})
Expand Down
18 changes: 18 additions & 0 deletions src/components/model/ListPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<model-table :appModel="appModel" v-model="data" ref="table"></model-table>
</template>
<script>
import list_mixin from 'vue-django/src/components/model/list_mixin'
export default{
mixins: [list_mixin],
data () {
return {
appModel: ''
}
},
created () {
let ps = this.$route.path.split('/')
this.appModel = `${ps[1]}.${ps[2]}`
}
}
</script>
11 changes: 6 additions & 5 deletions src/components/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,18 @@ export default function (appModel, defaults, eventor) {
return promise.then(({data}) => {
this.id = data.id
this.data = Object.assign({}, this.data, data)
this.emitPosted()
this.emitPosted(this.id)
return data
})//.catch((error) => this.onErrors(error))
},
delete(id){
destroy(id){
id = id || this.id
return axios.delete(this.getDetailUrl(id)).then(() => {
this.eventor.$emit('model-deleted', {model: this})
this.eventor.$emit('model-deleted', {appModel: this.appModel, id})
})
},
emitPosted(){
this.eventor.$emit('model-posted', {model: this})
emitPosted(id){
this.eventor.$emit('model-posted', {appModel: this.appModel, id})
},
onErrors(error){
if (error.code === 400) {
Expand Down
3 changes: 1 addition & 2 deletions src/components/model/Relations.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<el-tabs type="border-card">
<el-tabs type="border-card" v-if="items.length>0">
<el-tab-pane lazy v-for="m in modelItems" :key="m.name">
<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"
Expand Down Expand Up @@ -28,7 +28,6 @@
let m = a.model = Model(a.name)
a.icon = m.config.icon
a.label = m.config.verbose_name
a.items = m.config.listItems || ['__str__']
return a
})
},
Expand Down
1 change: 0 additions & 1 deletion src/components/model/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<model-select :field="f" v-model="value[f.name]" @input="onSearch"
:showCreate="false" :appModel="f.model"
v-if="f.model" :pageSize="100"></model-select>
&nbsp;
</template>
</div>
</template>
Expand Down

0 comments on commit 06dc43c

Please sign in to comment.