1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
9 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
10 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
11 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
12 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
13 | };
|
14 | var __metadata = (this && this.__metadata) || function (k, v) {
|
15 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
16 | };
|
17 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
18 | return function (target, key) { decorator(target, key, paramIndex); }
|
19 | };
|
20 | Object.defineProperty(exports, "__esModule", { value: true });
|
21 | var logger_1 = require("../logger");
|
22 | var context_1 = require("../context/context");
|
23 | var context_2 = require("../context/context");
|
24 | var ExpressionService = (function () {
|
25 | function ExpressionService() {
|
26 | this.expressionToFunctionCache = {};
|
27 | }
|
28 | ExpressionService.prototype.setBeans = function (loggerFactory) {
|
29 | this.logger = loggerFactory.create('ExpressionService');
|
30 | };
|
31 | ExpressionService.prototype.evaluate = function (expressionOrFunc, params) {
|
32 | if (typeof expressionOrFunc === 'function') {
|
33 |
|
34 | var func = expressionOrFunc;
|
35 | return func(params);
|
36 | }
|
37 | else if (typeof expressionOrFunc === 'string') {
|
38 |
|
39 | var expression = expressionOrFunc;
|
40 | return this.evaluateExpression(expression, params);
|
41 | }
|
42 | else {
|
43 | console.error('ag-Grid: value should be either a string or a function', expressionOrFunc);
|
44 | }
|
45 | };
|
46 | ExpressionService.prototype.evaluateExpression = function (expression, params) {
|
47 | try {
|
48 | var javaScriptFunction = this.createExpressionFunction(expression);
|
49 |
|
50 |
|
51 | var result = javaScriptFunction(params.value, params.context, params.oldValue, params.newValue, params.value, params.node, params.data, params.colDef, params.rowIndex, params.api, params.columnApi, params.getValue, params.column, params.columnGroup);
|
52 | return result;
|
53 | }
|
54 | catch (e) {
|
55 |
|
56 |
|
57 | console.log('Processing of the expression failed');
|
58 | console.log('Expression = ' + expression);
|
59 | console.log('Exception = ' + e);
|
60 | return null;
|
61 | }
|
62 | };
|
63 | ExpressionService.prototype.createExpressionFunction = function (expression) {
|
64 |
|
65 | if (this.expressionToFunctionCache[expression]) {
|
66 | return this.expressionToFunctionCache[expression];
|
67 | }
|
68 |
|
69 | var functionBody = this.createFunctionBody(expression);
|
70 | var theFunction = new Function('x, ctx, oldValue, newValue, value, node, data, colDef, rowIndex, api, columnApi, getValue, column, columnGroup', functionBody);
|
71 |
|
72 | this.expressionToFunctionCache[expression] = theFunction;
|
73 | return theFunction;
|
74 | };
|
75 | ExpressionService.prototype.createFunctionBody = function (expression) {
|
76 |
|
77 |
|
78 | if (expression.indexOf('return') >= 0) {
|
79 | return expression;
|
80 | }
|
81 | else {
|
82 | return 'return ' + expression + ';';
|
83 | }
|
84 | };
|
85 | __decorate([
|
86 | __param(0, context_2.Qualifier('loggerFactory')),
|
87 | __metadata("design:type", Function),
|
88 | __metadata("design:paramtypes", [logger_1.LoggerFactory]),
|
89 | __metadata("design:returntype", void 0)
|
90 | ], ExpressionService.prototype, "setBeans", null);
|
91 | ExpressionService = __decorate([
|
92 | context_1.Bean('expressionService')
|
93 | ], ExpressionService);
|
94 | return ExpressionService;
|
95 | }());
|
96 | exports.ExpressionService = ExpressionService;
|