import _ from 'lodash' import {FunctionRegiste, LibParamType} from "../types" import {Scope} from "../Scope" export function getRegList(): FunctionRegiste[] { let regList = _.get(window, 'yvanLib.regList') if (!regList) { regList = [] _.set(window, 'yvanLib.regList', regList) } return regList } export function getRegParamList(methodName: string) { let regParamList = _.get(window, 'yvanLib.regParamList') if (!regParamList) { regParamList = {} _.set(window, 'yvanLib.regParamList', regParamList) } if (!_.has(regParamList, methodName)) { regParamList[methodName] = [] } return regParamList[methodName] } /** * 模拟点击按钮 */ export function raiseClick(buttonHandle) { if (!buttonHandle) { return false } if (buttonHandle.disabled) { // 按钮是禁止状态 return false } if (buttonHandle.hidden) { // 按钮是隐藏状态 return false } // 按钮点击 buttonHandle.click() return true } /** * 标注系统全局函数的参数 * @param title 函数名称 * @param type 函数类型 * @param allowEmpty 可否为空 */ export function LibParam(title: string, type: LibParamType, allowEmpty = false) { return function (target: any, methodName: any, paramsIndex: any) { const systemFnArgs = getRegParamList(methodName) systemFnArgs[paramsIndex] = { type, title, name: methodName, allowEmpty, } } } /** * 标注函数变成"系统全局函数" */ export function Lib(registe: FunctionRegiste) { return function (target, propertyKey: string, descriptor: PropertyDescriptor) { const libList = getRegList() libList.push({ ...registe, name: propertyKey, target: target[propertyKey] }) if (registe.type === 'system') { _.set(window, 'yvanLib.system.' + propertyKey, target[propertyKey]) } else if (registe.type === 'format') { _.set(window, 'yvanLib.format.' + propertyKey, target[propertyKey]) } return target } } /** * 解析事件 * 'scope.私有方法名' * 'system.系统函数名' * 'format.格式化名' */ export function lookupFn(scope: Scope, event: string, errorWho?: any): Function { if (window["IS_DESIGN_MODE"]) { return Ext.emptyFn } if (!_.startsWith(event, 'scope.') && !_.startsWith(event, 'system.') && !_.startsWith(event, 'format.')) { console.error('无法识别的事件响应类型', event) throw new TypeError('无法识别的事件响应类型') } // 为 eval 准备环境 const {system, format} = window['yvanLib'] const data = scope.viewModel.data const keys: any[] = [] const values: any[] = [] _.forOwn(data, (value, key) => { const idxKey = key.indexOf('//') if (idxKey >= 0) { keys.push(key.substr(0, idxKey)) } else { keys.push(key) } values.push(value) }) const func: Function = //eval(event) Function('scope', 'system', 'format', ...keys, 'return ' + event) const funcResult = func(scope, system, format, ...values) if (typeof funcResult !== 'function') { console.error('无法识别的事件响应类型', event, func, errorWho) throw new TypeError('无法识别的事件响应类型') } return funcResult } /** * 从 ext.element 获取 scope 对象 */ export function lookupScope(extHandle: any): Scope { if (!extHandle) { // @ts-ignore return } if (extHandle.isScope) { return extHandle } if (extHandle.column) { // 列上的组件 return extHandle.column.lookupReferenceHolder().yvanScope } if(_.get(extHandle, 'alias[0]') === 'widget.button'){ // 列上的按钮 // return extHandle.lookupViewModel().yvanScope } const vm = extHandle.lookupViewModel() return vm.yvanScope }