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

Skip to content

Commit 790888a

Browse files
authored
Merge pull request gridstack#2709 from nickfulcher/feature/support-multiple-drag-handles
support for multiple drag handles
2 parents 31ecd3b + 0e94f7b commit 790888a

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/dd-draggable.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
5555
protected dragElementOriginStyle: Array<string>;
5656
/** @internal */
5757
protected dragEl: HTMLElement;
58+
/** @internal */
59+
protected dragEls: HTMLElement[];
5860
/** @internal true while we are dragging an item around */
5961
protected dragging: boolean;
6062
/** @internal last drag event */
@@ -80,7 +82,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
8082

8183
// get the element that is actually supposed to be dragged by
8284
let handleName = option.handle.substring(1);
83-
this.dragEl = el.classList.contains(handleName) ? el : el.querySelector(option.handle) || el;
85+
this.dragEls = el.classList.contains(handleName) ? [el] : Array.from(el.querySelectorAll(option.handle));
86+
if (this.dragEls.length === 0) {
87+
this.dragEls = [el];
88+
}
8489
// create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
8590
this._mouseDown = this._mouseDown.bind(this);
8691
this._mouseMove = this._mouseMove.bind(this);
@@ -100,23 +105,27 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
100105
public enable(): void {
101106
if (this.disabled === false) return;
102107
super.enable();
103-
this.dragEl.addEventListener('mousedown', this._mouseDown);
104-
if (isTouch) {
105-
this.dragEl.addEventListener('touchstart', touchstart);
106-
this.dragEl.addEventListener('pointerdown', pointerdown);
107-
// this.dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
108-
}
108+
this.dragEls.forEach(dragEl => {
109+
dragEl.addEventListener('mousedown', this._mouseDown);
110+
if (isTouch) {
111+
dragEl.addEventListener('touchstart', touchstart);
112+
dragEl.addEventListener('pointerdown', pointerdown);
113+
// dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
114+
}
115+
});
109116
this.el.classList.remove('ui-draggable-disabled');
110117
}
111118

112119
public disable(forDestroy = false): void {
113120
if (this.disabled === true) return;
114121
super.disable();
115-
this.dragEl.removeEventListener('mousedown', this._mouseDown);
116-
if (isTouch) {
117-
this.dragEl.removeEventListener('touchstart', touchstart);
118-
this.dragEl.removeEventListener('pointerdown', pointerdown);
119-
}
122+
this.dragEls.forEach(dragEl => {
123+
dragEl.removeEventListener('mousedown', this._mouseDown);
124+
if (isTouch) {
125+
dragEl.removeEventListener('touchstart', touchstart);
126+
dragEl.removeEventListener('pointerdown', pointerdown);
127+
}
128+
});
120129
if (!forDestroy) this.el.classList.add('ui-draggable-disabled');
121130
}
122131

@@ -161,7 +170,7 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
161170
delete DDManager.dragElement;
162171
delete DDManager.dropElement;
163172
// document handler so we can continue receiving moves as the item is 'fixed' position, and capture=true so WE get a first crack
164-
document.addEventListener('mousemove', this._mouseMove, { capture: true, passive: true}); // true=capture, not bubble
173+
document.addEventListener('mousemove', this._mouseMove, { capture: true, passive: true }); // true=capture, not bubble
165174
document.addEventListener('mouseup', this._mouseUp, true);
166175
if (isTouch) {
167176
this.dragEl.addEventListener('touchmove', touchmove);
@@ -292,10 +301,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
292301
this._mouseUp(this.mouseDownEvent);
293302
} else if (e.key === 'r' || e.key === 'R') {
294303
if (!Utils.canBeRotated(n)) return;
295-
n._origRotate = n._origRotate || {...n._orig}; // store the real orig size in case we Esc after doing rotation
304+
n._origRotate = n._origRotate || { ...n._orig }; // store the real orig size in case we Esc after doing rotation
296305
delete n._moving; // force rotate to happen (move waits for >50% coverage otherwise)
297306
grid.setAnimation(false) // immediate rotate so _getDragOffset() gets the right dom size below
298-
.rotate(n.el, {top: -this.dragOffset.offsetTop, left: -this.dragOffset.offsetLeft})
307+
.rotate(n.el, { top: -this.dragOffset.offsetTop, left: -this.dragOffset.offsetLeft })
299308
.setAnimation();
300309
n._moving = true;
301310
this.dragOffset = this._getDragOffset(this.lastDrag, n.el, this.helperContainment);

0 commit comments

Comments
 (0)