useConditionDataManager.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import _ from "lodash";
  2. import {empty} from "@/uni_modules/uv-ui-tools/libs/function/test";
  3. import {useConditionFactory} from "@/pagesOther/components/ie-condition/useConditionFactory";
  4. export const useConditionDataManager = function (configs, eventManager, queryParams, sharedData, console) {
  5. const container = ref(new Map())
  6. const rules = ref({})
  7. watch(queryParams, async () => {
  8. console.log('dataManager begin init', new Date().getTime())
  9. const keys = Object.keys(queryParams.value)
  10. if (!keys.length) return
  11. const keysConfig = configs.map(c => c.key)
  12. const missingKeys = _.difference(keys, keysConfig)
  13. if (missingKeys.length) console.warn('missing keys: ' + missingKeys.toString())
  14. // 没有被依赖的属性需要在改变时触发查询
  15. // 依赖是在config中指定的,但是否没有被依赖需要遍历所有条件才能知道
  16. const keysNotBeDependent = [...keys]
  17. keys.forEach(key => {
  18. const config = configs.find(c => c.key == key)
  19. if (!config) return _.pull(keysNotBeDependent, key)
  20. _.pull(keysNotBeDependent, ...config.dependentKeys)
  21. const condition = useConditionFactory(config, eventManager, queryParams, sharedData, console)
  22. container.value.set(key, condition)
  23. // useConditionFactory可能会对规则进行加工
  24. if (!empty(config.rule)) rules.value[config.key] = config.rule
  25. // 必须条件立马加入workingList,防止事件过早触发
  26. if (condition.config.required) eventManager.push('dataManager init of required', key)
  27. })
  28. // 一般来说必须得有1个非被依赖项,才能构成一个响应链,否则没有机会触发onSearch
  29. // TODO:这里只是当1个警告,实际运行效果还待评估
  30. if (!keysNotBeDependent.length) console.warn('At least one key must not be dependent!')
  31. keysNotBeDependent.forEach(k => {
  32. watch(() => toValue(queryParams)[k], () => eventManager.trigger('dataManager watch not be dependent of ' + k))
  33. })
  34. }, {immediate: true})
  35. const conditions = computed(() => [...container.value.values()])
  36. const filterConditions = computed(() => conditions.value.filter(c => !c.config.hidden))
  37. const needValidation = computed(() => !empty(rules.value))
  38. const reset = function () {
  39. container.value.clear()
  40. rules.value = {}
  41. }
  42. return {
  43. container,
  44. conditions: filterConditions,
  45. rules,
  46. needValidation,
  47. reset
  48. }
  49. }