2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-12 12:25:07 +00:00
|
|
|
const controls = require('./controls');
|
|
|
|
|
2018-01-24 11:52:05 +00:00
|
|
|
module.exports = class BaseForm {
|
2018-01-12 12:25:07 +00:00
|
|
|
constructor({doctype, parent, submit_label='Submit'}) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.doctype = doctype;
|
|
|
|
this.submit_label = submit_label;
|
|
|
|
|
|
|
|
this.controls = {};
|
|
|
|
this.controls_list = [];
|
|
|
|
|
|
|
|
this.meta = frappe.get_meta(this.doctype);
|
2018-01-24 11:52:05 +00:00
|
|
|
if (this.setup) {
|
|
|
|
this.setup();
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
this.make();
|
|
|
|
}
|
|
|
|
|
|
|
|
make() {
|
|
|
|
if (this.body || !this.parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.body = frappe.ui.add('div', 'form-body', this.parent);
|
2018-01-23 08:00:29 +00:00
|
|
|
this.make_toolbar();
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
this.form = frappe.ui.add('form', null, this.body);
|
|
|
|
for(let df of this.meta.fields) {
|
|
|
|
if (controls.get_control_class(df.fieldtype)) {
|
2018-01-23 12:48:27 +00:00
|
|
|
let control = controls.make_control(df, this);
|
2018-01-12 12:25:07 +00:00
|
|
|
this.controls_list.push(control);
|
|
|
|
this.controls[df.fieldname] = control;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 08:00:29 +00:00
|
|
|
make_toolbar() {
|
|
|
|
this.toolbar = frappe.ui.add('div', 'form-toolbar text-right', this.body);
|
|
|
|
this.toolbar.innerHTML = `
|
|
|
|
<button class="btn btn-outline-secondary btn-delete">Delete</button>
|
|
|
|
<button class="btn btn-primary btn-submit">Save</button>
|
|
|
|
`
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-01-23 08:00:29 +00:00
|
|
|
this.btn_submit = this.toolbar.querySelector('.btn-submit');;
|
|
|
|
this.btn_submit.addEventListener('click', async (event) => {
|
|
|
|
this.submit();
|
|
|
|
event.preventDefault();
|
|
|
|
})
|
|
|
|
|
|
|
|
this.btn_delete = this.toolbar.querySelector('.btn-delete');
|
|
|
|
this.btn_delete.addEventListener('click', async () => {
|
2018-01-12 12:25:07 +00:00
|
|
|
await this.doc.delete();
|
|
|
|
this.show_alert('Deleted', 'success');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
show_alert(message, type) {
|
|
|
|
this.clear_alert();
|
|
|
|
this.alert = frappe.ui.add('div', `alert alert-${type}`, this.body);
|
|
|
|
this.alert.textContent = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_alert() {
|
|
|
|
if (this.alert) {
|
|
|
|
frappe.ui.remove(this.alert);
|
|
|
|
this.alert = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async use(doc, is_new = false) {
|
|
|
|
if (this.doc) {
|
|
|
|
// clear handlers of outgoing doc
|
|
|
|
this.doc.clear_handlers();
|
|
|
|
}
|
|
|
|
this.clear_alert();
|
|
|
|
this.doc = doc;
|
|
|
|
this.is_new = is_new;
|
|
|
|
for (let control of this.controls_list) {
|
|
|
|
control.bind(this.doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
|
|
|
try {
|
2018-01-15 11:55:31 +00:00
|
|
|
if (this.is_new || this.doc.__not_inserted) {
|
2018-01-12 12:25:07 +00:00
|
|
|
await this.doc.insert();
|
|
|
|
} else {
|
|
|
|
await this.doc.update();
|
|
|
|
}
|
|
|
|
await this.refresh();
|
|
|
|
this.show_alert('Saved', 'success');
|
|
|
|
} catch (e) {
|
|
|
|
this.show_alert('Failed', 'danger');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
for(let control of this.controls_list) {
|
|
|
|
control.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 11:55:31 +00:00
|
|
|
}
|