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

Method moveNode

src/gridstack-engine.ts:929–987  ·  view source on GitHub ↗

return true if the passed in node was actually moved (checks for no-op and locked)

(node: GridStackNode, o: GridStackMoveOpts)

Source from the content-addressed store, hash-verified

927
928 /** return true if the passed in node was actually moved (checks for no-op and locked) */
929 public moveNode(node: GridStackNode, o: GridStackMoveOpts): boolean {
930 if (!node || /*node.locked ||*/ !o) return false;
931 let wasUndefinedPack: boolean;
932 if (o.pack === undefined && !this.batchMode) {
933 wasUndefinedPack = o.pack = true;
934 }
935
936 // constrain the passed in values and check if we're still changing our node
937 if (typeof o.x !== 'number') { o.x = node.x; }
938 if (typeof o.y !== 'number') { o.y = node.y; }
939 if (typeof o.w !== 'number') { o.w = node.w; }
940 if (typeof o.h !== 'number') { o.h = node.h; }
941 const resizing = (node.w !== o.w || node.h !== o.h);
942 const nn: GridStackNode = Utils.copyPos({}, node, true); // get min/max out first, then opt positions next
943 Utils.copyPos(nn, o);
944 this.nodeBoundFix(nn, resizing);
945 Utils.copyPos(o, nn);
946
947 if (!o.forceCollide && Utils.samePos(node, o)) return false;
948 const prevPos: GridStackPosition = Utils.copyPos({}, node);
949
950 // check if we will need to fix collision at our new location
951 const collides = this.collideAll(node, nn, o.skip);
952 let needToMove = true;
953 if (collides.length) {
954 const activeDrag = node._moving && !o.nested;
955 // check to make sure we actually collided over 50% surface area while dragging
956 let collide = activeDrag ? this.directionCollideCoverage(node, o, collides) : collides[0];
957 // if we're enabling creation of sub-grids on the fly, see if we're covering 80% of either one, if we didn't already do that
958 if (activeDrag && collide && node.grid?.opts?.subGridDynamic && !node.grid._isTemp) {
959 const over = Utils.areaIntercept(o.rect, collide._rect);
960 const a1 = Utils.area(o.rect);
961 const a2 = Utils.area(collide._rect);
962 const perc = over / (a1 < a2 ? a1 : a2);
963 if (perc > .8) {
964 collide.grid.makeSubGrid(collide.el, undefined, node);
965 collide = undefined;
966 }
967 }
968
969 if (collide) {
970 needToMove = !this._fixCollisions(node, nn, collide, o); // check if already moved...
971 } else {
972 needToMove = false; // we didn't cover >50% for a move, skip...
973 if (wasUndefinedPack) delete o.pack;
974 }
975 }
976
977 // now move (to the original ask vs the collision version which might differ) and repack things
978 if (needToMove && !Utils.samePos(node, nn)) {
979 node._dirty = true;
980 Utils.copyPos(node, nn);
981 }
982 if (o.pack) {
983 this._packNodes()
984 ._notify();
985 }
986 return !Utils.samePos(node, prevPos); // pack might have moved things back

Callers 2

_fixCollisionsMethod · 0.95
moveNodeCheckMethod · 0.95

Calls 11

nodeBoundFixMethod · 0.95
collideAllMethod · 0.95
_fixCollisionsMethod · 0.95
_packNodesMethod · 0.95
copyPosMethod · 0.80
samePosMethod · 0.80
areaInterceptMethod · 0.80
areaMethod · 0.80
makeSubGridMethod · 0.80
_notifyMethod · 0.80

Tested by

no test coverage detected