Skip to content

Commit

Permalink
when item is disabled, do not add event listeners (#485)
Browse files Browse the repository at this point in the history
* when item is disabled, do not add event listeners

* fix linting issue

* this.args is deprecated

* this.args is deprecated
  • Loading branch information
st-h authored Oct 14, 2022
1 parent 67a201a commit 1d24edc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion addon/modifiers/sortable-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,14 @@ export default class SortableItemModifier extends Modifier {
this.element.addEventListener('keydown', this.keyDown);
this.element.addEventListener('mousedown', this.mouseDown);
this.element.addEventListener('touchstart', this.touchStart);
this.listenersRegistered = true;
}

removeEventListener() {
this.element.removeEventListener('keydown', this.keyDown);
this.element.removeEventListener('mousedown', this.mouseDown);
this.element.removeEventListener('touchstart', this.touchStart);
this.listenersRegistered = false;
}

setupHandleElement() {
Expand All @@ -823,6 +825,11 @@ export default class SortableItemModifier extends Modifier {
element;
didSetup = false;

/**
* tracks if event listeners have been registered. Registering event handlers is unnecessary if item is disabled.
*/
listenersRegistered = false;

constructor(owner, args) {
super(owner, args);
registerDestructor(this, cleanup);
Expand All @@ -839,11 +846,16 @@ export default class SortableItemModifier extends Modifier {
this.setupHandleElement();

if (!this.didSetup) {
this.addEventListener();
this.element.dataset.sortableItem = true;
this.sortableService.registerItem(this.groupName, this);
this.didSetup = true;
}

if (this.named.disabled && this.listenersRegistered) {
this.removeEventListener();
} else if (!this.named.disabled && !this.listenersRegistered) {
this.addEventListener();
}
}
}

Expand Down

0 comments on commit 1d24edc

Please sign in to comment.