Skip to content

Commit cb5c04f

Browse files
committed
fix(drag-drop): event listeners subscriptions
1 parent 62c53eb commit cb5c04f

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

projects/igniteui-angular/src/lib/directives/drag-drop/drag-drop.directive.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
695695
public renderer: Renderer2,
696696
protected platformUtil: PlatformUtil
697697
) {
698+
this.onTransitionEnd = this.onTransitionEnd.bind(this);
699+
this.onPointerMove = this.onPointerMove.bind(this);
700+
this.onPointerUp = this.onPointerUp.bind(this);
701+
this.onPointerLost = this.onPointerLost.bind(this);
698702
}
699703

700704
/**
@@ -765,7 +769,7 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
765769
fromEvent(this.document.defaultView, 'mouseup').pipe(takeUntil(this._destroy))
766770
.subscribe((res) => this.onPointerUp(res));
767771
}
768-
this.element.nativeElement.addEventListener('transitionend', this.onTransitionEnd.bind(this));
772+
this.element.nativeElement.addEventListener('transitionend', this.onTransitionEnd);
769773
});
770774

771775
// Set transition duration to 0s. This also helps with setting `visibility: hidden` to the base to not lag.
@@ -779,13 +783,11 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
779783
this._destroy.next(true);
780784
this._destroy.complete();
781785

782-
if (this.ghost && this.ghostElement && this._removeOnDestroy) {
783-
this.ghostElement.parentNode.removeChild(this.ghostElement);
784-
this.ghostElement = null;
785-
786-
if (this._dynamicGhostRef) {
787-
this._dynamicGhostRef.destroy();
788-
this._dynamicGhostRef = null;
786+
if (this.ghostElement) {
787+
if (this._removeOnDestroy) {
788+
this.clearGhost();
789+
} else {
790+
this.detachGhost();
789791
}
790792
}
791793

@@ -1219,11 +1221,15 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
12191221
});
12201222
}
12211223

1222-
protected clearGhost() {
1224+
protected detachGhost() {
12231225
this.ghostElement.removeEventListener('pointermove', this.onPointerMove);
12241226
this.ghostElement.removeEventListener('pointerup', this.onPointerUp);
12251227
this.ghostElement.removeEventListener('lostpointercapture', this.onPointerLost);
12261228
this.ghostElement.removeEventListener('transitionend', this.onTransitionEnd);
1229+
}
1230+
1231+
protected clearGhost() {
1232+
this.detachGhost();
12271233
this.ghostElement.remove();
12281234
this.ghostElement = null;
12291235

@@ -1309,13 +1315,13 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
13091315
if (this._pointerDownId !== null) {
13101316
this.ghostElement.setPointerCapture(this._pointerDownId);
13111317
}
1312-
this.ghostElement.addEventListener('pointermove', this.onPointerMove.bind(this));
1313-
this.ghostElement.addEventListener('pointerup', this.onPointerUp.bind(this));
1314-
this.ghostElement.addEventListener('lostpointercapture', this.onPointerLost.bind(this));
1318+
this.ghostElement.addEventListener('pointermove', this.onPointerMove);
1319+
this.ghostElement.addEventListener('pointerup', this.onPointerUp);
1320+
this.ghostElement.addEventListener('lostpointercapture', this.onPointerLost);
13151321
}
13161322

13171323
// Transition animation when the ghostElement is released and it returns to it's original position.
1318-
this.ghostElement.addEventListener('transitionend', this.onTransitionEnd.bind(this));
1324+
this.ghostElement.addEventListener('transitionend', this.onTransitionEnd);
13191325

13201326
this.cdr.detectChanges();
13211327
}

projects/igniteui-angular/src/lib/directives/drag-drop/drag-drop.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('General igxDrag/igxDrop', () => {
2323
let dropAreaRects = { top: 0, left: 0, right: 0, bottom: 0};
2424
let dragDirsRects = [{ top: 0, left: 0, right: 0, bottom: 0}];
2525

26-
configureTestSuite();
26+
configureTestSuite({ checkLeaks: true });
2727
beforeAll(waitForAsync(() => {
2828
TestBed.configureTestingModule({
2929
imports: [TestDragDropComponent]
@@ -40,6 +40,13 @@ describe('General igxDrag/igxDrop', () => {
4040
dropAreaRects = getElemRects(dropArea.element.nativeElement);
4141
});
4242

43+
afterEach(() => {
44+
fix = null;
45+
dragDirsRects = null;
46+
dropArea = null;
47+
dropAreaRects = null;
48+
});
49+
4350
it('should correctly initialize drag and drop directives.', () => {
4451
const ignoredElem = fix.debugElement.query(By.css('.ignoredElem')).nativeElement;
4552

0 commit comments

Comments
 (0)