grid.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import drag from "./drag";
  2. import model from "./index";
  3. class Grid {
  4. private rows = 12;
  5. private cols: number = 0;
  6. public size: number = 0;
  7. public Margin = 5;
  8. private grid: number[][] = [];
  9. public init(width: number, height: number, maxCol: number) {
  10. this.size = (height / this.rows) | 0;
  11. this.cols = Math.max((width / this.size) | 0, maxCol);
  12. drag.container!.width(this.cols * this.size);
  13. this.grid = [...Array(this.rows)].map(() => [...Array(this.cols)].fill(0));
  14. }
  15. public resize(height: number) {
  16. this.size = (height / this.rows) | 0;
  17. const width = this.cols * this.size;
  18. drag.container!.animate(
  19. {
  20. width,
  21. left: Math.min(
  22. Math.max(
  23. parseInt(drag.container!.css("left")),
  24. model.dragWrapperWidth - width
  25. ),
  26. 0
  27. )
  28. },
  29. () => model.initScroll()
  30. );
  31. }
  32. public check() {
  33. if (!drag.oSelf || !drag.model) return;
  34. const col = (parseInt(drag.oSelf.css("left")) / this.size) | 0,
  35. row = (parseInt(drag.oSelf.css("top")) / this.size) | 0;
  36. this.addCols(col - this.cols);
  37. drag.oSelf[
  38. this._check(row, col, drag.targetRowSpan, drag.targetColSpan)
  39. ? "removeClass"
  40. : "addClass"
  41. ]("err");
  42. drag.model.css({ top: row * this.size, left: col * this.size });
  43. }
  44. private _check(row: number, col: number, rowSpan: number, colSpan: number) {
  45. if (row + rowSpan > this.rows) return false;
  46. return !this.grid
  47. .filter((r, i) => i >= row && i < row + rowSpan)
  48. .map(x => x.slice(col, col + colSpan))
  49. .flat(1)
  50. .some(x => x);
  51. }
  52. public addCols(cols: number) {
  53. if (cols <= 0) return;
  54. this.grid.forEach(c => c.splice(c.length, 0, ...Array(cols).fill(0)));
  55. this.cols += cols;
  56. const xw = Math.max(this.cols * this.size - drag.container!.width()!, 0);
  57. if (!xw) return;
  58. drag.container!.css({
  59. width: drag.container!.width()! + xw,
  60. left: parseInt(drag.container!.css("left")) - xw
  61. });
  62. console.log(
  63. drag.container!.css("width"),
  64. drag.container!.width(),
  65. this.cols * this.size
  66. );
  67. }
  68. public set({ row, col, rowSpan, colSpan }: Magnet, value: number) {
  69. if (!rowSpan || !colSpan) return false;
  70. this.addCols(col + colSpan - this.cols);
  71. return this.grid
  72. .filter((r, i) => i >= row && i < row + rowSpan)
  73. .forEach(x => x.splice(col, colSpan, ...Array(colSpan).fill(value)));
  74. }
  75. public getMax(magnet: Magnet) {
  76. this.set(magnet, 0);
  77. let { row, col, rowSpan, colSpan } = magnet,
  78. xr = 4 - magnet.rowSpan,
  79. xc = 4 - magnet.colSpan;
  80. while (xr) {
  81. if (this._check(row, col, rowSpan + xr, colSpan)) break;
  82. xr--;
  83. }
  84. while (xc) {
  85. if (this._check(row, col, rowSpan, colSpan + xc)) break;
  86. xc--;
  87. }
  88. this.set(magnet, 1);
  89. return {
  90. maxRows: rowSpan + xr,
  91. maxCols: colSpan + xc
  92. };
  93. }
  94. }
  95. export default new Grid();