Skip to content

Commit 5420a6f

Browse files
committed
fix(grid): fixed compact horizontal method not moving items to the left sometimes
Compact horizontal method was not moving grid items as left as possible in cases where those items where overflowing to the right. Right now this is fixed, and compact horizontal puts overflowing-right elements as bottom as needed without colliding and as left as possible fix #21
1 parent 806a2f8 commit 5420a6f

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

projects/angular-grid-layout/src/lib/utils/react-grid-layout.utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ export function compactItem(
274274
if (compactH && l.x + l.w > cols) {
275275
l.x = cols - l.w;
276276
l.y++;
277+
278+
// ALso move element as left as much as we can (ktd-custom-change)
279+
while (l.x > 0 && !getFirstCollision(compareWith, l)) {
280+
l.x--;
281+
}
277282
}
278283
}
279284

projects/angular-grid-layout/src/lib/utils/tests/grid.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,38 @@ describe('compact (custom tests)', () => {
6161
{y: 10, x: 0, h: 2, w: 1, id: '2', moved: false, static: false}
6262
]);
6363
});
64+
65+
it('compact horizontal should move all the elements to the left as much as possible', () => {
66+
const cols = 6
67+
const layout = [
68+
{y: 0, x: 0, h: 2, w: 2, id: '1'},
69+
{y: 0, x: 2, h: 2, w: 2, id: '2'},
70+
{y: 0, x: 4, h: 2, w: 2, id: '3'},
71+
{y: 2, x: 4, h: 2, w: 2, id: '4'}
72+
];
73+
74+
expect(compact(layout, 'horizontal', cols)).toEqual([
75+
{y: 0, x: 0, h: 2, w: 2, id: '1', moved: false, static: false},
76+
{y: 0, x: 2, h: 2, w: 2, id: '2', moved: false, static: false},
77+
{y: 0, x: 4, h: 2, w: 2, id: '3', moved: false, static: false},
78+
{y: 2, x: 0, h: 2, w: 2, id: '4', moved: false, static: false}
79+
]);
80+
});
81+
82+
it('compact horizontal should put overflowing-right elements as bottom needed without colliding and as left as possible', () => {
83+
const cols = 6
84+
const layout = [
85+
{y: 0, x: 0, h: 2, w: 2, id: '1'},
86+
{y: 0, x: 2, h: 2, w: 2, id: '2'},
87+
{y: 0, x: 4, h: 2, w: 2, id: '3'},
88+
{y: -2, x: -2, h: 2, w: 2, id: '4'}
89+
];
90+
91+
expect(compact(layout, 'horizontal', cols)).toEqual([
92+
{y: 0, x: 2, h: 2, w: 2, id: '1', moved: false, static: false},
93+
{y: 0, x: 4, h: 2, w: 2, id: '2', moved: false, static: false},
94+
{y: 2, x: 0, h: 2, w: 2, id: '3', moved: false, static: false},
95+
{y: 0, x: 0, h: 2, w: 2, id: '4', moved: false, static: false},
96+
]);
97+
});
6498
})

projects/demo-app/src/app/playground/playground.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ export class KtdPlaygroundComponent implements OnInit, OnDestroy {
118118
onCompactTypeChange(change: MatSelectChange) {
119119
console.log('onCompactTypeChange', change);
120120
this.compactType = change.value;
121-
this.layout = ktdGridCompact(this.layout, this.compactType, this.cols);
122121
}
123122

124123
onTransitionChange(change: MatSelectChange) {

0 commit comments

Comments
 (0)