UNPKG

4.77 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __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};
14var __metadata = (this && this.__metadata) || function (k, v) {
15 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16};
17var __param = (this && this.__param) || function (paramIndex, decorator) {
18 return function (target, key) { decorator(target, key, paramIndex); }
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21var logger_1 = require("../logger");
22var context_1 = require("../context/context");
23var context_2 = require("../context/context");
24var 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 // valueGetter is a function, so just call it
34 var func = expressionOrFunc;
35 return func(params);
36 }
37 else if (typeof expressionOrFunc === 'string') {
38 // valueGetter is an expression, so execute the expression
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 // the params don't have all these values, rather we add every possible
50 // value a params can have, which makes whatever is in the params available.
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 // the expression failed, which can happen, as it's the client that
56 // provides the expression. so print a nice message
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 // check cache first
65 if (this.expressionToFunctionCache[expression]) {
66 return this.expressionToFunctionCache[expression];
67 }
68 // if not found in cache, return the function
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 // store in cache
72 this.expressionToFunctionCache[expression] = theFunction;
73 return theFunction;
74 };
75 ExpressionService.prototype.createFunctionBody = function (expression) {
76 // if the expression has the 'return' word in it, then use as is,
77 // if not, then wrap it with return and ';' to make a function
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}());
96exports.ExpressionService = ExpressionService;