Skip to content

Commit 30dc3d2

Browse files
author
Alain Dumesny
committed
more setColumn() tweaks
* use a higher res layout when going up and cache is missing (better to downsample say from 12 then up-sample from 1!)
1 parent 1b20ccf commit 30dc3d2

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

spec/gridstack-spec.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ describe('gridstack', function() {
276276
});
277277
});
278278

279-
describe('grid.column', function() {
279+
describe('grid.setColumn', function() {
280280
beforeEach(function() {
281281
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
282282
});
@@ -341,7 +341,7 @@ describe('gridstack', function() {
341341
expect(node2.width).toBe(4);
342342
expect(node2.height).toBe(4);
343343

344-
// one column will have item1, item2
344+
// 1 column will have item1, item2
345345
grid.setColumn(1);
346346
node1 = $('#item1').data('_gridstack_node');
347347
node2 = $('#item2').data('_gridstack_node');
@@ -365,6 +365,27 @@ describe('gridstack', function() {
365365
expect(node3.width).toBe(1);
366366
expect(node3.height).toBe(1);
367367

368+
// 2 column will have item1, item2, item3 in 1 column still
369+
grid.setColumn(2);
370+
node1 = $('#item1').data('_gridstack_node');
371+
node2 = $('#item2').data('_gridstack_node');
372+
node3 = $('#item3').data('_gridstack_node');
373+
expect(grid.opts.column).toBe(2);
374+
expect(node1.x).toBe(0);
375+
expect(node1.y).toBe(0);
376+
expect(node1.width).toBe(1);
377+
expect(node1.height).toBe(2);
378+
379+
expect(node2.x).toBe(1);
380+
expect(node2.y).toBe(0);
381+
expect(node2.width).toBe(1);
382+
expect(node2.height).toBe(4);
383+
384+
expect(node3.x).toBe(0);
385+
expect(node3.y).toBe(6);
386+
expect(node3.width).toBe(1); // ??? could stay at 1 or take entire width still ?
387+
expect(node3.height).toBe(1);
388+
368389
// back to 12 column and initial layout (other than new item3)
369390
grid.setColumn(12);
370391
expect(grid.opts.column).toBe(12);
@@ -383,7 +404,7 @@ describe('gridstack', function() {
383404

384405
expect(node3.x).toBe(0);
385406
expect(node3.y).toBe(6);
386-
expect(node3.width).toBe(12); // take entire row still
407+
expect(node3.width).toBe(6); // ??? could 6 or taken entire width if it did above
387408
expect(node3.height).toBe(1);
388409
});
389410
});

src/gridstack.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,18 +1833,39 @@
18331833
//
18341834
// now update the nodes positions, using the original ones with new ratio
18351835
//
1836+
18361837
if (doNotPropagate === true || this.grid.nodes.length === 0) { return; }
18371838
var nodes = Utils.sort(this.grid.nodes, -1, oldColumn); // current column reverse sorting so we can insert last to front (limit collision)
18381839

18391840
// cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
18401841
var copy = [nodes.length];
1841-
nodes.forEach(function(n, i) {copy[i] = {x: n.x, y: n.y, width: n.width, _id: n._id}}); // clone to preserve changing x,y,w,h live objects
1842-
this.grid._layouts = this.grid._layouts || {};
1842+
nodes.forEach(function(n, i) {copy[i] = {x: n.x, y: n.y, width: n.width, _id: n._id}}); // only thing we use change is x,y,w and need id to find it back
1843+
this.grid._layouts = this.grid._layouts || []; // use array to find larger quick
18431844
this.grid._layouts[oldColumn] = copy;
18441845

1845-
// see if we have cached prev values and if so re-use those nodes that are still current...
1846-
var newNodes = [];
1846+
// see if we have cached previous layout. if NOT and we are going up in size (up-sampling) start with the largest layout we have (down-sampling) instead
1847+
var lastIndex = this.grid._layouts.length - 1;
18471848
var cacheNodes = this.grid._layouts[column] || [];
1849+
if (cacheNodes.length === 0 && column > oldColumn && lastIndex > column) {
1850+
cacheNodes = this.grid._layouts[lastIndex] || [];
1851+
if (cacheNodes.length) {
1852+
// pretend we came from that larger column by assigning those values at starting point)
1853+
oldColumn = lastIndex;
1854+
cacheNodes.forEach(function(cacheNode) {
1855+
var j = nodes.findIndex(function(n) {return n && n._id === cacheNode._id});
1856+
if (j !== -1) {
1857+
// still current, use cache info positions
1858+
nodes[j].x = cacheNode.x;
1859+
nodes[j].y = cacheNode.y;
1860+
nodes[j].width = cacheNode.width;
1861+
}
1862+
});
1863+
cacheNodes = []; // we still don't have new column cached data... will generate from larger one.
1864+
}
1865+
}
1866+
1867+
// if we found cache re-use those nodes that are still current
1868+
var newNodes = [];
18481869
cacheNodes.forEach(function(cacheNode) {
18491870
var j = nodes.findIndex(function(n) {return n && n._id === cacheNode._id});
18501871
if (j !== -1) {
@@ -1853,7 +1874,7 @@
18531874
nodes[j].y = cacheNode.y;
18541875
nodes[j].width = cacheNode.width;
18551876
newNodes.push(nodes[j]);
1856-
nodes[j] = null;
1877+
nodes[j] = null; // erase it so we know what's left
18571878
}
18581879
});
18591880
// ...and add any extra non-cached ones

0 commit comments

Comments
 (0)