Skip to content

Commit 62651ad

Browse files
Jakub Bialasllorenspujol
authored andcommitted
chore(grid): added prevent collision flag
1 parent 805bb20 commit 62651ad

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

projects/angular-grid-layout/src/lib/grid.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
129129
/** Whether or not to update the internal layout when some dependent property change. */
130130
@Input() compactOnPropsChange = true;
131131

132+
/** Prevent collision, consider setting it to true if in no compaction */
133+
@Input() preventCollision: boolean = false;
134+
132135
/** Type of compaction that will be applied to the layout (vertical, horizontal or free). Defaults to 'vertical' */
133136
@Input()
134137
get compactType(): KtdGridCompactType {
@@ -184,7 +187,8 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
184187
return {
185188
cols: this.cols,
186189
rowHeight: this.rowHeight,
187-
layout: this.layout
190+
layout: this.layout,
191+
preventCollision: this.preventCollision,
188192
};
189193
}
190194

@@ -385,7 +389,8 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
385389
const {layout, draggedItemPos} = calcNewStateFunc(gridItem.id, {
386390
layout: currentLayout,
387391
rowHeight: this.rowHeight,
388-
cols: this.cols
392+
cols: this.cols,
393+
preventCollision: this.preventCollision
389394
}, this.compactType, {
390395
pointerDownEvent,
391396
pointerDragEvent,
@@ -400,7 +405,8 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
400405
this._gridItemsRenderData = layoutToRenderItems({
401406
cols: this.cols,
402407
rowHeight: this.rowHeight,
403-
layout: newLayout
408+
layout: newLayout,
409+
preventCollision: this.preventCollision,
404410
}, gridElemClientRect.width, gridElemClientRect.height);
405411

406412
const placeholderStyles = parseRenderItemToPixels(this._gridItemsRenderData[gridItem.id]);

projects/angular-grid-layout/src/lib/grid.definitions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface KtdGridCfg {
1515
cols: number;
1616
rowHeight: number; // row height in pixels
1717
layout: KtdGridLayoutItem[];
18+
preventCollision: boolean;
1819
}
1920

2021
export type KtdGridLayout = KtdGridLayoutItem[];

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { compact, CompactType, LayoutItem, moveElement } from './react-grid-layout.utils';
1+
import { compact, CompactType, getAllCollisions, LayoutItem, moveElement } from './react-grid-layout.utils';
22
import { KtdDraggingData, KtdGridCfg, KtdGridCompactType, KtdGridItemRect, KtdGridLayout, KtdGridLayoutItem } from '../grid.definitions';
33
import { ktdPointerClientX, ktdPointerClientY } from './pointer.utils';
44
import { KtdDictionary } from '../../types';
@@ -98,7 +98,7 @@ export function ktdGridItemDragging(gridItemId: string, config: KtdGridCfg, comp
9898
layoutItem.x,
9999
layoutItem.y,
100100
true,
101-
false,
101+
config.preventCollision,
102102
compactionType,
103103
config.cols
104104
);
@@ -153,6 +153,25 @@ export function ktdGridItemResizing(gridItemId: string, config: KtdGridCfg, comp
153153
layoutItem.w = Math.max(1, config.cols - layoutItem.x);
154154
}
155155

156+
if (config.preventCollision) {
157+
const collisions = getAllCollisions(config.layout, layoutItem);
158+
159+
// If we're colliding, we need adjust the placeholder.
160+
if (collisions.length > 0) {
161+
// adjust w && h to maximum allowed space
162+
let leastX = Infinity;
163+
let leastY = Infinity;
164+
collisions.forEach((collision) => {
165+
if (collision.id === layoutItem.id) { return; }
166+
if (collision.x > layoutItem.x) { leastX = Math.min(leastX, collision.x); }
167+
if (collision.y > layoutItem.y) { leastY = Math.min(leastY, collision.y); }
168+
});
169+
170+
if (Number.isFinite(leastX)) { layoutItem.w = leastX - layoutItem.x; }
171+
if (Number.isFinite(leastY)) { layoutItem.h = leastY - layoutItem.y; }
172+
}
173+
}
174+
156175
const newLayoutItems: LayoutItem[] = config.layout.map((item) => {
157176
return item.id === gridItemId ? layoutItem : item;
158177
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ <h1>Angular Grid Layout - Playground</h1>
5656
(change)="onAutoResizeChange($event.checked)">
5757
Auto resize
5858
</mat-checkbox>
59+
<mat-checkbox color="accent"
60+
[checked]="preventCollision"
61+
(change)="onPreventCollisionChange($event.checked)">
62+
Prevent Collision
63+
</mat-checkbox>
5964
<mat-chip-list>
6065
<mat-chip color="accent" [selected]="isDragging">isDragging</mat-chip>
6166
<mat-chip color="accent" [selected]="isResizing">isResizing</mat-chip>
@@ -67,6 +72,7 @@ <h1>Angular Grid Layout - Playground</h1>
6772
[rowHeight]="rowHeight"
6873
[layout]="layout"
6974
[compactType]="compactType"
75+
[preventCollision]="preventCollision"
7076
[scrollableParent]="autoScroll ? document : null"
7177
(dragStarted)="onDragStarted($event)"
7278
(resizeStarted)="onResizeStarted($event)"

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class KtdPlaygroundComponent implements OnInit, OnDestroy {
5555
disableResize = false;
5656
disableRemove = false;
5757
autoResize = true;
58+
preventCollision = false;
5859
isDragging = false;
5960
isResizing = false;
6061
resizeSubscription: Subscription;
@@ -130,6 +131,10 @@ export class KtdPlaygroundComponent implements OnInit, OnDestroy {
130131
this.autoResize = checked;
131132
}
132133

134+
onPreventCollisionChange(checked: boolean) {
135+
this.preventCollision = checked;
136+
}
137+
133138
onColsChange(event: Event) {
134139
this.cols = coerceNumberProperty((event.target as HTMLInputElement).value);
135140
}

0 commit comments

Comments
 (0)