import { Component, OnInit, ViewChild, Input, OnChanges, Injector, Output, EventEmitter } from '@angular/core'; import { HttpClient } from "@angular/common/http"; import { DatagridComponent } from '@farris/ui-datagrid'; import { I18nFunctions } from '../i18n/I18nFunctions'; import { ExpressionEventService } from '../utils/expression-event-service'; import { ExpressionEditorLocalePipe } from '../utils/expression-editor-locale.pipe'; import { DeprecatedFunctions } from "../common/CommonResource"; import { FunctionEntity, FuncType, Parameter } from '../common/CommonEntity'; //计算表达式--函数区域展示 @Component({ selector: 'app-expr-func-grid', templateUrl: './expr-func-grid.component.html', styleUrls: ['./expr-func-grid.component.css'] }) export class ExprFuncGridComponent implements OnInit, OnChanges { @ViewChild('dg') dg: DatagridComponent; @Input() currentFuncTabType: string = "String"; @Input() funcExtend: FunctionEntity[] = []; @Output() selectFuncRow = new EventEmitter(); @Output() dbclickInsert = new EventEmitter(); private localeId: string; private localePipe: ExpressionEditorLocalePipe; columns = []; datas: FunctionEntity[] = []; standardFunctions = []; //规范函数 allFunctions = []; //所有函数,包含不规范 items: FuncOnEntity[] = []; //页面展示函数 private functions: I18nFunctions = new I18nFunctions(); constructor(private eventService: ExpressionEventService, private injector: Injector, public http: HttpClient) { //初始化国际化资源 this.localeId = localStorage.getItem('languageCode'); this.localePipe = new ExpressionEditorLocalePipe(this.localeId); this.allFunctions = this.functions.get(this.localeId) || this.functions.get("zh-CHS"); //获取i18n函数资源;过滤不规范命名函数 for (let i = 0; i < this.allFunctions.length; i++) { if (!DeprecatedFunctions.includes(this.allFunctions[i].key)) { this.standardFunctions.push(this.allFunctions[i]); } } } ngOnInit() { this.eventService.subClearFuncSelect().subscribe(() => { }) this.columns = [ { field: 'name', title: this.localePipe.transform("FunctionSemanticName"), width: 3 }, { field: 'key', title: this.localePipe.transform("FunctionName"), width: 2 }, { field: 'param', title: this.localePipe.transform("FunctionParameter"), width: 3 } ]; switch (this.currentFuncTabType) { case FuncType.Advanced: this.findFunctionByType(FuncType.Advanced); break; case FuncType.Analytical: this.findFunctionByType(FuncType.Analytical); break; case FuncType.DateTime: this.findFunctionByType(FuncType.DateTime); break; case FuncType.Math: this.findFunctionByType(FuncType.Math); break; case FuncType.String: this.findFunctionByType(FuncType.String); break; case FuncType.Translate: this.findFunctionByType(FuncType.Translate); break; case FuncType.Extend: this.findFunctionByType(FuncType.Extend); break; } this.items = [...this.items]; } //根据类别,过滤函数 findFunctionByType(type: string) { this.items = []; if (type == FuncType.Extend) { this.funcExtend.forEach(element => { let func: FuncOnEntity = { key: element.key, name: element.name, type: element.type, className: element.className, description: element.description, param: this.getParamStr(element.params), params: element.params, isLatestExtendFunc: element.isLatestExtendFunc }; this.items.push(func); }); } else { this.standardFunctions.forEach(element => { if (element.type == type) { let func: FuncOnEntity = { key: element.key, name: element.type == FuncType.Advanced ? element.key : element.name, type: element.type, className: element.className, description: element.description, param: this.getParamStr(element.params), params: element.params, isLatestExtendFunc: element.isLatestExtendFunc }; this.items.push(func); }; }); } } //获取参数列表的字符串展示 getParamStr(params: Parameter[]): string { let paramStr = "("; for (let i = 0; i < params.length - 1; i++) { paramStr = paramStr + params[i].key + ","; } if (params.length != 0) paramStr = paramStr + params[params.length - 1].key + ")"; else paramStr = paramStr + ")"; return paramStr; } ngOnChanges(): void { } ondblclickRow($event) { this.dbclickInsert.emit($event.rowData); } selectRowChange($event) { this.selectFuncRow.emit($event.data); } } class FuncOnEntity { key: string; name: string; type: string; className: string = ""; description: string; param: string; params: Parameter[]; isLatestExtendFunc: boolean = null; }