2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 19:29:02 +00:00
books/client/view/form.js

121 lines
3.5 KiB
JavaScript
Raw Normal View History

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-31 10:13:33 +00:00
const Observable = require('frappejs/utils/observable');
2018-01-12 12:25:07 +00:00
2018-01-31 10:13:33 +00:00
module.exports = class BaseForm extends Observable {
2018-01-12 12:25:07 +00:00
constructor({doctype, parent, submit_label='Submit'}) {
2018-01-31 10:13:33 +00:00
super();
2018-01-12 12:25:07 +00:00
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');
2018-01-31 10:13:33 +00:00
this.trigger('delete');
2018-01-12 12:25:07 +00:00
});
}
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);
}
// refresh value in control
this.doc.add_handler('change', (params) => {
let control = this.controls[params.fieldname];
if (control && control.get_input_value() !== control.format(params.fieldname)) {
control.set_doc_value();
}
});
2018-01-31 10:13:33 +00:00
this.trigger('use', {doc:doc});
2018-01-12 12:25:07 +00:00
}
async submit() {
try {
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');
}
2018-01-31 10:13:33 +00:00
await this.trigger('submit');
2018-01-12 12:25:07 +00:00
}
refresh() {
for(let control of this.controls_list) {
control.refresh();
}
}
2018-01-31 10:13:33 +00:00
show_alert(message, type, clear_after = 5) {
this.clear_alert();
this.alert = frappe.ui.add('div', `alert alert-${type}`, this.body);
this.alert.textContent = message;
setTimeout(() => {
this.clear_alert();
}, clear_after * 1000);
}
clear_alert() {
if (this.alert) {
frappe.ui.remove(this.alert);
this.alert = null;
}
}
}