find-node.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const findChildren = (stack, data, key, value) => {
  2. let findItem = data.find(item => item[key] == value)
  3. if (findItem) {
  4. stack.push({
  5. type: findItem.type,
  6. key: findItem.key,
  7. model: findItem.model
  8. })
  9. } else {
  10. for (let i = 0; i < data.length; i++) {
  11. const item = data[i]
  12. if (item.type === 'grid') {
  13. for (let j = 0; j < item.columns.length; j++) {
  14. if (item.columns[j].list?.length) {
  15. stack.push({
  16. type: item.type,
  17. key: item.key,
  18. model: item.model
  19. })
  20. let res = findChildren([...stack], item.columns[j].list, key, value)
  21. if (res.length > stack.length) {
  22. return res
  23. }
  24. stack.pop()
  25. }
  26. }
  27. }
  28. if (item.type === 'table') {
  29. if (item.tableColumns.length) {
  30. stack.push({
  31. type: item.type,
  32. key: item.key,
  33. model: item.model
  34. })
  35. let res = findChildren([...stack], item.tableColumns, key, value)
  36. if (res.length > stack.length) {
  37. return res
  38. }
  39. stack.pop()
  40. }
  41. }
  42. if (item.type === 'subform' || item.type === 'inline' || item.type === 'dialog'
  43. || item.type === 'card' || item.type === 'group'
  44. ) {
  45. if (item.list.length) {
  46. stack.push({
  47. type: item.type,
  48. key: item.key,
  49. model: item.model
  50. })
  51. let res = findChildren([...stack], item.list, key, value)
  52. if (res.length > stack.length) {
  53. return res
  54. }
  55. stack.pop()
  56. }
  57. }
  58. if (item.type === 'tabs' || item.type === 'collapse') {
  59. for (let j = 0; j < item.tabs.length; j++) {
  60. if (item.tabs[j].list?.length) {
  61. stack.push({
  62. type: item.type,
  63. key: item.key,
  64. model: item.model
  65. })
  66. let res = findChildren([...stack], item.tabs[j].list, key, value)
  67. if (res.length > stack.length) {
  68. return res
  69. }
  70. stack.pop()
  71. }
  72. }
  73. }
  74. if (item.type === 'report') {
  75. for (let r = 0; r < item.rows.length; r++) {
  76. for (let c = 0; c < item.rows[r].columns.length; c++) {
  77. if (item.rows[r].columns[c].list?.length) {
  78. stack.push({
  79. type: item.type,
  80. key: item.key,
  81. model: item.model
  82. })
  83. let res = findChildren([...stack], item.rows[r].columns[c].list, key, value)
  84. if (res.length > stack.length) {
  85. return res
  86. }
  87. stack.pop()
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return stack
  95. }
  96. export const findModelNode = (data, key, value) => {
  97. let res = []
  98. let array = findChildren([], data, key, value)
  99. for (let i = 0; i < array.length; i++) {
  100. if (array[i].type !== 'grid'
  101. && array[i].type !== 'inline'
  102. && array[i].type !== 'card'
  103. && array[i].type !== 'tabs'
  104. && array[i].type !== 'collapse'
  105. && array[i].type !== 'report'
  106. || array.length === i + 1
  107. ) {
  108. res.push(array[i].model)
  109. }
  110. }
  111. return res
  112. }
  113. export const findModelNodeString = (data, key, value) => {
  114. let res = findModelNode(data, key, value)
  115. if (res.length > 1) {
  116. res.pop()
  117. return res.join('.') + '.'
  118. } else {
  119. return ''
  120. }
  121. }