|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import { Utils, obsolete } from './utils'; |
10 | | -import { GridStackNode, LayoutOptions } from './types'; |
| 10 | +import { GridStackNode, ColumnOptions } from './types'; |
11 | 11 |
|
12 | 12 | export type onChangeCB = (nodes: GridStackNode[], removeDOM?: boolean) => void; |
13 | 13 |
|
@@ -537,7 +537,7 @@ export class GridStackEngine { |
537 | 537 | * @param layout specify the type of re-layout that will happen (position, size, etc...). |
538 | 538 | * Note: items will never be outside of the current column boundaries. default (moveScale). Ignored for 1 column |
539 | 539 | */ |
540 | | - public updateNodeWidths(oldColumn: number, column: number, nodes: GridStackNode[], layout: LayoutOptions = 'moveScale'): GridStackEngine { |
| 540 | + public updateNodeWidths(oldColumn: number, column: number, nodes: GridStackNode[], layout: ColumnOptions = 'moveScale'): GridStackEngine { |
541 | 541 | if (!this.nodes.length || oldColumn === column) { return this } |
542 | 542 |
|
543 | 543 | // cache the current layout in case they want to go back (like 12 -> 1 -> 12) as it requires original data |
@@ -584,27 +584,33 @@ export class GridStackEngine { |
584 | 584 | // if we found cache re-use those nodes that are still current |
585 | 585 | let newNodes: GridStackNode[] = []; |
586 | 586 | cacheNodes.forEach(cacheNode => { |
587 | | - let j = nodes.findIndex(n => n && n._id === cacheNode._id); |
| 587 | + let j = nodes.findIndex(n => n._id === cacheNode._id); |
588 | 588 | if (j !== -1) { |
589 | 589 | // still current, use cache info positions |
590 | 590 | nodes[j].x = cacheNode.x; |
591 | 591 | nodes[j].y = cacheNode.y; |
592 | 592 | nodes[j].width = cacheNode.width; |
593 | 593 | newNodes.push(nodes[j]); |
594 | | - nodes[j] = null; // erase it so we know what's left |
| 594 | + nodes.splice(j, 1); |
595 | 595 | } |
596 | 596 | }); |
597 | 597 | // ...and add any extra non-cached ones |
598 | | - let ratio = column / oldColumn; |
599 | | - let move = layout === 'move' || layout === 'moveScale'; |
600 | | - let scale = layout === 'scale' || layout === 'moveScale'; |
601 | | - nodes.forEach(node => { |
602 | | - if (!node) return this; |
603 | | - node.x = (column === 1 ? 0 : (move ? Math.round(node.x * ratio) : Math.min(node.x, column - 1))); |
604 | | - node.width = ((column === 1 || oldColumn === 1) ? 1 : |
605 | | - scale ? (Math.round(node.width * ratio) || 1) : (Math.min(node.width, column))); |
606 | | - newNodes.push(node); |
607 | | - }); |
| 598 | + if (nodes.length) { |
| 599 | + if (typeof layout === 'function') { |
| 600 | + layout(column, oldColumn, newNodes, nodes); |
| 601 | + } else { |
| 602 | + let ratio = column / oldColumn; |
| 603 | + let move = (layout === 'move' || layout === 'moveScale'); |
| 604 | + let scale = (layout === 'scale' || layout === 'moveScale'); |
| 605 | + nodes.forEach(node => { |
| 606 | + node.x = (column === 1 ? 0 : (move ? Math.round(node.x * ratio) : Math.min(node.x, column - 1))); |
| 607 | + node.width = ((column === 1 || oldColumn === 1) ? 1 : |
| 608 | + scale ? (Math.round(node.width * ratio) || 1) : (Math.min(node.width, column))); |
| 609 | + newNodes.push(node); |
| 610 | + }); |
| 611 | + nodes = []; |
| 612 | + } |
| 613 | + } |
608 | 614 |
|
609 | 615 | // finally re-layout them in reverse order (to get correct placement) |
610 | 616 | newNodes = Utils.sort(newNodes, -1, column); |
|
0 commit comments