2018-03-30 16:58:00 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-03-26 13:43:06 +00:00
|
|
|
const controls = require('./controls');
|
2018-03-27 16:49:23 +00:00
|
|
|
const Observable = require('frappejs/utils/observable');
|
2018-03-26 13:43:06 +00:00
|
|
|
|
2018-03-27 16:49:23 +00:00
|
|
|
module.exports = class FormLayout extends Observable {
|
2018-03-30 16:58:00 +00:00
|
|
|
constructor({fields, doc, layout, events = []}) {
|
2018-03-27 16:49:23 +00:00
|
|
|
super();
|
|
|
|
Object.assign(this, arguments[0]);
|
2018-03-27 09:13:56 +00:00
|
|
|
this.controls = {};
|
2018-03-26 13:43:06 +00:00
|
|
|
this.controlList = [];
|
2018-03-27 09:13:56 +00:00
|
|
|
this.sections = [];
|
2018-03-27 16:49:23 +00:00
|
|
|
this.links = [];
|
|
|
|
|
2018-03-27 09:13:56 +00:00
|
|
|
this.form = document.createElement('div');
|
|
|
|
this.form.classList.add('form-body');
|
2018-03-26 13:43:06 +00:00
|
|
|
|
2018-03-27 09:13:56 +00:00
|
|
|
this.makeLayout();
|
2018-03-30 16:58:00 +00:00
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
this.bindEvents(doc);
|
|
|
|
}
|
2018-03-27 09:13:56 +00:00
|
|
|
}
|
2018-03-26 13:43:06 +00:00
|
|
|
|
2018-03-27 09:13:56 +00:00
|
|
|
makeLayout() {
|
2018-03-26 13:43:06 +00:00
|
|
|
if (this.layout) {
|
|
|
|
for (let section of this.layout) {
|
|
|
|
this.makeSection(section);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.makeControls(this.fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
makeSection(section) {
|
|
|
|
const sectionElement = frappe.ui.add('div', 'form-section', this.form);
|
2018-03-30 16:58:00 +00:00
|
|
|
const sectionHead = frappe.ui.add('div', 'form-section-head', sectionElement);
|
|
|
|
const sectionBody = frappe.ui.add('div', 'form-section-body', sectionElement);
|
|
|
|
|
|
|
|
if (section.title) {
|
|
|
|
const head = frappe.ui.add('h6', 'uppercase', sectionHead);
|
|
|
|
head.textContent = section.title;
|
|
|
|
}
|
|
|
|
|
2018-03-26 13:43:06 +00:00
|
|
|
if (section.columns) {
|
2018-03-30 16:58:00 +00:00
|
|
|
sectionBody.classList.add('row');
|
2018-03-26 13:43:06 +00:00
|
|
|
for (let column of section.columns) {
|
2018-03-30 16:58:00 +00:00
|
|
|
let columnElement = frappe.ui.add('div', 'col', sectionBody);
|
2018-03-26 13:43:06 +00:00
|
|
|
this.makeControls(this.getFieldsFromLayoutElement(column.fields), columnElement);
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-30 16:58:00 +00:00
|
|
|
this.makeControls(this.getFieldsFromLayoutElement(section.fields), sectionBody);
|
2018-03-26 13:43:06 +00:00
|
|
|
}
|
2018-03-30 16:58:00 +00:00
|
|
|
this.sections.push(sectionBody);
|
2018-03-26 13:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getFieldsFromLayoutElement(fields) {
|
|
|
|
return this.fields.filter(d => fields.includes(d.fieldname));
|
|
|
|
}
|
|
|
|
|
|
|
|
makeControls(fields, parent) {
|
|
|
|
for(let field of fields) {
|
|
|
|
if (!field.hidden && controls.getControlClass(field.fieldtype)) {
|
|
|
|
let control = controls.makeControl({field: field, form: this, parent: parent});
|
|
|
|
this.controlList.push(control);
|
|
|
|
this.controls[field.fieldname] = control;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 16:49:23 +00:00
|
|
|
|
|
|
|
async bindEvents(doc) {
|
|
|
|
this.doc = doc;
|
|
|
|
this.controlList.forEach(control => {
|
|
|
|
control.bind(this.doc);
|
|
|
|
});
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
this.controlList.forEach(control => {
|
|
|
|
control.refresh();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bindFormEvents() {
|
|
|
|
if (this.events) {
|
|
|
|
for (let key in this.events) {
|
|
|
|
this.on(key, this.events[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 13:43:06 +00:00
|
|
|
}
|