Skip to content

Commit d745eb6

Browse files
author
Alain Dumesny
authored
Merge pull request #1057 from adumesny/develop
renamed setGridWidth() to setColumn()
2 parents 7ca8a49 + 1ea827f commit d745eb6

File tree

7 files changed

+160
-32
lines changed

7 files changed

+160
-32
lines changed

demo/column.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<!--[if lt IE 9]>
5+
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
6+
<![endif]-->
7+
8+
<meta charset="utf-8">
9+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
10+
<meta name="viewport" content="width=device-width, initial-scale=1">
11+
<title>Column grid demo</title>
12+
13+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
14+
<link rel="stylesheet" href="../dist/gridstack.css"/>
15+
16+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
17+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
18+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
19+
<script src="../src/gridstack.js"></script>
20+
<script src="../src/gridstack.jQueryUI.js"></script>
21+
22+
<style type="text/css">
23+
.grid-stack {
24+
background: lightgoldenrodyellow;
25+
}
26+
27+
.grid-stack-item-content {
28+
color: #2c3e50;
29+
text-align: center;
30+
background-color: #18bc9c;
31+
}
32+
</style>
33+
</head>
34+
<body>
35+
<div class="container-fluid">
36+
<h1>setColumn() grid demo</h1>
37+
<div>
38+
<a class="btn btn-primary" id="1column" href="#">1 Column</a>
39+
</div>
40+
<br><br>
41+
<div class="grid-stack"></div>
42+
</div>
43+
44+
45+
<script type="text/javascript">
46+
$(function () {
47+
$('.grid-stack').gridstack();
48+
var self = this;
49+
this.grid = $('.grid-stack').data('gridstack');
50+
51+
var items = [
52+
{x: 0, y: 0, width: 4, height: 2},
53+
{x: 4, y: 0, width: 4, height: 1},
54+
];
55+
items.forEach(function (node, i) {
56+
self.grid.addWidget($('<div><div class="grid-stack-item-content">' + i + '</div></div>'), node);
57+
});
58+
59+
$('#1column').click(function() {
60+
oneColumn();
61+
});
62+
63+
function oneColumn() {
64+
self.grid.setColumn(1);
65+
}
66+
});
67+
</script>
68+
</body>
69+
</html>

demo/responsive.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ <h1>Responsive grid demo</h1>
7272
if (isBreakpoint('xs')) {
7373
$('#grid-size').text('One column mode');
7474
} else if (isBreakpoint('sm')) {
75-
grid.setGridWidth(3);
75+
grid.setColumn(3);
7676
$('#grid-size').text(3);
7777
} else if (isBreakpoint('md')) {
78-
grid.setGridWidth(6);
78+
grid.setColumn(6);
7979
$('#grid-size').text(6);
8080
} else if (isBreakpoint('lg')) {
81-
grid.setGridWidth(12);
81+
grid.setColumn(12);
8282
$('#grid-size').text(12);
8383
}
8484
};

doc/CHANGES.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ Change log
2424

2525
## v0.5.2-dev (upcoming changes)
2626

27-
- grid options `width` is now `column`, and `height` is now `maxRow` which match what they are. Old names are still supported for now (with console warnings). Also various fixes for custom # of column and re-wrote entire doc section ([#1053](https://github.com/gridstack/gridstack.js/issues/1053)).
27+
- grid options `width` is now `column`, `height` now `maxRow`, and `setGridWidth()` now `setColumn()` to match what they are. Old names are still supported (console warnings). Various fixes for custom # of column and re-wrote entire doc section ([#1053](https://github.com/gridstack/gridstack.js/issues/1053)).
2828
- fix widgets not animating when animate: true is used. on every move, styles were recreated-fix should slightly improve gridstack.js speed ([#937](https://github.com/gridstack/gridstack.js/issues/937)).
2929
- fix moving widgets when having multiple grids. jquery-ui workaround ([#1043](https://github.com/gridstack/gridstack.js/issues/1043)).
3030
- switch to eslint ([#763](https://github.com/gridstack/gridstack.js/issues/763)).
31-
- null values to addWidget() exception fix ([#1042](https://github.com/gridstack/gridstack.js/issues/1042)).
31+
- fix null values `addWidget()` options ([#1042](https://github.com/gridstack/gridstack.js/issues/1042)).
3232

3333
## v0.5.2 (2019-11-13)
3434

35-
- undefined x,y position messes up grid ([#1017](https://github.com/gridstack/gridstack.js/issues/1017)).
35+
- undefined `x,y` position messes up grid ([#1017](https://github.com/gridstack/gridstack.js/issues/1017)).
3636
- changed code to 2 spaces.
3737
- fix minHeight during `onStartMoving()` ([#999](https://github.com/gridstack/gridstack.js/issues/999)).
3838
- TypeScript definition file now included - no need to include @types/gridstack, easier to update ([#1036](https://github.com/gridstack/gridstack.js/pull/1036)).
39-
- new addWidget(el, options) to pass object so you don't have to spell 10 params. ([#907](https://github.com/gridstack/gridstack.js/issues/907)).
39+
- new `addWidget(el, options)` to pass object so you don't have to spell 10 params. ([#907](https://github.com/gridstack/gridstack.js/issues/907)).
4040

4141
## v0.5.1 (2019-11-07)
4242

@@ -57,11 +57,11 @@ Change log
5757
## v0.4.0 (2018-05-11)
5858

5959
- widgets can have their own resize handles. Use `data-gs-resize-handles` element attribute to use. For example, `data-gs-resize-handles="e,w"` will make the particular widget only resize west and east. ([#494](https://github.com/gridstack/gridstack.js/issues/494)).
60-
- enable sidebar items to be duplicated properly. Pass `helper: 'clone'` in `draggable` options. ([#661](https://github.com/gridstack/gridstack.js/issues/661), ([#396](https://github.com/gridstack/gridstack.js/issues/396), ([#499](https://github.com/gridstack/gridstack.js/issues/499)).
60+
- enable sidebar items to be duplicated properly. Pass `helper: 'clone'` in `draggable` options. ([#661](https://github.com/gridstack/gridstack.js/issues/661), [#396](https://github.com/gridstack/gridstack.js/issues/396), [#499](https://github.com/gridstack/gridstack.js/issues/499)).
6161
- fix `staticGrid` grid option ([#743](https://github.com/gridstack/gridstack.js/issues/743))
6262
- preserve inline styles when moving/cloning items (thanks [@silverwind](https://github.com/silverwind))
6363
- fix bug causing heights not to get set ([#744](https://github.com/gridstack/gridstack.js/issues/744))
64-
- allow grid to have min-height, fixes ([#628](https://github.com/gridstack/gridstack.js/issues/628)) (thanks [@adumesny](https://github.com/adumesny))
64+
- allow grid to have min-height, fixes ([#628](https://github.com/gridstack/gridstack.js/issues/628)) thanks [@adumesny](https://github.com/adumesny)
6565
- widget x and y are now ints (thanks [@DonnchaC](https://github.com/donnchac))
6666
- allow all droppable options (thanks [@vigor-vlad](https://github.com/vigor-vlad))
6767
- properly track mouse position in `getCellFromPixel` (thanks [@aletorrado](https://github.com/aletorrado))
@@ -74,7 +74,7 @@ Change log
7474
- prevent extra checks for removing widget when dragging off grid.
7575
- trigger `added` when a widget is added via dropping from one grid to another.
7676
- trigger `removed` when a widget is removed via dropping from one grid to another.
77-
- trigger `removed` when a widget is removed via dropping on a removable zone ([#607](https://github.com/gridstack/gridstack.js/issues/607) and [#550])(https://github.com/gridstack/gridstack.js/issues/550)).
77+
- trigger `removed` when a widget is removed via dropping on a removable zone ([#607](https://github.com/gridstack/gridstack.js/issues/607) and [#550](https://github.com/gridstack/gridstack.js/issues/550)).
7878
- trigger custom event for `resizestop` called `gsresizestop` ([#577](https://github.com/gridstack/gridstack.js/issues/577) and [#398](https://github.com/gridstack/gridstack.js/issues/398)).
7979
- prevent dragging/resizing in `oneColumnMode` ([#593](https://github.com/gridstack/gridstack.js/issues/593)).
8080
- add `oneColumnModeClass` option to grid.
@@ -99,7 +99,7 @@ Change log
9999
- `cellHeight` and `verticalMargin` can now be string (e.g. '3em', '20px') (Thanks to @jlowcs).
100100
- add `maxWidth`/`maxHeight` methods.
101101
- add `enableMove`/`enableResize` methods.
102-
- fix window resize issue #331.
102+
- fix window resize issue [#331](https://github.com/gridstack/gridstack.js/issues/331)).
103103
- add options `disableDrag` and `disableResize`.
104104
- fix `batchUpdate`/`commit` (Thank to @radiolips)
105105
- remove dependency of FontAwesome
@@ -147,16 +147,16 @@ Change log
147147

148148
- fix grid initialization
149149
- add `cell_height`/`cell_width` API methods
150-
- fix boolean attributes (issue #31)
150+
- fix boolean attributes ([#31](https://github.com/gridstack/gridstack.js/issues/31))
151151

152152
## v0.2.1 (2014-12-09)
153153

154-
- add widgets locking (issue #19)
154+
- add widgets locking ([#19](https://github.com/gridstack/gridstack.js/issues/19))
155155
- add `will_it_fit` API method
156-
- fix auto-positioning (issue #20)
156+
- fix auto-positioning ([#20](https://github.com/gridstack/gridstack.js/issues/20))
157157
- add animation (thanks to @ishields)
158-
- fix `y` coordinate calculation when dragging (issue #18)
159-
- fix `remove_widget` (issue #16)
158+
- fix `y` coordinate calculation when dragging ([#18](https://github.com/gridstack/gridstack.js/issues/18))
159+
- fix `remove_widget` ([#16](https://github.com/gridstack/gridstack.js/issues/16))
160160
- minor fixes
161161

162162

doc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ gridstack.js API
4747
- [resize(el, width, height)](#resizeel-width-height)
4848
- [resizable(el, val)](#resizableel-val)
4949
- [setAnimation(doAnimate)](#setanimationdoanimate)
50-
- [setGridWidth(gridWidth, doNotPropagate)](#setgridwidthgridwidth-donotpropagate)
50+
- [setColumn(column, doNotPropagate)](#setcolumncolumn-donotpropagate)
5151
- [setStatic(staticValue)](#setstaticstaticvalue)
5252
- [update(el, x, y, width, height)](#updateel-x-y-width-height)
5353
- [verticalMargin()](#verticalmargin)
@@ -436,11 +436,11 @@ Toggle the grid animation state. Toggles the `grid-stack-animate` class.
436436

437437
- `doAnimate` - if `true` the grid will animate.
438438

439-
### setGridWidth(gridWidth, doNotPropagate)
439+
### setColumn(column, doNotPropagate)
440440

441441
(Experimental) Modify number of columns in the grid. Will attempt to update existing widgets to conform to new number of columns. Requires `gridstack-extra.css` or `gridstack-extra.min.css`.
442442

443-
- `gridWidth` - Integer between 1 and 12.
443+
- `column` - Integer between 1 and 12.
444444
- `doNotPropagate` - if true existing widgets will not be updated.
445445

446446
### setStatic(staticValue)

spec/gridstack-spec.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ describe('gridstack', function() {
33

44
var e;
55
var w;
6-
// grid has 4x2 and 4x4 top-left aligned - used all most test cases
6+
// grid has 4x2 and 4x4 top-left aligned - used on most test cases
77
var gridstackHTML =
88
'<div style="width: 992px; height: 800px" id="gs-cont">' +
99
' <div class="grid-stack">' +
@@ -271,6 +271,62 @@ describe('gridstack', function() {
271271
});
272272
});
273273

274+
describe('grid.column', function() {
275+
beforeEach(function() {
276+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
277+
});
278+
afterEach(function() {
279+
document.body.removeChild(document.getElementById('gs-cont'));
280+
});
281+
it('should have no changes', function() {
282+
var options = {
283+
column: 12
284+
};
285+
$('.grid-stack').gridstack(options);
286+
var grid = $('.grid-stack').data('gridstack');
287+
expect(grid.opts.column).toBe(12);
288+
grid.setColumn(12);
289+
expect(grid.opts.column).toBe(12);
290+
grid.setGridWidth(12); // old API
291+
expect(grid.opts.column).toBe(12);
292+
});
293+
it('should change column number, no relayout', function() {
294+
var options = {
295+
column: 12
296+
};
297+
$('.grid-stack').gridstack(options);
298+
var grid = $('.grid-stack').data('gridstack');
299+
var items = $('.grid-stack-item');
300+
301+
grid.setColumn(10, false);
302+
expect(grid.opts.column).toBe(10);
303+
for (var j = 0; j < items.length; j++) {
304+
expect(parseInt($(items[j]).attr('data-gs-y'), 10)).toBe(0);
305+
}
306+
307+
grid.setColumn(9, true);
308+
expect(grid.opts.column).toBe(9);
309+
for (var j = 0; j < items.length; j++) {
310+
expect(parseInt($(items[j]).attr('data-gs-y'), 10)).toBe(0);
311+
}
312+
});
313+
it('should change column number and relayout items', function() {
314+
var options = {
315+
column: 12
316+
};
317+
$('.grid-stack').gridstack(options);
318+
var grid = $('.grid-stack').data('gridstack');
319+
var items = $('.grid-stack-item');
320+
321+
grid.setColumn(1);
322+
expect(grid.opts.column).toBe(1);
323+
for (var j = 0; j < items.length; j++) {
324+
expect(parseInt($(items[j]).attr('data-gs-x'), 10)).toBe(0);
325+
// TODO: check Y position but I don't currently agree with order. [Alain]
326+
}
327+
});
328+
});
329+
274330
describe('grid.minWidth', function() {
275331
beforeEach(function() {
276332
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);

src/gridstack.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ interface GridStack {
264264
/**
265265
* (Experimental) Modify number of columns in the grid. Will attempt to update existing widgets
266266
* to conform to new number of columns. Requires `gridstack-extra.css` or `gridstack-extra.min.css`.
267-
* @param gridWidth - Integer between 1 and 12.
267+
* @param column - Integer between 1 and 12.
268268
* @param doNotPropagate if true existing widgets will not be updated.
269269
*/
270-
setGridWidth(gridWidth: number, doNotPropagate: boolean): void;
270+
setColumn(column: number, doNotPropagate: boolean): void;
271271

272272
/**
273273
* Toggle the grid static state. Also toggle the grid-stack-static class.

src/gridstack.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,8 @@
250250

251251
/*eslint-disable camelcase */
252252
Utils.is_intercepted = obsolete(Utils.isIntercepted, 'is_intercepted', 'isIntercepted');
253-
254253
Utils.create_stylesheet = obsolete(Utils.createStylesheet, 'create_stylesheet', 'createStylesheet');
255-
256254
Utils.remove_stylesheet = obsolete(Utils.removeStylesheet, 'remove_stylesheet', 'removeStylesheet');
257-
258255
Utils.insert_css_rule = obsolete(Utils.insertCSSRule, 'insert_css_rule', 'insertCSSRule');
259256
/*eslint-enable camelcase */
260257

@@ -697,7 +694,7 @@
697694
opts.itemClass = opts.itemClass || 'grid-stack-item';
698695
var isNested = this.container.closest('.' + opts.itemClass).length > 0;
699696

700-
this.opts = Utils.defaults(opts || {}, {
697+
this.opts = Utils.defaults(opts, {
701698
column: parseInt(this.container.attr('data-gs-column')) || 12,
702699
maxRow: parseInt(this.container.attr('data-gs-max-row')) || 0,
703700
itemClass: 'grid-stack-item',
@@ -1229,7 +1226,7 @@
12291226
var distance = ui.position.top - node._prevYPix;
12301227
node._prevYPix = ui.position.top;
12311228
Utils.updateScrollPosition(el[0], ui, distance);
1232-
if (el.data('inTrashZone') || x < 0 || x >= self.grid.width || y < 0 ||
1229+
if (el.data('inTrashZone') || x < 0 || x >= self.grid.column || y < 0 ||
12331230
(!self.grid.float && y > self.grid.getGridHeight())) {
12341231
if (!node._temporaryRemoved) {
12351232
if (self.opts.removable === true) {
@@ -1807,15 +1804,21 @@
18071804
this.grid.commit();
18081805
};
18091806

1810-
GridStack.prototype.setGridWidth = function(gridWidth,doNotPropagate) {
1807+
GridStack.prototype.setColumn = function(column, doNotPropagate) {
1808+
if (this.opts.column === column) { return; }
18111809
this.container.removeClass('grid-stack-' + this.opts.column);
18121810
if (doNotPropagate !== true) {
1813-
this._updateNodeWidths(this.opts.column, gridWidth);
1811+
this._updateNodeWidths(this.opts.column, column);
18141812
}
1815-
this.opts.column = gridWidth;
1816-
this.grid.width = gridWidth;
1817-
this.container.addClass('grid-stack-' + gridWidth);
1813+
this.opts.column = this.grid.column = column;
1814+
this.container.addClass('grid-stack-' + column);
18181815
};
1816+
// legacy call from <= 0.5.2 - use new method instead.
1817+
GridStack.prototype.setGridWidth = function(column, doNotPropagate) {
1818+
console.warn('gridstack.js: setGridWidth() is deprecated as of v0.5.3 and has been replaced ' +
1819+
'with setColumn(). It will be **completely** removed in v1.0.');
1820+
this.setColumn(column, doNotPropagate);
1821+
}
18191822

18201823
/*eslint-disable camelcase */
18211824
GridStackEngine.prototype.batch_update = obsolete(GridStackEngine.prototype.batchUpdate);

0 commit comments

Comments
 (0)