2019-10-13 21:56:20 +00:00
|
|
|
<template>
|
2019-11-08 10:49:06 +00:00
|
|
|
<div class="flex flex-col max-w-full">
|
2019-10-13 21:56:20 +00:00
|
|
|
<PageHeader>
|
2019-11-19 19:14:15 +00:00
|
|
|
<h1 slot="title" class="text-2xl font-bold">{{ report.title }}</h1>
|
2019-10-13 21:56:20 +00:00
|
|
|
<template slot="actions">
|
|
|
|
<SearchBar class="ml-2" />
|
|
|
|
</template>
|
|
|
|
</PageHeader>
|
2019-11-19 19:14:15 +00:00
|
|
|
<div class="mt-6 flex text-base px-8" v-if="report.filterFields">
|
|
|
|
<div class="ml-3 first:ml-0 w-32" v-for="df in report.filterFields">
|
|
|
|
<FormControl
|
|
|
|
size="small"
|
2019-11-21 19:07:27 +00:00
|
|
|
:background="true"
|
2019-11-19 19:14:15 +00:00
|
|
|
:df="df"
|
|
|
|
:value="filters[df.fieldname]"
|
|
|
|
@change="value => onFilterChange(df, value)"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-11-08 10:49:06 +00:00
|
|
|
<div class="px-8 mt-4">
|
2019-12-03 10:23:54 +00:00
|
|
|
<div>
|
|
|
|
<div ref="header" class="overflow-hidden">
|
2019-12-03 11:45:07 +00:00
|
|
|
<Row gap="2rem" :grid-template-columns="gridTemplateColumns">
|
2019-11-08 10:49:06 +00:00
|
|
|
<div
|
2019-12-03 10:23:54 +00:00
|
|
|
class="text-gray-600 text-base truncate py-4"
|
|
|
|
:class="{
|
|
|
|
'text-right': ['Int', 'Float', 'Currency'].includes(
|
|
|
|
column.fieldtype
|
|
|
|
)
|
|
|
|
}"
|
2019-11-08 10:49:06 +00:00
|
|
|
v-for="column in columns"
|
|
|
|
:key="column.label"
|
2019-11-19 19:14:15 +00:00
|
|
|
>
|
2019-12-03 10:23:54 +00:00
|
|
|
{{ column.label }}
|
2019-11-19 19:14:15 +00:00
|
|
|
</div>
|
2019-11-08 10:49:06 +00:00
|
|
|
</Row>
|
|
|
|
</div>
|
2019-12-03 10:23:54 +00:00
|
|
|
<WithScroll @scroll="onBodyScroll">
|
|
|
|
<div class="flex-1 overflow-auto" style="height: calc(100vh - 12rem)">
|
|
|
|
<Row
|
2019-12-03 11:45:07 +00:00
|
|
|
v-show="row.isShown"
|
2019-12-03 10:23:54 +00:00
|
|
|
v-for="(row, i) in rows"
|
|
|
|
:key="i"
|
2019-12-03 11:45:07 +00:00
|
|
|
gap="2rem"
|
|
|
|
:grid-template-columns="gridTemplateColumns"
|
2019-12-03 10:23:54 +00:00
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="text-gray-900 text-base truncate py-4"
|
2019-12-03 11:45:07 +00:00
|
|
|
:class="getCellClasses(row, column)"
|
2019-12-03 10:23:54 +00:00
|
|
|
v-for="column in columns"
|
|
|
|
:key="column.label"
|
2019-12-03 11:45:07 +00:00
|
|
|
@click="toggleChildren(row, i)"
|
2019-12-03 10:23:54 +00:00
|
|
|
>
|
2019-12-03 11:45:07 +00:00
|
|
|
<div class="inline-flex">
|
|
|
|
<feather-icon
|
|
|
|
v-if="row.isBranch && !row.isLeaf && column === columns[0]"
|
|
|
|
class="w-4 h-4 mr-2 flex-shrink-0"
|
|
|
|
:name="row.expanded ? 'chevron-down' : 'chevron-right'"
|
|
|
|
/>
|
|
|
|
<span class="truncate">
|
|
|
|
<component
|
|
|
|
:is="cellComponent(row[column.fieldname], column)"
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
</div>
|
2019-12-03 10:23:54 +00:00
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
</div>
|
|
|
|
</WithScroll>
|
2019-10-13 21:56:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import frappe from 'frappejs';
|
|
|
|
import PageHeader from '@/components/PageHeader';
|
|
|
|
import Button from '@/components/Button';
|
|
|
|
import SearchBar from '@/components/SearchBar';
|
|
|
|
import Row from '@/components/Row';
|
2019-12-03 10:23:54 +00:00
|
|
|
import WithScroll from '@/components/WithScroll';
|
2019-11-19 19:14:15 +00:00
|
|
|
import FormControl from '@/components/Controls/FormControl';
|
2019-10-13 21:56:20 +00:00
|
|
|
import reportViewConfig from '@/../reports/view';
|
2019-11-19 19:14:15 +00:00
|
|
|
import throttle from 'lodash/throttle';
|
2019-10-13 21:56:20 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Report',
|
2019-11-27 06:46:15 +00:00
|
|
|
props: ['reportName', 'defaultFilters'],
|
2019-10-13 21:56:20 +00:00
|
|
|
components: {
|
|
|
|
PageHeader,
|
|
|
|
Button,
|
|
|
|
SearchBar,
|
2019-11-19 19:14:15 +00:00
|
|
|
Row,
|
2019-12-03 10:23:54 +00:00
|
|
|
FormControl,
|
|
|
|
WithScroll
|
2019-11-19 19:14:15 +00:00
|
|
|
},
|
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
doc: this.filters
|
2019-11-27 06:46:15 +00:00
|
|
|
};
|
2019-10-13 21:56:20 +00:00
|
|
|
},
|
|
|
|
data() {
|
2019-11-19 19:14:15 +00:00
|
|
|
let filters = {};
|
|
|
|
for (let df of reportViewConfig[this.reportName].filterFields) {
|
|
|
|
filters[df.fieldname] = null;
|
|
|
|
}
|
|
|
|
|
2019-10-13 21:56:20 +00:00
|
|
|
return {
|
2019-11-19 19:14:15 +00:00
|
|
|
filters,
|
2019-10-13 21:56:20 +00:00
|
|
|
rows: [],
|
|
|
|
columns: []
|
|
|
|
};
|
|
|
|
},
|
2019-11-19 19:14:15 +00:00
|
|
|
async mounted() {
|
2019-10-13 21:56:20 +00:00
|
|
|
this.columns = this.report.getColumns();
|
2019-11-19 19:14:15 +00:00
|
|
|
await this.setDefaultFilters();
|
|
|
|
await this.fetchReportData();
|
2019-10-13 21:56:20 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-12-03 10:23:54 +00:00
|
|
|
onBodyScroll({ scrollLeft }) {
|
|
|
|
this.$refs.header.scrollLeft = scrollLeft;
|
|
|
|
},
|
2019-10-13 21:56:20 +00:00
|
|
|
async fetchReportData() {
|
|
|
|
let data = await frappe.call({
|
|
|
|
method: this.report.method,
|
2019-11-19 19:14:15 +00:00
|
|
|
args: this.filters
|
2019-10-13 21:56:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let rows, columns;
|
|
|
|
if (data.rows) {
|
|
|
|
rows = data.rows;
|
|
|
|
} else {
|
|
|
|
rows = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.columns) {
|
|
|
|
this.columns = this.report.getColumns(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rows) {
|
|
|
|
rows = [];
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:45:07 +00:00
|
|
|
this.rows = this.addTreeMeta(rows);
|
|
|
|
},
|
|
|
|
|
|
|
|
addTreeMeta(rows) {
|
|
|
|
return rows.map(row => {
|
|
|
|
if ('indent' in row) {
|
|
|
|
row.isBranch = true;
|
|
|
|
row.expanded = true;
|
|
|
|
row.isLeaf = !row.isGroup;
|
|
|
|
}
|
|
|
|
row.isShown = true;
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleChildren(row, rowIndex) {
|
|
|
|
if (!row.isBranch) return;
|
|
|
|
|
|
|
|
let flag;
|
|
|
|
if (row.expanded) {
|
|
|
|
row.expanded = false;
|
|
|
|
flag = false;
|
|
|
|
} else {
|
|
|
|
row.expanded = true;
|
|
|
|
flag = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let _rows = this.rows.slice(rowIndex + 1);
|
|
|
|
for (let _row of _rows) {
|
|
|
|
if (row.isBranch && _row.indent > row.indent) {
|
|
|
|
_row.expanded = flag;
|
|
|
|
_row.isShown = flag;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-11-19 19:14:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onFilterChange(df, value) {
|
|
|
|
this.filters[df.fieldname] = value;
|
|
|
|
this.fetchReportData();
|
|
|
|
},
|
|
|
|
|
|
|
|
async setDefaultFilters() {
|
|
|
|
for (let df of this.report.filterFields) {
|
|
|
|
let defaultValue = null;
|
|
|
|
if (df.default) {
|
|
|
|
if (typeof df.default === 'function') {
|
|
|
|
defaultValue = await df.default();
|
|
|
|
} else {
|
|
|
|
defaultValue = df.default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.filters[df.fieldname] = defaultValue;
|
|
|
|
}
|
2019-11-27 06:46:15 +00:00
|
|
|
|
|
|
|
if (this.defaultFilters) {
|
|
|
|
Object.assign(this.filters, this.defaultFilters);
|
|
|
|
}
|
2019-12-03 10:23:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
cellComponent(cellValue, column) {
|
|
|
|
if (typeof cellValue === 'object') {
|
|
|
|
// cellValue has a component definition
|
|
|
|
return cellValue;
|
|
|
|
}
|
|
|
|
if (column.component) {
|
|
|
|
// column has a component definition
|
|
|
|
return column.component(cellValue, column);
|
|
|
|
}
|
|
|
|
// default cell component
|
2019-12-03 11:45:07 +00:00
|
|
|
let formattedValue =
|
|
|
|
cellValue != null ? frappe.format(cellValue, column) : '';
|
2019-12-03 10:23:54 +00:00
|
|
|
return {
|
|
|
|
render(h) {
|
|
|
|
return h('span', formattedValue);
|
|
|
|
}
|
|
|
|
};
|
2019-12-03 11:45:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getCellClasses(row, column) {
|
|
|
|
let padding = ['pl-0', 'pl-6', 'pl-12', 'pl-18', 'pl-20'];
|
|
|
|
let treeCellClasses;
|
|
|
|
if (row.isBranch && column === this.columns[0]) {
|
|
|
|
treeCellClasses = [
|
|
|
|
padding[row.indent],
|
|
|
|
'hover:bg-gray-100 cursor-pointer'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
'text-right': ['Int', 'Float', 'Currency'].includes(column.fieldtype)
|
|
|
|
},
|
|
|
|
treeCellClasses
|
|
|
|
];
|
2019-10-13 21:56:20 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
report() {
|
|
|
|
return reportViewConfig[this.reportName];
|
2019-12-03 10:23:54 +00:00
|
|
|
},
|
|
|
|
columnWidth() {
|
|
|
|
return 'minmax(7rem, 1fr)';
|
2019-12-03 11:45:07 +00:00
|
|
|
},
|
|
|
|
gridTemplateColumns() {
|
|
|
|
return this.columns
|
|
|
|
.map(col => {
|
|
|
|
let multiplier = col.width;
|
|
|
|
if (!multiplier) {
|
|
|
|
multiplier = 1;
|
|
|
|
}
|
|
|
|
let minWidth = `${7 * multiplier}rem`;
|
|
|
|
let maxWidth = `${1 * multiplier}fr`;
|
|
|
|
|
|
|
|
return `minmax(${minWidth}, ${maxWidth})`;
|
|
|
|
})
|
|
|
|
.join(' ');
|
2019-10-13 21:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|