Skip to content

Commit 3ad2b31

Browse files
committed
fix setting marginTop to break resizing
* better fix than #1441 - thanks for suggestion! * when reisizng we incorrectly use getMargin() which is correctly 'undefined' if all 4 sides don't match (should had bee using top+bottom but actually doesn't need any margin, like horizontal) * fiuxed test cases to check for these scenarios
1 parent ca0990e commit 3ad2b31

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ Change log
4343
## 2.1.0-dev
4444

4545
- fix `class="ui-draggable-disabled ui-resizable-disabled"` have been added back to static grid items, so existing CSS rule to style continue working [1435](https://github.com/gridstack/gridstack.js/issues/1435)
46-
- add `data-gs-staticGrid` attribute
46+
- add `data-gs-static-grid` attribute
4747
- fix getting DOM element by id with number works (api that uses `GridStackElement` handle more string formats)
48+
- fix setting `marginTop` (or any 4 sides) to cause resize to break. Thanks [@deadivan](https://github.com/deadivan) for suggested fix.
4849

4950
## 2.1.0 (2020-10-28)
5051

spec/gridstack-spec.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,43 +1172,71 @@ describe('gridstack', function() {
11721172
it('should return margin', function() {
11731173
let options = {
11741174
cellHeight: 80,
1175-
margin: 10
1175+
margin: 12
11761176
};
11771177
let grid = GridStack.init(options);
1178-
let vm = grid.getMargin();
1179-
expect(vm).toBe(10);
1178+
expect(grid.getMargin()).toBe(12);
11801179
});
11811180
it('should return update margin', function() {
11821181
let options = {
11831182
cellHeight: 80,
11841183
margin: 5
11851184
};
11861185
let grid = GridStack.init(options);
1187-
grid.margin(11);
1186+
grid.margin('11rem');
11881187
expect(grid.getMargin()).toBe(11);
11891188
});
1190-
it('should do nothing', function() {
1189+
it('should change unit', function() {
11911190
let options = {
11921191
cellHeight: 80,
11931192
margin: 10,
11941193
};
11951194
let grid = GridStack.init(options);
11961195
expect(grid.getMargin()).toBe(10);
1197-
grid.margin(10);
1196+
grid.margin('10rem');
11981197
expect(grid.getMargin()).toBe(10);
11991198
});
1200-
/*
1201-
it('should not update styles', function() {
1199+
it('should not update styles, with same value', function() {
12021200
let options = {
12031201
cellHeight: 80,
12041202
margin: 5
12051203
};
12061204
let grid: any = GridStack.init(options);
1205+
expect(grid.getMargin()).toBe(5);
12071206
spyOn(grid, '_updateStyles');
1208-
grid.margin(11, false);
1207+
grid.margin('5px', false);
12091208
expect(grid._updateStyles).not.toHaveBeenCalled();
1209+
expect(grid.getMargin()).toBe(5);
1210+
});
1211+
it('should set top/bot/left value directly', function() {
1212+
let options = {
1213+
cellHeight: 80,
1214+
marginTop: 5,
1215+
marginBottom: 0,
1216+
marginLeft: 1,
1217+
};
1218+
let grid: any = GridStack.init(options);
1219+
expect(grid.getMargin()).toBe(undefined);
1220+
expect(grid.opts.marginTop).toBe(5);
1221+
expect(grid.opts.marginBottom).toBe(0);
1222+
expect(grid.opts.marginLeft).toBe(1);
1223+
expect(grid.opts.marginRight).toBe(10); // default value
1224+
});
1225+
it('should set all 4 sides, and overall margin', function() {
1226+
let options = {
1227+
cellHeight: 80,
1228+
marginTop: 5,
1229+
marginBottom: 5,
1230+
marginLeft: 5,
1231+
marginRight: 5,
1232+
};
1233+
let grid: any = GridStack.init(options);
1234+
expect(grid.getMargin()).toBe(5);
1235+
expect(grid.opts.marginTop).toBe(5);
1236+
expect(grid.opts.marginBottom).toBe(5);
1237+
expect(grid.opts.marginLeft).toBe(5);
1238+
expect(grid.opts.marginRight).toBe(5);
12101239
});
1211-
*/
12121240
});
12131241

12141242
describe('grid.opts.rtl', function() {

src/gridstack.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ export class GridStack {
10411041
return this;
10421042
}
10431043

1044-
/** returns current vertical margin value */
1044+
/** returns current margin value (undefined if all 4 sides don't match) */
10451045
public getMargin(): number { return this.opts.margin as number; }
10461046

10471047
/**
@@ -1324,7 +1324,7 @@ export class GridStack {
13241324
} else if (event.type === 'resize') {
13251325
if (x < 0) return;
13261326
width = Math.round(ui.size.width / cellWidth);
1327-
height = Math.round((ui.size.height + this.getMargin()) / cellHeight);
1327+
height = Math.round(ui.size.height / cellHeight);
13281328
}
13291329
// width and height are undefined if not resizing
13301330
let _lastTriedWidth = (width || node._lastTriedWidth);
@@ -1855,6 +1855,9 @@ export class GridStack {
18551855
delete this.opts.margin;
18561856
}
18571857
this.opts.marginUnit = data.unit; // in case side were spelled out, use those units instead...
1858+
if (this.opts.marginTop === this.opts.marginBottom && this.opts.marginLeft === this.opts.marginRight && this.opts.marginTop === this.opts.marginRight) {
1859+
this.opts.margin = this.opts.marginTop; // makes it easier to check for no-ops in setMargin()
1860+
}
18581861
return this;
18591862
}
18601863

0 commit comments

Comments
 (0)