Skip to content

Commit c85575b

Browse files
committed
TS: initial dragIn | dragInOptions
* hidding the jquery-ui drag&drop from external (ex: toolbar) functionality. * added grid options: dragIn | dragInOptions
1 parent 5eec1b5 commit c85575b

File tree

7 files changed

+57
-26
lines changed

7 files changed

+57
-26
lines changed

demo/advance.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ <h1>Advanced Demo</h1>
111111
},
112112
removable: '#trash',
113113
removeTimeout: 100,
114+
// dragIn: '.newWidget',
115+
// dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' },
114116
acceptWidgets: '.newWidget'
115117
});
116118

@@ -119,7 +121,6 @@ <h1>Advanced Demo</h1>
119121
items.forEach(function(item) { str += ' (x,y)=' + item.x + ',' + item.y; });
120122
console.log(e.type + ' ' + items.length + ' items:' + str );
121123
});
122-
123124
// TODO: switch jquery-ui out
124125
$('.newWidget').draggable({
125126
revert: 'invalid',

demo/two.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ <h1>Two grids demo</h1>
9797
float: false,
9898
removable: '.trash',
9999
removeTimeout: 100,
100+
dragIn: '.sidebar .grid-stack-item',
100101
acceptWidgets: function(el) { return true; } // function example, else can be simple: true | false | '.someClass' value
101102
};
102103
let grids = GridStack.initAll(options);
@@ -120,14 +121,6 @@ <h1>Two grids demo</h1>
120121
grid.commit();
121122
});
122123

123-
// TODO: switch jquery-ui out
124-
$('.sidebar .grid-stack-item').draggable({
125-
revert: 'invalid',
126-
handle: '.grid-stack-item-content',
127-
scroll: false,
128-
appendTo: 'body',
129-
});
130-
131124
function toggleFloat(button, i) {
132125
grids[i].float(! grids[i].getFloat());
133126
button.innerHTML = 'float: ' + grids[i].getFloat();

doc/CHANGES.md

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

3939
- re-write to native Typescript, removing all JQuery from main code and API (drag&drop plugin still using for now)
4040
- add `getGridItems()` to return list of HTML grid items
41+
- add `{dragIn | dragInOptions}` grid attributes to handle external drag&drop items
4142

4243
## 1.1.2 (2020-05-17)
4344

src/gridstack-dragdrop-plugin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88
/* eslint-disable @typescript-eslint/no-unused-vars */
99

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

1314
/** Drag&Drop drop options */
1415
export type DDDropOpt = {
@@ -48,6 +49,14 @@ export class GridStackDragDropPlugin {
4849
return this;
4950
}
5051

52+
public dragIn(el: GridStackElement, opts: DDDragInOpt): GridStackDragDropPlugin {
53+
return this;
54+
}
55+
56+
public isDraggable(el: GridStackElement): boolean {
57+
return false;
58+
}
59+
5160
public droppable(el: GridItemHTMLElement, opts: DDOpts | DDDropOpt, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
5261
return this;
5362
}

src/gridstack.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ export class GridStack {
187187
scroll: false,
188188
appendTo: 'body'
189189
},
190+
dragIn: undefined,
191+
dragInOptions : {
192+
revert: 'invalid',
193+
handle: '.grid-stack-item-content',
194+
scroll: false,
195+
appendTo: 'body'
196+
},
190197
disableDrag: false,
191198
disableResize: false,
192199
rtl: 'auto',
@@ -283,6 +290,7 @@ export class GridStack {
283290
window.addEventListener('resize', this._onResizeHandler.bind(this));
284291
this._onResizeHandler();
285292

293+
this._setupDragIn();
286294
this._setupRemoveDrop();
287295
this._setupAcceptWidget();
288296
};
@@ -1481,6 +1489,16 @@ export class GridStack {
14811489
return this;
14821490
}
14831491

1492+
/** @internal call to setup dragging in from the outside (say toolbar), with options */
1493+
private _setupDragIn(): GridStack {
1494+
if (!this.opts.staticGrid && typeof this.opts.dragIn === 'string') {
1495+
if (!this.dd.isDraggable(this.opts.dragIn)) {
1496+
this.dd.dragIn(this.opts.dragIn, this.opts.dragInOptions);
1497+
}
1498+
}
1499+
return this;
1500+
}
1501+
14841502
/** @internal called to setup a trash drop zone if the user specifies it */
14851503
private _setupRemoveDrop(): GridStack {
14861504
if (!this.opts.staticGrid && typeof this.opts.removable === 'string') {

src/jq/jqueryui-gridstack-dragdrop-plugin.ts

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

9-
import { GridStack } from '../gridstack';
9+
import { GridStack, GridStackElement } from '../gridstack';
1010
import { GridStackDragDropPlugin, DDOpts, DDKey, DDDropOpt, DDCallback, DDValue } from '../gridstack-dragdrop-plugin';
11-
import { GridItemHTMLElement } from '../types';
11+
import { GridItemHTMLElement, DDDragInOpt } from '../types';
1212

1313
// TODO: TEMPORARY until can remove jquery-ui drag&drop and this class and use HTML5 instead !
1414
// see https://stackoverflow.com/questions/35345760/importing-jqueryui-with-typescript-and-requirejs
@@ -66,6 +66,12 @@ export class JQueryUIGridStackDragDropPlugin extends GridStackDragDropPlugin {
6666
return this;
6767
}
6868

69+
public dragIn(el: GridStackElement, opts: DDDragInOpt): GridStackDragDropPlugin {
70+
let $el: JQuery = $(el);
71+
$el.draggable(opts);
72+
return this;
73+
}
74+
6975
public droppable(el: GridItemHTMLElement, opts: DDOpts | DDDropOpt, key?: DDKey, value?: DDValue): GridStackDragDropPlugin {
7076
let $el: JQuery = $(el);
7177
if (typeof opts.accept === 'function' && !opts._accept) {
@@ -82,6 +88,11 @@ export class JQueryUIGridStackDragDropPlugin extends GridStackDragDropPlugin {
8288
return Boolean($el.data('ui-droppable'));
8389
}
8490

91+
public isDraggable(el: GridStackElement): boolean {
92+
let $el: JQuery = $(el);
93+
return Boolean($el.data('ui-draggable'));
94+
}
95+
8596
public on(el: GridItemHTMLElement, name: string, callback: DDCallback): GridStackDragDropPlugin {
8697
let $el: JQuery = $(el);
8798
$el.on(name, (event, ui) => { callback(event as any, ui.draggable ? ui.draggable.get(0) : event.target) });
@@ -97,17 +108,3 @@ export class JQueryUIGridStackDragDropPlugin extends GridStackDragDropPlugin {
97108

98109
// finally register ourself
99110
GridStackDragDropPlugin.registerPlugin(JQueryUIGridStackDragDropPlugin);
100-
101-
/* OLD code for reference
102-
function JQueryUIGridStackDragDropPlugin(grid) {
103-
GridStack.DragDropPlugin.call(this, grid);
104-
}
105-
106-
GridStack.DragDropPlugin.registerPlugin(JQueryUIGridStackDragDropPlugin);
107-
108-
JQueryUIGridStackDragDropPlugin.prototype = Object.create(GridStack.DragDropPlugin.prototype);
109-
JQueryUIGridStackDragDropPlugin.prototype.constructor = JQueryUIGridStackDragDropPlugin;
110-
....
111-
scope.JQueryUIGridStackDragDropPlugin = JQueryUIGridStackDragDropPlugin;
112-
return JQueryUIGridStackDragDropPlugin;
113-
*/

src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export interface GridstackOptions {
7070
/** allows to override UI draggable options. (default?: { handle?: '.grid-stack-item-content', scroll?: true, appendTo?: 'body', containment: null }) */
7171
draggable?: DDDragOpt;
7272

73+
/** allows to drag external items using this selector - see dragInOption. (default: undefined) */
74+
dragIn?: string;
75+
76+
/** allows to drag external items using these options. (default?: { handle: '.grid-stack-item-content', revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone' }) */
77+
dragInOptions?: DDDragInOpt;
78+
7379
/** let user drag nested grid items out of a parent or not (default false) */
7480
dragOut?: boolean;
7581

@@ -219,6 +225,12 @@ export interface DDDragOpt {
219225
/** parent constraining where item can be dragged out from (default: null = no constrain) */
220226
containment?: string;
221227
}
228+
export interface DDDragInOpt extends DDDragOpt {
229+
/** used when draging item from the outside, and canceling (ex: 'invalid')*/
230+
revert?: string;
231+
/** helper function when dropping (ex: 'clone') */
232+
helper?: string;
233+
}
222234

223235
/**
224236
* internal descriptions describing the items in the grid

0 commit comments

Comments
 (0)