Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improved item placement in _updateNodeWidths() #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -1672,10 +1672,41 @@
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);

// We need an array of the elements to update so that we make sure that
// each item in the grid gets processed. We need to get a fixed list of
// array items, because the this.update() can shift items in the this.grid.nodes
// array which can lead to elements not being processed.
var nodes = _.map(this.grid.nodes, 'el')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.grid.nodes.map(n => n.el)
ES6 without lodash

, columnPos, columnWidth;

for (var i = 0; i < nodes.length; i++) {
// get the current live node data, in case a previous update has changed anything
node = this.grid.getNodeDataByDOMEl(nodes[i]);
/*
* Calculate the new column position for the item, but make sure it fits inside the
* grid range. If the calculated position is outside the range, then it will move
* it to the last column (newWidth-1).
*/
columnPos = Math.min(Math.round(node.x * newWidth / oldWidth), newWidth-1);
// calculate the new width of the item
columnWidth = Math.round(node.width * newWidth / oldWidth);

// if the item is too wide, we need to adjust our calculations
if( columnPos + columnWidth > newWidth ){
/*
* To keep the original calculated width, we could just move the item to the left to
* fit within the width of the grid, but this would cause other items to shift:
*
* columnPos = newWidth - columnWidth;
*
* Instead of shifting other items around, we will just make sure the item's
* width is resized to fit inside the width of the grid.
*/
columnWidth = (columnPos + columnWidth) - newWidth;
}

this.update(node.el, columnPos, undefined, columnWidth, undefined);
}
this.grid.commit();
};
Expand Down