Skip to content

Commit 6d5d4dc

Browse files
committed
removable:true working again (broke in 4.x)
* fix #1760
1 parent 1fddce4 commit 6d5d4dc

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

demo/two.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ <h1>Two grids demo</h1>
8989
float: false,
9090
// dragIn: '.sidebar .grid-stack-item', // add draggable to class
9191
// dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
92-
removable: '.trash', // drag-out delete class
92+
removable: '.trash', // true or drag-out delete class
9393
acceptWidgets: function(el) { return true; } // function example, else can be simple: true | false | '.someClass' value
9494
};
9595
let grids = GridStack.initAll(options);

doc/CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change log
55
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

8+
- [4.2.3-dev](#423-dev)
89
- [4.2.3 (2021-5-8)](#423-2021-5-8)
910
- [4.2.2 (2021-4-23)](#422-2021-4-23)
1011
- [4.2.1 (2021-4-18)](#421-2021-4-18)
@@ -56,6 +57,10 @@ Change log
5657
- [v0.1.0 (2014-11-18)](#v010-2014-11-18)
5758

5859
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
60+
## 4.2.3-dev
61+
62+
* fix [#1760](https://github.com/gridstack/gridstack.js/issues/1760) `removable:true` working again (broke in 4.x)
63+
5964
## 4.2.3 (2021-5-8)
6065

6166
- `Utils.getScrollParent()` -> `getScrollElement()` rename

src/gridstack-dd.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ GridStack.prototype._setupAcceptWidget = function(): GridStack {
156156
if (node && node.grid && node.grid !== this && !node._temporaryRemoved) {
157157
// TEST console.log('dropover without leave');
158158
let otherGrid = node.grid;
159-
otherGrid._leave(el.gridstackNode, el, helper, true); // MATCH line 222
159+
otherGrid._leave(el, helper);
160160
}
161161

162162
// get grid screen coordinates and cell dimensions
@@ -196,6 +196,9 @@ GridStack.prototype._setupAcceptWidget = function(): GridStack {
196196
node._temporaryRemoved = true; // so we can insert it
197197
}
198198

199+
// clear any marked for complete removal (Note: don't check _isAboutToRemove as that is cleared above - just do it)
200+
_itemRemoving(node.el, false);
201+
199202
GridStackDD.get().on(el, 'drag', onDrag);
200203
// make sure this is called at least once when going fast #1578
201204
onDrag(event as DragEvent, el, helper);
@@ -209,7 +212,7 @@ GridStack.prototype._setupAcceptWidget = function(): GridStack {
209212
// fix #1578 when dragging fast, we might get leave after other grid gets enter (which calls us to clean)
210213
// so skip this one if we're not the active grid really..
211214
if (!node.grid || node.grid === this) {
212-
this._leave(node, el, helper, true); // MATCH line 166
215+
this._leave(el, helper);
213216
}
214217
return false; // prevent parent from receiving msg (which may be grid as well)
215218
})
@@ -287,6 +290,14 @@ GridStack.prototype._setupAcceptWidget = function(): GridStack {
287290
return this;
288291
}
289292

293+
/** @internal mark item for removal */
294+
function _itemRemoving(el: GridItemHTMLElement, remove: boolean) {
295+
let node = el.gridstackNode;
296+
if (!node || !node.grid) return;
297+
remove ? node._isAboutToRemove = true : delete node._isAboutToRemove;
298+
remove ? el.classList.add('grid-stack-item-removing') : el.classList.remove('grid-stack-item-removing');
299+
}
300+
290301
/** @internal called to setup a trash drop zone if the user specifies it */
291302
GridStack.prototype._setupRemoveDrop = function(): GridStack {
292303
if (!this.opts.staticGrid && typeof this.opts.removable === 'string') {
@@ -297,18 +308,8 @@ GridStack.prototype._setupRemoveDrop = function(): GridStack {
297308
// and Native DD only has 1 event CB (having a list and technically a per grid removableOptions complicates things greatly)
298309
if (!GridStackDD.get().isDroppable(trashEl)) {
299310
GridStackDD.get().droppable(trashEl, this.opts.removableOptions)
300-
.on(trashEl, 'dropover', function(event, el) { // don't use => notation to avoid using 'this' as grid by mistake...
301-
let node = el.gridstackNode;
302-
if (!node || !node.grid) return;
303-
node._isAboutToRemove = true;
304-
el.classList.add('grid-stack-item-removing');
305-
})
306-
.on(trashEl, 'dropout', function(event, el) { // same
307-
let node = el.gridstackNode;
308-
if (!node || !node.grid) return;
309-
delete node._isAboutToRemove;
310-
el.classList.remove('grid-stack-item-removing');
311-
});
311+
.on(trashEl, 'dropover', (event, el) => _itemRemoving(el, true))
312+
.on(trashEl, 'dropout', (event, el) => _itemRemoving(el, false));
312313
}
313314
}
314315
return this;
@@ -494,12 +495,11 @@ GridStack.prototype._onStartMoving = function(el: GridItemHTMLElement, event: Ev
494495
* or shape is outside our boundaries. remove it from us, and mark temporary if this was
495496
* our item to start with else restore prev node values from prev grid it came from.
496497
**/
497-
GridStack.prototype._leave = function(node: GridStackNode, el: GridItemHTMLElement, helper?: GridItemHTMLElement, dropoutEvent = false) {
498+
GridStack.prototype._leave = function(el: GridItemHTMLElement, helper?: GridItemHTMLElement) {
499+
let node = el.gridstackNode;
498500
if (!node) return;
499501

500-
if (dropoutEvent) {
501-
GridStackDD.get().off(el, 'drag'); // no need to track while being outside
502-
}
502+
GridStackDD.get().off(el, 'drag'); // no need to track while being outside
503503

504504
// this gets called when cursor leaves and shape is outside, so only do this once
505505
if (node._temporaryRemoved) return;
@@ -508,6 +508,11 @@ GridStack.prototype._leave = function(node: GridStackNode, el: GridItemHTMLEleme
508508
this.engine.removeNode(node); // remove placeholder as well, otherwise it's a sign node is not in our list, which is a bigger issue
509509
node.el = node._isExternal && helper ? helper : el; // point back to real item being dragged
510510

511+
if (this.opts.removable === true) { // boolean vs a class string
512+
// item leaving us and we are supposed to remove on leave (no need to drag onto trash) mark it so
513+
_itemRemoving(el, true);
514+
}
515+
511516
// finally if item originally came from another grid, but left us, restore things back to prev info
512517
if (el._gridstackNodeOrig) {
513518
// TEST console.log('leave delete _gridstackNodeOrig')

src/gridstack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,5 +1519,5 @@ export class GridStack {
15191519
/** @internal handles actual drag/resize **/
15201520
public _dragOrResize(el: GridItemHTMLElement, event: Event, ui: DDUIData, node: GridStackNode, cellWidth: number, cellHeight: number): void { return }
15211521
/** @internal called when a node leaves our area (mouse out or shape outside) **/
1522-
public _leave(node: GridStackNode, el: GridItemHTMLElement, helper?: GridItemHTMLElement, dropoutEvent = false): void { return }
1522+
public _leave(el: GridItemHTMLElement, helper?: GridItemHTMLElement): void { return }
15231523
}

0 commit comments

Comments
 (0)