Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81c6493

Browse files
author
Aniket Bansal
committed
- added support for dragging gridstack from outside.
1 parent 0e0784e commit 81c6493

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ionweb/gridstack",
3-
"version": "10.0.1-ion6",
3+
"version": "10.0.1-ion7",
44
"license": "MIT",
55
"author": "Alain Dumesny <[email protected]> (https://github.com/adumesny)",
66
"contributors": [

src/dd-draggable.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface DDDraggableOpt {
1616
handle?: string;
1717
helper?: 'clone' | HTMLElement | ((event: Event) => HTMLElement);
1818
cancel?: string;
19+
dragElement?: string | HTMLElement;
1920
// containment?: string | HTMLElement; // TODO: not implemented yet
2021
// revert?: string | boolean | unknown; // TODO: not implemented yet
2122
// scroll?: boolean;
@@ -58,8 +59,6 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
5859
protected dragScale: DragScaleReciprocal = { x: 1, y: 1 };
5960
/** @internal */
6061
protected dragElementOriginStyle: Array<string>;
61-
/** @internal */
62-
protected dragEl: HTMLElement;
6362
/** @internal true while we are dragging an item around */
6463
protected dragging: boolean;
6564
/** @internal */
@@ -76,16 +75,23 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
7675
this.el = el;
7776
this.option = option;
7877

79-
// get the element that is actually supposed to be dragged by
80-
let handleName = option.handle.substring(1);
81-
this.dragEl = el.classList.contains(handleName) ? el : el.querySelector(option.handle) || el;
8278
// create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
8379
this._mouseDown = this._mouseDown.bind(this);
8480
this._mouseMove = this._mouseMove.bind(this);
8581
this._mouseUp = this._mouseUp.bind(this);
8682
this.enable();
8783
}
8884

85+
/** @internal */
86+
protected get dragElement () {
87+
let handleName = this.option.handle.substring(1);
88+
let dragEl = this.el.classList.contains(handleName) ? this.el : this.el.querySelector(this.option.handle) || this.el;
89+
if (this.option.dragElement) {
90+
dragEl = this.option.dragElement instanceof HTMLElement ? this.option.dragElement : (document.querySelector(this.option.dragElement) ?? dragEl);
91+
}
92+
return dragEl;
93+
}
94+
8995
public on(event: DDDragEvent, callback: (event: DragEvent) => void): void {
9096
super.on(event, callback);
9197
}
@@ -97,10 +103,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
97103
public enable(): void {
98104
if (this.disabled === false) return;
99105
super.enable();
100-
this.dragEl.addEventListener('mousedown', this._mouseDown);
106+
document.addEventListener('mousedown', this._mouseDown);
101107
if (isTouch) {
102-
this.dragEl.addEventListener('touchstart', touchstart);
103-
this.dragEl.addEventListener('pointerdown', pointerdown);
108+
document.addEventListener('touchstart', touchstart);
109+
document.addEventListener('pointerdown', pointerdown);
104110
// this.dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
105111
}
106112
this.el.classList.remove('ui-draggable-disabled');
@@ -109,10 +115,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
109115
public disable(forDestroy = false): void {
110116
if (this.disabled === true) return;
111117
super.disable();
112-
this.dragEl.removeEventListener('mousedown', this._mouseDown);
118+
document.removeEventListener('mousedown', this._mouseDown);
113119
if (isTouch) {
114-
this.dragEl.removeEventListener('touchstart', touchstart);
115-
this.dragEl.removeEventListener('pointerdown', pointerdown);
120+
document.removeEventListener('touchstart', touchstart);
121+
document.removeEventListener('pointerdown', pointerdown);
116122
}
117123
if (!forDestroy) this.el.classList.add('ui-draggable-disabled');
118124
}
@@ -139,6 +145,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
139145
if (DDManager.mouseHandled) return;
140146
if (e.button !== 0) return true; // only left click
141147

148+
if (e.target !== this.dragElement && !this.dragElement.contains(e.target as HTMLElement)) {
149+
return;
150+
}
151+
142152
// make sure we are not clicking on known object that handles mouseDown, or ones supplied by the user
143153
if ((e.target as HTMLElement).closest(skipMouseDown)) return true;
144154
if (this.option.cancel) {
@@ -161,8 +171,8 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
161171
document.addEventListener('mousemove', this._mouseMove, true); // true=capture, not bubble
162172
document.addEventListener('mouseup', this._mouseUp, true);
163173
if (isTouch) {
164-
this.dragEl.addEventListener('touchmove', touchmove);
165-
this.dragEl.addEventListener('touchend', touchend);
174+
this.dragElement.addEventListener('touchmove', touchmove);
175+
this.dragElement.addEventListener('touchend', touchend);
166176
}
167177

168178
e.preventDefault();
@@ -232,8 +242,8 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
232242
document.removeEventListener('mousemove', this._mouseMove, true);
233243
document.removeEventListener('mouseup', this._mouseUp, true);
234244
if (isTouch) {
235-
this.dragEl.removeEventListener('touchmove', touchmove, true);
236-
this.dragEl.removeEventListener('touchend', touchend, true);
245+
this.dragElement.removeEventListener('touchmove', touchmove, true);
246+
this.dragElement.removeEventListener('touchend', touchend, true);
237247
}
238248
if (this.dragging) {
239249
delete this.dragging;

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ export interface DDDragOpt {
377377
scroll?: boolean;
378378
/** prevents dragging from starting on specified elements, listed as comma separated selectors (eg: '.no-drag'). default built in is 'input,textarea,button,select,option' */
379379
cancel?: string;
380+
/** querySelector or HTMLElement reference for the element to be used for dragging the grid-stack-item */
381+
dragElement?: string | HTMLElement;
380382
}
381383
export interface DDDragInOpt extends DDDragOpt {
382384
/** helper function when dropping: 'clone' or your own method */

0 commit comments

Comments
 (0)