Skip to content

Commit 33db0cf

Browse files
committed
H5: fix dragInOption clone: myFunc()
* fixed h5 case when helper is function to correctly call it with event * updated two.html (and jq) demos to show it.
1 parent 39aff94 commit 33db0cf

11 files changed

+32
-11
lines changed

demo/two-jq.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ <h1>Two grids demo (Jquery version)</h1>
8989
cellHeight: 70,
9090
disableOneColumnMode: true,
9191
float: false,
92-
dragIn: '.sidebar .grid-stack-item', // add draggable to class
93-
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
92+
// dragIn: '.sidebar .grid-stack-item', // add draggable to class
93+
// dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
9494
removable: '.trash', // drag-out delete class
9595
acceptWidgets: function(el) { return true; } // function example, else can be simple: true | false | '.someClass' value
9696
};
9797
grids = GridStack.initAll(options);
9898
grids[1].float(true);
9999

100+
// new 4.x static method instead of setting up options on every grid (never been per grid really) but old options still works
101+
GridStack.setupDragIn('.sidebar .grid-stack-item', { revert: 'invalid', scroll: false, appendTo: 'body', helper: myClone });
102+
// GridStack.setupDragIn(); // second call will now work (cache last values)
103+
100104
let items = [
101105
{x: 0, y: 0, w: 2, h: 2},
102106
{x: 3, y: 1, h: 2},
@@ -110,6 +114,12 @@ <h1>Two grids demo (Jquery version)</h1>
110114
grid.load(items);
111115
});
112116
});
117+
118+
// decide what the dropped item will be - for now just a clone but can be anything
119+
function myClone(event) {
120+
return event.target.cloneNode(true);
121+
}
122+
113123
function toggleFloat(button, i) {
114124
grids[i].float(! grids[i].getFloat());
115125
button.innerHTML = 'float: ' + grids[i].getFloat();

demo/two.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ <h1>Two grids demo</h1>
9696
grids[1].float(true);
9797

9898
// new 4.x static method instead of setting up options on every grid (never been per grid really) but old options still works
99-
GridStack.setupDragIn('.sidebar .grid-stack-item', { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' });
99+
GridStack.setupDragIn('.sidebar .grid-stack-item', { revert: 'invalid', scroll: false, appendTo: 'body', helper: myClone });
100100
// GridStack.setupDragIn(); // second call will now work (cache last values)
101101

102102
let items = [
@@ -112,6 +112,11 @@ <h1>Two grids demo</h1>
112112
grid.load(items);
113113
});
114114

115+
// decide what the dropped item will be - for now just a clone but can be anything
116+
function myClone(event) {
117+
return event.target.cloneNode(true);
118+
}
119+
115120
function toggleFloat(button, i) {
116121
grids[i].float(! grids[i].getFloat());
117122
button.innerHTML = 'float: ' + grids[i].getFloat();

demo/web2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ <h1>Advanced Demo</h1>
6060
cellHeight: 70,
6161
acceptWidgets: true,
6262
dragIn: '.newWidget', // class that can be dragged from outside
63-
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' },
63+
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone or can be your function
6464
removable: '#trash', // drag-out delete class
6565
});
6666

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Change log
5353
## 4.0.1-dev
5454

5555
- fix [#1658](https://github.com/gridstack/gridstack.js/issues/1658) `enableMove(T/F)` not working correctly
56+
- fix `helper: myFunction` now working for H5 case for `dragInOptions` & `setupDragIn()`
5657
## 4.0.1 (2021-3-20)
5758

5859
- fix [#1669](https://github.com/gridstack/gridstack.js/issues/1669) JQ resize broken

doc/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ gridstack.js API
9696
- `dragInOptions` - options for items that can be dragged into grids
9797
* example `dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone', handle: '.grid-stack-item-content' }`
9898
* **Note**: if you have multiple grids, it's best to call `GridStack.setupDragIn()` with same params as it only need to be done once.
99+
* **Note2**: instead of 'clone' you can also pass your own function (get passed the event).
99100
- `draggable` - allows to override jQuery UI draggable options. (default: `{handle: '.grid-stack-item-content', scroll: false, appendTo: 'body', containment: null}`)
100101
- `dragOut` to let user drag nested grid items out of a parent or not (default false) See [example](http://gridstackjs.com/demo/nested.html)
101102
- `float` - enable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
@@ -317,6 +318,7 @@ grids.forEach(...)
317318
Called during `GridStack.init()` as options, but can also be called directly (last param are cached) in case the toolbar is dynamically create and needs to change later.
318319
* @param dragIn string selector (ex: `'.sidebar .grid-stack-item'`)
319320
* @param dragInOptions options - see `DDDragInOpt`. (default: `{revert: 'invalid', handle: '.grid-stack-item-content', scroll: false, appendTo: 'body'}`
321+
but you will probably also want `helper: 'clone'` or your own callback function).
320322

321323
## API
322324

spec/e2e/html/1143_nested_acceptWidget_types.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h3>show dragging into sub grid</h3>
5050
let gridNest = GridStack.init({
5151
acceptWidgets: '.newWidget',
5252
dragIn: '.newWidget', // class that can be dragged from outside
53-
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
53+
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone or can be your function
5454
itemClass: 'sub',
5555
}, '.grid-stack.nested');
5656
gridNest.load([

spec/e2e/html/1419-maxrow1-cant-insert.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
row: 1,
6363
cellHeight: 120,
6464
dragIn: '.sidebar .grid-stack-item', // class that can be dragged from outside
65-
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
65+
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone or can be your function
6666
acceptWidgets: true
6767
};
6868
let grid = GridStack.init(options);

spec/e2e/html/1571_drop_onto_full.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ <h1>drop onto full</h1>
8787
disableOneColumnMode: true,
8888
float: false,
8989
dragIn: '.sidebar .grid-stack-item', // class that can be dragged from outside
90-
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
90+
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone or can be your function
9191
removable: '.trash', // drag-out delete class
9292
acceptWidgets: function(el) { return true; } // function example, else can be simple: true | false | '.someClass' value
9393
};

spec/e2e/html/1658_enableMove.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<meta charset="utf-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<title>Float grid demo</title>
7+
<title>enableMove demo</title>
88

99
<link rel="stylesheet" href="../../../demo/demo.css"/>
1010
<script src="../../../dist/gridstack-h5.js"></script>
1111

1212
</head>
1313
<body>
1414
<div class="container-fluid">
15-
<h1>Float grid demo</h1>
15+
<h1>enableMove() demo</h1>
1616
<div>
1717
<a class="btn btn-primary" onClick="addNewWidget()" href="#">Add Widget</a>
1818
<a class="btn btn-primary" onclick="toggleFloat()" id="float" href="#">float: true</a>

src/h5/dd-draggable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
196196
private _createHelper(event: DragEvent): HTMLElement {
197197
let helper = this.el;
198198
if (typeof this.option.helper === 'function') {
199-
helper = this.option.helper.apply(this.el, event);
199+
helper = this.option.helper(event);
200200
} else if (this.option.helper === 'clone') {
201201
helper = DDUtils.clone(this.el);
202202
}

0 commit comments

Comments
 (0)