resolver-egretimp-plus 0.1.124 → 0.1.125

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.
@@ -0,0 +1,177 @@
1
+ import { toRaw } from "vue"
2
+ import { calcDisable } from "./const"
3
+ import { getFormItemRule, isCycleConfig } from "./render"
4
+ import AsyncValidator from 'async-validator'
5
+
6
+ export async function toValidate({
7
+ rootConfig, rootValue, mode, lang, messageInstance,
8
+ formRef, cb, dynamicMapComp
9
+ }) {
10
+ const flag = await validateConfig({
11
+ validConfig: rootConfig,
12
+ mode,
13
+ lang,
14
+ rootValue,
15
+ dynamicMapComp,
16
+ messageInstance
17
+ })
18
+ if (!flag) {
19
+ if (!cb) {
20
+ return formRef.value && formRef.value.validate().catch(err => {
21
+ validFailAction(err, dynamicMapComp)
22
+ return Promise.reject(err)
23
+ })
24
+ }
25
+ return formRef.value && formRef.value.validate((valid, errInfo) => {
26
+ errInfo && validFailAction(errInfo, dynamicMapComp)
27
+ cb && cb(valid, errInfo)
28
+ })
29
+ } else {
30
+ if (!cb) {
31
+ return Promise.resolve(true)
32
+ }
33
+ return cb?.(true)
34
+ }
35
+ }
36
+
37
+ // 校验的一些附加动作, 例如校验失败,tab需要聚焦到失败tab页面,组件失败,需要展开
38
+ function validFailAction(errInfo, dynamicMapComp) {
39
+ const errKyes = Object.keys(errInfo)
40
+ if (errKyes.length) {
41
+ let oneKey = errKyes[0]
42
+ const referComps = []
43
+ while (oneKey.indexOf('->') > -1) {
44
+ let idx = -1
45
+ let code = ''
46
+ const matchs = oneKey?.match(/^(.+)\[(\d+)\]$/)
47
+ if (matchs) {
48
+ idx = parseInt(matchs[2])
49
+ code = matchs[1]
50
+ } else {
51
+ code = oneKey
52
+ }
53
+
54
+ const compInfo = dynamicMapComp[code]
55
+ compInfo && referComps.unshift({
56
+ compInfo,
57
+ cycleIdx: idx
58
+ })
59
+ oneKey = oneKey?.split('->')?.slice(0, -1)?.join('->') || ''
60
+ }
61
+ const compInfo = dynamicMapComp[oneKey]
62
+ compInfo && referComps.unshift({
63
+ compInfo,
64
+ cycleIdx: -1
65
+ })
66
+ referComps.forEach(({compInfo, cycleIdx}) => {
67
+ validFail(compInfo, cycleIdx)
68
+ })
69
+ // 组件对应的页面展示更新后才能获取到校验住的元素
70
+ setTimeout(() => {
71
+ const {compInfo: config} = referComps.pop()
72
+ const veiwEl = config?.wrapVm?.vnode?.el
73
+ veiwEl && veiwEl.scrollIntoView({ behavior: 'smooth' })
74
+
75
+ // const fouceErrEle = document.querySelector('.el-form-item__error')
76
+ // const fouceEle = fouceErrEle.parentElement
77
+ // fouceEle && fouceEle.scrollIntoView({ behavior: 'smooth' })
78
+ }, 200)
79
+ }
80
+ }
81
+ // 校验不通过的时候,需要进行一下组件配置的调整,
82
+ // 标签组件,需要进行聚焦当前页面
83
+ // component组件,需要打开折叠
84
+ function validFail(config, cycleIdx) {
85
+ const type = config.renderby || config.metaType
86
+ switch (type) {
87
+ case 'CustomComponentTabPane':
88
+ case 'CustomComponentCycleTabPane':
89
+ config.parent && config.parent.pmPageMetaList && config.parent.pmPageMetaList.forEach(tab => {
90
+ if (tab.metaType === 'CustomComponentTabPane') {
91
+ tab.defaultShowFlag = '0'
92
+ }
93
+ if (tab.metaType === 'CustomComponentCycleTabPane' || tab?.isCycle == '1') {
94
+ tab.currentCode = ''
95
+ }
96
+ })
97
+ if (type === 'CustomComponentTabPane' && config?.isCycle != '1') {
98
+ config.defaultShowFlag = '1'
99
+ }
100
+ if (type === 'CustomComponentCycleTabPane' || config?.isCycle == '1') {
101
+ config.currentCode = `${config.metaCode}-${cycleIdx}`
102
+ }
103
+ break
104
+ case 'CustomComponentCollapse':
105
+ config.defaultOpenFlag = '1'
106
+ break
107
+ case 'CustomComponentDialog':
108
+ config.dialogVisible = true
109
+ break
110
+ default:
111
+ }
112
+ }
113
+
114
+ function validateConfig({
115
+ validConfig,
116
+ mode,
117
+ lang,
118
+ rootValue,
119
+ dynamicMapComp,
120
+ messageInstance
121
+ }) {
122
+ return travelConfig(validConfig, (config) => {
123
+ const compProps = {
124
+ disabled: calcDisable(config, mode)
125
+ }
126
+ const params = {
127
+ lang,
128
+ rootValue,
129
+ dynamicMapComp,
130
+ props: {
131
+ config: config,
132
+ modelValue: toRaw(config?.refValue),
133
+ rowScope: config.rowScope,
134
+ mode: mode
135
+ },
136
+ messageInstance
137
+ }
138
+ const rules = getFormItemRule(config, lang, params, compProps, true)
139
+ if (rules && rules.length) {
140
+ const validator = new AsyncValidator({
141
+ [config.dynamicHireRelat]: rules
142
+ })
143
+
144
+ async function toValidate() {
145
+ try {
146
+ await validator.validate({[config.dynamicHireRelat]: toRaw(config?.refValue)})
147
+ } catch (error) {
148
+ validFailAction({[config.dynamicHireRelat]: true}, dynamicMapComp)
149
+ return Promise.reject()
150
+ }
151
+ return true
152
+ }
153
+ return toValidate()
154
+ }
155
+ })
156
+ }
157
+
158
+ async function travelConfig(configs, cb) {
159
+ let travseConfigs = Array.isArray(configs) ? configs : [configs]
160
+ let currentConfig = null
161
+ while(travseConfigs.length) {
162
+ currentConfig = travseConfigs.shift()
163
+ try {
164
+ await cb?.(currentConfig)
165
+ } catch (error) {
166
+ return false
167
+ }
168
+ if (isCycleConfig(currentConfig)) {
169
+ (currentConfig.multiPmPageMetaList || []).forEach(pmPageMetaList => {
170
+ [].push.apply(travseConfigs, pmPageMetaList)
171
+ })
172
+ } else if (currentConfig?.pmPageMetaList?.length) {
173
+ [].push.apply(travseConfigs, currentConfig.pmPageMetaList)
174
+ }
175
+ }
176
+ return true
177
+ }