Skip to content

Commit 06051f6

Browse files
llorenspujolrishabhmhjnrajan-chavda
committed
feat: added Grid support for min and max sizes on grid items
Added support to set minW, minH, maxW and maxH on layout grid items. Co-Authored-By: Rishabh Mahajan <rishabh@statusbrew.com> Co-Authored-By: rajan-chavda <60097693+rajan-chavda@users.noreply.github.com>
1 parent a51cc26 commit 06051f6

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ rowHeight: number = 100;
7070
layout: KtdGridLayout = [
7171
{id: '0', x: 0, y: 0, w: 3, h: 3},
7272
{id: '1', x: 3, y: 0, w: 3, h: 3},
73-
{id: '2', x: 0, y: 3, w: 3, h: 3},
74-
{id: '3', x: 3, y: 3, w: 3, h: 3},
73+
{id: '2', x: 0, y: 3, w: 3, h: 3, minW: 2, minH: 3},
74+
{id: '3', x: 3, y: 3, w: 3, h: 3, minW: 2, maxW: 3, minH: 2, maxH: 5},
7575
];
7676
trackById = ktdTrackById
7777
```
@@ -152,10 +152,10 @@ Here is listed the basic API of both KtdGridComponent and KtdGridItemComponent.
152152
- [x] Add Real life example with charts and grid items with some kind of controls.
153153
- [x] Add dragStartThreshold option to grid items.
154154
- [x] Auto Scroll vertical/horizontal if container is scrollable when dragging a grid item. ([commit](https://github.com/katoid/angular-grid-layout/commit/d137d0e3f40cafdb5fdfd7b2bce4286670200c5d)).
155+
- [x] Grid support for minWidth/maxWidth and minHeight/maxHeight on grid items.
155156
- [ ] Add grid gap feature.
156157
- [ ] rowHeight to support also 'fit' as value instead of only CSS pixels ([issue](https://github.com/katoid/angular-grid-layout/issues/1)).
157158
- [ ] Grid support for static grid items.
158-
- [ ] Grid support for minWidth and minHeight on grid items.
159159
- [ ] Customizable drag placeholder.
160160
- [ ] Check grid compact horizontal algorithm, estrange behaviour when overflowing, also in react-grid-layout.
161161
- [ ] Add all other resize options (now is only available 'se-resize').

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,18 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
459459
this.renderer.removeChild(this.elementRef.nativeElement, placeholderElement);
460460

461461
if (newLayout) {
462+
// TODO: newLayout should already be pruned. If not, it should have type Layout, not KtdGridLayout as it is now.
462463
// Prune react-grid-layout compact extra properties.
463464
observer.next(newLayout.map(item => ({
464465
id: item.id,
465466
x: item.x,
466467
y: item.y,
467468
w: item.w,
468-
h: item.h
469+
h: item.h,
470+
minW: item.minW,
471+
minH: item.minH,
472+
maxW: item.maxW,
473+
maxH: item.maxH,
469474
})) as KtdGridLayout);
470475
} else {
471476
// TODO: Need we really to emit if there is no layout change but drag started and ended?

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface KtdGridLayoutItem {
77
y: number;
88
w: number;
99
h: number;
10+
minW?: number;
11+
minH?: number;
12+
maxW?: number;
13+
maxH?: number;
1014
}
1115

1216
export type KtdGridCompactType = CompactType;

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function ktdTrackById(index: number, item: {id: string}) {
1717
export function ktdGridCompact(layout: KtdGridLayout, compactType: KtdGridCompactType, cols: number): KtdGridLayout {
1818
return compact(layout, compactType, cols)
1919
// Prune react-grid-layout compact extra properties.
20-
.map(item => ({id: item.id, x: item.x, y: item.y, w: item.w, h: item.h}));
20+
.map(item => ({ id: item.id, x: item.x, y: item.y, w: item.w, h: item.h, minW: item.minW, minH: item.minH, maxW: item.maxW, maxH: item.maxH }));
2121
}
2222

2323
function screenXPosToGridValue(screenXPos: number, cols: number, width: number): number {
@@ -147,8 +147,9 @@ export function ktdGridItemResizing(gridItemId: string, config: KtdGridCfg, comp
147147
h: screenYPosToGridValue(height, config.rowHeight, gridElemClientRect.height)
148148
};
149149

150-
layoutItem.w = Math.max(1, layoutItem.w);
151-
layoutItem.h = Math.max(1, layoutItem.h);
150+
layoutItem.w = limitNumberWithinRange(layoutItem.w, layoutItem.minW, layoutItem.maxW);
151+
layoutItem.h = limitNumberWithinRange(layoutItem.h, layoutItem.minH, layoutItem.maxH);
152+
152153
if (layoutItem.x + layoutItem.w > config.cols) {
153154
layoutItem.w = Math.max(1, config.cols - layoutItem.x);
154155
}
@@ -216,3 +217,13 @@ function getDimensionToShrink(layoutItem, lastShrunk): 'w' | 'h' {
216217

217218
return lastShrunk === 'w' ? 'h' : 'w';
218219
}
220+
221+
/**
222+
* Given the current number and min/max values, returns the number within the range
223+
* @param number can be any numeric value
224+
* @param min minimum value of range
225+
* @param max maximum value of range
226+
*/
227+
function limitNumberWithinRange(num: number, min: number = 1, max: number = Infinity) {
228+
return Math.min(Math.max(num, min < 1 ? 1 : min), max);
229+
}

projects/demo-app/src/app/real-life-example/real-life-example.component.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export class KtdRealLifeExampleComponent implements OnInit, OnDestroy {
1919
cols = 12;
2020
rowHeight = 50;
2121
compactType: 'vertical' | 'horizontal' | null = 'vertical';
22-
layout = [
23-
{id: '0', x: 0, y: 5, w: 4, h: 10},
24-
{id: '1', x: 4, y: 5, w: 4, h: 10},
25-
{id: '2', x: 2, y: 0, w: 6, h: 5},
26-
{id: '5', x: 8, y: 0, w: 4, h: 5},
27-
{id: '3', x: 0, y: 0, w: 2, h: 5},
28-
{id: '4', x: 8, y: 5, w: 4, h: 10}
22+
layout: KtdGridLayout = [
23+
{id: '0', x: 0, y: 5, w: 4, h: 10, minW: 2, minH: 5},
24+
{id: '1', x: 4, y: 5, w: 4, h: 10, minW: 2, minH: 5},
25+
{id: '2', x: 2, y: 0, w: 6, h: 5, minW: 4, minH: 4, maxW: 8, maxH: 14},
26+
{id: '5', x: 8, y: 0, w: 4, h: 5, minW: 2, minH: 3},
27+
{id: '3', x: 0, y: 0, w: 2, h: 5, minH: 3},
28+
{id: '4', x: 8, y: 5, w: 4, h: 10, minH: 5, maxH: 12}
2929
];
3030

3131
layoutSizes: {[id: string]: [number, number]} = {};

0 commit comments

Comments
 (0)