Skip to content

Commit c79332a

Browse files
MKirovaMKirova
authored andcommitted
fix(moving): Remove some evt listeners and detach subscribers.
1 parent 0cad70d commit c79332a

File tree

8 files changed

+73
-26
lines changed

8 files changed

+73
-26
lines changed

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,7 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
762762
fromEvent(document.defaultView, 'mouseup').pipe(takeUntil(this._destroy))
763763
.subscribe((res) => this.onPointerUp(res));
764764
}
765-
766-
this.element.nativeElement.addEventListener('transitionend', (args) => {
767-
this.onTransitionEnd(args);
768-
});
765+
this.element.nativeElement.addEventListener('transitionend', this.onTransitionEnd.bind(this));
769766
});
770767

771768
// Set transition duration to 0s. This also helps with setting `visibility: hidden` to the base to not lag.
@@ -789,6 +786,8 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
789786
}
790787
}
791788

789+
this.element.nativeElement.removeEventListener('transitionend', this.onTransitionEnd);
790+
792791
if (this._containerScrollIntervalId) {
793792
clearInterval(this._containerScrollIntervalId);
794793
this._containerScrollIntervalId = null;
@@ -1194,12 +1193,7 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
11941193
if (ghostDestroyArgs.cancel) {
11951194
return;
11961195
}
1197-
this.ghostElement.remove();
1198-
this.ghostElement = null;
1199-
if (this._dynamicGhostRef) {
1200-
this._dynamicGhostRef.destroy();
1201-
this._dynamicGhostRef = null;
1202-
}
1196+
this.clearGhost();
12031197
} else if (!this.ghost) {
12041198
this.element.nativeElement.style.transitionProperty = '';
12051199
this.element.nativeElement.style.transitionDuration = '0.0s';
@@ -1222,6 +1216,20 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
12221216
});
12231217
}
12241218

1219+
protected clearGhost() {
1220+
this.ghostElement.removeEventListener('pointermove', this.onPointerMove);
1221+
this.ghostElement.removeEventListener('pointerup', this.onPointerUp);
1222+
this.ghostElement.removeEventListener('lostpointercapture', this.onPointerLost);
1223+
this.ghostElement.removeEventListener('transitionend', this.onTransitionEnd);
1224+
this.ghostElement.remove();
1225+
this.ghostElement = null;
1226+
1227+
if (this._dynamicGhostRef) {
1228+
this._dynamicGhostRef.destroy();
1229+
this._dynamicGhostRef = null;
1230+
}
1231+
}
1232+
12251233
/**
12261234
* @hidden
12271235
* Create ghost element - if a Node object is provided it creates a clone of that node,
@@ -1298,21 +1306,13 @@ export class IgxDragDirective implements AfterContentInit, OnDestroy {
12981306
if (this._pointerDownId !== null) {
12991307
this.ghostElement.setPointerCapture(this._pointerDownId);
13001308
}
1301-
this.ghostElement.addEventListener('pointermove', (args) => {
1302-
this.onPointerMove(args);
1303-
});
1304-
this.ghostElement.addEventListener('pointerup', (args) => {
1305-
this.onPointerUp(args);
1306-
});
1307-
this.ghostElement.addEventListener('lostpointercapture', (args) => {
1308-
this.onPointerLost(args);
1309-
});
1309+
this.ghostElement.addEventListener('pointermove', this.onPointerMove.bind(this));
1310+
this.ghostElement.addEventListener('pointerup', this.onPointerUp.bind(this));
1311+
this.ghostElement.addEventListener('lostpointercapture', this.onPointerLost.bind(this));
13101312
}
13111313

13121314
// Transition animation when the ghostElement is released and it returns to it's original position.
1313-
this.ghostElement.addEventListener('transitionend', (args) => {
1314-
this.onTransitionEnd(args);
1315-
});
1315+
this.ghostElement.addEventListener('transitionend', this.onTransitionEnd.bind(this));
13161316

13171317
this.cdr.detectChanges();
13181318
}

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3911,7 +3911,7 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
39113911
this.onPinnedRowsChanged(change);
39123912
});
39133913

3914-
this.addRowSnackbar?.clicked.subscribe(() => {
3914+
this.addRowSnackbar?.clicked.pipe(takeUntil(this.destroy$)).subscribe(() => {
39153915
const rec = this.filteredSortedData[this.lastAddedRowIndex];
39163916
this.scrollTo(rec, 0);
39173917
this.addRowSnackbar.close();

projects/igniteui-angular/src/lib/grids/moving/moving.drag.directive.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Directive, OnDestroy, Input, ElementRef, ViewContainerRef, NgZone, ChangeDetectorRef, Renderer2 } from '@angular/core';
22
import { IgxDragDirective } from '../../directives/drag-drop/drag-drop.directive';
3-
import { Subscription, fromEvent } from 'rxjs';
3+
import { Subscription, fromEvent, takeUntil } from 'rxjs';
44
import { PlatformUtil } from '../../core/utils';
55
import { IgxColumnMovingService } from './moving.service';
66
import { ColumnType } from '../common/grid.interface';
@@ -73,8 +73,7 @@ export class IgxColumnMovingDragDirective extends IgxDragDirective implements On
7373
source: this.column
7474
};
7575
this.column.grid.columnMovingStart.emit(movingStartArgs);
76-
77-
this.subscription$ = fromEvent(this.column.grid.document.defaultView, 'keydown').subscribe((ev: KeyboardEvent) => {
76+
this.subscription$ = fromEvent(this.column.grid.document.defaultView, 'keydown').pipe(takeUntil(this._destroy)).subscribe((ev: KeyboardEvent) => {
7877
if (ev.key === this.platformUtil.KEYMAP.ESCAPE) {
7978
this.onEscape(ev);
8079
}

src/app/app.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ export class AppComponent implements OnInit {
371371
icon: 'view_column',
372372
name: 'Grid Toolbar'
373373
},
374+
{
375+
link: '/gridReCreate',
376+
icon: 'view_column',
377+
name: 'Grid ReCreate'
378+
},
374379
{
375380
link: '/gridToolbarCustom',
376381
icon: 'view_column',

src/app/app.routes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ import { GridExportComponent } from './grid-export/grid-export.sample';
139139
import { DividerComponent } from './divider/divider.component';
140140
import { MonthPickerSampleComponent } from './month-picker/month-picker.sample';
141141
import { LabelSampleComponent } from "./label/label.sample";
142+
import { GridRecreateSampleComponent } from './grid-re-create/grid-re-create.sample';
142143

143144
export const appRoutes: Routes = [
144145
{
@@ -483,6 +484,10 @@ export const appRoutes: Routes = [
483484
path: 'gridRowDrag',
484485
component: GridRowDraggableComponent
485486
},
487+
{
488+
path: 'gridReCreate',
489+
component: GridRecreateSampleComponent
490+
},
486491
{
487492
path: 'gridToolbar',
488493
component: GridToolbarSampleComponent
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div *ngIf="create">
2+
<igx-grid height="300px">
3+
<igx-column [field]="'columnA'"></igx-column>
4+
<igx-column [field]="'columnB'"></igx-column>
5+
</igx-grid>
6+
</div>
7+
8+
<button (click)="create = !create">Create/Destroy</button>

src/app/grid-re-create/grid-re-create.sample.scss

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { NgFor, NgIf } from '@angular/common';
2+
import { Component, OnInit } from '@angular/core';
3+
import { IgxAvatarComponent, IgxColumnComponent, IgxGridComponent, IgxIconComponent, IgxListComponent, IgxListItemComponent, IgxPageNavigationComponent, IgxPageSizeSelectorComponent, IgxPaginatorComponent, IgxPaginatorContentDirective } from 'igniteui-angular';
4+
5+
6+
@Component({
7+
selector: 'app-grid-sample',
8+
styleUrls: ['grid-re-create.sample.scss'],
9+
templateUrl: 'grid-re-create.sample.html',
10+
standalone: true,
11+
providers: [
12+
],
13+
imports: [NgIf, NgFor, IgxGridComponent, IgxColumnComponent,IgxListComponent, IgxListItemComponent, IgxAvatarComponent, IgxIconComponent,
14+
IgxPaginatorComponent, IgxPaginatorContentDirective, IgxPageSizeSelectorComponent, IgxPageNavigationComponent]
15+
})
16+
export class GridRecreateSampleComponent implements OnInit {
17+
public data = [] as any[];
18+
public create = false;
19+
20+
public ngOnInit(): void {
21+
// new Array(100).fill({}).forEach((_, i) => {
22+
// this.data.push({
23+
// id: i,
24+
// columnA: `A ${i}`,
25+
// columnB: `B ${i}`,
26+
// });
27+
// });
28+
}
29+
30+
}

0 commit comments

Comments
 (0)