@@ -567,6 +567,17 @@ describe('CdkDrag', () => {
567567 expect ( dragElement . style . transform ) . toBe ( 'translate3d(13px, 37px, 0px)' ) ;
568568 } ) ) ;
569569
570+ it ( 'should allow for dragging to be constrained to an element' , fakeAsync ( ( ) => {
571+ const fixture = createComponent ( StandaloneDraggable ) ;
572+ fixture . componentInstance . boundarySelector = '.wrapper' ;
573+ fixture . detectChanges ( ) ;
574+ const dragElement = fixture . componentInstance . dragElement . nativeElement ;
575+
576+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
577+ dragElementViaMouse ( fixture , dragElement , 300 , 300 ) ;
578+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(100px, 100px, 0px)' ) ;
579+ } ) ) ;
580+
570581 } ) ;
571582
572583 describe ( 'draggable with a handle' , ( ) => {
@@ -1057,6 +1068,29 @@ describe('CdkDrag', () => {
10571068 expect ( preview . parentNode ) . toBeFalsy ( 'Expected preview to be removed from the DOM' ) ;
10581069 } ) ) ;
10591070
1071+ it ( 'should be able to constrain the preview position' , fakeAsync ( ( ) => {
1072+ const fixture = createComponent ( DraggableInDropZone ) ;
1073+ fixture . componentInstance . boundarySelector = '.cdk-drop-list' ;
1074+ fixture . detectChanges ( ) ;
1075+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
1076+ const listRect =
1077+ fixture . componentInstance . dropInstance . element . nativeElement . getBoundingClientRect ( ) ;
1078+
1079+ startDraggingViaMouse ( fixture , item ) ;
1080+
1081+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1082+
1083+ startDraggingViaMouse ( fixture , item , listRect . right + 50 , listRect . bottom + 50 ) ;
1084+ flush ( ) ;
1085+ dispatchMouseEvent ( document , 'mousemove' , listRect . right + 50 , listRect . bottom + 50 ) ;
1086+ fixture . detectChanges ( ) ;
1087+
1088+ const previewRect = preview . getBoundingClientRect ( ) ;
1089+
1090+ expect ( Math . floor ( previewRect . bottom ) ) . toBe ( Math . floor ( listRect . bottom ) ) ;
1091+ expect ( Math . floor ( previewRect . right ) ) . toBe ( Math . floor ( listRect . right ) ) ;
1092+ } ) ) ;
1093+
10601094 it ( 'should clear the id from the preview' , fakeAsync ( ( ) => {
10611095 const fixture = createComponent ( DraggableInDropZone ) ;
10621096 fixture . detectChanges ( ) ;
@@ -1108,7 +1142,7 @@ describe('CdkDrag', () => {
11081142 preview . style . transitionDuration = '500ms' ;
11091143
11101144 // Move somewhere so the draggable doesn't exit immediately.
1111- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1145+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
11121146 fixture . detectChanges ( ) ;
11131147
11141148 dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1166,7 +1200,7 @@ describe('CdkDrag', () => {
11661200 const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
11671201 preview . style . transition = 'opacity 500ms ease' ;
11681202
1169- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1203+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
11701204 fixture . detectChanges ( ) ;
11711205
11721206 dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1188,7 +1222,7 @@ describe('CdkDrag', () => {
11881222 const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
11891223 preview . style . transition = 'opacity 500ms ease, transform 1000ms ease' ;
11901224
1191- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1225+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
11921226 fixture . detectChanges ( ) ;
11931227
11941228 dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1679,6 +1713,29 @@ describe('CdkDrag', () => {
16791713 expect ( preview . textContent ! . trim ( ) ) . toContain ( 'Custom preview' ) ;
16801714 } ) ) ;
16811715
1716+ it ( 'should be able to constrain the position of a custom preview' , fakeAsync ( ( ) => {
1717+ const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
1718+ fixture . componentInstance . boundarySelector = '.cdk-drop-list' ;
1719+ fixture . detectChanges ( ) ;
1720+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
1721+ const listRect =
1722+ fixture . componentInstance . dropInstance . element . nativeElement . getBoundingClientRect ( ) ;
1723+
1724+ startDraggingViaMouse ( fixture , item ) ;
1725+
1726+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1727+
1728+ startDraggingViaMouse ( fixture , item , listRect . right + 50 , listRect . bottom + 50 ) ;
1729+ flush ( ) ;
1730+ dispatchMouseEvent ( document , 'mousemove' , listRect . right + 50 , listRect . bottom + 50 ) ;
1731+ fixture . detectChanges ( ) ;
1732+
1733+ const previewRect = preview . getBoundingClientRect ( ) ;
1734+
1735+ expect ( Math . floor ( previewRect . bottom ) ) . toBe ( Math . floor ( listRect . bottom ) ) ;
1736+ expect ( Math . floor ( previewRect . right ) ) . toBe ( Math . floor ( listRect . right ) ) ;
1737+ } ) ) ;
1738+
16821739 it ( 'should revert the element back to its parent after dragging with a custom ' +
16831740 'preview has stopped' , fakeAsync ( ( ) => {
16841741 const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
@@ -2298,19 +2355,23 @@ describe('CdkDrag', () => {
22982355
22992356@Component ( {
23002357 template : `
2301- <div
2302- cdkDrag
2303- (cdkDragStarted)="startedSpy($event)"
2304- (cdkDragEnded)="endedSpy($event)"
2305- #dragElement
2306- style="width: 100px; height: 100px; background: red;"></div>
2358+ <div class="wrapper" style="width: 200px; height: 200px; background: green;">
2359+ <div
2360+ cdkDrag
2361+ [cdkDragBoundary]="boundarySelector"
2362+ (cdkDragStarted)="startedSpy($event)"
2363+ (cdkDragEnded)="endedSpy($event)"
2364+ #dragElement
2365+ style="width: 100px; height: 100px; background: red;"></div>
2366+ </div>
23072367 `
23082368} )
23092369class StandaloneDraggable {
23102370 @ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
23112371 @ViewChild ( CdkDrag ) dragInstance : CdkDrag ;
23122372 startedSpy = jasmine . createSpy ( 'started spy' ) ;
23132373 endedSpy = jasmine . createSpy ( 'ended spy' ) ;
2374+ boundarySelector : string ;
23142375}
23152376
23162377@Component ( {
@@ -2414,6 +2475,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
24142475 *ngFor="let item of items"
24152476 cdkDrag
24162477 [cdkDragData]="item"
2478+ [cdkDragBoundary]="boundarySelector"
24172479 [style.height.px]="item.height"
24182480 [style.margin-bottom.px]="item.margin"
24192481 style="width: 100%; background: red;">{{item.value}}</div>
@@ -2431,6 +2493,7 @@ class DraggableInDropZone {
24312493 { value : 'Three' , height : ITEM_HEIGHT , margin : 0 }
24322494 ] ;
24332495 dropZoneId = 'items' ;
2496+ boundarySelector : string ;
24342497 sortedSpy = jasmine . createSpy ( 'sorted spy' ) ;
24352498 droppedSpy = jasmine . createSpy ( 'dropped spy' ) . and . callFake ( ( event : CdkDragDrop < string [ ] > ) => {
24362499 moveItemInArray ( this . items , event . previousIndex , event . currentIndex ) ;
@@ -2493,10 +2556,16 @@ class DraggableInHorizontalDropZone {
24932556@Component ( {
24942557 template : `
24952558 <div cdkDropList style="width: 100px; background: pink;">
2496- <div *ngFor="let item of items" cdkDrag
2559+ <div
2560+ *ngFor="let item of items"
2561+ cdkDrag
2562+ [cdkDragBoundary]="boundarySelector"
24972563 style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">
24982564 {{item}}
2499- <div class="custom-preview" *cdkDragPreview>Custom preview</div>
2565+ <div
2566+ class="custom-preview"
2567+ style="width: 50px; height: 50px; background: purple;"
2568+ *cdkDragPreview>Custom preview</div>
25002569 </div>
25012570 </div>
25022571 `
@@ -2505,6 +2574,7 @@ class DraggableInDropZoneWithCustomPreview {
25052574 @ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
25062575 @ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
25072576 items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2577+ boundarySelector : string ;
25082578}
25092579
25102580
0 commit comments