2018-06-27 14:38:27 +00:00
|
|
|
<template>
|
|
|
|
<div class="frappe-form">
|
|
|
|
<form-actions
|
|
|
|
v-if="shouldRenderForm"
|
2018-07-10 13:35:43 +00:00
|
|
|
:doc="doc"
|
2018-07-14 14:58:18 +00:00
|
|
|
:links="links"
|
2018-06-27 14:38:27 +00:00
|
|
|
@save="save"
|
2018-07-12 07:47:56 +00:00
|
|
|
@submit="submit"
|
|
|
|
@revert="revert"
|
2018-07-13 05:16:15 +00:00
|
|
|
@print="print"
|
2018-06-27 14:38:27 +00:00
|
|
|
/>
|
|
|
|
<div class="p-3">
|
|
|
|
<form-layout
|
|
|
|
v-if="shouldRenderForm"
|
|
|
|
:doc="doc"
|
|
|
|
:fields="meta.fields"
|
|
|
|
:layout="meta.layout"
|
|
|
|
:invalid="invalid"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<not-found v-if="notFound" />
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import frappe from 'frappejs';
|
|
|
|
import FormLayout from './FormLayout';
|
|
|
|
import FormActions from './FormActions';
|
|
|
|
import { _ } from 'frappejs/utils';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Form',
|
2018-07-09 16:35:43 +00:00
|
|
|
props: ['doctype', 'name', 'defaultValues'],
|
2018-06-27 14:38:27 +00:00
|
|
|
components: {
|
|
|
|
FormActions,
|
|
|
|
FormLayout
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
docLoaded: false,
|
|
|
|
notFound: false,
|
|
|
|
invalid: false,
|
2018-07-14 14:58:18 +00:00
|
|
|
invalidFields: [],
|
|
|
|
links: []
|
|
|
|
};
|
2018-06-27 14:38:27 +00:00
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
meta() {
|
|
|
|
return frappe.getMeta(this.doctype);
|
|
|
|
},
|
|
|
|
shouldRenderForm() {
|
|
|
|
return this.name && this.docLoaded;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async created() {
|
|
|
|
if (!this.name) return;
|
|
|
|
try {
|
|
|
|
this.doc = await frappe.getDoc(this.doctype, this.name);
|
2018-07-09 09:45:40 +00:00
|
|
|
|
|
|
|
if (this.doc._notInserted && this.meta.fields.map(df => df.fieldname).includes('name')) {
|
|
|
|
// For a user editable name field,
|
|
|
|
// it should be unset since it is autogenerated
|
|
|
|
this.doc.set('name', '');
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:43 +00:00
|
|
|
if (this.defaultValues) {
|
|
|
|
for (let fieldname in this.defaultValues) {
|
|
|
|
const value = this.defaultValues[fieldname];
|
|
|
|
this.doc.set(fieldname, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 14:38:27 +00:00
|
|
|
this.docLoaded = true;
|
2018-07-14 14:58:18 +00:00
|
|
|
} catch (e) {
|
2018-06-27 14:38:27 +00:00
|
|
|
this.notFound = true;
|
|
|
|
}
|
2018-07-14 14:58:18 +00:00
|
|
|
this.setLinks();
|
|
|
|
this.doc.on('change', this.setLinks);
|
2018-06-27 14:38:27 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async save() {
|
|
|
|
this.setValidity();
|
|
|
|
if (this.invalid) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.doc._notInserted) {
|
|
|
|
await this.doc.insert();
|
|
|
|
} else {
|
|
|
|
await this.doc.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$emit('save', this.doc);
|
2018-07-14 14:58:18 +00:00
|
|
|
|
2018-06-27 14:38:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-14 14:58:18 +00:00
|
|
|
setLinks() {
|
|
|
|
if (this.meta.links) {
|
|
|
|
let links = [];
|
|
|
|
for (let link of this.meta.links) {
|
|
|
|
if (link.condition(this)) {
|
|
|
|
link.handler = () => {
|
|
|
|
link.action(this);
|
|
|
|
};
|
|
|
|
links.push(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.links = links;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-12 07:47:56 +00:00
|
|
|
async submit() {
|
|
|
|
this.doc.set('submitted', 1);
|
|
|
|
await this.save();
|
|
|
|
},
|
|
|
|
|
|
|
|
async revert() {
|
|
|
|
this.doc.set('submitted', 0);
|
|
|
|
await this.save();
|
|
|
|
},
|
|
|
|
|
2018-07-13 05:16:15 +00:00
|
|
|
print() {
|
|
|
|
this.$router.push(`/print/${this.doctype}/${this.name}`);
|
|
|
|
},
|
|
|
|
|
2018-06-27 14:38:27 +00:00
|
|
|
onValidate(fieldname, isValid) {
|
|
|
|
if (!isValid && !this.invalidFields.includes(fieldname)) {
|
|
|
|
this.invalidFields.push(fieldname);
|
|
|
|
} else if (isValid) {
|
2018-07-14 14:58:18 +00:00
|
|
|
this.invalidFields = this.invalidFields.filter(
|
|
|
|
invalidField => invalidField !== fieldname
|
|
|
|
);
|
2018-06-27 14:38:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setValidity() {
|
|
|
|
const form = this.$el.querySelector('form');
|
|
|
|
let validity = form.checkValidity();
|
|
|
|
this.invalid = !validity;
|
2018-07-14 14:58:18 +00:00
|
|
|
}
|
2018-06-27 14:38:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<style>
|
|
|
|
</style>
|