2018-01-30 12:03:04 +00:00
|
|
|
const Page = require('frappejs/client/view/page');
|
|
|
|
const view = require('frappejs/client/view');
|
2018-02-08 09:38:47 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-30 12:03:04 +00:00
|
|
|
|
|
|
|
module.exports = class FormPage extends Page {
|
2018-02-19 16:41:10 +00:00
|
|
|
constructor(doctype) {
|
|
|
|
let meta = frappe.getMeta(doctype)
|
2018-02-22 07:36:28 +00:00
|
|
|
super({title: `Edit ${meta.name}`, hasRoute: true});
|
2018-02-19 16:41:10 +00:00
|
|
|
this.meta = meta;
|
2018-02-20 06:39:41 +00:00
|
|
|
this.doctype = doctype;
|
2018-02-19 16:41:10 +00:00
|
|
|
|
|
|
|
this.form = new (view.getFormClass(doctype))({
|
|
|
|
doctype: doctype,
|
|
|
|
parent: this.body,
|
|
|
|
container: this,
|
2018-02-21 09:38:56 +00:00
|
|
|
actions: ['submit', 'delete', 'duplicate', 'settings', 'print']
|
2018-02-19 16:41:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on('show', async (params) => {
|
|
|
|
await this.showDoc(params.doctype, params.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
// if name is different after saving, change the route
|
|
|
|
this.form.on('submit', async (params) => {
|
|
|
|
let route = frappe.router.get_route();
|
|
|
|
if (this.form.doc.name && !(route && route[2] === this.form.doc.name)) {
|
|
|
|
await frappe.router.setRoute('edit', this.form.doc.doctype, this.form.doc.name);
|
|
|
|
this.form.showAlert('Added', 'success');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.form.on('delete', async (params) => {
|
2018-02-20 06:39:41 +00:00
|
|
|
this.hide();
|
2018-02-19 16:41:10 +00:00
|
|
|
await frappe.router.setRoute('list', this.form.doctype);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async showDoc(doctype, name) {
|
|
|
|
try {
|
|
|
|
await this.form.setDoc(doctype, name);
|
2018-02-21 09:38:56 +00:00
|
|
|
frappe.desk.setActiveDoc(this.form.doc);
|
2018-02-19 16:41:10 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.renderError(e.status_code, e.message);
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 12:03:04 +00:00
|
|
|
}
|