瀏覽代碼

提交 / 删除 / 确认

luoyifan 3 年之前
父節點
當前提交
deec90785b
共有 4 個文件被更改,包括 235 次插入9 次删除
  1. 6 3
      src/Scope.ts
  2. 1 1
      src/controls/MainTab.js
  3. 4 0
      src/lib/lib.ts
  4. 224 5
      src/lib/systemLib.ts

+ 6 - 3
src/Scope.ts

@@ -126,17 +126,19 @@ export class Scope {
         }
         const holder = sender?.lookupReferenceHolder()
         // delete config.constrain
+        config.constrain = true
         const win = new Ext.Window(config);
         if (holder) {
             holder.add(win)
         }
 
-        win.addListener('show', function (sender) {
+        win.addListener('beforerender', function (sender) {
             // 记录句柄
             if (sender && !that._handle) {
                 that._handle = sender
             }
-
+        })
+        win.addListener('afterrender', function (sender) {
             // 调用onLoad回调
             try {
                 that.onLoad()
@@ -180,7 +182,8 @@ export class Scope {
                 if (sender && !that._handle) {
                     that._handle = sender
                 }
-
+            })
+            handle.addListener('afterrender', function (sender) {
                 // 调用onLoad回调
                 try {
                     that.onLoad()

+ 1 - 1
src/controls/MainTab.js

@@ -30,7 +30,7 @@ export default function () {
                 tabchange(tabPanel, newCard, oldCard, eOpts) {
                     _.defer(() => {
                         me.changeHash(newCard.path)
-                        window['cp'] = newCard
+                        window['cp'] = newCard.yvanScope
                     })
 
                     $(window).trigger('tabChange', {

+ 4 - 0
src/lib/lib.ts

@@ -70,6 +70,10 @@ export function lookupFn(scope: Scope, event: string): Function {
  * 从 ext.element 获取 scope 对象
  */
 export function lookupScope(extHandle: any): Scope {
+    if (!extHandle) {
+        // @ts-ignore
+        return
+    }
     if (extHandle.isScope) {
         return extHandle
     }

+ 224 - 5
src/lib/systemLib.ts

@@ -1,7 +1,6 @@
 import _ from 'lodash'
 import {Lib, lookupScope} from './lib'
 import {ajax} from "./config";
-import {Model} from 'src/types';
 import {msg, showErrorDialog} from "../message";
 
 export const SIMPLE_RE = /^(?:\{(?:(\d+)|([a-z_][\w\.]*))\})$/i
@@ -23,7 +22,8 @@ export function calcExpress(data, express) {
     if (SIMPLE_RE.test(express)) {
         // '{foo}' 简单表达式
         const path = express.substring(1, express.length - 1);
-        return _.get(data, path)
+        const ret = _.get(data, path)
+        return ret.isModel ? ret.data : ret
     }
 
     while (true) {
@@ -395,9 +395,220 @@ export function toPlainObject(obj) {
     return obj
 }
 
+export function confirm(msg, sender?): Promise<void> {
+    return new Promise<void>(resolve => {
+        const scope = lookupScope(sender)
+        const config: any = {
+            title: '请确认',
+            icon: Ext.Msg.QUESTION,
+            modal: true,
+            animateTarget: sender,
+            // constrainHeader: true,
+            // constrain: true,
+            // constrainTo: scope._handle?.el?.dom || Ext.getBody(),
+            items: [
+                {
+                    html: _.escape(msg)
+                }
+            ],
+            buttons: [
+                {
+                    xtype: 'button', text: '确定', iconCls: 'x-fa fa-check',
+                    handler() {
+                        resolve()
+                        win.close()
+                    }
+                },
+                {
+                    xtype: 'button', text: '取消', iconCls: 'x-fa fa-times',
+                    handler() {
+                        win.close()
+                    }
+                }
+            ]
+        }
+        if (scope?._handle) {
+            config.constrain = true
+        }
+        const win = new Ext.Window(config);
+
+        if (scope?._handle) {
+            scope?._handle?.add(win)
+        }
+
+        win.show()
+    })
+}
+
 class SystemEventFu {
 
     @Lib({
+        title: '弹出确认对话框,回答 YES 之后调用某方法',
+        author: '罗一帆',
+        createAt: '2021-07-06',
+        updateAt: '2021-07-06',
+        type: 'system',
+        category: '对话框',
+        args: [
+            {
+                type: 'string',
+                title: '对话框的确认文字',
+                name: 'text',
+            },
+            {
+                type: 'event',
+                title: '确认之后调用的方法',
+                name: 'fn',
+                allowEmpty: true,
+            }
+        ]
+    })
+    confirm(text, fn) {
+        return function (sender) {
+            const scope = lookupScope(sender)
+            const msg = calcExpress(scope.viewModel.data, text)
+            confirm(msg, sender).then(() => {
+                fn.call(scope, sender)
+            })
+        }
+    }
+
+    @Lib({
+        title: '查询表单,如果 url 为空值,就取消查询',
+        author: '罗一帆',
+        createAt: '2021-07-06',
+        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) {
+        return function (sender) {
+            if (!invokeUrl) {
+                return
+            }
+
+            const scope = lookupScope(sender)
+            scope.setLoading(true)
+
+            let data = scope.viewModel.data
+            if (invokeParam) {
+                data = calcObjectFlat(data, invokeParam)
+            }
+
+            invokeServer(invokeUrl, data).then(res => {
+                if (res.success) {
+
+                    if (typeof writeTarget === "string") {
+                        _.forOwn(res.data, (v, k) => {
+                            scope.viewModel.set(writeTarget + "." + k, v)
+                        })
+
+                    } else if (typeof writeTarget === 'function') {
+                        writeTarget.call(scope, res.data)
+
+                    } else if (typeof writeTarget === 'object') {
+                        const ret = calcObjectFlat(res.data, writeTarget)
+                        _.forOwn(ret, (v, k) => {
+                            scope.viewModel.set(k, v)
+                        })
+                    }
+
+                } else {
+                    showErrorDialog(res.msg || '未知错误', sender)
+                }
+
+            }).catch((e) => {
+                const msg = e.response?.data?.msg
+                showErrorDialog(msg || e.toString(), sender)
+
+            }).finally(() => {
+                scope.setLoading(false)
+            })
+        }
+
+    }
+
+    @Lib({
+        title: '提交(不做校验、不关闭窗体)',
+        author: '罗一帆',
+        createAt: '2021-07-02',
+        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) {
+        return function (sender) {
+            const scope = lookupScope(sender)
+
+            scope.setLoading(true)
+
+            let data = scope.viewModel.data
+            if (arg0) {
+                data = calcObjectFlat(data, arg0)
+            }
+
+            invokeServer(groovyUrl, data).then(res => {
+                if (res.success) {
+                    if (res.msg) {
+                        msg(res.msg)
+                    }
+                    if (typeof successCallback === 'function') {
+                        successCallback.call(scope, sender, res.data)
+                    }
+
+                } else {
+                    showErrorDialog(res.msg || '未知错误', sender)
+                }
+
+            }).catch((e) => {
+                const msg = e.response?.data?.msg
+                showErrorDialog(msg || e.toString(), sender)
+
+            }).finally(() => {
+                scope.setLoading(false)
+            })
+        }
+    }
+
+    @Lib({
         title: '提交表单',
         author: '罗一帆',
         createAt: '2021-07-02',
@@ -421,9 +632,14 @@ class SystemEventFu {
     formCommit(groovyUrl: string, arg0: any) {
         return function (sender) {
             const scope = lookupScope(sender)
-            const valid = scope.down('form').isValid()
-            if (!valid) {
-                return
+
+            const form = scope.down('form')
+            if (form) {
+                // 如果下级有表单,就做表单校验
+                const valid = form.isValid()
+                if (!valid) {
+                    return
+                }
             }
 
             scope.setLoading(true)
@@ -435,6 +651,9 @@ class SystemEventFu {
 
             invokeServer(groovyUrl, data).then(res => {
                 if (res.success) {
+                    if (res.msg) {
+                        msg(res.msg)
+                    }
                     scope.dialogSuccess(res)
                 } else {
                     showErrorDialog(res.msg || '未知错误', sender)