|
469 | 469 | }; |
470 | 470 |
|
471 | 471 | GridStackEngine.prototype.addNode = function(node, triggerAddEvent) { |
| 472 | + var prev = {x: node.x, y: node.y, width: node.width, height: node.height}; |
| 473 | + |
472 | 474 | node = this._prepareNode(node); |
473 | 475 |
|
474 | 476 | if (node.maxWidth !== undefined) { node.width = Math.min(node.width, node.maxWidth); } |
|
502 | 504 | if (triggerAddEvent) { |
503 | 505 | this._addedNodes.push(node); |
504 | 506 | } |
| 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 | + } |
505 | 511 |
|
506 | 512 | this._fixCollisions(node); |
507 | 513 | this._packNodes(); |
|
1832 | 1838 |
|
1833 | 1839 | // cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data |
1834 | 1840 | 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 |
1836 | 1842 | this.grid._layouts = this.grid._layouts || {}; |
1837 | 1843 | this.grid._layouts[oldColumn] = copy; |
1838 | 1844 |
|
|
1842 | 1848 | cacheNodes.forEach(function(cacheNode) { |
1843 | 1849 | var j = nodes.findIndex(function(n) {return n && n._id === cacheNode._id}); |
1844 | 1850 | 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]); |
1846 | 1856 | nodes[j] = null; |
1847 | 1857 | } |
1848 | 1858 | }); |
1849 | 1859 | // ...and add any extra non-cached ones |
1850 | 1860 | var ratio = column / oldColumn; |
1851 | 1861 | nodes.forEach(function(node) { |
1852 | 1862 | 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); |
1854 | 1866 | }); |
1855 | 1867 | newNodes = Utils.sort(newNodes, -1, column); |
1856 | 1868 |
|
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) |
1859 | 1870 | 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 |
1861 | 1872 | 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 |
1865 | 1875 | }, this); |
1866 | | - this.grid._removedNodes = []; // prevent add/remove from being called (kept DOM) only change event |
1867 | | - this.grid._addedNodes = []; |
1868 | 1876 | this.commit(); |
1869 | 1877 | }; |
1870 | 1878 |
|
|
0 commit comments