2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00

implement templates for control

This commit is contained in:
Rushabh Mehta 2018-02-19 12:01:07 +05:30
parent 2422e33ec0
commit 4d3a176d98
6 changed files with 35 additions and 11 deletions

View File

@ -106,11 +106,11 @@ module.exports = class Desk {
return this.pages.forms[doctype]; return this.pages.forms[doctype];
} }
showFormModal(doctype, name) { async showFormModal(doctype, name) {
if (!this.pages.formModals[doctype]) { if (!this.pages.formModals[doctype]) {
this.pages.formModals[doctype] = new FormModal(doctype); this.pages.formModals[doctype] = new FormModal(doctype);
} }
this.pages.formModals[doctype].showWith(doctype, name); await this.pages.formModals[doctype].showWith(doctype, name);
return this.pages.formModals[doctype]; return this.pages.formModals[doctype];
} }

View File

@ -18,20 +18,37 @@ class BaseControl {
if (this.setup) { if (this.setup) {
this.setup(); this.setup();
} }
this.make(); if (this.template) {
this.wrapper = frappe.ui.add('div', 'field-template', this.parent);
this.renderTemplate();
} else {
this.make();
}
} }
bind(doc) { bind(doc) {
this.doc = doc; this.doc = doc;
this.setDocValue(); this.refresh();
} }
refresh() { refresh() {
this.setDocValue(); if (this.template) {
this.renderTemplate();
} else {
this.setDocValue();
}
}
renderTemplate() {
if (this.form.doc) {
this.wrapper.innerHTML = this.template(this.form.doc, this.doc);
} else {
this.wrapper.innerHTML = '';
}
} }
setDocValue() { setDocValue() {
if (this.doc) { if (this.doc && !this.template) {
this.setInputValue(this.doc.get(this.fieldname)); this.setInputValue(this.doc.get(this.fieldname));
} }
} }

View File

@ -34,10 +34,14 @@ class LinkControl extends BaseControl {
if (e.text && e.text.value === '__newItem') { if (e.text && e.text.value === '__newItem') {
e.preventDefault(); e.preventDefault();
const newDoc = await frappe.getNewDoc(this.target); const newDoc = await frappe.getNewDoc(this.target);
const formModal = frappe.desk.showFormModal(this.target, newDoc.name); const formModal = await frappe.desk.showFormModal(this.target, newDoc.name);
if (formModal.form.doc.meta.hasField('name')) {
formModal.form.doc.set('name', this.input.value);
}
formModal.once('submit', async () => { formModal.once('submit', async () => {
await this.updateDocValue(formModal.form.doc.name); await this.updateDocValue(formModal.form.doc.name);
}) });
} }
}); });

View File

@ -27,7 +27,7 @@ class TableControl extends BaseControl {
this.datatable = new DataTable(this.wrapper.querySelector('.datatable-wrapper'), { this.datatable = new DataTable(this.wrapper.querySelector('.datatable-wrapper'), {
columns: this.getColumns(), columns: this.getColumns(),
data: this.getTableData(), data: this.getTableData(),
takeAvailableSpace: true, layout: 'fluid',
addCheckboxColumn: true, addCheckboxColumn: true,
getEditor: this.getTableInput.bind(this), getEditor: this.getTableInput.bind(this),
}); });
@ -127,7 +127,6 @@ class TableControl extends BaseControl {
id: field.fieldname, id: field.fieldname,
field: field, field: field,
content: field.label, content: field.label,
width: 120,
editable: (this.disabled || field.disabled) ? false : true, editable: (this.disabled || field.disabled) ? false : true,
sortable: false, sortable: false,
resizable: true, resizable: true,

View File

@ -132,7 +132,7 @@ module.exports = class BaseForm extends Observable {
// only single value changed // only single value changed
let control = this.controls[params.fieldname]; let control = this.controls[params.fieldname];
if (control && control.getInputValue() !== control.format(params.fieldname)) { if (control && control.getInputValue() !== control.format(params.fieldname)) {
control.setDocValue(); control.refresh();
} }
} else { } else {
// multiple values changed // multiple values changed

View File

@ -13,6 +13,10 @@ module.exports = class BaseMeta extends BaseDocument {
} }
} }
hasField(fieldname) {
return this.getField(fieldname) ? true : false;
}
getField(fieldname) { getField(fieldname) {
if (!this._field_map) { if (!this._field_map) {
this._field_map = {}; this._field_map = {};