Browse Source

eventDialog

luoyifan 3 years ago
parent
commit
b86b9b8c69
3 changed files with 94 additions and 193 deletions
  1. 37 3
      src/lib/lib.ts
  2. 41 184
      src/lib/systemLib.ts
  3. 16 6
      src/types.ts

+ 37 - 3
src/lib/lib.ts

@@ -1,8 +1,8 @@
 import _ from 'lodash'
-import {FunctionRegiste} from "../types"
+import {FunctionRegiste, LibParamType} from "../types"
 import {Scope} from "../Scope"
 
-export function getRegList() {
+export function getRegList(): FunctionRegiste[] {
     let regList = _.get(window, 'yvanLib.regList')
     if (!regList) {
         regList = []
@@ -11,10 +11,44 @@ export function getRegList() {
     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]
+}
+
+/**
+ * 标注系统全局函数的参数
+ * @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()
-        console.log("libregiste", target[propertyKey], libList.length)
         libList.push({
             ...registe,
             name: propertyKey,

+ 41 - 184
src/lib/systemLib.ts

@@ -1,5 +1,5 @@
 import _ from 'lodash'
-import {Lib, lookupScope} from './lib'
+import {Lib, lookupScope, LibParam} from './lib'
 import {ajax} from "./config";
 import {msg as msgSimple, showErrorDialog as showErrorDialogSimple} from "../message";
 
@@ -431,6 +431,13 @@ export function invokeServer(url: string, ...args: any[]) {
 }
 
 export function clearViewModelValues(viewModel, propertyName) {
+    if (!propertyName) {
+        const dd = viewModel.getData()
+        _.forOwn(dd, (value, key) => {
+            viewModel.set(key, '')
+        })
+        return
+    }
     const dd = _.get(viewModel.getData(), propertyName)
     _.forOwn(dd, (value, key) => {
         viewModel.set(propertyName + '.' + key, '')
@@ -514,8 +521,14 @@ export function confirm(msg, sender?): Promise<void> {
     })
 }
 
-export class SystemEventFu {
+/**
+ * 任何符号字符串都替换成下换线
+ */
+export function normId(value: string) {
+    return _.replace(value, /[^\w]/g, '_')
+}
 
+export class SystemEventFu {
     @Lib({
         title: '弹出确认对话框,回答 YES 之后调用某方法',
         author: '罗一帆',
@@ -523,21 +536,9 @@ export class SystemEventFu {
         updateAt: '2021-07-06',
         type: 'system',
         category: '对话框',
-        args: [
-            {
-                type: 'string',
-                title: '对话框的确认文字',
-                name: 'text',
-            },
-            {
-                type: 'event',
-                title: '确认之后调用的方法',
-                name: 'fn',
-                allowEmpty: true,
-            }
-        ]
     })
-    confirm(text, fn) {
+    confirm(@LibParam('对话框的确认文字', 'string')text,
+            @LibParam('确认之后的调用的方法', 'event')   fn) {
         return function (sender) {
             const scope = lookupScope(sender)
             const msg = calcExpress(scope.viewModel.data, text)
@@ -548,22 +549,14 @@ export class SystemEventFu {
     }
 
     @Lib({
-        title: '删除表格当前的选中行',
+        title: '删除表格当前的选中行(前端删除)',
         author: '罗一帆',
         createAt: '2021-07-08',
         updateAt: '2021-07-08',
         type: 'system',
         category: '表格',
-        args: [
-            {
-                type: 'refs',
-                title: 'gridRef 表格引用名',
-                allowEmpty: true,
-                name: 'gridRefName',
-            }
-        ]
     })
-    gridRemoveCurrentRow(gridRefName) {
+    gridRemoveCurrentRow(@LibParam('表格引用名', 'refs') gridRefName) {
         return function (sender) {
             const scope = lookupScope(sender)
             const grid = scope.refs[gridRefName]
@@ -581,27 +574,11 @@ export class SystemEventFu {
         updateAt: '2021-07-06',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'string',
-                title: 'groovy 服务路径',
-                name: 'invokeUrl',
-            },
-            {
-                type: 'object',
-                title: 'invoke 参数的 lookup 表达式, 如果不填 默认提交所有的 viewModel.data',
-                name: 'invokeParam',
-                allowEmpty: true,
-            },
-            {
-                type: 'string',
-                title: '服务读取后,数据的回写位置,可以是 scope.XX / system.XX / string / lookup表达式',
-                name: 'writeTarget',
-                allowEmpty: true,
-            }
-        ]
     })
-    loadForm(invokeUrl: string, invokeParam: any, writeTarget: any) {
+    loadForm(@LibParam('后端服务路径', 'string')invokeUrl: string,
+             @LibParam('后端需要的数据,默认情况提交所有', 'lookup', true) invokeParam: any,
+             //可以是 scope.XX / system.XX / string / lookup表达式
+             @LibParam('提交成功后的回调', 'viewModel', true) writeTarget: any) {
         return function (sender) {
             if (!invokeUrl) {
                 return
@@ -655,27 +632,10 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'string',
-                title: 'groovy 服务路径',
-                name: 'groovyUrl',
-            },
-            {
-                type: 'object',
-                title: '参数的 lookup 表达式, 如果不填 默认提交所有的 viewModel.data',
-                name: 'arg0',
-                allowEmpty: true,
-            },
-            {
-                type: 'event',
-                title: '成功后的回调',
-                name: 'successCallback',
-                allowEmpty: true,
-            }
-        ]
     })
-    commit(groovyUrl: string, arg0: any, successCallback) {
+    commit(@LibParam('后端服务路径', 'string')groovyUrl: string,
+           @LibParam('后端需要的数据,默认情况提交所有', 'lookup', true) arg0: any,
+           @LibParam('提交成功后的回调', 'event', true)  successCallback) {
         return function (sender) {
             const scope = lookupScope(sender)
 
@@ -716,21 +676,9 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'string',
-                title: 'groovy 服务路径',
-                name: 'groovyUrl',
-            },
-            {
-                type: 'object',
-                title: '参数的 lookup 表达式, 如果不填 默认提交所有的 viewModel.data',
-                name: 'arg0',
-                allowEmpty: true,
-            }
-        ]
     })
-    formCommit(groovyUrl: string, arg0: any) {
+    formCommit(@LibParam('后端服务路径', 'string')groovyUrl: string,
+               @LibParam('后端需要的参数,默认情况提交所有', 'lookup', true)arg0: any) {
         return function (sender) {
             const scope = lookupScope(sender)
 
@@ -777,16 +725,8 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '对话框',
-        args: [
-            {
-                type: 'object',
-                title: '傳回成功數據的 lookup 表達式',
-                name: 'lookupObject',
-                allowEmpty: true,
-            }
-        ]
     })
-    dialogSuccess(lookupObject) {
+    dialogSuccess(@LibParam('传回给父窗口的成功参数对象', 'lookup', true)lookupObject) {
         return function (sender) {
             const scope = lookupScope(sender)
             if (lookupObject) {
@@ -806,15 +746,8 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'viewModel',
-                title: 'propertyName 属性路径',
-                name: 'propertyName',
-            }
-        ]
     })
-    clearViewModelValues(propertyName: string) {
+    clearViewModelValues(@LibParam('属性路径', 'viewModel', true)propertyName: string) {
         return function (sender) {
             const scope = lookupScope(sender)
             clearViewModelValues(scope.viewModel, propertyName)
@@ -828,21 +761,8 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'viewModel',
-                title: 'propertyName 属性路径',
-                name: 'propertyName',
-            },
-            {
-                type: 'refs',
-                title: 'gridRef 表格引用名',
-                allowEmpty: true,
-                name: 'gridRefName',
-            }
-        ]
     })
-    getGrid(url) {
+    getGrid(@LibParam('定义文件', 'module')url) {
         return function (sender, config) {
             // @ts-ignore
             require([url], (grid) => {
@@ -859,21 +779,9 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'viewModel',
-                title: 'propertyName 属性路径',
-                name: 'propertyName',
-            },
-            {
-                type: 'refs',
-                title: 'gridRef 表格引用名',
-                allowEmpty: true,
-                name: 'gridRefName',
-            }
-        ]
     })
-    clearViewModelReloadGrid(propertyName: string, gridRefName?: string) {
+    clearViewModelReloadGrid(@LibParam('属性路径', 'viewModel')propertyName: string,
+                             @LibParam('要刷新的表格', 'refs', true)gridRefName?: string) {
         return function (sender) {
             const scope = lookupScope(sender)
             clearViewModelValues(scope.viewModel, propertyName)
@@ -893,16 +801,8 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'refs',
-                title: 'gridRef 表格引用名, 不填写的情况下刷新所有',
-                allowEmpty: true,
-                name: 'gridRefName',
-            }
-        ]
     })
-    reloadGrid(gridRefName: string) {
+    reloadGrid(@LibParam('表格引用对象', 'refs', true) gridRefName: string) {
         return function (sender) {
             const scope = lookupScope(sender)
             if (!gridRefName) {
@@ -921,27 +821,10 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '对话框',
-        args: [
-            {
-                type: 'module',
-                title: '业务模块名',
-                name: 'url',
-            },
-            {
-                type: 'object',
-                title: '参数数据 lookup 表达式',
-                name: 'lookupForData',
-                allowEmpty: true,
-            },
-            {
-                type: 'event',
-                title: '成功回调',
-                name: 'successCallback',
-                allowEmpty: true,
-            }
-        ]
     })
-    showDialog(url: string, lookupForData: any, successCallback) {
+    showDialog(@LibParam('业务模块名', 'module') url: string,
+               @LibParam('参数数据 lookup 表达式', 'lookup', true) lookupForData: any,
+               @LibParam('成功回调', 'event', true) successCallback) {
         return function (sender) {
             const scope = lookupScope(sender)
             const data = scope.viewModel.data
@@ -964,21 +847,9 @@ export class SystemEventFu {
         updateAt: '2021-07-02',
         type: 'system',
         category: '对话框',
-        args: [
-            {
-                type: 'module',
-                title: '模块名 (WidgetDialog)',
-                name: 'widgetUrl',
-            },
-            {
-                type: 'object',
-                title: 'lookup 映射关系',
-                name: 'lookupSetting',
-                allowEmpty: true,
-            }
-        ]
     })
-    showWidget(widgetUrl, lookup) {
+    showWidget(@LibParam('模块名 (WidgetDialog)', 'module')widgetUrl,
+               @LibParam('lookup 映射关系', 'lookup', true)lookup) {
         return function (sender, queryValue) {
             showWidget(widgetUrl, lookup, sender, queryValue)
         }
@@ -991,15 +862,8 @@ export class SystemEventFu {
         updateAt: '2021-07-05',
         type: 'system',
         category: '表单',
-        args: [
-            {
-                type: 'viewModel',
-                title: 'lookup 设值',
-                name: 'lookup',
-            },
-        ]
     })
-    clearViewModelByLookup(lookup) {
+    clearViewModelByLookup(@LibParam('设值表达式', 'lookup') lookup) {
         return function (sender) {
             clearViewModelByLookup(sender, lookup)
         }
@@ -1012,15 +876,8 @@ export class SystemEventFu {
         updateAt: '2021-07-05',
         type: 'system',
         category: '对话框',
-        args: [
-            {
-                type: 'event',
-                title: '对话框的返回值回调',
-                name: 'callBack',
-            },
-        ]
     })
-    closeMe(callBack) {
+    closeMe(@LibParam('对话框的返回值回调', 'event', true) callBack) {
         return function (sender) {
             const scope = lookupScope(sender)
             scope.close()

+ 16 - 6
src/types.ts

@@ -21,6 +21,21 @@ export interface Model {
 export type ConfigProcess = (me, config) => any
 
 /**
+ * 参数的填写类型
+ *    module 选择一个功能模块
+ *    control 从当前模块的 controller 中选择一个方法
+ *    viewModel 从当前模块的 model 中,选择一个属性
+ *    refs 从当前模块的 refs 中选一个控件
+ *    event 当成事件输入框来填写
+ *    lookup 填写 lookup 表达式对象
+ *    ()=> Promise<string[], <id:string,value:string>[]> 函数的返回结果,作为填写范围
+ *    string 任意字符串
+ *    object 任意对象 (JSON5)
+ */
+export type LibParamType = 'module' | 'control' | 'viewModel' | 'refs' | 'event' | 'lookup' | 'string' | 'object'
+    | (() => Promise<string[] | { id: string, value: string }[]>)
+
+/**
  * 注册函数中的参数
  */
 export interface FunctionArgument {
@@ -34,7 +49,7 @@ export interface FunctionArgument {
      *    string 任意字符串
      *    object 任意对象 (JSON5)
      */
-    type: 'module' | 'control' | 'viewModel' | 'refs' | 'event' | 'string' | 'object'
+    type: LibParamType
 
     /**
      * 参数中文说明
@@ -105,11 +120,6 @@ export interface FunctionRegiste {
     category?: string
 
     /**
-     * 参数值
-     */
-    args?: FunctionArgument[]
-
-    /**
      * 文档链接
      */
     link?: string