Skip to content

Commit f40a4c8

Browse files
committed
fix(drag): add throttling to drag event
1 parent 932d474 commit f40a4c8

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

projects/ng-sortgrid-demo/src/app/introduction/introduction.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
22

33
import { IntroductionComponent } from './introduction.component';
4+
import {IntroductionModule} from './introduction.module';
45

56
describe('IntroductionComponent', () => {
67
let component: IntroductionComponent;
78
let fixture: ComponentFixture<IntroductionComponent>;
89

910
beforeEach(async(() => {
1011
TestBed.configureTestingModule({
11-
declarations: [ IntroductionComponent ]
12+
imports: [IntroductionModule]
1213
})
1314
.compileComponents();
1415
}));

projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -219,33 +219,4 @@ describe('NgsgItemDirective', () => {
219219
sut.drop();
220220
expect(consoleWarnSpy).toHaveBeenCalledWith(expectedWarniningMessage);
221221
});
222-
223-
describe('Drag', () => {
224-
225-
it('should not call the scrollHelper if autoScroll is set to false', () => {
226-
spyOn(scrollHelperService, 'scrollIfNecessary');
227-
sut.drag({});
228-
expect(scrollHelperService.scrollIfNecessary).not.toHaveBeenCalled();
229-
});
230-
231-
it('should call the scrollHelper with the event, the scrollpoints and the scrollspeed', () => {
232-
spyOn(scrollHelperService, 'scrollIfNecessary');
233-
const event = 'A very cool event';
234-
const scrollPointTop = 10;
235-
const scrollPointBottom = 80;
236-
const scrollSpeed = 100;
237-
238-
sut.scrollPointTop = scrollPointTop;
239-
sut.scrollPointBottom = scrollPointBottom;
240-
sut.scrollSpeed = scrollSpeed;
241-
sut.autoScroll = true;
242-
243-
sut.drag(event);
244-
expect(scrollHelperService.scrollIfNecessary).toHaveBeenCalledWith(event, {
245-
top: scrollPointTop,
246-
bottom: scrollPointBottom
247-
}, scrollSpeed);
248-
});
249-
250-
});
251222
});

projects/ng-sortgrid/src/lib/ngsg-item.directive.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
SimpleChanges
1313
} from '@angular/core';
1414

15-
import {Subject} from 'rxjs';
16-
import {takeUntil} from 'rxjs/operators';
15+
import {fromEvent, Observable, Subject} from 'rxjs';
16+
import {takeUntil, takeWhile, throttleTime} from 'rxjs/operators';
1717
import {NgsgSortService} from './sort/sort/ngsg-sort.service';
1818
import {NgsgSelectionService} from './mutliselect/ngsg-selection.service';
1919
import {NgsgReflectService} from './sort/reflection/ngsg-reflect.service';
@@ -36,6 +36,7 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
3636

3737
private selected = false;
3838
private destroy$ = new Subject();
39+
private drag$: Observable<Event>;
3940

4041
constructor(
4142
public el: ElementRef,
@@ -52,6 +53,18 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
5253
this.ngsgEventService.dropped$.pipe(
5354
takeUntil(this.destroy$)
5455
).subscribe(() => this.selected = false);
56+
57+
fromEvent<DragEvent>(this.el.nativeElement, 'drag').pipe(
58+
throttleTime(20),
59+
takeUntil(this.destroy$),
60+
takeWhile(() => this.autoScroll)
61+
).subscribe(() => {
62+
this.scrollHelperService.scrollIfNecessary(event, {
63+
top: this.scrollPointTop,
64+
bottom: this.scrollPointBottom
65+
}, this.scrollSpeed);
66+
}
67+
);
5568
}
5669

5770
ngOnChanges(changes: SimpleChanges): void {
@@ -91,16 +104,6 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
91104
this.sortService.sort(this.el.nativeElement);
92105
}
93106

94-
@HostListener('drag', ['$event'])
95-
drag(event): void {
96-
if (this.autoScroll) {
97-
this.scrollHelperService.scrollIfNecessary(event, {
98-
top: this.scrollPointTop,
99-
bottom: this.scrollPointBottom
100-
}, this.scrollSpeed);
101-
}
102-
}
103-
104107
@HostListener('dragover', ['$event'])
105108
dragOver(event): boolean {
106109
if (event.preventDefault) {

0 commit comments

Comments
 (0)