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 {
|
|
|
|
constructor(doctype) {
|
2018-02-08 06:46:38 +00:00
|
|
|
let meta = frappe.getMeta(doctype)
|
2018-02-14 16:44:50 +00:00
|
|
|
super({title: `Edit ${meta.name}`});
|
2018-01-30 12:03:04 +00:00
|
|
|
this.meta = meta;
|
|
|
|
|
2018-02-13 10:42:44 +00:00
|
|
|
this.form = new (view.getFormClass(doctype))({
|
2018-01-30 12:03:04 +00:00
|
|
|
doctype: doctype,
|
2018-02-08 09:38:47 +00:00
|
|
|
parent: this.body,
|
2018-02-13 10:42:44 +00:00
|
|
|
container: this,
|
2018-02-14 12:50:56 +00:00
|
|
|
actions: ['submit', 'delete', 'duplicate', 'settings']
|
2018-01-30 12:03:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on('show', async (params) => {
|
2018-02-13 10:42:44 +00:00
|
|
|
await this.showDoc(params.doctype, params.name);
|
2018-01-30 12:03:04 +00:00
|
|
|
});
|
2018-01-31 10:13:33 +00:00
|
|
|
|
|
|
|
// if name is different after saving, change the route
|
|
|
|
this.form.on('submit', async (params) => {
|
|
|
|
let route = frappe.router.get_route();
|
2018-02-06 17:14:07 +00:00
|
|
|
if (this.form.doc.name && !(route && route[2] === this.form.doc.name)) {
|
2018-02-08 09:38:47 +00:00
|
|
|
await frappe.router.setRoute('edit', this.form.doc.doctype, this.form.doc.name);
|
|
|
|
this.form.showAlert('Added', 'success');
|
2018-01-31 10:13:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.form.on('delete', async (params) => {
|
2018-02-08 09:38:47 +00:00
|
|
|
await frappe.router.setRoute('list', this.form.doctype);
|
2018-01-31 10:13:33 +00:00
|
|
|
});
|
2018-01-30 12:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 10:42:44 +00:00
|
|
|
async showDoc(doctype, name) {
|
2018-01-30 12:03:04 +00:00
|
|
|
try {
|
2018-02-13 10:42:44 +00:00
|
|
|
await this.form.setDoc(doctype, name);
|
2018-02-15 09:53:28 +00:00
|
|
|
this.setActiveListRow(doctype, name);
|
2018-01-30 12:03:04 +00:00
|
|
|
} catch (e) {
|
2018-02-08 09:38:47 +00:00
|
|
|
this.renderError(e.status_code, e.message);
|
2018-01-30 12:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 09:53:28 +00:00
|
|
|
|
|
|
|
setActiveListRow(doctype, name) {
|
|
|
|
let activeListRow = document.querySelector('.list-page .list-body .list-row.active');
|
|
|
|
if (activeListRow) {
|
|
|
|
activeListRow.classList.remove('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
let myListRow = document.querySelector(`.list-body[data-doctype="${doctype}"] .list-row[data-name="${name}"]`);
|
|
|
|
if (myListRow) {
|
|
|
|
myListRow.classList.add('active');
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 12:03:04 +00:00
|
|
|
}
|