MCPcopy Index your code
hub / github.com/gridstack/gridstack.js / layoutsNodesChange

Method layoutsNodesChange

src/gridstack-engine.ts:1044–1081  ·  view source on GitHub ↗

@internal called whenever a node is added or moved - updates the cached layouts

(nodes: GridStackNode[])

Source from the content-addressed store, hash-verified

1042
1043 /** @internal called whenever a node is added or moved - updates the cached layouts */
1044 public layoutsNodesChange(nodes: GridStackNode[]): GridStackEngine {
1045 if (!this._layouts || this._inColumnResize) return this;
1046 // remove smaller layouts - we will re-generate those on the fly... larger ones need to update
1047 this._layouts.forEach((layout, column) => {
1048 if (!layout || column === this.column) return this;
1049 if (column < this.column) {
1050 this._layouts[column] = undefined;
1051 }
1052 else {
1053 // we save the original x,y,w (h isn't cached) to see what actually changed to propagate better.
1054 // NOTE: we don't need to check against out of bound scaling/moving as that will be done when using those cache values. #1785
1055 const ratio = column / this.column;
1056 nodes.forEach(node => {
1057 if (!node._orig) return; // didn't change (newly added ?)
1058 const n = layout.find(l => l._id === node._id);
1059 if (!n) return; // no cache for new nodes. Will use those values.
1060 // Y changed, push down same amount
1061 // TODO: detect doing item 'swaps' will help instead of move (especially in 1 column mode)
1062 if (n.y >= 0 && node.y !== node._orig.y) {
1063 n.y += (node.y - node._orig.y);
1064 if (n.y < 0) n.y = 0;
1065 }
1066 // X changed, scale from new position
1067 if (node.x !== node._orig.x) {
1068 n.x = Math.round(node.x * ratio);
1069 if (n.x < 0) n.x = 0;
1070 }
1071 // width changed, scale from new width
1072 if (node.w !== node._orig.w) {
1073 n.w = Math.round(node.w * ratio);
1074 if (n.w < 1) n.w = 1;
1075 }
1076 // ...height always carries over from cache
1077 });
1078 }
1079 });
1080 return this;
1081 }
1082
1083 /**
1084 * @internal Called to scale the widget width & position up/down based on the column change.

Callers 2

_triggerChangeEventMethod · 0.80
_triggerAddEventMethod · 0.80

Calls 1

findMethod · 0.80

Tested by

no test coverage detected