2
0
mirror of https://github.com/frappe/books.git synced 2024-12-28 21:02:44 +00:00
books/ui/components/Form/FormLayout.vue

120 lines
2.8 KiB
Vue
Raw Normal View History

2018-06-27 14:38:27 +00:00
<template>
<form :class="['frappe-form-layout', { 'was-validated': invalid }]">
2019-07-17 10:02:49 +00:00
<div
class="form-row"
2019-08-05 11:46:35 +00:00
v-if="layoutConfig && showSection(i)"
2019-07-17 10:02:49 +00:00
v-for="(section, i) in layoutConfig.sections"
:key="i"
2018-06-27 14:38:27 +00:00
>
<div class="col" v-for="(column, j) in section.columns" :key="j">
<frappe-control
2019-08-05 11:46:35 +00:00
v-for="(fieldname, k) in column.fields"
v-if="shouldRenderField(fieldname)"
2019-08-05 11:46:35 +00:00
:key="k"
2018-06-27 14:38:27 +00:00
:docfield="getDocField(fieldname)"
:value="$data[fieldname]"
:doc="doc"
:autofocus="doc.isNew() && (i === currentSection || i === 0) && j === 0 && k === 0 && !$data[fieldname]"
2018-06-27 14:38:27 +00:00
@change="value => updateDoc(fieldname, value)"
/>
</div>
</div>
</form>
</template>
<script>
export default {
name: 'FormLayout',
props: ['doc', 'fields', 'layout', 'invalid', 'currentSection'],
data() {
const dataObj = {};
for (let df of this.fields) {
dataObj[df.fieldname] = this.doc[df.fieldname];
if (df.fieldtype === 'Table' && !dataObj[df.fieldname]) {
dataObj[df.fieldname] = [];
}
}
return dataObj;
},
created() {
this.doc.on('change', ({ doc, fieldname }) => {
if (fieldname) {
// update value
this[fieldname] = doc[fieldname];
} else {
// update all values
this.fields.forEach(df => {
this[df.fieldname] = doc[df.fieldname];
});
}
});
2019-09-03 10:43:05 +00:00
this.setLabelOptions();
2018-06-27 14:38:27 +00:00
},
methods: {
getDocField(fieldname) {
return this.fields.find(df => df.fieldname === fieldname);
},
shouldRenderField(fieldname) {
let hidden;
try {
hidden = Boolean(this.getDocField(fieldname).hidden(this.doc));
} catch (e) {
hidden = Boolean(this.getDocField(fieldname).hidden) || false;
}
if (hidden) {
return false;
}
2018-11-09 19:07:14 +00:00
if (fieldname === 'name' && !this.doc.isNew()) {
return false;
}
return true;
},
2019-09-03 10:43:05 +00:00
setLabelOptions() {
this.fields.forEach(field => {
if (field.labelOption) {
field.labelOption = field.labelOption(this.doc);
}
});
},
2018-06-27 14:38:27 +00:00
updateDoc(fieldname, value) {
this.doc.set(fieldname, value);
2018-10-23 04:16:50 +00:00
this.$emit('updateDoc', {
fieldname,
value
});
2018-06-27 14:38:27 +00:00
},
showSection(i) {
if (this.layoutConfig.paginated) {
return this.currentSection === i;
}
return true;
}
},
computed: {
layoutConfig() {
let layout = this.layout;
2018-06-27 14:38:27 +00:00
if (!layout) {
const fields = this.fields.map(df => df.fieldname);
2019-07-17 10:02:49 +00:00
layout = [
{
columns: [{ fields }]
}
];
}
2018-06-27 14:38:27 +00:00
if (Array.isArray(layout)) {
layout = {
sections: layout
2019-07-17 10:02:49 +00:00
};
2018-06-27 14:38:27 +00:00
}
return layout;
2018-06-27 14:38:27 +00:00
}
}
};
</script>