Skip to content

Commit

Permalink
Merge pull request #1442 from adumesny/develop
Browse files Browse the repository at this point in the history
fix setting marginTop to break resizing
  • Loading branch information
adumesny authored Nov 5, 2020
2 parents dbf4bdd + 3ad2b31 commit 32bbc95
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
3 changes: 2 additions & 1 deletion doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ Change log
## 2.1.0-dev

- 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)
- add `data-gs-staticGrid` attribute
- add `data-gs-static-grid` attribute
- fix getting DOM element by id with number works (api that uses `GridStackElement` handle more string formats)
- fix setting `marginTop` (or any 4 sides) to cause resize to break. Thanks [@deadivan](https://github.com/deadivan) for suggested fix.

## 2.1.0 (2020-10-28)

Expand Down
48 changes: 38 additions & 10 deletions spec/gridstack-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1172,43 +1172,71 @@ describe('gridstack', function() {
it('should return margin', function() {
let options = {
cellHeight: 80,
margin: 10
margin: 12
};
let grid = GridStack.init(options);
let vm = grid.getMargin();
expect(vm).toBe(10);
expect(grid.getMargin()).toBe(12);
});
it('should return update margin', function() {
let options = {
cellHeight: 80,
margin: 5
};
let grid = GridStack.init(options);
grid.margin(11);
grid.margin('11rem');
expect(grid.getMargin()).toBe(11);
});
it('should do nothing', function() {
it('should change unit', function() {
let options = {
cellHeight: 80,
margin: 10,
};
let grid = GridStack.init(options);
expect(grid.getMargin()).toBe(10);
grid.margin(10);
grid.margin('10rem');
expect(grid.getMargin()).toBe(10);
});
/*
it('should not update styles', function() {
it('should not update styles, with same value', function() {
let options = {
cellHeight: 80,
margin: 5
};
let grid: any = GridStack.init(options);
expect(grid.getMargin()).toBe(5);
spyOn(grid, '_updateStyles');
grid.margin(11, false);
grid.margin('5px', false);
expect(grid._updateStyles).not.toHaveBeenCalled();
expect(grid.getMargin()).toBe(5);
});
it('should set top/bot/left value directly', function() {
let options = {
cellHeight: 80,
marginTop: 5,
marginBottom: 0,
marginLeft: 1,
};
let grid: any = GridStack.init(options);
expect(grid.getMargin()).toBe(undefined);
expect(grid.opts.marginTop).toBe(5);
expect(grid.opts.marginBottom).toBe(0);
expect(grid.opts.marginLeft).toBe(1);
expect(grid.opts.marginRight).toBe(10); // default value
});
it('should set all 4 sides, and overall margin', function() {
let options = {
cellHeight: 80,
marginTop: 5,
marginBottom: 5,
marginLeft: 5,
marginRight: 5,
};
let grid: any = GridStack.init(options);
expect(grid.getMargin()).toBe(5);
expect(grid.opts.marginTop).toBe(5);
expect(grid.opts.marginBottom).toBe(5);
expect(grid.opts.marginLeft).toBe(5);
expect(grid.opts.marginRight).toBe(5);
});
*/
});

describe('grid.opts.rtl', function() {
Expand Down
7 changes: 5 additions & 2 deletions src/gridstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ export class GridStack {
return this;
}

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

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

Expand Down

0 comments on commit 32bbc95

Please sign in to comment.