2
0
mirror of https://github.com/frappe/books.git synced 2025-01-24 07:38:25 +00:00

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-02-06 22:44:07 +05:30
const frappe = require('frappejs');
const BaseControl = require('./base');
2018-03-05 22:15:21 +05:30
const ModelTable = require('frappejs/client/ui/modelTable');
2018-02-06 22:44:07 +05:30
class TableControl extends BaseControl {
make() {
2018-02-15 15:23:28 +05:30
this.makeWrapper();
2018-03-05 22:15:21 +05:30
this.modelTable = new ModelTable({
doctype: this.childtype,
parent: this.wrapper.querySelector('.datatable-wrapper'),
parentControl: this,
2018-03-08 19:01:22 +05:30
layout: this.layout || 'ratio',
2018-03-26 14:23:46 +05:30
getTableData: () => this.getTableData(),
getRowData: (rowIndex) => this.doc[this.fieldname][rowIndex],
2018-03-07 16:07:58 +05:30
isDisabled: () => this.isDisabled(),
2018-03-05 22:15:21 +05:30
});
2018-02-15 15:23:28 +05:30
this.setupToolbar();
}
2018-02-07 18:53:52 +05:30
2018-02-15 15:23:28 +05:30
makeWrapper() {
this.wrapper = frappe.ui.add('div', 'table-wrapper', this.getInputParent());
this.wrapper.innerHTML =
2018-03-08 19:01:22 +05:30
`<div class="datatable-wrapper" style="width: 100%"></div>
2018-02-15 15:23:28 +05:30
<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 17:24:57 +05:30
}
2018-02-07 18:53:52 +05:30
2018-02-13 17:24:57 +05:30
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 22:15:21 +05:30
let checked = this.modelTable.getChecked();
2018-03-08 19:01:22 +05:30
this.doc[this.fieldname] = this.doc[this.fieldname].filter(d => !checked.includes(d.idx + ''));
2018-02-13 17:24:57 +05:30
await this.doc.commit();
this.refresh();
2018-03-05 22:15:21 +05:30
this.modelTable.checkAll(false);
2018-02-13 17:24:57 +05:30
});
2018-02-06 22:44:07 +05:30
}
2018-02-08 15:08:47 +05:30
getInputValue() {
2018-02-06 22:44:07 +05:30
return this.doc[this.fieldname];
}
2018-02-08 15:08:47 +05:30
setInputValue(value) {
2018-03-05 22:15:21 +05:30
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 22:44:07 +05:30
}
2018-02-08 17:15:32 +05:30
getTableData(value) {
2018-03-08 19:01:22 +05:30
return (value && value.length) ? value : this.getDefaultData();
2018-02-06 22:44:07 +05:30
}
2018-02-08 17:15:32 +05:30
getDefaultData() {
2018-02-06 22:44:07 +05:30
// build flat table
if (!this.doc) {
return [];
}
2018-03-08 19:01:22 +05:30
2018-02-06 22:44:07 +05:30
if (!this.doc[this.fieldname]) {
2018-02-12 15:12:26 +05:30
this.doc[this.fieldname] = [{idx: 0}];
2018-02-06 22:44:07 +05:30
}
2018-03-08 19:01:22 +05:30
if (this.doc[this.fieldname].length === 0 && this.neverEmpty) {
this.doc[this.fieldname] = [{idx: 0}];
}
2018-02-06 22:44:07 +05:30
return this.doc[this.fieldname];
}
2018-02-12 15:12:26 +05:30
checkValidity() {
2018-03-05 22:15:21 +05:30
if (!this.modelTable) {
2018-02-19 22:11:10 +05:30
return true;
}
2018-03-05 22:15:21 +05:30
return this.modelTable.checkValidity();
2018-02-12 15:12:26 +05:30
}
2018-02-06 22:44:07 +05:30
};
module.exports = TableControl;