1 |
|
2 | import {Bean, Autowired} from "../context/context";
|
3 | import {Column} from "../entities/column";
|
4 | import {RowNode} from "../entities/rowNode";
|
5 | import {GridOptionsWrapper} from "../gridOptionsWrapper";
|
6 | import {ExpressionService} from "../valueService/expressionService";
|
7 | import {ValueFormatterParams} from "../entities/colDef";
|
8 |
|
9 | @Bean('valueFormatterService')
|
10 | export class ValueFormatterService {
|
11 |
|
12 | @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
|
13 | @Autowired('expressionService') private expressionService: ExpressionService;
|
14 |
|
15 | public formatValue(column: Column,
|
16 | rowNode: RowNode,
|
17 | $scope: any,
|
18 | value: any): string {
|
19 |
|
20 | let formatter: (value: any)=>string;
|
21 | let colDef = column.getColDef();
|
22 |
|
23 | if (rowNode && rowNode.rowPinned) {
|
24 | formatter = colDef.pinnedRowValueFormatter ? colDef.pinnedRowValueFormatter : colDef.valueFormatter;
|
25 | } else {
|
26 | formatter = colDef.valueFormatter;
|
27 | }
|
28 | let result: string = null;
|
29 | if (formatter) {
|
30 | let params: ValueFormatterParams = {
|
31 | value: value,
|
32 | node: rowNode,
|
33 | data: rowNode ? rowNode.data : null,
|
34 | colDef: column.getColDef(),
|
35 | column: column,
|
36 | api: this.gridOptionsWrapper.getApi(),
|
37 | columnApi: this.gridOptionsWrapper.getColumnApi(),
|
38 | context: this.gridOptionsWrapper.getContext()
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | (<any>params).$scope = $scope;
|
47 |
|
48 | result = this.expressionService.evaluate(formatter, params);
|
49 | } else if(colDef.refData) {
|
50 | return colDef.refData[value];
|
51 | }
|
52 |
|
53 |
|
54 | if ( (result===null || result===undefined) && Array.isArray(value)) {
|
55 | result = value.join(', ');
|
56 | }
|
57 |
|
58 | return result;
|
59 | }
|
60 | }
|