2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 20:11:15 +00:00
books/client/view/controls/base.js

205 lines
4.8 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
class BaseControl {
2018-02-06 17:14:07 +00:00
constructor({field, parent, form}) {
2018-02-09 12:55:55 +00:00
BaseControl.count++;
2018-02-06 17:14:07 +00:00
Object.assign(this, field);
this.parent = parent;
2018-01-23 12:48:27 +00:00
this.form = form;
2018-02-09 12:55:55 +00:00
this.id = 'control-' + BaseControl.count;
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();
}
2018-02-19 06:31:07 +00:00
if (this.template) {
this.wrapper = frappe.ui.add('div', 'field-template', this.parent);
this.renderTemplate();
} else {
this.make();
}
2018-01-12 12:25:07 +00:00
}
bind(doc) {
this.doc = doc;
2018-02-19 06:31:07 +00:00
this.refresh();
2018-01-12 12:25:07 +00:00
}
refresh() {
2018-02-19 06:31:07 +00:00
if (this.template) {
this.renderTemplate();
} else {
this.setDocValue();
}
this.setDisabled();
2018-02-19 06:31:07 +00:00
}
renderTemplate() {
2018-03-27 04:20:42 +00:00
if (this.form && this.form.doc) {
2018-02-19 06:31:07 +00:00
this.wrapper.innerHTML = this.template(this.form.doc, this.doc);
} else {
this.wrapper.innerHTML = '';
}
2018-01-12 12:25:07 +00:00
}
2018-02-08 09:38:47 +00:00
setDocValue() {
2018-02-19 06:31:07 +00:00
if (this.doc && !this.template) {
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-15 09:53:28 +00:00
if (!this.onlyInput) {
2018-03-05 16:45:21 +00:00
this.makeInputContainer();
2018-02-15 09:53:28 +00:00
this.makeLabel();
}
this.makeInput();
this.addChangeHandler();
2018-01-12 12:25:07 +00:00
}
2018-03-05 16:45:21 +00:00
makeInputContainer(className = 'form-group') {
this.inputContainer = frappe.ui.add('div', className, this.parent);
2018-01-12 12:25:07 +00:00
}
2018-03-05 16:45:21 +00:00
makeLabel(labelClass = null) {
2018-03-08 13:31:22 +00:00
this.labelElement = frappe.ui.add('label', labelClass, this.inputContainer, this.label);
2018-02-09 12:55:55 +00:00
this.labelElement.setAttribute('for', this.id);
2018-03-26 12:18:07 +00:00
if (this.inline) {
this.labelElement.classList.add("sr-only");
}
2018-01-12 12:25:07 +00:00
}
2018-03-05 16:45:21 +00:00
makeInput(inputClass='form-control') {
this.input = frappe.ui.add('input', inputClass, this.getInputParent());
2018-02-09 12:55:55 +00:00
this.input.autocomplete = "off";
this.input.id = this.id;
2018-02-20 14:11:44 +00:00
this.setInputName();
this.setRequiredAttribute();
this.setDisabled();
if (!this.onlyInput) {
this.makeDescription();
}
2018-03-27 13:55:26 +00:00
if (this.placeholder || this.inline) {
this.input.setAttribute('placeholder', this.placeholder || this.label);
2018-03-26 12:18:07 +00:00
}
2018-02-20 14:11:44 +00:00
2018-02-09 12:55:55 +00:00
}
isDisabled() {
2018-03-08 13:31:22 +00:00
let disabled = this.disabled;
if (this.doc && this.doc.submitted) {
disabled = true;
}
if (this.formula && this.fieldtype !== 'Table') {
disabled = true;
}
return disabled;
}
2018-02-09 12:55:55 +00:00
setDisabled() {
this.input.disabled = this.isDisabled();
2018-01-12 12:25:07 +00:00
}
2018-02-14 12:50:56 +00:00
getInputParent() {
2018-03-05 16:45:21 +00:00
return this.inputContainer || 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-09 12:55:55 +00:00
setRequiredAttribute() {
if (this.required) {
this.input.required = true;
2018-02-14 12:50:56 +00:00
this.input.classList.add('font-weight-bold');
2018-02-09 12:55:55 +00:00
}
}
2018-02-08 11:45:32 +00:00
makeDescription() {
2018-01-12 12:25:07 +00:00
if (this.description) {
2018-03-08 13:31:22 +00:00
this.description_element = frappe.ui.add('small', 'form-text text-muted',
this.inputContainer, this.description);
2018-01-12 12:25:07 +00:00
}
}
2018-02-08 09:38:47 +00:00
setInputValue(value) {
this.input.value = this.format(value);
}
format(value) {
2018-01-12 12:25:07 +00:00
if (value === undefined || value === null) {
value = '';
}
return value;
2018-01-12 12:25:07 +00:00
}
2018-02-08 11:45:32 +00:00
async getParsedValue() {
2018-02-20 14:11:44 +00:00
return await this.parse(this.getInputValue());
2018-01-12 12:25:07 +00:00
}
2018-02-08 09:38:47 +00:00
getInputValue() {
return this.input.value;
}
2018-01-12 12:25:07 +00:00
async parse(value) {
return value;
}
async validate(value) {
return value;
}
2018-02-09 12:55:55 +00:00
addChangeHandler() {
this.input.addEventListener('change', () => {
if (this.skipChangeEvent) return;
this.handleChange();
});
2018-01-12 12:25:07 +00:00
}
2018-02-09 12:55:55 +00:00
async handleChange(event) {
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-02-13 11:54:57 +00:00
await this.updateDocValue(value);
}
async updateDocValue(value) {
2018-03-27 04:20:42 +00:00
if (!this.doc) return;
if (this.doc[this.fieldname] !== value) {
2018-02-13 11:54:57 +00:00
if (this.parentControl) {
// its a child
2018-02-08 11:45:32 +00:00
this.doc[this.fieldname] = value;
2018-03-08 13:31:22 +00:00
this.parentControl.doc._dirty = true;
await this.parentControl.doc.applyChange(this.fieldname);
2018-02-13 11:54:57 +00:00
} else {
// parent
await this.doc.set(this.fieldname, value);
2018-02-06 17:14:07 +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
2018-03-08 13:31:22 +00:00
setFocus() {
2018-02-06 17:14:07 +00:00
this.input.focus();
}
2018-01-12 12:25:07 +00:00
}
2018-02-09 12:55:55 +00:00
BaseControl.count = 0;
2018-01-12 12:25:07 +00:00
module.exports = BaseControl;