util.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { cloneDeep, toUpperFirstCase } from '@cip/utils/util'
  2. import { isLayoutType } from '@cip/components/cip-form-layout/util'
  3. // 允许有otherValue的字段
  4. export const twoValueComponentList = ['dateRange', 'timeRange', 'numberRange', 'resourceFormTable', 'dataDictionary', 'staff', 'roleDictionary', 'office', 'formCountersignPerson']
  5. export const threeValueComponentList = ['roleDictionary']
  6. let cacheKey = 0
  7. export const generateFieldKey = (type = 'error') => {
  8. cacheKey++ // 避免复制组的时候key id重复
  9. return `${type}_${Date.now()}${cacheKey}`
  10. }
  11. // 甚于类型的复制方式
  12. export const getCopyItem = (item) => {
  13. const result = cloneDeep(item)
  14. const type = item.config.type
  15. const sign = generateFieldKey(type)
  16. result.id = sign
  17. result.key = sign
  18. if (twoValueComponentList.includes(type)) {
  19. result.config.otherKey = `other${toUpperFirstCase(sign)}`
  20. }
  21. if (threeValueComponentList.includes(type)) {
  22. result.config.extraKey = `extra${toUpperFirstCase(sign)}`
  23. }
  24. return result
  25. }
  26. // layout 类型的复制方式
  27. export const getCopyLayout = (layout) => {
  28. const newLayout = getCopyItem(layout) // 修改自身标记
  29. newLayout.config.options.forEach(option => { // 修改子row标记
  30. const children = option.children || []
  31. if (children.length > 0) {
  32. option.children = children.map(getCopyRow)
  33. }
  34. })
  35. return newLayout
  36. }
  37. // table 的复制方式
  38. export const getCopyTable = (table) => {
  39. const newTable = getCopyItem(table) // 修改自身标记
  40. const options = newTable.config?.options || []
  41. if (options?.length > 0) {
  42. newTable.config.options = options.map(getCopyRow)
  43. }
  44. return newTable
  45. }
  46. // 复制一列
  47. export const getCopyRow = (row) => {
  48. const type = row.config?.type
  49. if (isLayoutType(type)) {
  50. return getCopyLayout(row)
  51. } else if (type === 'table') {
  52. return getCopyTable(row)
  53. } else {
  54. return getCopyItem(row)
  55. }
  56. }
  57. export const getTableItem = (item) => {
  58. const result = cloneDeep(item)
  59. const type = item.config.type
  60. result.id = item.config.key
  61. result.key = item.config.key
  62. if (twoValueComponentList.includes(type)) {
  63. result.config.otherKey = `other${toUpperFirstCase(item.config.key)}`
  64. }
  65. return result
  66. }
  67. /**
  68. * 列表设计左侧组件处理
  69. * except是排除不需要在列表中展示的组件, 格栅布局需要展开后,在最下边排除
  70. */
  71. export const except = ['image', 'file', 'table', 'resourceFormTable']
  72. // 展开格栅布局中的组件
  73. export const formConfigListFlat = (list = [], cb, formList = []) => {
  74. list.map(item => {
  75. if (isLayoutType(item.config.type)) {
  76. // eslint-disable-next-line
  77. const options = item.config.options || []
  78. const children = options.map(option => option.children).flat()
  79. formConfigListFlat(children, cb, formList)
  80. } else {
  81. if (cb) {
  82. cb(formList, item)
  83. } else {
  84. formList.push(item)
  85. }
  86. }
  87. })
  88. return formList
  89. }