Skip to content

Commit a40f498

Browse files
committed
h5: del ddPlugin grid option
* always thought it was weird to have a (per) grid option when we had a list of D&D plugins but last one won. * now we only have a global static DD that is shared. Grid instance can be infered by element for any needed options.
1 parent f83e509 commit a40f498

File tree

8 files changed

+49
-66
lines changed

8 files changed

+49
-66
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Change log
4343

4444
## 2.2.0-dev
4545

46-
- TBD
46+
- del `ddPlugin` grid option as we only have one drag&drop plugin at runtime, which is defined by the include you use (HTML5 vs jquery vs none)
4747

4848
## 2.2.0 (2020-11-7)
4949

doc/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ gridstack.js API
8282
* 0 or null, in which case the library will not generate styles for rows. Everything must be defined in CSS files.
8383
* `'auto'` - height will be square cells initially.
8484
- `column` - number of columns (default: `12`) which can change on the fly with `column(N)` as well. See [example](http://gridstackjs.com/demo/column.html)
85-
- `ddPlugin` - class that implement drag'n'drop functionality for gridstack. If `false` grid will be static. (default: `null` - first available plugin will be used)
8685
- `disableDrag` - disallows dragging of widgets (default: `false`).
8786
- `disableOneColumnMode` - disables the onColumnMode when the grid width is less than minWidth (default: 'false')
8887
- `disableResize` - disallows resizing of widgets (default: `false`).

spec/gridstack-spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,7 @@ describe('gridstack', function() {
16341634
});
16351635
});
16361636

1637+
/*
16371638
describe('ddPlugin option', function() {
16381639
beforeEach(function() {
16391640
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
@@ -1652,7 +1653,8 @@ describe('gridstack', function() {
16521653
expect(grid.dd.isDroppable()).toBe(false);
16531654
});
16541655
});
1655-
1656+
*/
1657+
16561658
describe('grid.on events', function() {
16571659
beforeEach(function() {
16581660
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);

src/dragdrop/gridstack-dd-native.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
import { DDManager } from './dd-manager';
99
import { DDElement } from './dd-element';
1010

11-
import { GridStack, GridStackElement } from '../gridstack';
11+
import { GridStackElement } from '../gridstack';
1212
import { GridStackDD, DDOpts, DDKey, DDDropOpt, DDCallback, DDValue } from '../gridstack-dd';
1313
import { GridItemHTMLElement, DDDragInOpt } from '../types';
1414

1515
/**
1616
* HTML 5 Native DragDrop based drag'n'drop plugin.
1717
*/
1818
export class GridStackDDNative extends GridStackDD {
19-
public constructor(grid: GridStack) {
20-
super(grid);
21-
}
2219

2320
public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): GridStackDDNative {
2421
let dEl = this.getGridStackDDElement(el);
@@ -31,9 +28,10 @@ export class GridStackDDNative extends GridStackDD {
3128
} else if (opts === 'option') {
3229
dEl.setupResizable({ [key]: value });
3330
} else {
34-
let handles = dEl.el.getAttribute('gs-resize-handles') ? dEl.el.getAttribute('gs-resize-handles') : this.grid.opts.resizable.handles;
31+
const grid = el.gridstackNode.grid;
32+
let handles = dEl.el.getAttribute('gs-resize-handles') ? dEl.el.getAttribute('gs-resize-handles') : grid.opts.resizable.handles;
3533
dEl.setupResizable({
36-
...this.grid.opts.resizable,
34+
...grid.opts.resizable,
3735
...{ handles: handles },
3836
...{
3937
start: opts.start,
@@ -56,12 +54,13 @@ export class GridStackDDNative extends GridStackDD {
5654
} else if (opts === 'option') {
5755
dEl.setupDraggable({ [key]: value });
5856
} else {
57+
const grid = el.gridstackNode.grid;
5958
dEl.setupDraggable({
60-
...this.grid.opts.draggable,
59+
...grid.opts.draggable,
6160
...{
62-
containment: (this.grid.opts._isNested && !this.grid.opts.dragOut)
63-
? this.grid.el.parentElement
64-
: (this.grid.opts.draggable.containment || null),
61+
containment: (grid.opts._isNested && !grid.opts.dragOut)
62+
? grid.el.parentElement
63+
: (grid.opts.draggable.containment || null),
6564
start: opts.start,
6665
stop: opts.stop,
6766
drag: opts.drag

src/gridstack-dd.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
/* eslint-disable @typescript-eslint/no-unused-vars */
99

10-
import { GridStack, GridStackElement } from './gridstack';
10+
import { GridStackElement } from './gridstack';
1111
import { GridItemHTMLElement, DDDragInOpt } from './types';
1212

1313
/** Drag&Drop drop options */
@@ -29,7 +29,6 @@ export type DDCallback = (event: Event, arg2: GridItemHTMLElement, helper?: Grid
2929
* Base class for drag'n'drop plugin.
3030
*/
3131
export class GridStackDD {
32-
protected grid: GridStack;
3332
static registeredPlugins: typeof GridStackDD;
3433

3534
/** call this method to register your plugin instead of the default no-op one */
@@ -42,10 +41,6 @@ export class GridStackDD {
4241
return GridStackDD.registeredPlugins || GridStackDD;
4342
}
4443

45-
public constructor(grid: GridStack) {
46-
this.grid = grid;
47-
}
48-
4944
/** removes any drag&drop present (called during destroy) */
5045
public remove(el: GridItemHTMLElement): GridStackDD {
5146
this.draggable(el, 'destroy').resizable(el, 'destroy');

src/gridstack.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ interface GridCSSStyleSheet extends CSSStyleSheet {
4242
_max?: number; // internal tracker of the max # of rows we created\
4343
}
4444

45+
/** current drag&drop plugin being used - first grid will initialize */
46+
let dd: GridStackDD;
47+
4548
/**
4649
* Main gridstack class - you will need to call `GridStack.init()` first to initialize your grid.
4750
* Note: your grid elements MUST have the following classes for the CSS layout to work:
@@ -123,9 +126,6 @@ export class GridStack {
123126
/** grid options - public for classes to access, but use methods to modify! */
124127
public opts: GridStackOptions;
125128

126-
/** current drag&drop plugin being used */
127-
public dd: GridStackDD;
128-
129129
/** @internal */
130130
private placeholder: HTMLElement;
131131
/** @internal */
@@ -223,12 +223,7 @@ export class GridStack {
223223
this.opts = Utils.defaults(opts, defaults);
224224
this.initMargin();
225225

226-
if (this.opts.ddPlugin === false) {
227-
this.opts.ddPlugin = GridStackDD;
228-
} else if (this.opts.ddPlugin === undefined) {
229-
this.opts.ddPlugin = GridStackDD.get();
230-
}
231-
this.dd = new (this.opts.ddPlugin as typeof GridStackDD)(this);
226+
dd = dd || new (GridStackDD.get())();
232227

233228
if (this.opts.rtl === 'auto') {
234229
this.opts.rtl = el.style.direction === 'rtl';
@@ -792,11 +787,11 @@ export class GridStack {
792787
if (!node) { return }
793788
node.noMove = !(val || false);
794789
if (node.noMove) {
795-
this.dd.draggable(el, 'disable');
790+
dd.draggable(el, 'disable');
796791
el.classList.remove('ui-draggable-handle');
797792
} else {
798793
this._prepareDragDropByNode(node); // init DD if need be
799-
this.dd.draggable(el, 'enable');
794+
dd.draggable(el, 'enable');
800795
el.classList.remove('ui-draggable-handle');
801796
}
802797
});
@@ -904,7 +899,7 @@ export class GridStack {
904899

905900
// remove our DOM data (circular link) and drag&drop permanently
906901
delete el.gridstackNode;
907-
this.dd.remove(el);
902+
dd.remove(el);
908903

909904
this.engine.removeNode(node, removeDOM, triggerEvent);
910905

@@ -927,7 +922,7 @@ export class GridStack {
927922
// always remove our DOM data (circular link) before list gets emptied and drag&drop permanently
928923
this.engine.nodes.forEach(n => {
929924
delete n.el.gridstackNode;
930-
this.dd.remove(n.el);
925+
dd.remove(n.el);
931926
});
932927
this.engine.removeAll(removeDOM);
933928
this._triggerRemoveEvent();
@@ -962,10 +957,10 @@ export class GridStack {
962957
if (!node) { return; }
963958
node.noResize = !(val || false);
964959
if (node.noResize) {
965-
this.dd.resizable(el, 'disable');
960+
dd.resizable(el, 'disable');
966961
} else {
967962
this._prepareDragDropByNode(node); // init DD if need be
968-
this.dd.resizable(el, 'enable');
963+
dd.resizable(el, 'enable');
969964
}
970965
});
971966
return this;
@@ -1230,7 +1225,7 @@ export class GridStack {
12301225
if (this.opts.staticGrid || node.locked ||
12311226
((node.noMove || this.opts.disableDrag) && (node.noResize || this.opts.disableResize))) {
12321227
if (node._initDD) {
1233-
this.dd.remove(node.el); // nukes everything instead of just disable, will add some styles back next
1228+
dd.remove(node.el); // nukes everything instead of just disable, will add some styles back next
12341229
delete node._initDD;
12351230
}
12361231
node.el.classList.add('ui-draggable-disabled', 'ui-resizable-disabled'); // add styles one might depend on #1435
@@ -1274,8 +1269,8 @@ export class GridStack {
12741269
node._beforeDragY = node.y;
12751270
node._prevYPix = ui.position.top;
12761271

1277-
this.dd.resizable(el, 'option', 'minWidth', cellWidth * (node.minWidth || 1));
1278-
this.dd.resizable(el, 'option', 'minHeight', cellHeight * (node.minHeight || 1));
1272+
dd.resizable(el, 'option', 'minWidth', cellWidth * (node.minWidth || 1));
1273+
dd.resizable(el, 'option', 'minHeight', cellHeight * (node.minHeight || 1));
12791274
}
12801275

12811276
/** called when item is being dragged/resized */
@@ -1357,7 +1352,7 @@ export class GridStack {
13571352
gridToNotify._gsEventHandler[event.type](event, target);
13581353
}
13591354
gridToNotify.engine.removedNodes.push(node);
1360-
gridToNotify.dd.remove(el);
1355+
dd.remove(el);
13611356
delete el.gridstackNode; // hint we're removing it next and break circular link
13621357
gridToNotify._triggerRemoveEvent();
13631358
if (el.parentElement) {
@@ -1392,7 +1387,7 @@ export class GridStack {
13921387
}
13931388
}
13941389

1395-
this.dd
1390+
dd
13961391
.draggable(el, {
13971392
start: onStartMoving,
13981393
stop: onEndMoving,
@@ -1407,10 +1402,10 @@ export class GridStack {
14071402

14081403
// finally fine tune drag vs move by disabling any part...
14091404
if (node.noMove || this.opts.disableDrag) {
1410-
this.dd.draggable(el, 'disable');
1405+
dd.draggable(el, 'disable');
14111406
}
14121407
if (node.noResize || this.opts.disableResize) {
1413-
this.dd.resizable(el, 'disable');
1408+
dd.resizable(el, 'disable');
14141409
}
14151410
return this;
14161411
}
@@ -1585,8 +1580,8 @@ export class GridStack {
15851580
/** @internal call to setup dragging in from the outside (say toolbar), with options */
15861581
private _setupDragIn(): GridStack {
15871582
if (!this.opts.staticGrid && typeof this.opts.dragIn === 'string') {
1588-
if (!this.dd.isDraggable(this.opts.dragIn)) {
1589-
this.dd.dragIn(this.opts.dragIn, this.opts.dragInOptions);
1583+
if (!dd.isDraggable(this.opts.dragIn)) {
1584+
dd.dragIn(this.opts.dragIn, this.opts.dragInOptions);
15901585
}
15911586
}
15921587
return this;
@@ -1597,10 +1592,10 @@ export class GridStack {
15971592
if (!this.opts.staticGrid && typeof this.opts.removable === 'string') {
15981593
let trashZone = document.querySelector(this.opts.removable) as HTMLElement;
15991594
if (!trashZone) return this;
1600-
if (!this.dd.isDroppable(trashZone)) {
1601-
this.dd.droppable(trashZone, this.opts.removableOptions);
1595+
if (!dd.isDroppable(trashZone)) {
1596+
dd.droppable(trashZone, this.opts.removableOptions);
16021597
}
1603-
this.dd
1598+
dd
16041599
.on(trashZone, 'dropover', (event, el) => {
16051600
let node = el.gridstackNode;
16061601
if (!node || node.grid !== this) return;
@@ -1650,7 +1645,7 @@ export class GridStack {
16501645
}
16511646
};
16521647

1653-
this.dd
1648+
dd
16541649
.droppable(this.el, {
16551650
accept: (el: GridItemHTMLElement) => {
16561651
let node: GridStackNode = el.gridstackNode;
@@ -1690,7 +1685,7 @@ export class GridStack {
16901685
el.gridstackNode = newNode;
16911686
el._gridstackNodeOrig = node;
16921687

1693-
this.dd.on(el, 'drag', onDrag);
1688+
dd.on(el, 'drag', onDrag);
16941689
return false; // prevent parent from receiving msg (which may be grid as well)
16951690
})
16961691
.on(this.el, 'dropout', (event, el: GridItemHTMLElement) => {
@@ -1700,7 +1695,7 @@ export class GridStack {
17001695
if (!node || !node._isOutOfGrid) {
17011696
return;
17021697
}
1703-
this.dd.off(el, 'drag');
1698+
dd.off(el, 'drag');
17041699
node.el = null;
17051700
this.engine.removeNode(node);
17061701
if (this.placeholder.parentNode === this.el) {
@@ -1729,7 +1724,7 @@ export class GridStack {
17291724
this.engine.cleanupNode(node); // removes all internal _xyz values (including the _id so add that back)
17301725
node._id = _id;
17311726
node.grid = this;
1732-
this.dd.off(el, 'drag');
1727+
dd.off(el, 'drag');
17331728
// if we made a copy ('helper' which is temp) of the original node then insert a copy, else we move the original node (#1102)
17341729
// as the helper will be nuked by jqueryui otherwise
17351730
if (helper !== el) {
@@ -1738,7 +1733,7 @@ export class GridStack {
17381733
el = el.cloneNode(true) as GridItemHTMLElement;
17391734
} else {
17401735
el.remove(); // reduce flicker as we change depth here, and size further down
1741-
this.dd.remove(el);
1736+
dd.remove(el);
17421737
}
17431738
el.gridstackNode = node;
17441739
node.el = el;

src/jq/gridstack-dd-jqueryui.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* gridstack.js may be freely distributed under the MIT license.
77
*/
88

9-
import { GridStack, GridStackElement } from '../gridstack';
9+
import { GridStackElement } from '../gridstack';
1010
import { GridStackDD, DDOpts, DDKey, DDDropOpt, DDCallback, DDValue } from '../gridstack-dd';
1111
import { GridItemHTMLElement, DDDragInOpt } from '../types';
1212

@@ -22,9 +22,6 @@ import './jquery-ui';
2222
* legacy Jquery-ui based drag'n'drop plugin.
2323
*/
2424
export class GridStackDDJQueryUI extends GridStackDD {
25-
public constructor(grid: GridStack) {
26-
super(grid);
27-
}
2825

2926
public resizable(el: GridItemHTMLElement, opts: DDOpts, key?: DDKey, value?: DDValue): GridStackDD {
3027
let $el: JQuery = $(el);
@@ -37,9 +34,10 @@ export class GridStackDDJQueryUI extends GridStackDD {
3734
} else if (opts === 'option') {
3835
$el.resizable(opts, key, value);
3936
} else {
40-
let handles = $el.data('gs-resize-handles') ? $el.data('gs-resize-handles') : this.grid.opts.resizable.handles;
37+
const grid = el.gridstackNode.grid;
38+
let handles = $el.data('gs-resize-handles') ? $el.data('gs-resize-handles') : grid.opts.resizable.handles;
4139
$el.resizable({
42-
...this.grid.opts.resizable,
40+
...grid.opts.resizable,
4341
...{ handles: handles },
4442
...{
4543
start: opts.start, // || function() {},
@@ -62,9 +60,10 @@ export class GridStackDDJQueryUI extends GridStackDD {
6260
} else if (opts === 'option') {
6361
$el.draggable(opts, key, value);
6462
} else {
65-
$el.draggable({...this.grid.opts.draggable, ...{ // was using $.extend()
66-
containment: (this.grid.opts._isNested && !this.grid.opts.dragOut) ?
67-
$(this.grid.el).parent() : (this.grid.opts.draggable.containment || null),
63+
const grid = el.gridstackNode.grid;
64+
$el.draggable({...grid.opts.draggable, ...{ // was using $.extend()
65+
containment: (grid.opts._isNested && !grid.opts.dragOut) ?
66+
$(grid.el).parent() : (grid.opts.draggable.containment || null),
6867
start: opts.start, // || function() {},
6968
stop: opts.stop, // || function() {},
7069
drag: opts.drag // || function() {}

src/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import { GridStack } from './gridstack';
10-
import { GridStackDD } from './gridstack-dd';
1110

1211

1312
/** different layout options when changing # of columns,
@@ -61,11 +60,6 @@ export interface GridStackOptions {
6160
/** number of columns (default?: 12). Note: IF you change this, CSS also have to change. See https://github.com/gridstack/gridstack.js#change-grid-columns */
6261
column?: number;
6362

64-
/** class that implement drag'n'drop functionality for gridstack. If false grid will be static.
65-
* (default?: undefined - first available plugin will be used)
66-
*/
67-
ddPlugin?: false | typeof GridStackDD;
68-
6963
/** disallows dragging of widgets (default?: false) */
7064
disableDrag?: boolean;
7165

0 commit comments

Comments
 (0)