|
@@ -2,6 +2,8 @@ import _ from 'lodash'
|
|
import {Lib, lookupScope} from './lib'
|
|
import {Lib, lookupScope} from './lib'
|
|
import {ajax} from "./config";
|
|
import {ajax} from "./config";
|
|
|
|
|
|
|
|
+export const SIMPLE_RE = /^(?:\{(?:(\d+)|([a-z_][\w\.]*))\})$/i
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 对某个表达式进行求值
|
|
* 对某个表达式进行求值
|
|
* a:{query.a},b:{query.b} -> a:aValue,b:bValue
|
|
* a:{query.a},b:{query.b} -> a:aValue,b:bValue
|
|
@@ -16,6 +18,12 @@ import {ajax} from "./config";
|
|
export function calcExpress(data, express) {
|
|
export function calcExpress(data, express) {
|
|
let result = express
|
|
let result = express
|
|
|
|
|
|
|
|
+ if (SIMPLE_RE.test(express)) {
|
|
|
|
+ // '{foo}' 简单表达式
|
|
|
|
+ const path = express.substring(1, express.length - 1);
|
|
|
|
+ return _.get(data, path)
|
|
|
|
+ }
|
|
|
|
+
|
|
while (true) {
|
|
while (true) {
|
|
const mlist = result.match(/{(.*?)}/)
|
|
const mlist = result.match(/{(.*?)}/)
|
|
if (!mlist) {
|
|
if (!mlist) {
|
|
@@ -110,8 +118,8 @@ export function calcObjectFlat(data, paramObject) {
|
|
* @param value 目标值
|
|
* @param value 目标值
|
|
*/
|
|
*/
|
|
export function tryWriteByExpress(viewModel, express, value) {
|
|
export function tryWriteByExpress(viewModel, express, value) {
|
|
- if (/^(?:\{(?:(\d+)|([a-z_][\w\.]*))\})$/i.test(express)) {
|
|
|
|
- // If we have '{foo}' alone it is a literal 简单表达式
|
|
|
|
|
|
+ if (SIMPLE_RE.test(express)) {
|
|
|
|
+ // '{foo}' 简单表达式
|
|
express = express.substring(1, express.length - 1);
|
|
express = express.substring(1, express.length - 1);
|
|
viewModel.set(express, value)
|
|
viewModel.set(express, value)
|
|
}
|
|
}
|
|
@@ -120,7 +128,7 @@ export function tryWriteByExpress(viewModel, express, value) {
|
|
/**
|
|
/**
|
|
* 尝试根据含表达式的对象回写, calcObjectFlat 的逆向方法
|
|
* 尝试根据含表达式的对象回写, calcObjectFlat 的逆向方法
|
|
* @example
|
|
* @example
|
|
- * tryWriteObject(null, { a:'{query.a}', b:{b1:'{query.b1}', b2:'{query.b2}'},c:'aa',d:['{query.d1}','{query.d2}'] }, { a: 'aValue', b: { b1: 'b1Value', b2: 'b2Value' }, c: 'aa', d: [ '1', '2' ] })
|
|
|
|
|
|
+ * tryWriteObject({ a:'{query.a}', b:{b1:'{query.b1}', b2:'{query.b2}'},c:'aa',d:['{query.d1}','{query.d2}']}, {a:'aValue', b:{b1:'b1Value', b2:'b2Value'}, c:'aa', d:[1,2]})
|
|
*
|
|
*
|
|
* expressObject:
|
|
* expressObject:
|
|
* {
|
|
* {
|
|
@@ -157,7 +165,7 @@ export function tryWriteByExpress(viewModel, express, value) {
|
|
*
|
|
*
|
|
* @param expressObject 含表达式的对象
|
|
* @param expressObject 含表达式的对象
|
|
* @param valueObject 表达式计算完成之后的结果对象
|
|
* @param valueObject 表达式计算完成之后的结果对象
|
|
- * @param writeFn 忽略方法 (key,value)=>boolean ,如果这个方法返回 false 代表不需要系统做处理
|
|
|
|
|
|
+ * @param writeFn 写入的方法 (path, value)=>void
|
|
*/
|
|
*/
|
|
export function tryWriteObject(expressObject, valueObject, writeFn) {
|
|
export function tryWriteObject(expressObject, valueObject, writeFn) {
|
|
|
|
|
|
@@ -173,7 +181,7 @@ export function tryWriteObject(expressObject, valueObject, writeFn) {
|
|
|
|
|
|
} else if (_.isString(value)) {
|
|
} else if (_.isString(value)) {
|
|
// 字符串直接用 calcExpress 表达式求解
|
|
// 字符串直接用 calcExpress 表达式求解
|
|
- if (/^(?:\{(?:(\d+)|([a-z_][\w\.]*))\})$/i.test(value)) {
|
|
|
|
|
|
+ if (SIMPLE_RE.test(value)) {
|
|
// If we have '{foo}' alone it is a literal 简单表达式
|
|
// If we have '{foo}' alone it is a literal 简单表达式
|
|
const targetPath = value.substring(1, value.length - 1);
|
|
const targetPath = value.substring(1, value.length - 1);
|
|
const targetValue = _.get(valueObject, (pathPrefix + "." + key).substr(1))
|
|
const targetValue = _.get(valueObject, (pathPrefix + "." + key).substr(1))
|
|
@@ -185,7 +193,15 @@ export function tryWriteObject(expressObject, valueObject, writeFn) {
|
|
}
|
|
}
|
|
} else if (_.isArray(value)) {
|
|
} else if (_.isArray(value)) {
|
|
_.each(value, (v, idx) => {
|
|
_.each(value, (v, idx) => {
|
|
-
|
|
|
|
|
|
+ if (SIMPLE_RE.test(v)) {
|
|
|
|
+ const targetPath = (pathPrefix + "." + key).substr(1) + "[" + idx + "]";
|
|
|
|
+ const targetValue = _.get(valueObject, (pathPrefix + "." + key).substr(1) + "[" + idx + "]")
|
|
|
|
+ if (!writeFn) {
|
|
|
|
+ console.log(`viewModel.set('${targetPath}', '${targetValue}')`)
|
|
|
|
+ } else {
|
|
|
|
+ writeFn(targetPath, targetValue)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
})
|
|
})
|
|
}
|
|
}
|
|
})
|
|
})
|
|
@@ -498,4 +514,155 @@ class SystemEventFu {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Lib({
|
|
|
|
+ title: '弹出查找框(不借助 search)',
|
|
|
|
+ author: '罗一帆',
|
|
|
|
+ createAt: '2021-07-02',
|
|
|
|
+ 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) {
|
|
|
|
+ return function (sender, queryValue) {
|
|
|
|
+ showWidget(widgetUrl, lookup, sender, queryValue)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Lib({
|
|
|
|
+ title: '根据 lookup 清空 viewModel',
|
|
|
|
+ author: '罗一帆',
|
|
|
|
+ createAt: '2021-07-05',
|
|
|
|
+ updateAt: '2021-07-05',
|
|
|
|
+ type: 'system',
|
|
|
|
+ category: '表单',
|
|
|
|
+ args: [
|
|
|
|
+ {
|
|
|
|
+ type: 'viewModel',
|
|
|
|
+ title: 'lookup 设值',
|
|
|
|
+ name: 'lookup',
|
|
|
|
+ },
|
|
|
|
+ ]
|
|
|
|
+ })
|
|
|
|
+ clearViewModelByLookup(lookup) {
|
|
|
|
+ return function (sender) {
|
|
|
|
+ clearViewModelByLookup(sender, lookup)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Lib({
|
|
|
|
+ title: '关闭对话框',
|
|
|
|
+ author: '罗一帆',
|
|
|
|
+ createAt: '2021-07-05',
|
|
|
|
+ updateAt: '2021-07-05',
|
|
|
|
+ type: 'system',
|
|
|
|
+ category: '对话框',
|
|
|
|
+ args: [
|
|
|
|
+ {
|
|
|
|
+ type: 'event',
|
|
|
|
+ title: '对话框的返回值回调',
|
|
|
|
+ name: 'callBack',
|
|
|
|
+ },
|
|
|
|
+ ]
|
|
|
|
+ })
|
|
|
|
+ closeMe(callBack) {
|
|
|
|
+ return function (sender) {
|
|
|
|
+ const scope = lookupScope(sender)
|
|
|
|
+ scope.close()
|
|
|
|
+
|
|
|
|
+ if (callBack) {
|
|
|
|
+ callBack.call(sender)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function clearViewModelByLookup(sender, lookup) {
|
|
|
|
+ if (_.isPlainObject(lookup)) {
|
|
|
|
+ const parentScope = lookupScope(sender)
|
|
|
|
+
|
|
|
|
+ _.forOwn(lookup, (value, key) => {
|
|
|
|
+ if (SIMPLE_RE.test(value)) {
|
|
|
|
+ // '{foo}' 简单表达式
|
|
|
|
+ const path = value.substring(1, value.length - 1);
|
|
|
|
+ if (path !== 'queryValue') {
|
|
|
|
+ parentScope.viewModel.set(path, '')
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function showWidget(widgetUrl, lookup, sender, queryValue, vjson = {}) {
|
|
|
|
+ const parentScope = lookupScope(sender)
|
|
|
|
+ const me = sender
|
|
|
|
+
|
|
|
|
+ // @ts-ignore
|
|
|
|
+ require([widgetUrl], (widgetScope) => {
|
|
|
|
+ const WidgetScopeClass = widgetScope.default
|
|
|
|
+ widgetScope = new WidgetScopeClass()
|
|
|
|
+
|
|
|
|
+ // 传递进 widget.model 的数据
|
|
|
|
+ const widgetDialogData = calcObjectFlat({
|
|
|
|
+ queryValue: queryValue,
|
|
|
|
+ ...parentScope.viewModel.data
|
|
|
|
+ }, lookup)
|
|
|
|
+ widgetScope.parentScope = parentScope
|
|
|
|
+ widgetScope.searchWidgetSuccess = (data) => {
|
|
|
|
+ if (typeof lookup === 'string') {
|
|
|
|
+ // lookup 是字符串的情况下,就是取某个列作为 value 值
|
|
|
|
+ me.setValue(data[lookup])
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * lookup: {
|
|
|
|
+ * // 扩展到 viewModel 的值做更改
|
|
|
|
+ * WH_CODE: "{queryValue}",
|
|
|
|
+ * WH_NAME: "{query.WH_NAME}",
|
|
|
|
+ * }
|
|
|
|
+ */
|
|
|
|
+ if (_.isPlainObject(lookup)) {
|
|
|
|
+ const parentScope = lookupScope(sender)
|
|
|
|
+
|
|
|
|
+ tryWriteObject(lookup, data, (path, value) => {
|
|
|
|
+ if (path === 'queryValue') {
|
|
|
|
+ me.setValue(value)
|
|
|
|
+ } else {
|
|
|
|
+ parentScope.viewModel.set(path, value)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ widgetScope.showDialog(sender, vjson, {data: widgetDialogData})
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 停止事件的默认行为
|
|
|
|
+ * @param e
|
|
|
|
+ */
|
|
|
|
+export function stopEvent(e) {
|
|
|
|
+ e.preventDefault()
|
|
|
|
+ e.stopPropagation()
|
|
|
|
+ // @ts-ignore
|
|
|
|
+ window.event.cancelBubble = true
|
|
|
|
+ e.returnValue = false;
|
|
|
|
+ e.cancelBubble = true;
|
|
}
|
|
}
|