Skip to content

Commit

Permalink
feat(core/reports): add new widget for paginated tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sara-gnucoop committed Sep 13, 2022
1 parent 33b4420 commit 14c6241
Show file tree
Hide file tree
Showing 18 changed files with 645 additions and 119 deletions.
52 changes: 40 additions & 12 deletions projects/core/models/src/utils/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export class AjfExpressionUtils {
REPEAT: {fn: REPEAT},
EVALUATE: {fn: EVALUATE},
buildDataset: {fn: buildDataset},
buildAlignedDataset: {fn: buildAlignedDataset},
buildFormDataset: {fn: buildFormDataset},
buildAlignedFormDataset: {fn: buildAlignedFormDataset},
buildWidgetDataset: {fn: buildWidgetDataset},
buildWidgetDatasetWithDialog: {fn: buildWidgetDatasetWithDialog},
FILTER_BY_VARS: {fn: FILTER_BY_VARS},
Expand Down Expand Up @@ -1049,15 +1051,24 @@ export function MODE(forms: (Form | MainForm)[], fieldName: string): number[] {
.map(v => +v);
}

export function buildDataset(
dataset: (string | number | string[] | number[])[],
colspans: number[],
): AjfTableCell[][] {
return buildAlignedDataset(dataset, colspans, []);
}

/**
* Build a dataset for ajf dynamic table
* @param dataset the dataset for the table
* @param colspans colspan for each value in the dataset
* @param textAlign alignment for each value in the dataset
* @returns An AjfTableCell list
*/
export function buildDataset(
export function buildAlignedDataset(
dataset: (string | number | string[] | number[])[],
colspans: number[],
textAlign: string[],
): AjfTableCell[][] {
const res: AjfTableCell[][] = [];
const normalizeDataset: any[][] = [];
Expand All @@ -1079,7 +1090,7 @@ export function buildDataset(
colspan: colspans[cellIndex],
rowspan: 1,
style: {
textAlign: 'center',
textAlign: textAlign[cellIndex] ? textAlign[cellIndex] : 'center',
color: 'black',
backgroundColor: index % 2 === 0 ? 'white' : '#ddd',
},
Expand All @@ -1104,16 +1115,33 @@ export function buildFormDataset(
dataset: MainForm[],
fields: string[],
rowLink: {link: string; position: number} | null,
backgroundColorA?: string,
backgroundColorB?: string,
_backgroundColorA?: string,
_backgroundColorB?: string,
): AjfTableCell[][] {
return buildAlignedFormDataset(dataset, fields, [], [], rowLink);
}

/**
* Build a dataset based on a list of Forms, for ajf dynamic table
* @param dataset the dataset for the table
* @param fields the list of fields name for each row
* @param colspans colspan for each value in the dataset
* @param textAlign alignment for each value in the dataset
* @param rowLink the http link for the row, with the form field name with the link value and the column position for the link.
* ie: {'link': 'home_link', 'position': 0}
* @returns An AjfTableCell list
*/
export function buildAlignedFormDataset(
dataset: MainForm[],
fields: string[],
colspans: number[],
textAlign: string[],
rowLink: {link: string; position: number} | null,
): AjfTableCell[][] {
const res: AjfTableCell[][] = [];
if (backgroundColorA == null) {
backgroundColorA = 'white';
}
if (backgroundColorB == null) {
backgroundColorB = '#ddd';
}

const backgroundColorA = 'white';
const backgroundColorB = '#ddd';
if (dataset) {
let index: number = 0;
dataset.forEach((data: MainForm) => {
Expand All @@ -1127,10 +1155,10 @@ export function buildFormDataset(
}
row.push({
value: cellValue,
colspan: 1,
colspan: colspans[cellIdx] && colspans[cellIdx] > 0 ? colspans[cellIdx] : 1,
rowspan: 1,
style: {
textAlign: 'center',
textAlign: textAlign[cellIdx] ? textAlign[cellIdx] : 'center',
color: 'black',
backgroundColor: index % 2 === 0 ? backgroundColorA : backgroundColorB,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfTableCell} from '@ajf/core/table';
import {AjfPaginatedTableWidget} from '../widgets/paginated-table-widget';
import {AjfDataWidgetInstance} from './data-widget-instance';

export interface AjfPaginatedTableWidgetInstance extends AjfDataWidgetInstance {
widget: AjfPaginatedTableWidget;
exportable: boolean;
data: AjfTableCell[][];
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {AjfPaginatedListWidgetInstance} from './paginated-list-widget-instance';
import {AjfTableWidgetInstance} from './table-widget-instance';
import {AjfTextWidgetInstance} from './text-widget-instance';
import {AjfWidgetWithContentInstance} from './widget-with-content-instance';
import {AjfPaginatedTableWidgetInstance} from './paginated-table-widget-instance';

export interface AjfUnknownWidgetInstance extends AjfBaseWidgetInstance {
widget: AjfUnknownWidget;
Expand All @@ -61,4 +62,5 @@ export type AjfWidgetInstance =
| AjfPaginatedListWidgetInstance
| AjfTableWidgetInstance
| AjfTextWidgetInstance
| AjfWidgetWithContentInstance;
| AjfWidgetWithContentInstance
| AjfPaginatedTableWidgetInstance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfWidgetType} from './widget-type';
import {AjfFormula} from '@ajf/core/models';
import {AjfDataWidget} from './data-widget';
import {AjfTableDataset} from '../dataset/table-dataset';

export interface AjfPaginatedTableWidget extends AjfDataWidget {
widgetType: AjfWidgetType.PaginatedTable;
pageSize: number;

/**
* it should return a AjfTableCell[][]
*/
rowDefinition: AjfFormula;
cellStyles: any;
dataset: AjfTableDataset[];
exportable?: boolean;
}
1 change: 1 addition & 0 deletions projects/core/reports/src/interface/widgets/widget-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export enum AjfWidgetType {
PaginatedList,
Dialog,
HeatMap,
PaginatedTable,
LENGTH,
}
4 changes: 3 additions & 1 deletion projects/core/reports/src/interface/widgets/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {AjfTextWidget} from './text-widget';
import {AjfWidgetWithContent} from './widget-with-content';
import {AjfWidgetType} from './widget-type';
import {AjfGraphWidget} from './graph-widget';
import {AjfPaginatedTableWidget} from './paginated-table-widget';

export interface AjfUnknownWidget extends AjfBaseWidget {
widgetType: AjfWidgetType.LENGTH;
Expand All @@ -60,4 +61,5 @@ export type AjfWidget =
| AjfPaginatedListWidget
| AjfTextWidget
| AjfUnknownWidget
| AjfWidgetWithContent;
| AjfWidgetWithContent
| AjfPaginatedTableWidget;
2 changes: 2 additions & 0 deletions projects/core/reports/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export * from './interface/widgets/layout-widget';
export * from './interface/widgets/map-widget';
export * from './interface/widgets/page-break-widget';
export * from './interface/widgets/paginated-list-widget';
export * from './interface/widgets/paginated-table-widget';
export * from './interface/widgets/table-widget';
export * from './interface/widgets/text-widget';
export * from './interface/widgets/widget';
Expand All @@ -74,6 +75,7 @@ export * from './interface/widgets-instances/layout-widget-instance';
export * from './interface/widgets-instances/map-widget-instance';
export * from './interface/widgets-instances/page-break-widget-instance';
export * from './interface/widgets-instances/paginated-list-widget-instance';
export * from './interface/widgets-instances/paginated-table-widget-instance';
export * from './interface/widgets-instances/table-widget-instance';
export * from './interface/widgets-instances/text-widget-instance';
export * from './interface/widgets-instances/widget-instance';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfPaginatedTableWidgetInstance} from '../../interface/widgets-instances/paginated-table-widget-instance';
import {AjfWidgetInstance} from '../../interface/widgets-instances/widget-instance';
import {isPaginatedTableWidget} from '../widgets/is-paginated-table-widget';

export const isPaginatedTableWidgetInstance = (
instance: AjfWidgetInstance,
): instance is AjfPaginatedTableWidgetInstance => {
return instance != null && isPaginatedTableWidget(instance.widget);
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ import {isHeatMapWidget} from '../widgets/is-heat-map-widget';
import {isImageContainerWidget} from '../widgets/is-image-container-widget';
import {isImageWidget} from '../widgets/is-image-widget';
import {isMapWidget} from '../widgets/is-map-widget';
import {isPaginatedListWidget} from '../widgets/is-paginated-list-widget';
import {isPaginatedTableWidget} from '../widgets/is-paginated-table-widget';
import {isWidgetWithContent} from '../widgets/is-widget-with-content';
import {isTableWidget} from '../widgets/is-table-widget';
import {isTextWidget} from '../widgets/is-text-widget';
import {isPaginatedListWidget} from '../widgets/is-paginated-list-widget';
import {componentsMap} from '../widgets/widgets-map';
import {isChartWidgetInstance} from '../widgets-instances/is-chart-widget-instance';
import {isDialogWidgetInstance} from '../widgets-instances/is-dialog-widget-instance';
Expand All @@ -54,10 +55,11 @@ import {isHeatMapWidgetInstance} from '../widgets-instances/is-heat-map-widget-i
import {isImageContainerWidgetInstance} from '../widgets-instances/is-image-container-widget-instance';
import {isImageWidgetInstance} from '../widgets-instances/is-image-widget-instance';
import {isMapWidgetInstance} from '../widgets-instances/is-map-widget-instance';
import {isPaginatedListWidgetInstance} from '../widgets-instances/is-paginated-list-widget-instance';
import {isPaginatedTableWidgetInstance} from './is-paginated-table-widget-instance';
import {isTableWidgetInstance} from '../widgets-instances/is-table-widget-instance';
import {isTextWidgetInstance} from '../widgets-instances/is-text-widget-instance';
import {isWidgetWithContentInstance} from '../widgets-instances/is-widget-with-content-instance';
import {isPaginatedListWidgetInstance} from '../widgets-instances/is-paginated-list-widget-instance';

import {createWidgetInstance} from './create-widget-instance';
import {evaluateProperty, trFormula} from './widget-instance-utils';
Expand Down Expand Up @@ -184,7 +186,10 @@ export function widgetToWidgetInstance(
};
}),
);
} else if (isDynamicTableWidget(widget) && isDynamicTableWidgetInstance(wi)) {
} else if (
(isDynamicTableWidget(widget) && isDynamicTableWidgetInstance(wi)) ||
(isPaginatedTableWidget(widget) && isPaginatedTableWidgetInstance(wi))
) {
wi.dataset = widget.dataset.map((cell: AjfTableDataset) => {
return cell.formula instanceof Array
? cell.formula.map(f => trFormula(f, context, ts))
Expand Down Expand Up @@ -231,6 +236,7 @@ export function widgetToWidgetInstance(
};
});
wi.data = header.length === 0 ? [...dataset] : [[...header], ...dataset];
wi.styles = {...wi.styles, alignItems: 'flex-start'};
} else if (isPaginatedListWidget(widget) && isPaginatedListWidgetInstance(wi)) {
let content: AjfWidgetInstance[] = [];
if (widget.contentDefinition) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfPaginatedTableWidget} from '../../interface/widgets/paginated-table-widget';
import {AjfWidget} from '../../interface/widgets/widget';
import {AjfWidgetType} from '../../interface/widgets/widget-type';

export const isPaginatedTableWidget = (widget: AjfWidget): widget is AjfPaginatedTableWidget => {
return widget != null && widget.widgetType === AjfWidgetType.PaginatedTable;
};

0 comments on commit 14c6241

Please sign in to comment.