* Attempt to swap the positions of two nodes if they meet swapping criteria. * Nodes can swap if they are the same size or in the same column/row, not locked, and touching. * * @param a first node to swap * @param b second node to swap * @returns true if swap was successful, false if
(a: GridStackNode, b: GridStackNode)
| 312 | * } |
| 313 | */ |
| 314 | public swap(a: GridStackNode, b: GridStackNode): boolean | undefined { |
| 315 | if (!b || b.locked || !a || a.locked) return false; |
| 316 | |
| 317 | function _doSwap(): true { // assumes a is before b IFF they have different height (put after rather than exact swap) |
| 318 | const x = b.x, y = b.y; |
| 319 | b.x = a.x; b.y = a.y; // b -> a position |
| 320 | if (a.h != b.h) { |
| 321 | a.x = x; a.y = b.y + b.h; // a -> goes after b |
| 322 | } else if (a.w != b.w) { |
| 323 | a.x = b.x + b.w; a.y = y; // a -> goes after b |
| 324 | } else { |
| 325 | a.x = x; a.y = y; // a -> old b position |
| 326 | } |
| 327 | a._dirty = b._dirty = true; |
| 328 | return true; |
| 329 | } |
| 330 | let touching: boolean; // remember if we called it (vs undefined) |
| 331 | |
| 332 | // same size and same row or column, and touching |
| 333 | if (a.w === b.w && a.h === b.h && (a.x === b.x || a.y === b.y) && (touching = Utils.isTouching(a, b))) |
| 334 | return _doSwap(); |
| 335 | if (touching === false) return; // IFF ran test and fail, bail out |
| 336 | |
| 337 | // check for taking same columns (but different height) and touching |
| 338 | if (a.w === b.w && a.x === b.x && (touching || (touching = Utils.isTouching(a, b)))) { |
| 339 | if (b.y < a.y) { const t = a; a = b; b = t; } // swap a <-> b vars so a is first |
| 340 | return _doSwap(); |
| 341 | } |
| 342 | if (touching === false) return; |
| 343 | |
| 344 | // check if taking same row (but different width) and touching |
| 345 | if (a.h === b.h && a.y === b.y && (touching || (touching = Utils.isTouching(a, b)))) { |
| 346 | if (b.x < a.x) { const t = a; a = b; b = t; } // swap a <-> b vars so a is first |
| 347 | return _doSwap(); |
| 348 | } |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Check if the specified rectangular area is empty (no nodes occupy any part of it). |
no test coverage detected