Skip to content

Commit 1b20ccf

Browse files
author
Alain Dumesny
committed
tweaks to setColumn()
* improved code, now using gridEngine.addNode() instead of removeAll() + addWidget() which is much lighter now
1 parent ebf6017 commit 1b20ccf

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

demo/column.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,30 @@ <h1>setColumn() grid demo</h1>
4848
}
4949

5050
var items = [
51-
{x: 0, y: 4, width: 12, height: 1},
52-
{x: 5, y: 3, width: 2, height: 1},
53-
{x: 1, y: 3, width: 4, height: 1},
54-
{x: 0, y: 0, width: 1, height: 1},
55-
{}, // autoPosition testing
56-
{x: 5, y: 0, width: 1, height: 1},
51+
{x: 0, y: 0, width: 2, height: 2},
5752
{x: 2, y: 0, width: 2, height: 1},
58-
{x: 0, y: 0, width: 2, height: 2}
53+
{x: 5, y: 0, width: 1, height: 1},
54+
{text: ' auto'}, // autoPosition testing
55+
{x: 1, y: 3, width: 4, height: 1},
56+
{x: 5, y: 3, width: 2, height: 1},
57+
{x: 0, y: 4, width: 12, height: 1}
5958
];
6059
var count = 0;
6160
grid.batchUpdate();
6261
for (count=0; count<3; count++) {
63-
grid.addWidget($('<div><div class="grid-stack-item-content">' + count + '</div></div>'), items.pop());
62+
var n = items[count];
63+
grid.addWidget($('<div><div class="grid-stack-item-content">' + count + (n.text ? n.text : '') + '</div></div>'), n);
6464
};
6565
grid.commit();
6666

6767
$('#add-widget').click(function() {
68-
var node = items.pop() || {
68+
var n = items[count++] || {
6969
x: Math.round(12 * Math.random()),
7070
y: Math.round(5 * Math.random()),
7171
width: Math.round(1 + 3 * Math.random()),
7272
height: Math.round(1 + 3 * Math.random())
7373
};
74-
grid.addWidget($('<div><div class="grid-stack-item-content">' + count++ + '</div></div>'), node);
74+
grid.addWidget($('<div><div class="grid-stack-item-content">' + count + (n.text ? n.text : '') + '</div></div>'), n);
7575
});
7676

7777
$('#1column').click(function() { grid.setColumn(1); });

src/gridstack.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@
469469
};
470470

471471
GridStackEngine.prototype.addNode = function(node, triggerAddEvent) {
472+
var prev = {x: node.x, y: node.y, width: node.width, height: node.height};
473+
472474
node = this._prepareNode(node);
473475

474476
if (node.maxWidth !== undefined) { node.width = Math.min(node.width, node.maxWidth); }
@@ -502,6 +504,10 @@
502504
if (triggerAddEvent) {
503505
this._addedNodes.push(node);
504506
}
507+
// use single equal as they come as string/undefined but end as number....
508+
if (!node._dirty && (prev.x != node.x || prev.y != node.y || prev.width != node.width || prev.height != node.height)) {
509+
node._dirty = true;
510+
}
505511

506512
this._fixCollisions(node);
507513
this._packNodes();
@@ -1832,7 +1838,7 @@
18321838

18331839
// cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data
18341840
var copy = [nodes.length];
1835-
nodes.forEach(function(n, i) {copy[i] = Utils.clone(n)}); // clone to preserve _id that gets reset during removal, and changing x,y,w,h live objects
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
18361842
this.grid._layouts = this.grid._layouts || {};
18371843
this.grid._layouts[oldColumn] = copy;
18381844

@@ -1842,29 +1848,31 @@
18421848
cacheNodes.forEach(function(cacheNode) {
18431849
var j = nodes.findIndex(function(n) {return n && n._id === cacheNode._id});
18441850
if (j !== -1) {
1845-
newNodes.push(cacheNode); // still current, use cache info
1851+
// still current, use cache info positions
1852+
nodes[j].x = cacheNode.x;
1853+
nodes[j].y = cacheNode.y;
1854+
nodes[j].width = cacheNode.width;
1855+
newNodes.push(nodes[j]);
18461856
nodes[j] = null;
18471857
}
18481858
});
18491859
// ...and add any extra non-cached ones
18501860
var ratio = column / oldColumn;
18511861
nodes.forEach(function(node) {
18521862
if (!node) return;
1853-
newNodes.push($.extend({}, node, {x: Math.round(node.x * ratio), width: Math.round(node.width * ratio) || 1}));
1863+
node.x = Math.round(node.x * ratio);
1864+
node.width = Math.round(node.width * ratio) || 1;
1865+
newNodes.push(node);
18541866
});
18551867
newNodes = Utils.sort(newNodes, -1, column);
18561868

1857-
// now temporary remove the existing gs info and add them from last to make sure we insert them where needed
1858-
// (batch mode will set float=true so we can position anywhere and do gravity relayout after)
1869+
// finally relayout them in reverse order (to get correct placement)
18591870
this.batchUpdate();
1860-
this.grid.removeAll(false); // 'false' = leave DOm elements behind
1871+
this.grid.nodes = []; // pretend we have no nodes to start with (we use same structures) to simplify layout
18611872
newNodes.forEach(function(node) {
1862-
var newNode = this.addWidget(node.el, node).data('_gridstack_node');
1863-
newNode._id = node._id; // keep same ID so we can re-use caches
1864-
newNode._dirty = true;
1873+
this.grid.addNode(node, false); // 'false' for add event trigger
1874+
node._dirty = true; // force attr update
18651875
}, this);
1866-
this.grid._removedNodes = []; // prevent add/remove from being called (kept DOM) only change event
1867-
this.grid._addedNodes = [];
18681876
this.commit();
18691877
};
18701878

0 commit comments

Comments
 (0)