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

Method _packNodes

src/gridstack-engine.ts:457–492  ·  view source on GitHub ↗

@internal called to top gravity pack the items back OR revert back to original Y positions when floating

()

Source from the content-addressed store, hash-verified

455
456 /** @internal called to top gravity pack the items back OR revert back to original Y positions when floating */
457 protected _packNodes(): GridStackEngine {
458 if (this.batchMode) { return this; }
459 this.sortNodes(); // first to last
460
461 if (this.float) {
462 // restore original Y pos
463 this.nodes.forEach(n => {
464 if (n._updating || n._orig === undefined || n.y === n._orig.y) return;
465 let newY = n.y;
466 while (newY > n._orig.y) {
467 --newY;
468 const collide = this.collide(n, {x: n.x, y: newY, w: n.w, h: n.h});
469 if (!collide) {
470 n._dirty = true;
471 n.y = newY;
472 }
473 }
474 });
475 } else {
476 // top gravity pack
477 this.nodes.forEach((n, i) => {
478 if (n.locked) return;
479 while (n.y > 0) {
480 const newY = i === 0 ? 0 : n.y - 1;
481 const canBeMoved = i === 0 || !this.collide(n, {x: n.x, y: newY, w: n.w, h: n.h});
482 if (!canBeMoved) break;
483 // Note: must be dirty (from last position) for GridStack::OnChange CB to update positions
484 // and move items back. The user 'change' CB should detect changes from the original
485 // starting position instead.
486 n._dirty = (n.y !== newY);
487 n.y = newY;
488 }
489 });
490 }
491 return this;
492 }
493
494 /**
495 * Prepare and validate a node's coordinates and values for the current grid.

Callers 7

batchUpdateMethod · 0.95
_fixCollisionsMethod · 0.95
floatMethod · 0.95
addNodeMethod · 0.95
removeNodeMethod · 0.95
moveNodeMethod · 0.95

Calls 2

sortNodesMethod · 0.95
collideMethod · 0.95

Tested by

no test coverage detected