2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/client/ui/modelTable.js

134 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-03-05 16:45:21 +00:00
const frappe = require('frappejs');
const DataTable = require('frappe-datatable');
const controls = require('frappejs/client/view/controls');
const Modal = require('frappejs/client/ui/modal');
2018-03-26 12:18:07 +00:00
const utils = require('./utils');
2018-03-05 16:45:21 +00:00
module.exports = class ModelTable {
2018-03-26 08:53:46 +00:00
constructor({doctype, parent, layout, parentControl, getRowData,
2018-03-07 10:37:58 +00:00
isDisabled, getTableData}) {
2018-03-05 16:45:21 +00:00
Object.assign(this, arguments[0]);
this.meta = frappe.getMeta(this.doctype);
this.make();
}
make() {
this.datatable = new DataTable(this.parent, {
columns: this.getColumns(),
data: [],
2018-03-08 13:31:22 +00:00
layout: this.meta.layout || this.layout || 'fluid',
2018-03-05 16:45:21 +00:00
addCheckboxColumn: true,
getEditor: this.getTableInput.bind(this),
});
}
2018-03-08 13:31:22 +00:00
resize() {
this.datatable.setDimensions();
}
2018-03-05 16:45:21 +00:00
getColumns() {
2018-03-26 12:18:07 +00:00
return utils.convertFieldsToDatatableColumns(this.getTableFields(), this.layout);
2018-03-05 16:45:21 +00:00
}
getTableFields() {
return this.meta.fields.filter(f => f.hidden ? false : true);
}
getTableInput(colIndex, rowIndex, value, parent) {
let field = this.datatable.getColumn(colIndex).field;
2018-03-08 13:31:22 +00:00
if (field.disabled || (this.isDisabled && this.isDisabled())) {
2018-03-05 16:45:21 +00:00
return false;
}
if (field.fieldtype==='Text') {
// text in modal
parent = this.getControlModal(field).getBody();
}
2018-03-08 13:31:22 +00:00
const editor = this.getControl(field, parent);
return editor;
2018-03-05 16:45:21 +00:00
}
getControl(field, parent) {
field.onlyInput = true;
const control = controls.makeControl({field: field, parent: parent});
// change will be triggered by datatable
control.skipChangeEvent = true;
return {
2018-03-26 08:53:46 +00:00
initValue: async (value, rowIndex, column) => {
2018-03-05 16:45:21 +00:00
column.activeControl = control;
control.parentControl = this.parentControl;
2018-03-26 08:53:46 +00:00
control.doc = await this.getRowData(rowIndex);
2018-03-08 13:31:22 +00:00
control.setFocus();
control.setInputValue(control.doc[column.id]);
return control;
2018-03-05 16:45:21 +00:00
},
setValue: async (value, rowIndex, column) => {
2018-03-26 08:53:46 +00:00
await this.setValue(control);
2018-03-05 16:45:21 +00:00
},
getValue: () => {
return control.getInputValue();
}
}
}
2018-03-26 08:53:46 +00:00
async setValue(control) {
await control.handleChange();
}
2018-03-05 16:45:21 +00:00
getControlModal(field) {
this.modal = new Modal({
title: frappe._('Edit {0}', field.label),
body: '',
primary: {
label: frappe._('Submit'),
action: (modal) => {
this.datatable.cellmanager.submitEditing();
modal.hide();
}
}
});
this.modal.on('hide', () => {
this.datatable.cellmanager.deactivateEditing();
this.datatable.cellmanager.$focusedCell.focus();
});
return this.modal;
}
checkValidity() {
if (!this.datatable) {
return true;
}
let data = this.getTableData();
for (let rowIndex=0; rowIndex < data.length; rowIndex++) {
let row = data[rowIndex];
for (let column of this.datatable.datamanager.columns) {
if (column.field && column.field.required) {
let value = row[column.field.fieldname];
if (value==='' || value===undefined || value===null) {
let $cell = this.datatable.cellmanager.getCell$(column.colIndex, rowIndex);
this.datatable.cellmanager.activateEditing($cell);
return false;
}
}
}
}
return true;
}
refresh(data) {
return this.datatable.refresh(data);
}
getChecked() {
return this.datatable.rowmanager.getCheckedRows();
}
checkAll(check) {
return this.datatable.rowmanager.checkAll(check);
}
}