2018-02-13 10:42:44 +00:00
|
|
|
const Modal = require('frappejs/client/ui/modal');
|
|
|
|
const view = require('frappejs/client/view');
|
|
|
|
|
|
|
|
module.exports = class FormModal extends Modal {
|
|
|
|
constructor(doctype, name) {
|
|
|
|
super({title: doctype});
|
|
|
|
this.doctype = doctype;
|
2018-02-15 16:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async showWith(doctype, name) {
|
2018-02-21 09:38:56 +00:00
|
|
|
if (!name) name = doctype;
|
2018-02-15 16:39:50 +00:00
|
|
|
this.show();
|
|
|
|
await this.setDoc(doctype, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setDoc(doctype, name) {
|
|
|
|
if (!this.form) {
|
|
|
|
this.makeForm();
|
|
|
|
}
|
|
|
|
await this.form.setDoc(doctype, name);
|
2018-02-21 09:38:56 +00:00
|
|
|
let input = this.modal.querySelector('input') || this.modal.querySelector('select');
|
|
|
|
input && input.focus();
|
2018-02-13 10:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
makeForm() {
|
2018-02-20 06:39:41 +00:00
|
|
|
this.form = new (view.getFormClass(this.doctype))({
|
|
|
|
doctype: this.doctype,
|
|
|
|
parent: this.getBody(),
|
2018-02-13 10:42:44 +00:00
|
|
|
container: this,
|
|
|
|
actions: ['submit']
|
|
|
|
});
|
|
|
|
|
2018-02-15 16:39:50 +00:00
|
|
|
this.form.on('submit', async () => {
|
|
|
|
await this.trigger('submit');
|
2018-02-13 10:42:44 +00:00
|
|
|
this.hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addButton(label, className, action) {
|
|
|
|
if (className === 'primary') {
|
|
|
|
this.addPrimary(label, action);
|
|
|
|
} else {
|
|
|
|
this.addSecondary(label, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|