Skip to content

Commit 9831d36

Browse files
committed
feat: add a way of setting the scroll container if not the window
1 parent c174023 commit 9831d36

4 files changed

+55
-7
lines changed

src/drag-and-drop.module.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import { NgModule } from '@angular/core';
22
import { DraggableDirective } from './draggable.directive';
33
import { DroppableDirective } from './droppable.directive';
4+
import { DraggableScrollContainerDirective } from './draggable-scroll-container.directive';
45

56
@NgModule({
6-
declarations: [DraggableDirective, DroppableDirective],
7-
exports: [DraggableDirective, DroppableDirective]
7+
declarations: [
8+
DraggableDirective,
9+
DroppableDirective,
10+
DraggableScrollContainerDirective
11+
],
12+
exports: [
13+
DraggableDirective,
14+
DroppableDirective,
15+
DraggableScrollContainerDirective
16+
]
817
})
918
export class DragAndDropModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Directive, ElementRef } from '@angular/core';
2+
3+
@Directive({
4+
selector: '[mwlDraggableScrollContainer]'
5+
})
6+
export class DraggableScrollContainerDirective {
7+
constructor(public elementRef: ElementRef) {}
8+
}

src/droppable.directive.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
EventEmitter,
88
NgZone,
99
Input,
10-
Renderer2
10+
Renderer2,
11+
Optional
1112
} from '@angular/core';
1213
import { Subscription } from 'rxjs';
1314
import { distinctUntilChanged, pairwise, filter, map } from 'rxjs/operators';
1415
import { DraggableHelper } from './draggable-helper.provider';
16+
import { DraggableScrollContainerDirective } from './draggable-scroll-container.directive';
1517

1618
function isCoordinateWithinRectangle(
1719
clientX: number,
@@ -70,7 +72,8 @@ export class DroppableDirective implements OnInit, OnDestroy {
7072
private element: ElementRef<HTMLElement>,
7173
private draggableHelper: DraggableHelper,
7274
private zone: NgZone,
73-
private renderer: Renderer2
75+
private renderer: Renderer2,
76+
@Optional() private scrollContainer: DraggableScrollContainerDirective
7477
) {}
7578

7679
ngOnInit() {
@@ -84,7 +87,9 @@ export class DroppableDirective implements OnInit, OnDestroy {
8487

8588
/* istanbul ignore next */
8689
const deregisterScrollListener = this.renderer.listen(
87-
'window',
90+
this.scrollContainer
91+
? this.scrollContainer.elementRef.nativeElement
92+
: 'window',
8893
'scroll',
8994
() => {
9095
droppableRectangle = this.element.nativeElement.getBoundingClientRect();

test/droppable.directive.spec.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as sinon from 'sinon';
55
import { triggerDomEvent } from './util';
66
import { DragAndDropModule } from '../src/index';
77
import { DraggableDirective } from '../src/draggable.directive';
8-
import { st } from '@angular/core/src/render3';
98
import { DroppableDirective } from '../src/droppable.directive';
9+
import { DraggableScrollContainerDirective } from '../src/draggable-scroll-container.directive';
1010

1111
describe('droppable directive', () => {
1212
@Component({
@@ -64,10 +64,28 @@ describe('droppable directive', () => {
6464
dragActiveClass: string;
6565
}
6666

67+
@Component({
68+
// tslint:disable-line max-classes-per-file
69+
template: `
70+
<div mwlDraggableScrollContainer>
71+
<div
72+
#droppableElement
73+
mwlDroppable>
74+
Drop here
75+
</div>
76+
</div>
77+
`
78+
})
79+
class ScrollTestComponent {
80+
@ViewChild(DroppableDirective) droppable: DroppableDirective;
81+
@ViewChild(DraggableScrollContainerDirective)
82+
scrollContainer: DraggableScrollContainerDirective;
83+
}
84+
6785
beforeEach(() => {
6886
TestBed.configureTestingModule({
6987
imports: [DragAndDropModule],
70-
declarations: [TestComponent]
88+
declarations: [TestComponent, ScrollTestComponent]
7189
});
7290
});
7391

@@ -228,4 +246,12 @@ describe('droppable directive', () => {
228246
fixture = TestBed.createComponent(TestComponent);
229247
expect(() => fixture.destroy()).not.to.throw();
230248
});
249+
250+
it('should link the droppable element to the scroll container', () => {
251+
const scrollFixture = TestBed.createComponent(ScrollTestComponent);
252+
scrollFixture.detectChanges();
253+
expect(
254+
scrollFixture.componentInstance.droppable['scrollContainer']
255+
).to.equal(scrollFixture.componentInstance.scrollContainer);
256+
});
231257
});

0 commit comments

Comments
 (0)