localPositionUtil.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. export function initializePosition (items) {
  21. return items.map((i) => ({
  22. x: i.x,
  23. y: i.y,
  24. w: i.width,
  25. h: i.height,
  26. i: `${i.id}`
  27. }))
  28. }
  29. export function changePosition (prev, current) {
  30. current.forEach((item, index) => {
  31. const prevItem = prev[index]
  32. prevItem.x = item.x
  33. prevItem.y = item.y
  34. if (prevItem.w !== item.w || prevItem.h !== item.h) {
  35. prevItem.w = item.w
  36. prevItem.h = item.h
  37. }
  38. })
  39. return prev
  40. }
  41. export function diffPosition (origin, current) {
  42. let sign = false
  43. for (let i = 0, cl = current.length; i < cl; i += 1) {
  44. const oItem = origin[i]
  45. const cItem = current[i]
  46. if (oItem.x !== cItem.x ||
  47. oItem.y !== cItem.y ||
  48. oItem.w !== cItem.w ||
  49. oItem.h !== cItem.h) {
  50. sign = true
  51. break
  52. }
  53. }
  54. return sign
  55. }
  56. // list 转成树形json
  57. export function listToTree (list, parentId) {
  58. const ret = []
  59. for (const i in list) {
  60. if (list[i].parentId === parentId) {
  61. const leftList = list.filter((l) => l.id !== list[i].id)
  62. list[i].children = listToTree(leftList, list[i].id)
  63. ret.push(list[i])
  64. }
  65. }
  66. return ret
  67. }
  68. // 获取第一个dashboard的id
  69. export function findFirstLeaf (tree) {
  70. if (tree.children.length === 0) {
  71. return -1
  72. }
  73. for (const child of tree.children) {
  74. if (child.type === 1) {
  75. return child.id
  76. } else {
  77. const leafId = findFirstLeaf(child)
  78. if (leafId > 0) {
  79. return leafId
  80. }
  81. }
  82. }
  83. return -1
  84. }