Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5697a98

Browse files
authored
Merge pull request gridstack#1382 from adumesny/develop
preserve attributes dragging between grids
2 parents 08b8823 + d1abf84 commit 5697a98

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Change log
4444
- fix remove window resize event when `grid.destroy()` [1369](https://github.com/gridstack/gridstack.js/issues/1369)
4545
- fix nested grid resize [1361](https://github.com/gridstack/gridstack.js/issues/1361)
4646
- fix resize with `cellHeight` '6rem' '6em' not working [1356](https://github.com/gridstack/gridstack.js/issues/1356)
47+
- fix preserve attributes (min/max/id/etc...) when dragging between grids [1367](https://github.com/gridstack/gridstack.js/issues/1367)
4748

4849
## 2.0.0 (2020-09-07)
4950

src/gridstack-engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ export class GridStackEngine {
187187
*/
188188
public prepareNode(node: GridStackNode, resizing?: boolean): GridStackNode {
189189
node = node || {};
190+
node._id = node._id || GridStackEngine._idSeq++;
191+
190192
// if we're missing position, have the grid position us automatically (before we set them to 0,0)
191193
if (node.x === undefined || node.y === undefined || node.x === null || node.y === null) {
192194
node.autoPosition = true;
@@ -287,8 +289,6 @@ export class GridStackEngine {
287289
public addNode(node: GridStackNode, triggerAddEvent = false): GridStackNode {
288290
node = this.prepareNode(node);
289291

290-
node._id = node._id || GridStackEngine._idSeq++;
291-
292292
if (node.autoPosition) {
293293
this._sortNodes();
294294

src/gridstack.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ export class GridStack {
11521152
// check for css min height
11531153
let cssMinHeight = parseInt(getComputedStyle(this.el)['min-height']);
11541154
if (cssMinHeight > 0) {
1155-
let minRow = Math.round(cssMinHeight / this.getCellHeight());
1155+
let minRow = Math.round(cssMinHeight / this.getCellHeight(true));
11561156
if (row < minRow) {
11571157
row = minRow;
11581158
}
@@ -1610,24 +1610,22 @@ export class GridStack {
16101610
}
16111611
})
16121612
.on(this.el, 'dropover', (event, el: GridItemHTMLElement) => {
1613-
let width, height;
16141613

16151614
// see if we already have a node with widget/height and check for attributes
1616-
let node = el.gridstackNode;
1617-
if (!node || !node.width || !node.height) {
1615+
let node = el.gridstackNode || {};
1616+
if (!node.width || !node.height) {
16181617
let w = parseInt(el.getAttribute('data-gs-width'));
1619-
if (w > 0) { node = node || {}; node.width = w; }
1618+
if (w > 0) { node.width = w; }
16201619
let h = parseInt(el.getAttribute('data-gs-height'));
1621-
if (h > 0) { node = node || {}; node.height = h; }
1620+
if (h > 0) { node.height = h; }
16221621
}
16231622

16241623
// if not calculate the grid size based on element outer size
1625-
let cellWidth = this.cellWidth();
1626-
let cellHeight = this.getCellHeight();
1627-
width = node && node.width ? node.width : Math.round(el.offsetWidth / cellWidth) || 1;
1628-
height = node && node.height ? node.height : Math.round(el.offsetHeight / cellHeight) || 1;
1624+
let width = node.width || Math.round(el.offsetWidth / this.cellWidth()) || 1;
1625+
let height = node.height || Math.round(el.offsetHeight / this.getCellHeight(true)) || 1;
16291626

1630-
let newNode = this.engine.prepareNode({width, height, _added: false, _temporary: true});
1627+
// copy the node original values (min/max/id/etc...) but override width/height/other flags which are this grid specific
1628+
let newNode = this.engine.prepareNode({...node, ...{width, height, _added: false, _temporary: true}});
16311629
newNode._isOutOfGrid = true;
16321630
el.gridstackNode = newNode;
16331631
el._gridstackNodeOrig = node;
@@ -1655,7 +1653,7 @@ export class GridStack {
16551653
.on(this.el, 'drop', (event, el: GridItemHTMLElement, helper: GridItemHTMLElement) => {
16561654
this.placeholder.remove();
16571655

1658-
// notify of removal from prev grid...
1656+
// notify previous grid of removal
16591657
let origNode = el._gridstackNodeOrig;
16601658
delete el._gridstackNodeOrig;
16611659
if (origNode && origNode.grid && origNode.grid !== this) {
@@ -1666,12 +1664,14 @@ export class GridStack {
16661664
oGrid._triggerRemoveEvent();
16671665
}
16681666

1669-
let node: GridStackNode = el.gridstackNode; // use existing placeholder node as it's already in our list with drop location
1670-
this.engine.cleanupNode(node); // remove all internal _xyz values
1667+
let node = el.gridstackNode; // use existing placeholder node as it's already in our list with drop location
1668+
const _id = node._id;
1669+
this.engine.cleanupNode(node); // removes all internal _xyz values (including the _id so add that back)
1670+
node._id = _id;
16711671
node.grid = this;
16721672
this.dd.off(el, 'drag');
16731673
// if we made a copy ('helper' which is temp) of the original node then insert a copy, else we move the original node (#1102)
1674-
// as the helper will be nuked by default (by jqueryui and here to make it the same)
1674+
// as the helper will be nuked by jqueryui otherwise
16751675
if (helper !== el) {
16761676
helper.remove();
16771677
el.gridstackNode = origNode; // original item (left behind) is re-stored to pre dragging as the node now has drop info
@@ -1699,7 +1699,7 @@ export class GridStack {
16991699
}
17001700

17011701
// wait till we return out of the drag callback to set the new drag&resize handler or they may get messed up
1702-
// IFF we are still there (soe application will use as placeholder and insert their real widget instead)
1702+
// IFF we are still there (some application will use as placeholder and insert their real widget instead)
17031703
window.setTimeout(() => {
17041704
if (node.el && node.el.parentElement) this._prepareDragDropByNode(node);
17051705
});

0 commit comments

Comments
 (0)