From a21f9419549e030a14794ff9c567c258812c45a0 Mon Sep 17 00:00:00 2001 From: nulen Date: Wed, 25 Oct 2017 11:51:16 +0200 Subject: [PATCH] Fix for changing grid width Updated setGridWidth method to update nodes width only after grid width is changed. Updated _updateNodeWidths method to prevent changing list during looping throgh it. --- src/gridstack.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/gridstack.js b/src/gridstack.js index 518942def..b8d386d6b 100644 --- a/src/gridstack.js +++ b/src/gridstack.js @@ -1671,23 +1671,31 @@ GridStack.prototype._updateNodeWidths = function(oldWidth, newWidth) { this.grid._sortNodes(); this.grid.batchUpdate(); - var node = {}; - for (var i = 0; i < this.grid.nodes.length; i++) { - node = this.grid.nodes[i]; - this.update(node.el, Math.round(node.x * newWidth / oldWidth), undefined, - Math.round(node.width * newWidth / oldWidth), undefined); + + var node = {}, + nodes = _.cloneDeep(this.grid.nodes); + + for (var i = 0; i < nodes.length; i++) { + node = nodes[i]; + this.update(node.el, + Math.round(node.x * newWidth / oldWidth), undefined, + math.max(node.minWidth, Math.round(node.width * newWidth / oldWidth)), undefined + ); } this.grid.commit(); }; GridStack.prototype.setGridWidth = function(gridWidth,doNotPropagate) { - this.container.removeClass('grid-stack-' + this.opts.width); - if (doNotPropagate !== true) { - this._updateNodeWidths(this.opts.width, gridWidth); - } + var oldWidth = this.grid.width; + + this.container.removeClass('grid-stack-' + oldWidth); + this.container.addClass('grid-stack-' + gridWidth); this.opts.width = gridWidth; this.grid.width = gridWidth; - this.container.addClass('grid-stack-' + gridWidth); + + if (doNotPropagate !== true) { + this._updateNodeWidths(oldWidth, gridWidth); + } }; // jscs:disable requireCamelCaseOrUpperCaseIdentifiers