2
0
mirror of https://github.com/frappe/books.git synced 2024-11-14 01:14:03 +00:00
books/client/view/controls/table.js

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-02-06 17:14:07 +00:00
const frappe = require('frappejs');
const BaseControl = require('./base');
2018-03-05 16:45:21 +00:00
const ModelTable = require('frappejs/client/ui/modelTable');
2018-02-06 17:14:07 +00:00
class TableControl extends BaseControl {
make() {
2018-02-15 09:53:28 +00:00
this.makeWrapper();
2018-03-05 16:45:21 +00:00
this.modelTable = new ModelTable({
doctype: this.childtype,
parent: this.wrapper.querySelector('.datatable-wrapper'),
parentControl: this,
2018-03-08 13:31:22 +00:00
layout: this.layout || 'ratio',
2018-03-26 08:53:46 +00:00
getTableData: () => this.getTableData(),
getRowData: (rowIndex) => this.doc[this.fieldname][rowIndex],
2018-03-07 10:37:58 +00:00
isDisabled: () => this.isDisabled(),
2018-03-05 16:45:21 +00:00
});
2018-02-15 09:53:28 +00:00
this.setupToolbar();
}
2018-02-07 13:23:52 +00:00
2018-02-15 09:53:28 +00:00
makeWrapper() {
this.wrapper = frappe.ui.add('div', 'table-wrapper', this.getInputParent());
this.wrapper.innerHTML =
2018-03-08 13:31:22 +00:00
`<div class="datatable-wrapper" style="width: 100%"></div>
2018-02-15 09:53:28 +00:00
<div class="table-toolbar">
<button type="button" class="btn btn-sm btn-outline-secondary btn-add">
${frappe._("Add")}</button>
<button type="button" class="btn btn-sm btn-outline-secondary btn-remove">
${frappe._("Remove")}</button>
</div>`;
2018-02-13 11:54:57 +00:00
}
2018-02-07 13:23:52 +00:00
2018-02-13 11:54:57 +00:00
setupToolbar() {
this.wrapper.querySelector('.btn-add').addEventListener('click', async (event) => {
this.doc[this.fieldname].push({});
await this.doc.commit();
this.refresh();
});
this.wrapper.querySelector('.btn-remove').addEventListener('click', async (event) => {
2018-03-05 16:45:21 +00:00
let checked = this.modelTable.getChecked();
2018-03-08 13:31:22 +00:00
this.doc[this.fieldname] = this.doc[this.fieldname].filter(d => !checked.includes(d.idx + ''));
2018-02-13 11:54:57 +00:00
await this.doc.commit();
this.refresh();
2018-03-05 16:45:21 +00:00
this.modelTable.checkAll(false);
2018-02-13 11:54:57 +00:00
});
2018-02-06 17:14:07 +00:00
}
2018-02-08 09:38:47 +00:00
getInputValue() {
2018-02-06 17:14:07 +00:00
return this.doc[this.fieldname];
}
2018-02-08 09:38:47 +00:00
setInputValue(value) {
2018-03-05 16:45:21 +00:00
this.modelTable.refresh(this.getTableData(value));
}
setDisabled() {
this.refreshToolbar();
}
getToolbar() {
return this.wrapper.querySelector('.table-toolbar');
}
refreshToolbar() {
const toolbar = this.wrapper.querySelector('.table-toolbar');
if (toolbar) {
toolbar.classList.toggle('hide', this.isDisabled() ? true : false);
}
2018-02-06 17:14:07 +00:00
}
2018-02-08 11:45:32 +00:00
getTableData(value) {
2018-03-08 13:31:22 +00:00
return (value && value.length) ? value : this.getDefaultData();
2018-02-06 17:14:07 +00:00
}
2018-02-08 11:45:32 +00:00
getDefaultData() {
2018-02-06 17:14:07 +00:00
// build flat table
if (!this.doc) {
return [];
}
2018-03-08 13:31:22 +00:00
2018-02-06 17:14:07 +00:00
if (!this.doc[this.fieldname]) {
2018-02-12 09:42:26 +00:00
this.doc[this.fieldname] = [{idx: 0}];
2018-02-06 17:14:07 +00:00
}
2018-03-08 13:31:22 +00:00
if (this.doc[this.fieldname].length === 0 && this.neverEmpty) {
this.doc[this.fieldname] = [{idx: 0}];
}
2018-02-06 17:14:07 +00:00
return this.doc[this.fieldname];
}
2018-02-12 09:42:26 +00:00
checkValidity() {
2018-03-05 16:45:21 +00:00
if (!this.modelTable) {
2018-02-19 16:41:10 +00:00
return true;
}
2018-03-05 16:45:21 +00:00
return this.modelTable.checkValidity();
2018-02-12 09:42:26 +00:00
}
2018-02-06 17:14:07 +00:00
};
module.exports = TableControl;