use-field-depend.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { computed, ref, toRef, toRefs, watch } from 'vue'
  2. import { getChangeIndex, getValuesByKeys, judgeUseFn } from '@cip/components/cip-form-item/util'
  3. import { cloneDeep, getFieldValue } from '@cip/utils/util'
  4. export const useWatchFieldDepend = (props, context, { updateModelValue, updateOtherValue, clearValues }) => {
  5. const changeCount = ref(0)
  6. const {
  7. model,
  8. fieldKey
  9. } = toRefs(props)
  10. // 配置的config 注:大部分情况配置config与runningConfig一致
  11. const tableDependOnValues = toRef(props, 'tableDependOnValues')
  12. const dependOnValues = ref({})
  13. const outDependOnValues = ref({})
  14. const runningConfig = ref() // 运行时的config
  15. const securityConfig = computed(() => props.config ?? {})
  16. // watch(securityConfig, (val) => {
  17. // console.log('securityConfig change', val)
  18. // })
  19. const dependOn = computed(() => securityConfig.value.dependOn || [])
  20. const outDependOn = computed(() => securityConfig.value.outDependOn || [])
  21. // 收集依赖信息
  22. const collectDependInfo = () => {
  23. const values = getValuesByKeys(model.value, dependOn.value)
  24. const outValues = getValuesByKeys(tableDependOnValues.value, outDependOn.value)
  25. dependOnValues.value = values
  26. outDependOnValues.value = outValues
  27. }
  28. // 依赖变化时的监听
  29. const dependOnWatchCb = ({ changeKeys, changeOldValues }, executeChangeValueEffect) => {
  30. const values = dependOnValues.value
  31. const outValues = outDependOnValues.value
  32. // 获取局部effect的key
  33. const privateEffectKeys = changeKeys
  34. .map((key, index) => {
  35. if (typeof key === 'object') return { ...key, _index: index }
  36. return key
  37. }) // 塞入初始的index
  38. .filter(key => typeof key === 'object')
  39. // 获取全局effect的key
  40. const hasGlobalEffectKey = changeKeys.find(key => typeof key === 'string')
  41. // 执行全局effect的回调
  42. if (hasGlobalEffectKey) {
  43. // eslint-disable-next-line standard/no-callback-literal
  44. cb({
  45. values,
  46. outValues,
  47. keys: changeKeys,
  48. oldValues: changeOldValues,
  49. executeChangeValueEffect
  50. })
  51. }
  52. // 执行局部effect的回调
  53. privateEffectKeys.forEach(object => {
  54. const privateEffect = object.effect || {}
  55. // eslint-disable-next-line standard/no-callback-literal
  56. cb({
  57. values,
  58. outValues,
  59. keys: object.key,
  60. oldValues: changeOldValues[object._index],
  61. effect: privateEffect,
  62. executeChangeValueEffect
  63. })
  64. })
  65. // }
  66. }
  67. const cb = ({ values, outValues, keys, oldValues, effect, executeChangeValueEffect }) => {
  68. keys = [].concat(keys) // 转化为数组
  69. oldValues = [].concat(oldValues) // 转化为数组
  70. // 经支持 privateEffect
  71. if (
  72. !(keys.length === 1 && keys[0] === fieldKey.value) && // 不能只有自己变了变了
  73. oldValues.some(val => val !== undefined) && // 存在变更的依赖原始值不为undefined [1,undefined] true [undefined,undefined] false
  74. executeChangeValueEffect // model值相同
  75. ) {
  76. if (effect?.resetValue !== undefined) { // effect.resetValue的值优先级高于config.value.resetValue
  77. if (typeof effect.resetValue === 'function') {
  78. // effect.resetValue的类型为Function
  79. effect.resetValue(getFieldValue(model.value, props.fieldKey), values, outValues) && clearValues()
  80. } else {
  81. effect.resetValue && clearValues()
  82. }
  83. } else if (securityConfig.value.resetValue) {
  84. clearValues()
  85. }
  86. }
  87. // immediateChangeValue 覆盖掉 executeChangeValueEffect
  88. if (!props.readonly && (securityConfig.value.immediateChangeValue || executeChangeValueEffect)) {
  89. // changeValue && changeValueByOld 仅在非readonly且model相等下生效
  90. const changeValueCb = judgeUseFn('changeValue', securityConfig.value, effect)
  91. if (typeof changeValueCb === 'function') changeValue(changeValueCb, values, outValues)
  92. const changeValueByOldCb = judgeUseFn('changeValueByOld', securityConfig.value, effect)
  93. if (typeof changeValueByOldCb === 'function') { // 不合并
  94. keys.forEach((key, i) => {
  95. const oldValue = oldValues[i]
  96. if (typeof key === 'object') {
  97. key = key.key
  98. }
  99. changeValueByOld(changeValueByOldCb, { key, oldValue }, values, outValues)
  100. })
  101. }
  102. }
  103. const changeConfigCb = judgeUseFn('changeConfig', securityConfig.value, effect)
  104. if (typeof changeConfigCb === 'function') changeConfig(changeConfigCb, values, outValues)
  105. }
  106. // 变更字段config的方式
  107. const changeConfig = async (cb, values, outValues) => {
  108. runningConfig.value = await cb(cloneDeep(securityConfig.value), values, outValues)
  109. }
  110. const changeValue = async (cb, values, outValues) => {
  111. const data = await cb(values, outValues)
  112. if (data) {
  113. const { value, otherValue } = data
  114. updateModelValue(value)
  115. updateOtherValue(otherValue)
  116. }
  117. }
  118. const changeValueByOld = async (cb, { key, oldValue }, values, outValues) => {
  119. // eslint-disable-next-line standard/no-callback-literal
  120. const data = await cb({ key, oldValue }, values, outValues)
  121. if (data !== undefined) {
  122. const { value, otherValue } = data
  123. updateModelValue(value)
  124. updateOtherValue(otherValue)
  125. }
  126. }
  127. const watchValue = (target, dependOn) => dependOn.map(key => {
  128. if (typeof key === 'object') key = key.key
  129. return () => getFieldValue(target.value, key)
  130. })
  131. const generateWatchValue = () => {
  132. let result = watchValue(model, dependOn.value)
  133. if (props.inTable) {
  134. result = result.concat(watchValue(tableDependOnValues, outDependOn.value))
  135. }
  136. return result
  137. }
  138. const getChange = (values, oldValues, depend) => { // 转为纯函数
  139. const changeIndex = getChangeIndex(values, oldValues)
  140. const changeValue = changeIndex.map(index => values[index])
  141. const changeOldValues = changeIndex.map(index => oldValues[index])
  142. const changeKeys = changeIndex.map(index => depend[index]) // 此处depend为函数私有
  143. return { changeValue, changeOldValues, changeKeys }
  144. }
  145. const depend = props.inTable ? dependOn.value.concat(outDependOn.value) : dependOn.value
  146. // 值变化 [reset changeValue changValueByOld]
  147. // 配置变化 [changeConfig]
  148. // 更换对象: 依赖收集 触发配置变化 不触发值变化
  149. // 未更换对象: 依赖收集 触发配置变化 触发值变化
  150. if (depend.length > 0) {
  151. let unwatch
  152. watch(model, () => {
  153. changeCount.value++ // 增加一个model变化计数器
  154. // 重新开启监听
  155. if (unwatch) unwatch() // model变化时重新开启监听
  156. let firstChange = true
  157. unwatch = watch(generateWatchValue(dependOn, outDependOn), (values, oldValues) => {
  158. const change = getChange(values, oldValues, depend)
  159. // 相同的对象 判断存在数据变化才触发依赖值更新
  160. collectDependInfo()
  161. dependOnWatchCb(change, !firstChange)
  162. firstChange = false
  163. }, { deep: true, immediate: true })
  164. }, { immediate: true })
  165. }
  166. return {
  167. changeCount, // model变化总计
  168. clearValues,
  169. dependOnValues,
  170. outDependOnValues,
  171. runningConfig
  172. }
  173. }