UNPKG

@malagu/core

Version:
108 lines 4.36 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); 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; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpressionHandlerImpl = void 0; const annotation_1 = require("../annotation"); const expression_protocol_1 = require("./expression-protocol"); const traverse = require("traverse"); const inversify_1 = require("inversify"); let ExpressionHandlerImpl = class ExpressionHandlerImpl { init() { } getContext(ctx) { const c = ctx || this.expressionContextProvider.provide(); if (!ctx && c !== this._ctx) { this._ctx = c; this.handle(c, c); } return c; } handle(textOrObj, ctx) { if (typeof textOrObj === 'string') { return this.doHandle(textOrObj, ctx); } else { const self = this; traverse(textOrObj).forEach(function (value) { if (typeof value === 'string') { if (typeof value === 'string') { this.update(self.handle(value, ctx)); } else if (value && value._ignoreEl === true) { this.update(value, true); } } }); return textOrObj; } } doHandle(text, ctx) { const sections = this.expressionCompiler.compileSections(text); if (sections.length > 0) { if (this.hasExpression(sections)) { const c = this.getContext(ctx); if (sections.length === 1) { let value = sections[0].evalSync(c); if (typeof value === 'string') { value = this.handle(value, c); } return value; } const result = []; for (const section of sections) { if (typeof section === 'string') { result.push(section); } else { let value = section.evalSync(c); if (typeof value === 'string') { value = this.handle(value, c); } result.push(value); } } return result.join(''); } } return text; } hasExpression(sections) { for (const section of sections) { if (typeof section !== 'string') { return true; } } return false; } }; __decorate([ annotation_1.Autowired(expression_protocol_1.JexlEngineProvider), __metadata("design:type", Object) ], ExpressionHandlerImpl.prototype, "jexlEngineProvider", void 0); __decorate([ annotation_1.Autowired(expression_protocol_1.ExpressionContextProvider), __metadata("design:type", Object) ], ExpressionHandlerImpl.prototype, "expressionContextProvider", void 0); __decorate([ annotation_1.Autowired(expression_protocol_1.ExpressionCompiler), __metadata("design:type", Object) ], ExpressionHandlerImpl.prototype, "expressionCompiler", void 0); __decorate([ inversify_1.postConstruct(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ExpressionHandlerImpl.prototype, "init", null); ExpressionHandlerImpl = __decorate([ annotation_1.Component(expression_protocol_1.ExpressionHandler) ], ExpressionHandlerImpl); exports.ExpressionHandlerImpl = ExpressionHandlerImpl; //# sourceMappingURL=expression-handler.js.map