Skip to content

Commit

Permalink
feat(core/reports): add support for user defined init functions in cu…
Browse files Browse the repository at this point in the history
…stom widgets
  • Loading branch information
trik committed Aug 28, 2020
1 parent 7b55695 commit 6d0c788
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 49 deletions.
37 changes: 37 additions & 0 deletions src/core/reports/interface/widgets/widget-components-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @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 {AjfContext} from '@ajf/core/models';
import {Type} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';

import {AjfBaseWidgetComponent} from '../../base-widget';
import {AjfWidgetInstance} from '../widgets-instances/widget-instance';

export interface AjfWidgetComponentsMap {
[key: number]: {
component: Type<AjfBaseWidgetComponent>,
inputs?: {[key: string]: any},
initInstance?: (widgetInstance: AjfWidgetInstance, context: AjfContext,
translateService: TranslateService) => AjfWidgetInstance;
};
}
2 changes: 1 addition & 1 deletion src/core/reports/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export * from './serializers/report-container-serializer';
export * from './serializers/report-serializer';
export * from './serializers/widget-serializer';
export * from './widget';
export * from './widget-components-map';
export * from './interface/widgets/widget-components-map';
export * from './widget-host';
export * from './widget-service';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {AjfWidget} from '../../interface/widgets/widget';
import {AjfWidgetType} from '../../interface/widgets/widget-type';
import {AjfWidgetWithContent} from '../../interface/widgets/widget-with-content';
import {evaluateAggregation} from '../aggregation/evaluate-aggregation';
import {componentsMap} from '../widgets/widgets-map';

import {createWidgetInstance} from './create-widget-instance';
import {trFormula} from './widget-instance-utils';
Expand Down Expand Up @@ -143,22 +144,21 @@ export function widgetToWidgetInstance(
trFormula(cell.formula!, context as AjfContext, ts);
}));

twi.data = (tw.dataset ||
[]).map(row => row.map(cell => {
let evf = '';
try {
evf = cell.formula instanceof Array ?
cell.formula.map(f => trFormula(f as AjfFormula, context as AjfContext, ts)) :
trFormula(cell.formula!, context as AjfContext, ts);
} catch (_e) {
}
return ({
value: evf,
style: { ...tw.cellStyles, ...cell.style },
rowspan: cell.rowspan,
colspan: cell.colspan,
});
}));
twi.data = (tw.dataset || []).map(row => row.map(cell => {
let evf = '';
try {
evf = cell.formula instanceof Array ?
cell.formula.map(f => trFormula(f as AjfFormula, context as AjfContext, ts)) :
trFormula(cell.formula!, context as AjfContext, ts);
} catch (_e) {
}
return ({
value: evf,
style: {...tw.cellStyles, ...cell.style},
rowspan: cell.rowspan,
colspan: cell.colspan,
});
}));
} else if (widget.widgetType === AjfWidgetType.DynamicTable) {
const tdw = widget as AjfDynamicTableWidget;
const tdwi = wi as AjfTableWidgetInstance;
Expand All @@ -175,35 +175,31 @@ export function widgetToWidgetInstance(
try {
if (trf instanceof Array) {
trf = trf.map(
v => v != null && typeof v === 'string' && v.trim().length > 0 ? ts.instant(v) : v);
v => v != null && typeof v === 'string' && v.trim().length > 0 ? ts.instant(v) : v);
} else {
trf = trf != null && typeof trf === 'string' && trf.trim().length > 0 ?
ts.instant(trf) : trf;
trf = trf != null && typeof trf === 'string' && trf.trim().length > 0 ? ts.instant(trf) :
trf;
}
} catch (_e) {
}
return ({
...cell,
value: trf
});
return ({...cell, value: trf});
}));

const header =
(tdw.dataset || []).map(cell => {
let evf = '';
try {
evf = cell.formula instanceof Array ?
const header = (tdw.dataset || []).map(cell => {
let evf = '';
try {
evf = cell.formula instanceof Array ?
cell.formula.map(f => trFormula(f as AjfFormula, context as AjfContext, ts)) :
trFormula(cell.formula!, context as AjfContext, ts);
} catch (_e) {
}
return ({
value: evf,
style: { ...tdw.cellStyles, ...cell.style },
rowspan: cell.rowspan,
colspan: cell.colspan,
});
} catch (_e) {
}
return ({
value: evf,
style: {...tdw.cellStyles, ...cell.style},
rowspan: cell.rowspan,
colspan: cell.colspan,
});
});
tdwi.data = [[...header], ...dataset];
} else if (widget.widgetType === AjfWidgetType.Image) {
const iw = widget as AjfImageWidget;
Expand Down Expand Up @@ -266,6 +262,13 @@ export function widgetToWidgetInstance(
const mw = widget as AjfMapWidget;
const mwi = wi as AjfMapWidgetInstance;
mwi.coordinate = evaluateExpression(mw.coordinate.formula, context);
} else if (widget.widgetType > 100) {
const iiFn = componentsMap[widget.widgetType] != null ?
componentsMap[widget.widgetType].initInstance :
null;
if (iiFn != null) {
return iiFn(wi, context, ts);
}
}
return wi;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*
*/

import {Type} from '@angular/core';
import {AjfWidgetComponentsMap} from '../../interface/widgets/widget-components-map';

import {AjfBaseWidgetComponent} from './base-widget';

export interface AjfWidgetComponentsMap {
[key: number]: {
component: Type<AjfBaseWidgetComponent>,
inputs?: {[key: string]: any},
};
}
export const componentsMap: AjfWidgetComponentsMap = {};
5 changes: 3 additions & 2 deletions src/core/reports/widget-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import {Type} from '@angular/core';

import {AjfBaseWidgetComponent} from './base-widget';
import {AjfWidgetComponentsMap} from './widget-components-map';
import {AjfWidgetComponentsMap} from './interface/widgets/widget-components-map';
import {componentsMap} from './utils/widgets/widgets-map';

export abstract class AjfWidgetService {
readonly componentsMap: AjfWidgetComponentsMap = {};
readonly componentsMap: AjfWidgetComponentsMap = componentsMap;

registerCustomWidget(widget: {
widgetType: number,
Expand Down
2 changes: 1 addition & 1 deletion src/core/reports/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '@angular/core';

import {AjfWidgetInstance} from './interface/widgets-instances/widget-instance';
import {AjfWidgetComponentsMap} from './widget-components-map';
import {AjfWidgetComponentsMap} from './interface/widgets/widget-components-map';
import {AjfWidgetHost} from './widget-host';

@Directive()
Expand Down

0 comments on commit 6d0c788

Please sign in to comment.