2
0
mirror of https://github.com/frappe/books.git synced 2025-01-26 00:28:25 +00:00
books/client/desk/reportpage.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-03-26 17:48:07 +05:30
const Page = require('frappejs/client/view/page');
2018-04-24 13:26:28 +05:30
const FormLayout = require('frappejs/client/view/formLayout');
2018-03-26 17:48:07 +05:30
const DataTable = require('frappe-datatable');
const frappe = require('frappejs');
const utils = require('frappejs/client/ui/utils');
2018-03-27 19:25:26 +05:30
const Observable = require('frappejs/utils/observable');
2018-03-26 17:48:07 +05:30
// baseclass for report
// `url` url for report
// `getColumns` return columns
module.exports = class ReportPage extends Page {
2018-04-19 15:44:24 +05:30
constructor({title, filterFields = []}) {
2018-03-26 17:48:07 +05:30
super({title: title, hasRoute: true});
this.fullPage = true;
2018-03-27 19:25:26 +05:30
this.filterFields = filterFields;
2018-03-26 17:48:07 +05:30
2018-03-27 19:25:26 +05:30
this.filterWrapper = frappe.ui.add('div', 'filter-toolbar', this.body);
2018-03-26 17:48:07 +05:30
this.tableWrapper = frappe.ui.add('div', 'table-page-wrapper', this.body);
this.btnNew = this.addButton(frappe._('Refresh'), 'btn-primary', async () => {
await this.run();
});
2018-03-27 19:25:26 +05:30
this.makeFilters();
2018-04-26 15:50:20 +05:30
this.setDefaultFilterValues();
2018-03-26 17:48:07 +05:30
}
getColumns() {
// overrride
}
2018-04-19 15:44:24 +05:30
getRowsForDataTable(data) {
return data;
}
2018-03-27 19:25:26 +05:30
makeFilters() {
2018-04-24 13:26:28 +05:30
this.filters = new FormLayout({
2018-03-27 19:25:26 +05:30
parent: this.filterWrapper,
2018-04-24 13:26:28 +05:30
fields: this.filterFields,
2018-03-27 19:25:26 +05:30
doc: new Observable(),
2018-04-24 13:26:28 +05:30
inline: true
2018-03-27 19:25:26 +05:30
});
2018-04-24 13:26:28 +05:30
this.filterWrapper.appendChild(this.filters.form);
2018-03-26 17:48:07 +05:30
}
2018-04-26 15:50:20 +05:30
setDefaultFilterValues() {
}
2018-03-26 17:48:07 +05:30
getFilterValues() {
const values = {};
2018-04-24 13:26:28 +05:30
for (let control of this.filters.controlList) {
2018-03-27 19:25:26 +05:30
values[control.fieldname] = control.getInputValue();
if (control.required && !values[control.fieldname]) {
2018-03-26 17:48:07 +05:30
frappe.ui.showAlert({message: frappe._('{0} is mandatory', control.label), color: 'red'});
return false;
}
}
return values;
}
async show(params) {
super.show();
await this.run();
}
async run() {
2018-03-27 19:25:26 +05:30
if (frappe.params && frappe.params.filters) {
for (let key in frappe.params.filters) {
2018-04-24 13:26:28 +05:30
if (this.filters.controls[key]) {
this.filters.controls[key].setInputValue(frappe.params.filters[key]);
2018-03-27 19:25:26 +05:30
}
}
}
frappe.params = null;
2018-03-26 17:48:07 +05:30
if (!this.datatable) {
this.makeDataTable();
}
const filterValues = this.getFilterValues();
if (filterValues === false) return;
2018-04-16 18:34:16 +05:30
let data = await frappe.call({
method: this.method,
args: filterValues
});
2018-04-19 15:44:24 +05:30
const rows = this.getRowsForDataTable(data);
const columns = utils.convertFieldsToDatatableColumns(this.getColumns(data), this.layout);
this.datatable.refresh(rows, columns);
2018-03-26 17:48:07 +05:30
}
makeDataTable() {
2018-04-19 15:44:24 +05:30
this.datatable = new DataTable(this.tableWrapper, Object.assign({
2018-03-26 17:48:07 +05:30
columns: utils.convertFieldsToDatatableColumns(this.getColumns(), this.layout),
data: [],
layout: this.layout || 'fluid',
2018-04-19 15:44:24 +05:30
}, this.datatableOptions || {}));
2018-03-26 17:48:07 +05:30
}
}