Skip to content

Commit afc96ea

Browse files
authored
Merge pull request #218 from swiety85/98-width-height-for-different-breakpoints
feat(config): Provide support for different item width/height for different breakpoints
2 parents c066c95 + 6f95dd3 commit afc96ea

File tree

13 files changed

+302
-64
lines changed

13 files changed

+302
-64
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ gridsterOptions = {
7878
dragAndDrop: false, // possible to change items position by drag n drop
7979
resizable: false, // possible to resize items by drag n drop by item edge/corner
8080
useCSSTransforms: true, // Uses CSS3 translate() instead of position top/left - significant performance boost.
81+
responsiveSizes: true, // allow to set different item sizes for different breakpoints
8182
// ResponsiveOptions can overwrite default configuration with any option available for specific breakpoint.
8283
responsiveOptions: [
8384
{
@@ -110,7 +111,7 @@ gridsterOptions = {
110111

111112
**Warning**
112113

113-
If you use `responsiveOptions`, then coords should be assigned to specific breakpoint attributes:
114+
If you use `responsiveOptions`, then item coords will be assigned to different breakpoint attributes:
114115

115116
- till `sm` (480px), it uses `x` and `y` attributes
116117
- `sm` (480px - 768px), it uses `xSm` and `ySm` attributes
@@ -120,6 +121,14 @@ If you use `responsiveOptions`, then coords should be assigned to specific break
120121

121122
(widths in px are only example and works for `responsiveOptions in example above).
122123

124+
If you set `responsiveSizes: true`, then item sizes will be assigned to different breakpoint attributes:
125+
126+
- till `sm` (480px), it uses `w` and `h` attributes
127+
- `sm` (480px - 768px), it uses `wSm` and `hSm` attributes
128+
- `md` (768px - 1250px), it uses `wMd` and `hMd` attributes
129+
- `lg` (1250px - 1800px), it uses `wLg` and `hLg` attributes
130+
- from `xl` (1800px), it uses `wXl` and `hXl` attributes
131+
123132
## Demo
124133

125134
Clone or download this repository. Demo folder is dedicated nester project build on Angular CLI. To run:

demo/src/app/app.component.html

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747
Max item size: {{itemOptions.maxWidth}} x {{itemOptions.maxHeight}}
4848
</div>
4949
<div class="widgetbar">
50-
<div gridsterItemPrototype [config]="{helper: true}" [w]="1" [h]="1"
50+
<div gridsterItemPrototype [config]="{helper: true}"
51+
[w]="1" [h]="1"
52+
[wSm]="1" [hSm]="1"
53+
[wMd]="2" [hMd]="1"
54+
[wLg]="2" [hLg]="2"
55+
[wXl]="3" [hXl]="2"
5156
(drop)="addWidgetFromDrag(gridster, $event)"
5257
(enter)="over($event)"
5358
(out)="out($event)"
@@ -61,7 +66,8 @@ <h5 _ngcontent-uoe-2="" class="panel-title">New widget</h5>
6166
</div>
6267
</div>
6368

64-
<div gridsterItemPrototype [config]="{helper: true}" [w]="1" [h]="2"
69+
<div gridsterItemPrototype [config]="{helper: true}"
70+
[w]="2" [h]="2"
6571
(drop)="addWidgetFromDrag(gridster, $event)"
6672
(enter)="over($event)"
6773
(out)="out($event)"
@@ -75,7 +81,8 @@ <h5 _ngcontent-uoe-2="" class="panel-title">New widget</h5>
7581
</div>
7682
</div>
7783

78-
<div gridsterItemPrototype [config]="{helper: false}" [w]="2" [h]="1"
84+
<div gridsterItemPrototype [config]="{helper: false}"
85+
[w]="2" [h]="1"
7986
(drop)="addWidgetFromDrag(gridster, $event)"
8087
(enter)="over($event)"
8188
(out)="out($event)"
@@ -104,14 +111,21 @@ <h5 _ngcontent-uoe-2="" class="panel-title">New widget</h5>
104111
[(xLg)]="widget.xLg" [(yLg)]="widget.yLg"
105112
[(xXl)]="widget.xXl" [(yXl)]="widget.yXl"
106113
[(w)]="widget.w" [(h)]="widget.h"
114+
[(wSm)]="widget.wSm" [(hSm)]="widget.hSm"
115+
[(wMd)]="widget.wMd" [(hMd)]="widget.hMd"
116+
[(wLg)]="widget.wLg" [(hLg)]="widget.hLg"
117+
[(wXl)]="widget.wXl" [(hXl)]="widget.hXl"
107118
(change)="itemChange($event, gridster1)">
108119

109120
<div class="panel-heading">
110121
<h5 class="panel-title">{{ widget.title }}</h5>
111122
</div>
112123

113124
<div class="panel-body">
114-
<app-test></app-test>
125+
<p>
126+
Position: {{itemComp?.item?.x}} x {{itemComp?.item?.y}}<br>
127+
Size: {{itemComp?.item?.w}} x {{itemComp?.item?.h}}<br>
128+
</p>
115129
<p>
116130
<label>
117131
<input type="checkbox" [(ngModel)]="widget.dragAndDrop" value="true"> Draggable
@@ -127,15 +141,13 @@ <h5 class="panel-title">{{ widget.title }}</h5>
127141
<p>
128142
<button (click)="remove($event, indx,gridster1)">remove</button>
129143
</p>
130-
<p>
131-
Position: {{itemComp?.item?.x}} x {{itemComp?.item?.y}}<br>
132-
Size: {{widget.w}} x {{widget.h}}
133-
</p>
144+
<app-test></app-test>
145+
134146
<pre>{{ widget | json }}</pre>
135147

136148
</div>
137149

138150
</gridster-item>
139151

140152
</gridster>
141-
</div>
153+
</div>

demo/src/app/app.component.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ export class AppComponent implements OnInit {
2424
xl: 'yXl'
2525
};
2626

27+
static W_PROPERTY_MAP: any = {
28+
sm: 'wSm',
29+
md: 'wMd',
30+
lg: 'wLg',
31+
xl: 'wXl'
32+
};
33+
34+
static H_PROPERTY_MAP: any = {
35+
sm: 'hSm',
36+
md: 'hMd',
37+
lg: 'hLg',
38+
xl: 'hXl'
39+
};
40+
2741
@ViewChild(GridsterComponent) gridster: GridsterComponent;
2842
itemOptions = {
2943
maxWidth: 3,
@@ -51,6 +65,7 @@ export class AppComponent implements OnInit {
5165
useCSSTransforms: true,
5266
responsiveView: true, // turn on adopting items sizes on window resize and enable responsiveOptions
5367
responsiveDebounce: 500, // window resize debounce time
68+
responsiveSizes: true,
5469
// List of different gridster configurations for different breakpoints.
5570
// Each breakpoint is defined by name stored in "breakpoint" property. There is fixed set of breakpoints
5671
// available to use with default minWidth assign to each.
@@ -92,30 +107,53 @@ export class AppComponent implements OnInit {
92107
{
93108
x: 0, y: 0,
94109
w: 1, h: 2,
110+
wSm: 1, hSm: 1,
111+
wMd: 1, hMd: 2,
112+
wLg: 1, hLg: 1,
113+
wXl: 2, hXl: 2,
95114
dragAndDrop: true,
96115
resizable: true,
97116
title: 'Basic form inputs 1'
98117
},
99118
{
100-
x: 1, y: 0, w: 3, h: 1,
119+
x: 1, y: 0,
120+
w: 1, h: 2,
121+
wSm: 2, hSm: 1,
122+
wMd: 2, hMd: 1,
123+
wLg: 3, hLg: 1,
124+
wXl: 3, hXl: 1,
101125
dragAndDrop: true,
102126
resizable: true,
103127
title: 'Basic form inputs 2'
104128
},
105129
{
106-
x: 1, y: 1, w: 2, h: 1,
130+
x: 1, y: 1,
131+
w: 2, h: 1,
132+
wSm: 1, hSm: 2,
133+
wMd: 1, hMd: 2,
134+
wLg: 2, hLg: 1,
135+
wXl: 3, hXl: 1,
107136
dragAndDrop: true,
108137
resizable: true,
109138
title: 'Basic form inputs 3'
110139
},
111140
{
112-
x: 3, y: 1, w: 1, h: 2,
141+
x: 3, y: 1,
142+
w: 1, h: 2,
143+
wSm: 1, hSm: 2,
144+
wMd: 1, hMd: 2,
145+
wLg: 3, hLg: 1,
146+
wXl: 3, hXl: 1,
113147
dragAndDrop: true,
114148
resizable: true,
115149
title: 'Basic form inputs 4'
116150
},
117151
{
118152
w: 1, h: 2,
153+
wSm: 1, hSm: 2,
154+
wMd: 1, hMd: 2,
155+
wLg: 3, hLg: 1,
156+
wXl: 3, hXl: 1,
119157
dragAndDrop: true,
120158
resizable: true,
121159
title: 'Basic form inputs x'
@@ -184,14 +222,21 @@ export class AppComponent implements OnInit {
184222
const item = event.item;
185223
const breakpoint = gridster.options.breakpoint;
186224
const widget = {
187-
w: item.w, h: item.h,
188225
dragAndDrop: true,
189226
resizable: true,
190227
title: 'New widget'
191228
};
192229

193-
widget[AppComponent.X_PROPERTY_MAP[breakpoint]] = item.x;
194-
widget[AppComponent.Y_PROPERTY_MAP[breakpoint]] = item.y;
230+
widget[AppComponent.W_PROPERTY_MAP[breakpoint] || 'w'] = item.w;
231+
widget[AppComponent.H_PROPERTY_MAP[breakpoint] || 'h'] = item.h;
232+
widget[AppComponent.X_PROPERTY_MAP[breakpoint] || 'x'] = item.x;
233+
widget[AppComponent.Y_PROPERTY_MAP[breakpoint] || 'y'] = item.y;
234+
235+
for (const rwdProp of ['wSm', 'hSm', 'wMd', 'hMd', 'wLg', 'hLg', 'wXl', 'hXl']) {
236+
if (event.item.itemPrototype.hasOwnProperty(rwdProp)) {
237+
widget[rwdProp] = event.item.itemPrototype[rwdProp];
238+
}
239+
}
195240

196241
this.widgets.push(widget);
197242

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "./dist/index.js",
66
"scripts": {
77
"install-demo": "npm install --prefix ./demo",
8+
"link-demo": "ln -s $PWD/src/ $PWD/demo/src/app/gridster",
89
"serve-demo": "npm start --prefix ./demo",
910
"start": "npm start --prefix ./demo",
1011
"lint": "tslint src/**/*.ts && npm run lint --prefix ./demo",

src/GridsterOptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class GridsterOptions {
99
widthHeightRatio: number;
1010
heightToFontSizeRatio: number;
1111
responsiveView: boolean;
12+
responsiveSizes: boolean;
1213
dragAndDrop: boolean;
1314
resizable: boolean;
1415
shrink: boolean;
@@ -21,6 +22,7 @@ export class GridsterOptions {
2122
widthHeightRatio: 1,
2223
shrink: false,
2324
responsiveView: true,
25+
responsiveSizes: false,
2426
dragAndDrop: true,
2527
resizable: false,
2628
useCSSTransforms: false,

src/IGridsterOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface IGridsterOptions {
2020
floating?: boolean;
2121
responsiveView?: boolean;
2222
responsiveDebounce?: number;
23+
responsiveSizes?: boolean;
2324
lines?: {
2425
visible?: boolean,
2526
color?: string,

0 commit comments

Comments
 (0)