Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for multiple drag handles #2709

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/dd-draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
protected dragElementOriginStyle: Array<string>;
/** @internal */
protected dragEl: HTMLElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should remove that if you are using a list instead... and fix line 146

/** @internal */
protected dragEls: HTMLElement[];
/** @internal true while we are dragging an item around */
protected dragging: boolean;
/** @internal last drag event */
Expand All @@ -80,7 +82,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt

// get the element that is actually supposed to be dragged by
let handleName = option.handle.substring(1);
this.dragEl = el.classList.contains(handleName) ? el : el.querySelector(option.handle) || el;
this.dragEls = el.classList.contains(handleName) ? [el] : Array.from(el.querySelectorAll(option.handle));
if (this.dragEls.length === 0) {
this.dragEls = [el];
}
// create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
this._mouseDown = this._mouseDown.bind(this);
this._mouseMove = this._mouseMove.bind(this);
Expand All @@ -100,23 +105,27 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
public enable(): void {
if (this.disabled === false) return;
super.enable();
this.dragEl.addEventListener('mousedown', this._mouseDown);
if (isTouch) {
this.dragEl.addEventListener('touchstart', touchstart);
this.dragEl.addEventListener('pointerdown', pointerdown);
// this.dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
}
this.dragEls.forEach(dragEl => {
dragEl.addEventListener('mousedown', this._mouseDown);
if (isTouch) {
dragEl.addEventListener('touchstart', touchstart);
dragEl.addEventListener('pointerdown', pointerdown);
// dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
}
});
this.el.classList.remove('ui-draggable-disabled');
}

public disable(forDestroy = false): void {
if (this.disabled === true) return;
super.disable();
this.dragEl.removeEventListener('mousedown', this._mouseDown);
if (isTouch) {
this.dragEl.removeEventListener('touchstart', touchstart);
this.dragEl.removeEventListener('pointerdown', pointerdown);
}
this.dragEls.forEach(dragEl => {
dragEl.removeEventListener('mousedown', this._mouseDown);
if (isTouch) {
dragEl.removeEventListener('touchstart', touchstart);
dragEl.removeEventListener('pointerdown', pointerdown);
}
});
if (!forDestroy) this.el.classList.add('ui-draggable-disabled');
}

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