Skip to content

Commit ebf6017

Browse files
author
Alain Dumesny
committed
new compact() method
* relayout grid items to reclaim any empty space * update two.html demo to have a compact buton, and test case
1 parent c582b6e commit ebf6017

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

demo/two.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ <h1>Two grids demo</h1>
7474
<div class="row">
7575
<div class="col-md-6">
7676
<a class="btn btn-primary" id="float1" href="#">float: false</a>
77+
<a class="btn btn-primary" id="compact1" href="#">Compact</a>
7778
<div class="grid-stack grid-stack-6" id="grid1">
7879
</div>
7980
</div>
8081
<div class="col-md-6">
8182
<a class="btn btn-primary" id="float2" href="#">float: true</a>
83+
<a class="btn btn-primary" id="compact2" href="#">Compact</a>
8284
<div class="grid-stack grid-stack-6" id="grid2">
8385
</div>
8486
</div>
@@ -136,13 +138,21 @@ <h1>Two grids demo</h1>
136138
appendTo: 'body',
137139
});
138140

139-
$('#float1').click(function() { toggleFloat('#float1', '#grid1'); });
140-
$('#float2').click(function() { toggleFloat('#float2', '#grid2'); });
141+
$('#float1').click(function() { toggleFloat('#float1', '#grid1') });
142+
$('#float2').click(function() { toggleFloat('#float2', '#grid2') });
141143
function toggleFloat(button, grid) {
142144
var grid = $(grid).data('gridstack');
143145
grid.float(! grid.float());
144146
$(button).html('float: ' + grid.float());
145147
}
148+
149+
$('#compact1').click(function() { compact('#grid1') });
150+
$('#compact2').click(function() { compact('#grid2') });
151+
function compact(grid) {
152+
var grid = $(grid).data('gridstack');
153+
grid.compact();
154+
}
155+
146156
});
147157
</script>
148158
</body>

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Change log
2828
## v0.5.5-dev (upcoming changes)
2929

3030
- add `float(val)` to set/get the grid float mode [#1088](https://github.com/gridstack/gridstack.js/pull/1088)
31+
- add `compact()` relayout grid items to reclaim any empty space [#1101](https://github.com/gridstack/gridstack.js/pull/1101)
3132
- Allow percentage as a valid unit for height [#1093](https://github.com/gridstack/gridstack.js/pull/1093)
3233
- fixed callbacks to get either `added, removed, change` or combination if adding a node require also to change its (x,y) for example.
3334
Also you can now call `batchUpdate()` before calling a bunch of `addWidget()` and get a single event callback (more efficient).

doc/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ gridstack.js API
2323
- [addWidget(el, [options])](#addwidgetel-options)
2424
- [addWidget(el, [x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id])](#addwidgetel-x-y-width-height-autoposition-minwidth-maxwidth-minheight-maxheight-id)
2525
- [batchUpdate()](#batchupdate)
26+
- [compact()](#compact)
2627
- [cellHeight()](#cellheight)
2728
- [cellHeight(val, noUpdate)](#cellheightval-noupdate)
2829
- [cellWidth()](#cellwidth)
@@ -252,6 +253,10 @@ grid.addWidget(el, 0, 0, 3, 2, true);
252253

253254
starts batch updates. You will see no changes until `commit()` method is called.
254255

256+
### compact()
257+
258+
relayout grid items to reclaim any empty space.
259+
255260
### cellHeight()
256261

257262
Gets current cell height.

spec/gridstack-spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,4 +1251,38 @@ describe('gridstack', function() {
12511251
}
12521252
});
12531253
});
1254+
1255+
describe('grid.compact', function() {
1256+
beforeEach(function() {
1257+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1258+
});
1259+
afterEach(function() {
1260+
document.body.removeChild(document.getElementById('gs-cont'));
1261+
});
1262+
it('should move all 3 items to top-left with no space', function() {
1263+
$('.grid-stack').gridstack({float: true});
1264+
var grid = $('.grid-stack').data('gridstack');
1265+
1266+
var el3 = grid.addWidget(widgetHTML, {x: 3, y: 5});
1267+
expect(parseInt(el3.attr('data-gs-x'))).toBe(3);
1268+
expect(parseInt(el3.attr('data-gs-y'))).toBe(5);
1269+
1270+
grid.compact();
1271+
expect(parseInt(el3.attr('data-gs-x'))).toBe(8);
1272+
expect(parseInt(el3.attr('data-gs-y'))).toBe(0);
1273+
});
1274+
it('not move locked item', function() {
1275+
$('.grid-stack').gridstack({float: true});
1276+
var grid = $('.grid-stack').data('gridstack');
1277+
1278+
var el3 = grid.addWidget(widgetHTML, {x: 3, y: 5, locked: true, noMove: true});
1279+
expect(parseInt(el3.attr('data-gs-x'))).toBe(3);
1280+
expect(parseInt(el3.attr('data-gs-y'))).toBe(5);
1281+
1282+
grid.compact();
1283+
expect(parseInt(el3.attr('data-gs-x'))).toBe(3);
1284+
expect(parseInt(el3.attr('data-gs-y'))).toBe(5);
1285+
});
1286+
1287+
});
12541288
});

src/gridstack.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ interface GridStack {
9696
*/
9797
commit(): void;
9898

99+
/**
100+
* relayout grid items to reclaim any empty space
101+
*/
102+
compact(): void;
103+
99104
/**
100105
* Destroys a grid instance.
101106
* @param detachGrid if false nodes and grid will not be removed from the DOM (Optional. Default true).

src/gridstack.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@
476476
if (node.minWidth !== undefined) { node.width = Math.max(node.width, node.minWidth); }
477477
if (node.minHeight !== undefined) { node.height = Math.max(node.height, node.minHeight); }
478478

479-
node._id = ++idSeq;
479+
node._id = node._id || ++idSeq;
480480
// node._dirty = true; will be addEvent instead, unless it changes below...
481481

482482
if (node.autoPosition) {
@@ -489,10 +489,10 @@
489489
continue;
490490
}
491491
if (!this.nodes.find(Utils._isAddNodeIntercepted, {x: x, y: y, node: node})) {
492+
node._dirty = (node.x !== x || node.y !== y);
492493
node.x = x;
493494
node.y = y;
494495
delete node.autoPosition; // found our slot
495-
node._dirty = (node.x !== x || node.y !== y);
496496
break;
497497
}
498498
}
@@ -1697,6 +1697,24 @@
16971697
});
16981698
};
16991699

1700+
/**
1701+
* relayout grid items to reclaim any empty space
1702+
*/
1703+
GridStack.prototype.compact = function() {
1704+
if (this.grid.nodes.length === 0) { return; }
1705+
this.batchUpdate();
1706+
this.grid._sortNodes();
1707+
var nodes = this.grid.nodes;
1708+
this.grid.nodes = []; // pretend we have no nodes to conflict layout to start with...
1709+
nodes.forEach(function(n) {
1710+
if (!n.noMove && !n.locked) {
1711+
n.autoPosition = true;
1712+
}
1713+
this.grid.addNode(n, false); // 'false' for add event trigger
1714+
}, this);
1715+
this.commit();
1716+
};
1717+
17001718
GridStack.prototype.verticalMargin = function(val, noUpdate) {
17011719
if (val === undefined) {
17021720
return this.opts.verticalMargin;

0 commit comments

Comments
 (0)