* Prepare and validate a node's coordinates and values for the current grid. * This ensures the node has valid position, size, and properties before being added to the grid. * * @param node the node to prepare and validate * @param resizing if true, resize the node down if it's out of bo
(node: GridStackNode, resizing?: boolean)
| 505 | * console.log('Node prepared at:', prepared.x, prepared.y); |
| 506 | */ |
| 507 | public prepareNode(node: GridStackNode, resizing?: boolean): GridStackNode { |
| 508 | node._id = node._id ?? GridStackEngine._idSeq++; |
| 509 | |
| 510 | // make sure USER supplied id are unique in our list, else assign a new one as it will create issues during load/update/etc... |
| 511 | const id = node.id; |
| 512 | if (id) { |
| 513 | let count = 1; // append nice _n rather than some random number |
| 514 | while (this.nodes.find(n => n.id === node.id && n !== node)) { |
| 515 | node.id = id + '_' + (count++); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // if we're missing position, have the grid position us automatically (before we set them to 0,0) |
| 520 | if (node.x === undefined || node.y === undefined || node.x === null || node.y === null) { |
| 521 | node.autoPosition = true; |
| 522 | } |
| 523 | |
| 524 | // assign defaults for missing required fields |
| 525 | const defaults: GridStackNode = { x: 0, y: 0, w: 1, h: 1}; |
| 526 | Utils.defaults(node, defaults); |
| 527 | |
| 528 | if (!node.autoPosition) { delete node.autoPosition; } |
| 529 | if (!node.noResize) { delete node.noResize; } |
| 530 | if (!node.noMove) { delete node.noMove; } |
| 531 | Utils.sanitizeMinMax(node); |
| 532 | |
| 533 | // check for NaN (in case messed up strings were passed. can't do parseInt() || defaults.x above as 0 is valid #) |
| 534 | if (typeof node.x == 'string') { node.x = Number(node.x); } |
| 535 | if (typeof node.y == 'string') { node.y = Number(node.y); } |
| 536 | if (typeof node.w == 'string') { node.w = Number(node.w); } |
| 537 | if (typeof node.h == 'string') { node.h = Number(node.h); } |
| 538 | if (isNaN(node.x)) { node.x = defaults.x; node.autoPosition = true; } |
| 539 | if (isNaN(node.y)) { node.y = defaults.y; node.autoPosition = true; } |
| 540 | if (isNaN(node.w)) { node.w = defaults.w; } |
| 541 | if (isNaN(node.h)) { node.h = defaults.h; } |
| 542 | |
| 543 | this.nodeBoundFix(node, resizing); |
| 544 | return node; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Part 2 of preparing a node to fit inside the grid - validates and fixes coordinates and dimensions. |
no test coverage detected