2
0
mirror of https://github.com/frappe/books.git synced 2025-01-10 18:24:40 +00:00
books/src/pages/QuickEditForm/index.vue

131 lines
3.4 KiB
Vue
Raw Normal View History

2019-10-04 20:18:10 +00:00
<template>
<div class="border-l h-full">
<div class="flex justify-end px-4 pt-4">
<Button @click="routeToList">
2019-10-04 20:18:10 +00:00
<XIcon class="w-3 h-3 stroke-current text-gray-700" />
</Button>
<Button @click="insertDoc" type="primary" v-if="doc._notInserted" class="ml-2 flex">
<feather-icon name="check" class="text-white" />
</Button>
2019-10-04 20:18:10 +00:00
</div>
<div class="px-4 pt-2 pb-4 border-b flex items-center justify-between">
<FormControl
ref="titleControl"
v-if="titleDocField"
input-class="focus:shadow-outline-px"
:df="titleDocField"
:value="doc[titleDocField.fieldname]"
@change="value => valueChange(titleDocField, value)"
/>
<span v-if="showSaved" class="text-xs text-gray-600">{{ _('Saved') }}</span>
2019-10-04 20:18:10 +00:00
</div>
<div class="text-xs">
<div
class="grid border-b"
2019-10-04 20:18:10 +00:00
style="grid-template-columns: 1fr 2fr"
v-for="df in fields"
:key="df.fieldname"
>
<div class="py-3 pl-4 text-gray-600">{{ df.label }}</div>
2019-10-06 12:33:21 +00:00
<div class="py-3 pr-4">
<FormControl
input-class="focus:shadow-outline-px"
:df="df"
:value="doc[df.fieldname]"
@change="value => valueChange(df, value)"
/>
</div>
2019-10-04 20:18:10 +00:00
</div>
</div>
</div>
</template>
<script>
import frappe from 'frappejs';
import Button from '@/components/Button';
import XIcon from '@/components/Icons/X';
import FormControl from '@/components/Controls/FormControl';
2019-10-04 20:18:10 +00:00
export default {
name: 'QuickEditForm',
2019-10-06 12:33:21 +00:00
props: ['doctype', 'name', 'values'],
2019-10-04 20:18:10 +00:00
components: {
Button,
XIcon,
FormControl
2019-10-04 20:18:10 +00:00
},
2019-10-06 12:33:21 +00:00
provide() {
return {
doctype: this.doctype,
name: this.name
}
},
2019-10-04 20:18:10 +00:00
data() {
return {
meta: null,
2019-10-04 20:18:10 +00:00
doc: {},
fields: [],
titleDocField: null,
showSaved: false
2019-10-04 20:18:10 +00:00
};
},
async mounted() {
this.meta = frappe.getMeta(this.doctype);
this.fields = this.meta
2019-10-04 20:18:10 +00:00
.getQuickEditFields()
.map(fieldname => this.meta.getField(fieldname));
this.titleDocField = this.meta.getField(this.meta.titleField);
await this.fetchDoc();
// setup the title field
if (this.doc._notInserted) {
this.doc.set(this.titleDocField.fieldname, '');
}
2019-10-06 12:33:21 +00:00
if (this.values) {
this.doc.set(this.values);
}
setTimeout(() => {
this.$refs.titleControl.focus()
}, 300);
},
methods: {
valueChange(df, value) {
if (!value) return;
let oldValue = this.doc.get(df.fieldname);
if (df.fieldname === 'name' && oldValue !== value && !this.doc._notInserted) {
this.doc.rename(value);
this.doc.once('afterRename', () => {
this.$router.push(`/list/${this.doctype}/${this.doc.name}`);
});
return;
}
this.doc.set(df.fieldname, value);
if (this.doc._dirty && !this.doc._notInserted) {
this.updateDoc();
}
},
async fetchDoc() {
this.doc = await frappe.getDoc(this.doctype, this.name);
},
async updateDoc() {
try {
await this.doc.update();
this.triggerSaved();
} catch (e) {
await this.fetchDoc();
}
},
triggerSaved() {
this.showSaved = true;
setTimeout(() => (this.showSaved = false), 1000);
},
insertDoc() {
this.doc.insert();
},
routeToList() {
this.$router.push(`/list/${this.doctype}`);
}
2019-10-04 20:18:10 +00:00
}
};
</script>