Skip to content

Commit 9fdd6a6

Browse files
committed
grid.setupDragIn()
* add `GridStack.setupDragIn()` so user can update external draggable after the grid has been created. * you can pass in new selector and options, else it uses the grid last option values. (called on during init() but may be needed when items change dynamically). * safe to call multiple times and on already draggable items * fix #1637
1 parent 659ffa3 commit 9fdd6a6

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

demo/two.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ <h1>Two grids demo</h1>
8787
cellHeight: 70,
8888
disableOneColumnMode: true,
8989
float: false,
90-
dragIn: '.sidebar .grid-stack-item', // add draggable to class
91-
dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
90+
// dragIn: '.sidebar .grid-stack-item', // add draggable to class
91+
// dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }, // clone
9292
removable: '.trash', // drag-out delete class
9393
removeTimeout: 100,
9494
acceptWidgets: function(el) { return true; } // function example, else can be simple: true | false | '.someClass' value
9595
};
9696
let grids = GridStack.initAll(options);
9797
grids[1].float(true);
9898

99+
// new 4.x static method instead of setting up options on every grid (never been per grid really but old options still work)
100+
GridStack.setupDragIn('.sidebar .grid-stack-item', { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' });
101+
// GridStack.setupDragIn(); // will now work as well (cache last values)
102+
99103
let items = [
100104
{x: 0, y: 0, w: 2, h: 2},
101105
{x: 3, y: 1, h: 2},

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Change log
5555
- handle mid point of dragged over items (>50%) rather than a new row/column and check for the most covered when multiple items collide.
5656
- fix [1617](https://github.com/gridstack/gridstack.js/issues/1617) FireFox DOM order issue. Thanks [@marcel-necker](https://github.com/marcel-necker)
5757
- add `drag | resize` events while dragging [1616](https://github.com/gridstack/gridstack.js/pull/1616). Thanks [@MrCorba](https://github.com/MrCorba)
58+
- add `GridStack.setupDragIn()` so user can update external draggable after the grid has been created [1637](https://github.com/gridstack/gridstack.js/issues/1637)
5859

5960
## 3.3.0 (2021-2-2)
6061

doc/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ gridstack.js API
5252
- [save(saveContent = true): GridStackWidget[]](#savesavecontent--true-gridstackwidget)
5353
- [setAnimation(doAnimate)](#setanimationdoanimate)
5454
- [setStatic(staticValue)](#setstaticstaticvalue)
55+
- [GridStack.setupDragIn(dragIn?: string, dragInOptions?: DDDragInOpt)](#gridstacksetupdragindragin-string-draginoptions-dddraginopt)
5556
- [update(el: GridStackElement, opts: GridStackWidget)](#updateel-gridstackelement-opts-gridstackwidget)
5657
- [willItFit(x, y, width, height, autoPosition)](#willitfitx-y-width-height-autoposition)
5758
- [Utils](#utils)
@@ -86,8 +87,12 @@ gridstack.js API
8687
- `disableDrag` - disallows dragging of widgets (default: `false`).
8788
- `disableOneColumnMode` - disables the onColumnMode when the grid width is less than minW (default: 'false')
8889
- `disableResize` - disallows resizing of widgets (default: `false`).
89-
- `dragIn` - specify the class of items that can be dragged into the grid (ex: dragIn: '.newWidget'
90-
- `dragInOptions` - options for items that can be dragged into the grid (ex: dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }
90+
- `dragIn` - specify the class of items that can be dragged into grids
91+
* example: `dragIn: '.newWidget'`.
92+
* **Note**: if you have multiple grids, it's best to call `GridStack.setupDragIn()` with same params as it only need to be done once.
93+
- `dragInOptions` - options for items that can be dragged into grids
94+
* example `dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone', handle: '.grid-stack-item-content' }`
95+
* **Note**: if you have multiple grids, it's best to call `GridStack.setupDragIn()` with same params as it only need to be done once.
9196
- `draggable` - allows to override jQuery UI draggable options. (default: `{handle: '.grid-stack-item-content', scroll: false, appendTo: 'body', containment: null}`)
9297
- `dragOut` to let user drag nested grid items out of a parent or not (default false) See [example](http://gridstackjs.com/demo/nested.html)
9398
- `float` - enable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
@@ -494,6 +499,15 @@ Toggle the grid static state. Also toggle the `grid-stack-static` class.
494499

495500
- `staticValue` - if `true` the grid becomes static.
496501

502+
### GridStack.setupDragIn(dragIn?: string, dragInOptions?: DDDragInOpt)
503+
504+
* call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
505+
* Called during `GridStack.init()` as options, but can also be called directly (last param are cached) in case the toolbar
506+
* is dynamically create and needs to change later.
507+
* `dragIn` string selector (ex: `'.sidebar .grid-stack-item'`)
508+
* `dragInOptions` options - see `DDDragInOpt`. (default: `{revert: 'invalid', handle: '.grid-stack-item-content', scroll: false, appendTo: 'body'}`
509+
510+
497511
### update(el: GridStackElement, opts: GridStackWidget)
498512

499513
Parameters:

src/gridstack-dd.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,31 @@ GridStack.prototype._clearRemovingTimeout = function(el: GridItemHTMLElement): G
328328
return this;
329329
}
330330

331-
/** @internal call to setup dragging in from the outside (say toolbar), with options */
332-
GridStack.prototype._setupDragIn = function(): GridStack {
333-
if (!this.opts.staticGrid && typeof this.opts.dragIn === 'string') {
334-
if (!GridStackDD.get().isDraggable(this.opts.dragIn)) {
335-
GridStackDD.get().dragIn(this.opts.dragIn, this.opts.dragInOptions);
336-
}
331+
/**
332+
* call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
333+
* Called during GridStack.init() as options, but can also be called directly (last param are cached) in case the toolbar
334+
* is dynamically create and needs to change later.
335+
**/
336+
GridStack.setupDragIn = function(_dragIn?: string, _dragInOptions?: DDDragInOpt) {
337+
// cache in the passed in values (form grid init?) so they don't have to resend them each time
338+
if (_dragIn) {
339+
dragIn = _dragIn;
340+
dragInOptions = {...dragInDefaultOptions, ...(_dragInOptions || {})};
337341
}
338-
return this;
342+
if (typeof dragIn !== 'string') return;
343+
let dd = GridStackDD.get();
344+
Utils.getElements(dragIn).forEach(el => {
345+
if (!dd.isDraggable(el)) dd.dragIn(el, dragInOptions);
346+
});
339347
}
348+
let dragIn: string;
349+
let dragInOptions: DDDragInOpt;
350+
const dragInDefaultOptions: DDDragInOpt = {
351+
revert: 'invalid',
352+
handle: '.grid-stack-item-content',
353+
scroll: false,
354+
appendTo: 'body'
355+
};
340356

341357
/** @internal prepares the element for drag&drop **/
342358
GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): GridStack {

src/gridstack.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { GridStackEngine } from './gridstack-engine';
1010
import { obsoleteOpts, obsoleteAttr, Utils, HeightData } from './utils';
1111
import { ColumnOptions, GridItemHTMLElement, GridStackElement, GridStackEventHandlerCallback,
12-
GridStackNode, GridStackOptions, GridStackWidget, numberOrString, DDUIData } from './types';
12+
GridStackNode, GridStackOptions, GridStackWidget, numberOrString, DDUIData, DDDragInOpt } from './types';
1313
import { GridStackDDI } from './gridstack-ddi';
1414

1515
// export all dependent file as well to make it easier for users to just import the main file
@@ -70,13 +70,6 @@ const GridDefaults: GridStackOptions = {
7070
scroll: false,
7171
appendTo: 'body'
7272
},
73-
dragIn: undefined,
74-
dragInOptions : {
75-
revert: 'invalid',
76-
handle: '.grid-stack-item-content',
77-
scroll: false,
78-
appendTo: 'body'
79-
},
8073
disableDrag: false,
8174
disableResize: false,
8275
rtl: 'auto',
@@ -147,6 +140,7 @@ export class GridStack {
147140
GridStack.getGridElements(selector).forEach(el => {
148141
if (!el.gridstack) {
149142
el.gridstack = new GridStack(el, {...options});
143+
delete options.dragIn; delete options.dragInOptions; // only need to be done once (really a static global thing, not per grid)
150144
}
151145
grids.push(el.gridstack);
152146
});
@@ -357,7 +351,11 @@ export class GridStack {
357351
this.el.classList.add('grid-stack-' + this.opts.column);
358352
}
359353

360-
this._setupDragIn();
354+
// legacy support to appear 'per grid` options when really global.
355+
if (this.opts.dragIn) GridStack.setupDragIn(this.opts.dragIn, this.opts.dragInOptions);
356+
delete this.opts.dragIn;
357+
delete this.opts.dragInOptions;
358+
361359
this._setupRemoveDrop();
362360
this._setupAcceptWidget();
363361
this._updateWindowResizeEvent();
@@ -1457,6 +1455,15 @@ export class GridStack {
14571455
*/
14581456
/* eslint-disable @typescript-eslint/no-unused-vars */
14591457

1458+
/**
1459+
* call to setup dragging in from the outside (say toolbar), by specifying the class selection and options.
1460+
* Called during GridStack.init() as options, but can also be called directly (last param are cached) in case the toolbar
1461+
* is dynamically create and needs to change later.
1462+
* @param dragIn string selector (ex: '.sidebar .grid-stack-item')
1463+
* @param dragInOptions options - see DDDragInOpt. (default: {revert: 'invalid', handle: '.grid-stack-item-content', scroll: false, appendTo: 'body'}
1464+
**/
1465+
public static setupDragIn(dragIn?: string, dragInOptions?: DDDragInOpt): void { /* implemented in gridstack-dd.ts */ }
1466+
14601467
/**
14611468
* Enables/Disables moving. No-op for static grids.
14621469
* @param els widget or selector to modify.
@@ -1503,6 +1510,7 @@ export class GridStack {
15031510
* doEnable`s value by changing the disableResize grid option (default: true).
15041511
*/
15051512
public enableResize(doEnable: boolean, includeNewWidgets = true): GridStack { return this }
1513+
15061514
/** @internal called to add drag over support to support widgets */
15071515
public _setupAcceptWidget(): GridStack { return this }
15081516
/** @internal called to setup a trash drop zone if the user specifies it */
@@ -1511,8 +1519,6 @@ export class GridStack {
15111519
public _setupRemovingTimeout(el: GridItemHTMLElement): GridStack { return this }
15121520
/** @internal */
15131521
public _clearRemovingTimeout(el: GridItemHTMLElement): GridStack { return this }
1514-
/** @internal call to setup dragging in from the outside (say toolbar), with options */
1515-
public _setupDragIn(): GridStack { return this }
15161522
/** @internal prepares the element for drag&drop **/
15171523
public _prepareDragDropByNode(node: GridStackNode): GridStack { return this }
15181524
/** @internal handles actual drag/resize start **/

0 commit comments

Comments
 (0)