* Updates widget position/size and other info. This is used to change widget properties after creation. * Can update position, size, content, and other widget properties. * * Note: If you need to call this on all nodes, use load() instead which will update what changed. * Setting the sam
(els: GridStackElement, opt: GridStackWidget)
| 1548 | * }); |
| 1549 | */ |
| 1550 | public update(els: GridStackElement, opt: GridStackWidget): GridStack { |
| 1551 | |
| 1552 | GridStack.getElements(els).forEach(el => { |
| 1553 | const n = el?.gridstackNode; |
| 1554 | if (!n) return; |
| 1555 | const w = {...Utils.copyPos({}, n), ...Utils.cloneDeep(opt)}; // make a copy we can modify in case they re-use it or multiple items |
| 1556 | this.engine.nodeBoundFix(w); |
| 1557 | delete w.autoPosition; |
| 1558 | |
| 1559 | // move/resize widget if anything changed |
| 1560 | const keys = ['x', 'y', 'w', 'h']; |
| 1561 | let m: GridStackWidget; |
| 1562 | if (keys.some(k => w[k] !== undefined && w[k] !== n[k])) { |
| 1563 | m = {}; |
| 1564 | keys.forEach(k => { |
| 1565 | m[k] = (w[k] !== undefined) ? w[k] : n[k]; |
| 1566 | delete w[k]; |
| 1567 | }); |
| 1568 | } |
| 1569 | // for a move as well IFF there is any min/max fields set |
| 1570 | if (!m && (w.minW || w.minH || w.maxW || w.maxH)) { |
| 1571 | m = {}; // will use node position but validate values |
| 1572 | } |
| 1573 | |
| 1574 | // check for content changing |
| 1575 | if (w.content !== undefined) { |
| 1576 | const itemContent = el.querySelector('.grid-stack-item-content') as HTMLElement; |
| 1577 | if (itemContent && itemContent.textContent !== w.content) { |
| 1578 | n.content = w.content; |
| 1579 | GridStack.renderCB(itemContent, w); |
| 1580 | // restore any sub-grid back |
| 1581 | if (n.subGrid?.el) { |
| 1582 | itemContent.appendChild(n.subGrid.el); |
| 1583 | n.subGrid._updateContainerHeight(); |
| 1584 | } |
| 1585 | } |
| 1586 | delete w.content; |
| 1587 | } |
| 1588 | |
| 1589 | // any remaining fields are assigned, but check for dragging changes, resize constrain |
| 1590 | let changed = false; |
| 1591 | let ddChanged = false; |
| 1592 | for (const key in w) { |
| 1593 | if (key[0] !== '_' && n[key] !== w[key]) { |
| 1594 | n[key] = w[key]; |
| 1595 | changed = true; |
| 1596 | ddChanged = ddChanged || (!this.opts.staticGrid && (key === 'noResize' || key === 'noMove' || key === 'locked')); |
| 1597 | } |
| 1598 | } |
| 1599 | Utils.sanitizeMinMax(n); |
| 1600 | |
| 1601 | // finally move the widget and update attr |
| 1602 | if (m) { |
| 1603 | const widthChanged = (m.w !== undefined && m.w !== n.w); |
| 1604 | this.moveNode(n, m); |
| 1605 | if (widthChanged && n.subGrid) { |
| 1606 | // if we're animating the client size hasn't changed yet, so force a change (not exact size) |
| 1607 | n.subGrid.onResize(this.hasAnimationCSS() ? n.w : undefined); |
no test coverage detected