Skip to content

Commit 3449bde

Browse files
author
Alain Dumesny
committed
renamed setGridWidth() to setColumn()
part of #1053 changes * kept old method as stub with warning (no code break) * added new (hidden for now) column.html demo * updated test coverage
1 parent 9c60e28 commit 3449bde

File tree

7 files changed

+148
-20
lines changed

7 files changed

+148
-20
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ 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`, and `height` is now `maxRow`, and method `setGridWidth()` is now `setColumn()` 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)).
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)).

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)