Skip to content

Commit 7dce5ce

Browse files
committed
removed dropped event setTimeout()
* better fix gridstack#2489 * removed the timeout in the dropped event handler and call dropped event last * tested two.hml and seems to work - can't recall wy we set event handler on a timeout (might have been pre mouse handler code)
1 parent 97b05af commit 7dce5ce

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

demo/events.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ function addEvents(grid, id) {
1515
console.log(g + 'disable');
1616
})
1717
.on('dragstart', function(event, el) {
18-
let node = el.gridstackNode;
18+
let n = el.gridstackNode;
1919
let x = el.getAttribute('gs-x'); // verify node (easiest) and attr are the same
2020
let y = el.getAttribute('gs-y');
21-
console.log(g + 'dragstart ' + el.textContent + ' pos: (' + node.x + ',' + node.y + ') = (' + x + ',' + y + ')');
21+
console.log(g + 'dragstart ' + (n.content || '') + ' pos: (' + n.x + ',' + n.y + ') = (' + x + ',' + y + ')');
2222
})
2323
.on('drag', function(event, el) {
24-
let node = el.gridstackNode;
24+
let n = el.gridstackNode;
2525
let x = el.getAttribute('gs-x'); // verify node (easiest) and attr are the same
2626
let y = el.getAttribute('gs-y');
27-
// console.log(g + 'drag ' + el.textContent + ' pos: (' + node.x + ',' + node.y + ') = (' + x + ',' + y + ')');
27+
// console.log(g + 'drag ' + (n.content || '') + ' pos: (' + n.x + ',' + n.y + ') = (' + x + ',' + y + ')');
2828
})
2929
.on('dragstop', function(event, el) {
30-
let node = el.gridstackNode;
30+
let n = el.gridstackNode;
3131
let x = el.getAttribute('gs-x'); // verify node (easiest) and attr are the same
3232
let y = el.getAttribute('gs-y');
33-
console.log(g + 'dragstop ' + el.textContent + ' pos: (' + node.x + ',' + node.y + ') = (' + x + ',' + y + ')');
33+
console.log(g + 'dragstop ' + (n.content || '') + ' pos: (' + n.x + ',' + n.y + ') = (' + x + ',' + y + ')');
3434
})
3535
.on('dropped', function(event, previousNode, newNode) {
3636
if (previousNode) {
@@ -43,17 +43,17 @@ function addEvents(grid, id) {
4343
.on('resizestart', function(event, el) {
4444
let n = el.gridstackNode;
4545
let rec = el.getBoundingClientRect();
46-
console.log(`${g} resizestart ${el.textContent} size: (${n.w}x${n.h}) = (${Math.round(rec.width)}x${Math.round(rec.height)})px`);
46+
console.log(`${g} resizestart ${n.content || ''} size: (${n.w}x${n.h}) = (${Math.round(rec.width)}x${Math.round(rec.height)})px`);
4747

4848
})
4949
.on('resize', function(event, el) {
5050
let n = el.gridstackNode;
5151
let rec = el.getBoundingClientRect();
52-
console.log(`${g} resize ${el.textContent} size: (${n.w}x${n.h}) = (${Math.round(rec.width)}x${Math.round(rec.height)})px`);
52+
console.log(`${g} resize ${n.content || ''} size: (${n.w}x${n.h}) = (${Math.round(rec.width)}x${Math.round(rec.height)})px`);
5353
})
5454
.on('resizestop', function(event, el) {
5555
let n = el.gridstackNode;
5656
let rec = el.getBoundingClientRect();
57-
console.log(`${g} resizestop ${el.textContent} size: (${n.w}x${n.h}) = (${Math.round(rec.width)}x${Math.round(rec.height)})px`);
57+
console.log(`${g} resizestop ${n.content || ''} size: (${n.w}x${n.h}) = (${Math.round(rec.width)}x${Math.round(rec.height)})px`);
5858
});
5959
}

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Change log
105105

106106
## 9.4.0-dev (TBD)
107107
* fix [#1275](https://github.com/gridstack/gridstack.js/issues/1275) div scale support - Thank you [elmehdiamlou](https://github.com/elmehdiamlou) for implementing this teh right way (add scale to current code)
108+
* fix [#2489](https://github.com/gridstack/gridstack.js/commit/2489) moved the dropped event handler to after doing everything (no more setTimeout) - Thanks [arnoudb](https://github.com/arnoudb) for suggesting a fix.
108109

109110
## 9.4.0 (2023-10-15)
110111
* revert [#2263](https://github.com/gridstack/gridstack.js/issues/2263) div scale support - causing too many issues for now (#2498 #2491)

src/gridstack.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,7 @@ export class GridStack {
21342134
this.engine.cleanupNode(node); // removes all internal _xyz values
21352135
node.grid = this;
21362136
}
2137+
delete node.grid._isTemp;
21372138
dd.off(el, 'drag');
21382139
// if we made a copy ('helper' which is temp) of the original node then insert a copy, else we move the original node (#1102)
21392140
// as the helper will be nuked by jquery-ui otherwise. TODO: update old code path
@@ -2161,6 +2162,7 @@ export class GridStack {
21612162
subGrid.parentGridItem = node;
21622163
if (!subGrid.opts.styleInHead) subGrid._updateStyles(true); // re-create sub-grid styles now that we've moved
21632164
}
2165+
this._prepareDragDropByNode(node);
21642166
this._updateContainerHeight();
21652167
this.engine.addedNodes.push(node);// @ts-ignore
21662168
this._triggerAddEvent();// @ts-ignore
@@ -2170,18 +2172,7 @@ export class GridStack {
21702172
if (this._gsEventHandler['dropped']) {
21712173
this._gsEventHandler['dropped']({...event, type: 'dropped'}, origNode && origNode.grid ? origNode : undefined, node);
21722174
}
2173-
2174-
// wait till we return out of the drag callback to set the new drag&resize handler or they may get messed up
2175-
window.setTimeout(() => {
2176-
// IFF we are still there (some application will use as placeholder and insert their real widget instead and better call makeWidget())
2177-
if (node.el && node.el.parentElement) {
2178-
this._prepareDragDropByNode(node);
2179-
} else {
2180-
this.engine.removeNode(node);
2181-
}
2182-
delete node.grid._isTemp;
2183-
});
2184-
2175+
21852176
return false; // prevent parent from receiving msg (which may be grid as well)
21862177
});
21872178
return this;

0 commit comments

Comments
 (0)