2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 20:00:29 +00:00
books/client/desk/reportpage.js

105 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-03-26 12:18:07 +00:00
const Page = require('frappejs/client/view/page');
2018-04-24 07:56:28 +00:00
const FormLayout = require('frappejs/client/view/formLayout');
2018-03-26 12:18:07 +00:00
const DataTable = require('frappe-datatable');
const frappe = require('frappejs');
const utils = require('frappejs/client/ui/utils');
2018-03-27 13:55:26 +00:00
const Observable = require('frappejs/utils/observable');
2018-03-26 12:18:07 +00:00
// baseclass for report
// `url` url for report
// `getColumns` return columns
module.exports = class ReportPage extends Page {
2018-04-19 10:14:24 +00:00
constructor({title, filterFields = []}) {
2018-03-26 12:18:07 +00:00
super({title: title, hasRoute: true});
this.fullPage = true;
2018-03-27 13:55:26 +00:00
this.filterFields = filterFields;
2018-03-26 12:18:07 +00:00
2018-03-27 13:55:26 +00:00
this.filterWrapper = frappe.ui.add('div', 'filter-toolbar', this.body);
2018-03-26 12:18:07 +00:00
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 13:55:26 +00:00
this.makeFilters();
2018-04-26 10:20:20 +00:00
this.setDefaultFilterValues();
2018-03-26 12:18:07 +00:00
}
getColumns() {
// overrride
}
2018-04-19 10:14:24 +00:00
getRowsForDataTable(data) {
return data;
}
2018-03-27 13:55:26 +00:00
makeFilters() {
2018-04-24 07:56:28 +00:00
this.filters = new FormLayout({
2018-03-27 13:55:26 +00:00
parent: this.filterWrapper,
2018-04-24 07:56:28 +00:00
fields: this.filterFields,
2018-03-27 13:55:26 +00:00
doc: new Observable(),
2018-04-24 07:56:28 +00:00
inline: true
2018-03-27 13:55:26 +00:00
});
2018-04-24 07:56:28 +00:00
this.filterWrapper.appendChild(this.filters.form);
2018-03-26 12:18:07 +00:00
}
2018-04-26 10:20:20 +00:00
setDefaultFilterValues() {
}
2018-03-26 12:18:07 +00:00
getFilterValues() {
const values = {};
2018-04-24 07:56:28 +00:00
for (let control of this.filters.controlList) {
2018-03-27 13:55:26 +00:00
values[control.fieldname] = control.getInputValue();
if (control.required && !values[control.fieldname]) {
2018-03-26 12:18:07 +00:00
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 13:55:26 +00:00
if (frappe.params && frappe.params.filters) {
for (let key in frappe.params.filters) {
2018-04-24 07:56:28 +00:00
if (this.filters.controls[key]) {
this.filters.controls[key].setInputValue(frappe.params.filters[key]);
2018-03-27 13:55:26 +00:00
}
}
}
frappe.params = null;
2018-03-26 12:18:07 +00:00
if (!this.datatable) {
this.makeDataTable();
}
const filterValues = this.getFilterValues();
if (filterValues === false) return;
2018-04-16 13:04:16 +00:00
let data = await frappe.call({
method: this.method,
args: filterValues
});
2018-04-19 10:14:24 +00:00
const rows = this.getRowsForDataTable(data);
const columns = utils.convertFieldsToDatatableColumns(this.getColumns(data), this.layout);
this.datatable.refresh(rows, columns);
2018-03-26 12:18:07 +00:00
}
makeDataTable() {
2018-04-19 10:14:24 +00:00
this.datatable = new DataTable(this.tableWrapper, Object.assign({
2018-03-26 12:18:07 +00:00
columns: utils.convertFieldsToDatatableColumns(this.getColumns(), this.layout),
data: [],
layout: this.layout || 'fluid',
2018-04-19 10:14:24 +00:00
}, this.datatableOptions || {}));
2018-03-26 12:18:07 +00:00
}
}