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

Method compact

src/gridstack-engine.ts:388–408  ·  view source on GitHub ↗

* Re-layout grid items to reclaim any empty space. * This optimizes the grid layout by moving items to fill gaps. * * @param layout layout algorithm to use: * - 'compact' (default): find truly empty spaces, may reorder items * - 'list': keep the sort order exactly the same, move i

(layout: CompactOptions = 'compact', doSort = true)

Source from the content-addressed store, hash-verified

386 * engine.compact('list');
387 */
388 public compact(layout: CompactOptions = 'compact', doSort = true): GridStackEngine {
389 if (this.nodes.length === 0) return this;
390 if (doSort) this.sortNodes();
391 const wasBatch = this.batchMode;
392 if (!wasBatch) this.batchUpdate();
393 const wasColumnResize = this._inColumnResize;
394 if (!wasColumnResize) this._inColumnResize = true; // faster addNode()
395 const copyNodes = this.nodes;
396 this.nodes = []; // pretend we have no nodes to conflict layout to start with...
397 copyNodes.forEach((n, index, list) => {
398 let after: GridStackNode;
399 if (!n.locked) {
400 n.autoPosition = true;
401 if (layout === 'list' && index) after = list[index - 1];
402 }
403 this.addNode(n, false, after); // 'false' for add event trigger
404 });
405 if (!wasColumnResize) delete this._inColumnResize;
406 if (!wasBatch) this.batchUpdate(false);
407 return this;
408 }
409
410 /**
411 * Enable/disable floating widgets (default: `false`).

Callers 1

columnChangedMethod · 0.95

Calls 3

sortNodesMethod · 0.95
batchUpdateMethod · 0.95
addNodeMethod · 0.95

Tested by

no test coverage detected