2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
|
|
|
class BaseControl {
|
2018-02-06 17:14:07 +00:00
|
|
|
constructor({field, parent, form}) {
|
|
|
|
Object.assign(this, field);
|
|
|
|
this.parent = parent;
|
2018-01-23 12:48:27 +00:00
|
|
|
this.form = form;
|
2018-02-06 17:14:07 +00:00
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
if (!this.fieldname) {
|
|
|
|
this.fieldname = frappe.slug(this.label);
|
|
|
|
}
|
2018-02-06 17:14:07 +00:00
|
|
|
if (!this.parent) {
|
|
|
|
this.parent = this.form.form;
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
if (this.setup) {
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bind(doc) {
|
|
|
|
this.doc = doc;
|
2018-02-08 09:38:47 +00:00
|
|
|
this.setDocValue();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
this.make();
|
2018-02-08 09:38:47 +00:00
|
|
|
this.setDocValue();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
setDocValue() {
|
2018-01-12 12:25:07 +00:00
|
|
|
if (this.doc) {
|
2018-02-08 09:38:47 +00:00
|
|
|
this.setInputValue(this.doc.get(this.fieldname));
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
make() {
|
2018-02-06 17:14:07 +00:00
|
|
|
if (!this.input) {
|
2018-02-08 11:45:32 +00:00
|
|
|
if (!this.onlyInput) {
|
|
|
|
this.makeFormGroup();
|
|
|
|
this.makeLabel();
|
2018-02-06 17:14:07 +00:00
|
|
|
}
|
2018-02-08 11:45:32 +00:00
|
|
|
this.makeInput();
|
|
|
|
this.setInputName();
|
|
|
|
if (!this.onlyInput) {
|
|
|
|
this.makeDescription();
|
2018-02-06 17:14:07 +00:00
|
|
|
}
|
2018-02-08 11:45:32 +00:00
|
|
|
this.bindChangeEvent();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
makeFormGroup() {
|
|
|
|
this.formGroup = frappe.ui.add('div', 'form-group', this.parent);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
makeLabel() {
|
|
|
|
this.label_element = frappe.ui.add('label', null, this.formGroup);
|
2018-01-12 12:25:07 +00:00
|
|
|
this.label_element.textContent = this.label;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
makeInput() {
|
2018-02-06 17:14:07 +00:00
|
|
|
this.input = frappe.ui.add('input', 'form-control', this.get_input_parent());
|
2018-01-25 10:04:48 +00:00
|
|
|
this.input.setAttribute('autocomplete', 'off');
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 17:14:07 +00:00
|
|
|
get_input_parent() {
|
2018-02-08 11:45:32 +00:00
|
|
|
return this.formGroup || this.parent;
|
2018-02-06 17:14:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
setInputName() {
|
2018-01-12 12:25:07 +00:00
|
|
|
this.input.setAttribute('name', this.fieldname);
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
makeDescription() {
|
2018-01-12 12:25:07 +00:00
|
|
|
if (this.description) {
|
2018-02-08 11:45:32 +00:00
|
|
|
this.description_element = frappe.ui.add('small', 'form-text text-muted', this.formGroup);
|
2018-01-12 12:25:07 +00:00
|
|
|
this.description_element.textContent = this.description;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
setInputValue(value) {
|
2018-01-25 10:04:48 +00:00
|
|
|
this.input.value = this.format(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
format(value) {
|
2018-01-12 12:25:07 +00:00
|
|
|
if (value === undefined || value === null) {
|
|
|
|
value = '';
|
|
|
|
}
|
2018-01-25 10:04:48 +00:00
|
|
|
return value;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
async getParsedValue() {
|
2018-01-12 12:25:07 +00:00
|
|
|
return await this.parse(this.input.value);
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
getInputValue() {
|
2018-01-25 10:04:48 +00:00
|
|
|
return this.input.value;
|
|
|
|
}
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
async parse(value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async validate(value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
bindChangeEvent() {
|
|
|
|
this.input.addEventListener('change', (e) => this.handleChange());
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:45:32 +00:00
|
|
|
async handleChange() {
|
2018-02-08 09:38:47 +00:00
|
|
|
let value = await this.parse(this.getInputValue());
|
2018-01-12 12:25:07 +00:00
|
|
|
value = await this.validate(value);
|
2018-01-25 10:04:48 +00:00
|
|
|
if (this.doc[this.fieldname] !== value) {
|
2018-02-06 17:14:07 +00:00
|
|
|
if (this.doc.set) {
|
|
|
|
await this.doc.set(this.fieldname, value);
|
|
|
|
}
|
|
|
|
if (this.parent_control) {
|
2018-02-08 11:45:32 +00:00
|
|
|
this.doc[this.fieldname] = value;
|
2018-02-08 09:38:47 +00:00
|
|
|
await this.parent_control.doc.set(this.fieldname, this.parent_control.getInputValue());
|
2018-02-06 17:14:07 +00:00
|
|
|
}
|
2018-01-25 10:04:48 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2018-01-24 11:52:05 +00:00
|
|
|
|
|
|
|
disable() {
|
|
|
|
this.input.setAttribute('disabled', 'disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
|
|
|
this.input.removeAttribute('disabled');
|
|
|
|
}
|
2018-02-06 17:14:07 +00:00
|
|
|
|
|
|
|
set_focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BaseControl;
|