@ticatec/uniface-flexi-module 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,13 @@ export type Dictionaries = {
5
5
  export default class FlexiContext {
6
6
  private readonly _dicLoad;
7
7
  private static instance;
8
+ private readonly language?;
8
9
  /**
9
10
  * 初始化Context
10
- * @param dictLoad
11
+ * @param dictLoad 数据字典加载函数
12
+ * @param language 当前语言
11
13
  */
12
- static initialize(dictLoad: DictionaryLoad): FlexiContext;
14
+ static initialize(dictLoad: DictionaryLoad, language?: string): FlexiContext;
13
15
  /**
14
16
  * 获取单例实例
15
17
  */
@@ -17,7 +19,8 @@ export default class FlexiContext {
17
19
  /**
18
20
  * 构造函数
19
21
  * @private
20
- * @param dictLoad
22
+ * @param dictLoad 数据字典加载函数
23
+ * @param language 当前语言
21
24
  */
22
25
  private constructor();
23
26
  /**
@@ -25,4 +28,13 @@ export default class FlexiContext {
25
28
  * @param dicNames
26
29
  */
27
30
  loadDictionaries(dicNames: Array<string> | string): Promise<Dictionaries>;
31
+ /**
32
+ * 简化多语言配置,在初始化时合并语言值和默认值
33
+ * @param config 多语言配置对象,包含 "default" 和其他语言键
34
+ * @returns 合并后的对象,当前语言的值已覆盖默认值,不存在的值使用 "Missing value" 提示
35
+ */
36
+ simplify<T extends Record<string, any>>(config: {
37
+ default: T;
38
+ [language: string]: Partial<T>;
39
+ }): T;
28
40
  }
@@ -3,15 +3,16 @@ import { registerCriteriaFieldBuilder } from "./criteria-panel";
3
3
  export default class FlexiContext {
4
4
  _dicLoad;
5
5
  static instance;
6
+ language;
6
7
  /**
7
8
  * 初始化Context
8
- * @param dictLoad
9
+ * @param dictLoad 数据字典加载函数
10
+ * @param language 当前语言
9
11
  */
10
- static initialize(dictLoad) {
12
+ static initialize(dictLoad, language) {
11
13
  if (FlexiContext.instance == null) {
12
14
  console.log('初始化FlexContext');
13
- const instance = new FlexiContext(dictLoad);
14
- FlexiContext.instance = instance;
15
+ FlexiContext.instance = new FlexiContext(dictLoad, language);
15
16
  registerFormFieldBuilder();
16
17
  registerCriteriaFieldBuilder();
17
18
  }
@@ -29,10 +30,12 @@ export default class FlexiContext {
29
30
  /**
30
31
  * 构造函数
31
32
  * @private
32
- * @param dictLoad
33
+ * @param dictLoad 数据字典加载函数
34
+ * @param language 当前语言
33
35
  */
34
- constructor(dictLoad) {
36
+ constructor(dictLoad, language) {
35
37
  this._dicLoad = dictLoad;
38
+ this.language = language;
36
39
  }
37
40
  /**
38
41
  * 读取数据字典
@@ -42,4 +45,15 @@ export default class FlexiContext {
42
45
  dicNames = Array.isArray(dicNames) ? dicNames : [dicNames];
43
46
  return dicNames.length > 0 ? await this._dicLoad(dicNames) : {};
44
47
  }
48
+ /**
49
+ * 简化多语言配置,在初始化时合并语言值和默认值
50
+ * @param config 多语言配置对象,包含 "default" 和其他语言键
51
+ * @returns 合并后的对象,当前语言的值已覆盖默认值,不存在的值使用 "Missing value" 提示
52
+ */
53
+ simplify(config) {
54
+ const defaultValues = config.default || {};
55
+ const languageValues = this.language ? (config[this.language] || {}) : {};
56
+ // 创建合并后的对象,当前语言的值覆盖默认值
57
+ return { ...defaultValues, ...languageValues };
58
+ }
45
59
  }
@@ -1,4 +1,5 @@
1
1
  import CriteriaComponentBuilder from "./CriteriaComponentBuilder";
2
+ import utils from "../../utils";
2
3
  export default class FlexiCriteriaField {
3
4
  #set;
4
5
  component;
@@ -13,7 +14,7 @@ export default class FlexiCriteriaField {
13
14
  this.#set = set;
14
15
  this.visible = schema.visible != false;
15
16
  this.size = schema.size;
16
- this.label = schema.label;
17
+ this.label = schema.label.startsWith("@") ? utils.getVariable(schema.label) : schema.label;
17
18
  if (schema.events) {
18
19
  for (let key in schema.events) {
19
20
  let eventHandler = schema.events[key];
@@ -1,4 +1,4 @@
1
- import utils from "../flexi-form/lib/utils";
1
+ import utils from "../utils";
2
2
  import FlexiContext from "..";
3
3
  export default class FlexiDataTable {
4
4
  #columns = [];
@@ -33,7 +33,7 @@ export default class FlexiDataTable {
33
33
  const render = colSchema.render ? this[colSchema.render] : null;
34
34
  const compareFunction = colSchema.compareFunction ? this[colSchema.compareFunction] : null;
35
35
  const column = {
36
- text: colSchema.text,
36
+ text: colSchema.text.startsWith("@") ? utils.getVariable(colSchema.text) : colSchema.text,
37
37
  field: colSchema.field,
38
38
  frozen: colSchema.frozen,
39
39
  align: colSchema.align,
@@ -1,9 +1,9 @@
1
1
  import FlexiCard from "../flexi_card/FlexiCard";
2
2
  import { Visibility } from "../lib";
3
3
  import ComponentBuilder from "../lib/ComponentBuilder";
4
- import utils from "../lib/utils";
4
+ import utils from "../../utils";
5
5
  import i18nRes from "../../i18n-res";
6
- import i18n, { i18nUtils } from "@ticatec/i18n";
6
+ import { i18nUtils } from "@ticatec/i18n";
7
7
  export default class FlexiField {
8
8
  #parent;
9
9
  data;
@@ -24,7 +24,7 @@ export default class FlexiField {
24
24
  constructor(parent, data, mode, schema) {
25
25
  this.#parent = parent;
26
26
  this.mode = mode;
27
- this.label = schema.label;
27
+ this.label = schema.label.startsWith("@") ? utils.getVariable(schema.label) : schema.label;
28
28
  this.variant = schema.variant;
29
29
  this.isReadonly = schema.readonly == true;
30
30
  this.disabled = schema.disabled == true;
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,12 @@
1
1
  declare const _default: {
2
+ copyAttrs: (src: any, attrs: Array<string>) => any;
3
+ extractExpression: (str: string) => string | null;
4
+ getParentAndKey: (data: any, keyPath: string) => {
5
+ parent: any;
6
+ key: string;
7
+ };
8
+ formatText: (s: string, obj: any) => string;
2
9
  propsFilter: (attrs: any, excludes?: Array<string>) => any;
10
+ getVariable: (key: string) => any;
3
11
  };
4
12
  export default _default;
package/dist/utils.js CHANGED
@@ -5,4 +5,61 @@ const propsFilter = (attrs, excludes = []) => {
5
5
  }
6
6
  return obj;
7
7
  };
8
- export default { propsFilter };
8
+ const getVariable = (key) => {
9
+ let attrs = key.split('.');
10
+ if (attrs.length == 2) {
11
+ // @ts-ignore
12
+ return window[attrs[0]]?.[attrs[1]] ?? `Missing value ${key}`;
13
+ }
14
+ else {
15
+ return `Invalid key: ${key}`;
16
+ }
17
+ };
18
+ const copyAttrs = (src, attrs) => {
19
+ let obj = {};
20
+ for (let attr of attrs) {
21
+ obj[attr] = src?.[attr];
22
+ }
23
+ return obj;
24
+ };
25
+ const extractExpression = (str) => {
26
+ const match = /^\$exp\{([^}]*)\}$/.exec(str);
27
+ return match ? match[1] : null;
28
+ };
29
+ /**
30
+ * 根据点分路径字符串获取目标属性的父对象和键名。
31
+ * 过程中如果发现父级对象为空(null 或 undefined),会自动为其赋值一个空对象 {}。
32
+ * * @param {object} data - 待查找和/或修改的原始对象。
33
+ * @param data
34
+ * @param {string} keyPath - 点分路径字符串,例如 "a.b.c"。
35
+ */
36
+ const getParentAndKey = (data, keyPath) => {
37
+ if (!keyPath || typeof keyPath !== 'string') {
38
+ throw Error('invalidate key field');
39
+ }
40
+ if (keyPath.includes('.')) {
41
+ const keys = keyPath.split('.');
42
+ const targetKey = keys.pop();
43
+ let current = data ?? {};
44
+ for (const key of keys) {
45
+ if (current[key] == null) {
46
+ current[key] = {};
47
+ }
48
+ current = current[key];
49
+ }
50
+ return {
51
+ parent: current,
52
+ key: targetKey
53
+ };
54
+ }
55
+ else {
56
+ return { parent: data, key: keyPath };
57
+ }
58
+ };
59
+ const formatText = (s, obj) => {
60
+ return s.replace(/\{\{(.*?)\}\}/g, (_, key) => {
61
+ key = key.trim();
62
+ return obj.hasOwnProperty(key) ? String(obj[key]) : `{{${key}}}`;
63
+ });
64
+ };
65
+ export default { copyAttrs, extractExpression, getParentAndKey, formatText, propsFilter, getVariable };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ticatec/uniface-flexi-module",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "A flexible form framework for Svelte applications with dynamic field types, criteria panels, and modular components",
5
5
  "keywords": [
6
6
  "svelte",
@@ -1,10 +0,0 @@
1
- declare const _default: {
2
- copyAttrs: (src: any, attrs: Array<string>) => any;
3
- extractExpression: (str: string) => string | null;
4
- getParentAndKey: (data: any, keyPath: string) => {
5
- parent: any;
6
- key: string;
7
- };
8
- formatText: (s: string, obj: any) => string;
9
- };
10
- export default _default;
@@ -1,48 +0,0 @@
1
- const copyAttrs = (src, attrs) => {
2
- let obj = {};
3
- for (let attr of attrs) {
4
- obj[attr] = src?.[attr];
5
- }
6
- return obj;
7
- };
8
- const extractExpression = (str) => {
9
- const match = /^\$exp\{([^}]*)\}$/.exec(str);
10
- return match ? match[1] : null;
11
- };
12
- /**
13
- * 根据点分路径字符串获取目标属性的父对象和键名。
14
- * 过程中如果发现父级对象为空(null 或 undefined),会自动为其赋值一个空对象 {}。
15
- * * @param {object} data - 待查找和/或修改的原始对象。
16
- * @param data
17
- * @param {string} keyPath - 点分路径字符串,例如 "a.b.c"。
18
- */
19
- const getParentAndKey = (data, keyPath) => {
20
- if (!keyPath || typeof keyPath !== 'string') {
21
- throw Error('invalidate key field');
22
- }
23
- if (keyPath.includes('.')) {
24
- const keys = keyPath.split('.');
25
- const targetKey = keys.pop();
26
- let current = data ?? {};
27
- for (const key of keys) {
28
- if (current[key] == null) {
29
- current[key] = {};
30
- }
31
- current = current[key];
32
- }
33
- return {
34
- parent: current,
35
- key: targetKey
36
- };
37
- }
38
- else {
39
- return { parent: data, key: keyPath };
40
- }
41
- };
42
- const formatText = (s, obj) => {
43
- return s.replace(/\{\{(.*?)\}\}/g, (_, key) => {
44
- key = key.trim();
45
- return obj.hasOwnProperty(key) ? String(obj[key]) : `{{${key}}}`;
46
- });
47
- };
48
- export default { copyAttrs, extractExpression, getParentAndKey, formatText };