sort.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { DragactLayoutItem } from '../dragact-type'
  2. export function quickSort(a: number[]): any {
  3. return a.length <= 1
  4. ? a
  5. : quickSort(a.slice(1).filter(item => item <= a[0])).concat(
  6. a[0],
  7. quickSort(a.slice(1).filter(item => item > a[0]))
  8. )
  9. }
  10. export const sortLayout = (layout: any) => {
  11. return [].concat(layout).sort((a: any, b: any) => {
  12. if (a.GridY > b.GridY || (a.GridY === b.GridY && a.GridX > b.GridX)) {
  13. if (a.static) return 0 //为了静态,排序的时候尽量把静态的放在前面
  14. return 1
  15. } else if (a.GridY === b.GridY && a.GridX === b.GridX) {
  16. return 0
  17. }
  18. return -1
  19. })
  20. }
  21. /**
  22. * 这个函数带有记忆功能
  23. */
  24. export const getMaxContainerHeight = (function() {
  25. var lastOneYNH = 0
  26. return function(
  27. layout: DragactLayoutItem[],
  28. elementHeight = 30,
  29. elementMarginBottom = 10,
  30. currentHeight: number,
  31. useCache?: Boolean
  32. ) {
  33. if (useCache !== false) {
  34. const length = layout.length
  35. const currentLastOne = layout[length - 1]
  36. if (currentLastOne.GridY + currentLastOne.h === lastOneYNH) {
  37. return currentHeight
  38. }
  39. lastOneYNH = currentLastOne.GridY + currentLastOne.h
  40. }
  41. const ar = layout.map(item => {
  42. return item.GridY + item.h
  43. })
  44. const h = quickSort(ar)[ar.length - 1]
  45. const height =
  46. h * (elementHeight + elementMarginBottom) + elementMarginBottom
  47. return height
  48. }
  49. })()