UNPKG

2.52 kBPlain TextView Raw
1
2import {Bean, Autowired} from "../context/context";
3import {Column} from "../entities/column";
4import {RowNode} from "../entities/rowNode";
5import {GridOptionsWrapper} from "../gridOptionsWrapper";
6import {ExpressionService} from "../valueService/expressionService";
7import {ValueFormatterParams} from "../entities/colDef";
8
9@Bean('valueFormatterService')
10export 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 // if floating, give preference to the floating formatter
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 // originally we put the angular 1 scope here, but we don't want the scope
42 // in the params interface, as other frameworks will see the interface, and
43 // angular 1 is not cool any more. so we hack the scope in here (we cannot
44 // include it above, as it's not in the interface, so would cause a compile error).
45 // in the future, when we stop supporting angular 1, we can take this out.
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 // if we don't do this, then arrays get displayed as 1,2,3, but we want 1, 2, 3 (ie with spaces)
54 if ( (result===null || result===undefined) && Array.isArray(value)) {
55 result = value.join(', ');
56 }
57
58 return result;
59 }
60}