@@ -493,6 +493,7 @@ describe('CdkDrag', () => {
493
493
expect ( dragElement . style . touchAction )
494
494
. not . toEqual ( 'none' , 'should not disable touchAction on when there is a drag handle' ) ;
495
495
} ) ;
496
+
496
497
it ( 'should be able to reset a freely-dragged item to its initial position' , fakeAsync ( ( ) => {
497
498
const fixture = createComponent ( StandaloneDraggable ) ;
498
499
fixture . detectChanges ( ) ;
@@ -545,6 +546,17 @@ describe('CdkDrag', () => {
545
546
expect ( fixture . componentInstance . endedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
546
547
} ) ) ;
547
548
549
+ it ( 'should allow for dragging to be constrained to an elemnet' , fakeAsync ( ( ) => {
550
+ const fixture = createComponent ( StandaloneDraggable ) ;
551
+ fixture . componentInstance . boundrySelector = '.wrapper' ;
552
+ fixture . detectChanges ( ) ;
553
+ const dragElement = fixture . componentInstance . dragElement . nativeElement ;
554
+
555
+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
556
+ dragElementViaMouse ( fixture , dragElement , 300 , 300 ) ;
557
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(100px, 100px, 0px)' ) ;
558
+ } ) ) ;
559
+
548
560
} ) ;
549
561
550
562
describe ( 'draggable with a handle' , ( ) => {
@@ -989,6 +1001,29 @@ describe('CdkDrag', () => {
989
1001
expect ( preview . parentNode ) . toBeFalsy ( 'Expected preview to be removed from the DOM' ) ;
990
1002
} ) ) ;
991
1003
1004
+ it ( 'should be able to constrain the preview position' , fakeAsync ( ( ) => {
1005
+ const fixture = createComponent ( DraggableInDropZone ) ;
1006
+ fixture . componentInstance . boundrySelector = '.cdk-drop-list' ;
1007
+ fixture . detectChanges ( ) ;
1008
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
1009
+ const listRect =
1010
+ fixture . componentInstance . dropInstance . element . nativeElement . getBoundingClientRect ( ) ;
1011
+
1012
+ startDraggingViaMouse ( fixture , item ) ;
1013
+
1014
+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1015
+
1016
+ startDraggingViaMouse ( fixture , item , listRect . right + 50 , listRect . bottom + 50 ) ;
1017
+ flush ( ) ;
1018
+ dispatchMouseEvent ( document , 'mousemove' , listRect . right + 50 , listRect . bottom + 50 ) ;
1019
+ fixture . detectChanges ( ) ;
1020
+
1021
+ const previewRect = preview . getBoundingClientRect ( ) ;
1022
+
1023
+ expect ( Math . floor ( previewRect . bottom ) ) . toBe ( Math . floor ( listRect . bottom ) ) ;
1024
+ expect ( Math . floor ( previewRect . right ) ) . toBe ( Math . floor ( listRect . right ) ) ;
1025
+ } ) ) ;
1026
+
992
1027
it ( 'should clear the id from the preview' , fakeAsync ( ( ) => {
993
1028
const fixture = createComponent ( DraggableInDropZone ) ;
994
1029
fixture . detectChanges ( ) ;
@@ -1040,7 +1075,7 @@ describe('CdkDrag', () => {
1040
1075
preview . style . transitionDuration = '500ms' ;
1041
1076
1042
1077
// Move somewhere so the draggable doesn't exit immediately.
1043
- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1078
+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
1044
1079
fixture . detectChanges ( ) ;
1045
1080
1046
1081
dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1098,7 +1133,7 @@ describe('CdkDrag', () => {
1098
1133
const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1099
1134
preview . style . transition = 'opacity 500ms ease' ;
1100
1135
1101
- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1136
+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
1102
1137
fixture . detectChanges ( ) ;
1103
1138
1104
1139
dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1120,7 +1155,7 @@ describe('CdkDrag', () => {
1120
1155
const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1121
1156
preview . style . transition = 'opacity 500ms ease, transform 1000ms ease' ;
1122
1157
1123
- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1158
+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
1124
1159
fixture . detectChanges ( ) ;
1125
1160
1126
1161
dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1611,6 +1646,29 @@ describe('CdkDrag', () => {
1611
1646
expect ( preview . textContent ! . trim ( ) ) . toContain ( 'Custom preview' ) ;
1612
1647
} ) ) ;
1613
1648
1649
+ it ( 'should be able to constrain the position of a custom preview' , fakeAsync ( ( ) => {
1650
+ const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
1651
+ fixture . componentInstance . boundrySelector = '.cdk-drop-list' ;
1652
+ fixture . detectChanges ( ) ;
1653
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
1654
+ const listRect =
1655
+ fixture . componentInstance . dropInstance . element . nativeElement . getBoundingClientRect ( ) ;
1656
+
1657
+ startDraggingViaMouse ( fixture , item ) ;
1658
+
1659
+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1660
+
1661
+ startDraggingViaMouse ( fixture , item , listRect . right + 50 , listRect . bottom + 50 ) ;
1662
+ flush ( ) ;
1663
+ dispatchMouseEvent ( document , 'mousemove' , listRect . right + 50 , listRect . bottom + 50 ) ;
1664
+ fixture . detectChanges ( ) ;
1665
+
1666
+ const previewRect = preview . getBoundingClientRect ( ) ;
1667
+
1668
+ expect ( Math . floor ( previewRect . bottom ) ) . toBe ( Math . floor ( listRect . bottom ) ) ;
1669
+ expect ( Math . floor ( previewRect . right ) ) . toBe ( Math . floor ( listRect . right ) ) ;
1670
+ } ) ) ;
1671
+
1614
1672
it ( 'should revert the element back to its parent after dragging with a custom ' +
1615
1673
'preview has stopped' , fakeAsync ( ( ) => {
1616
1674
const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
@@ -2214,19 +2272,23 @@ describe('CdkDrag', () => {
2214
2272
2215
2273
@Component ( {
2216
2274
template : `
2217
- <div
2218
- cdkDrag
2219
- (cdkDragStarted)="startedSpy($event)"
2220
- (cdkDragEnded)="endedSpy($event)"
2221
- #dragElement
2222
- style="width: 100px; height: 100px; background: red;"></div>
2275
+ <div class="wrapper" style="width: 200px; height: 200px; background: green;">
2276
+ <div
2277
+ cdkDrag
2278
+ [cdkDragBoundry]="boundrySelector"
2279
+ (cdkDragStarted)="startedSpy($event)"
2280
+ (cdkDragEnded)="endedSpy($event)"
2281
+ #dragElement
2282
+ style="width: 100px; height: 100px; background: red;"></div>
2283
+ </div>
2223
2284
`
2224
2285
} )
2225
2286
class StandaloneDraggable {
2226
2287
@ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
2227
2288
@ViewChild ( CdkDrag ) dragInstance : CdkDrag ;
2228
2289
startedSpy = jasmine . createSpy ( 'started spy' ) ;
2229
2290
endedSpy = jasmine . createSpy ( 'ended spy' ) ;
2291
+ boundrySelector : string ;
2230
2292
}
2231
2293
2232
2294
@Component ( {
@@ -2317,6 +2379,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
2317
2379
*ngFor="let item of items"
2318
2380
cdkDrag
2319
2381
[cdkDragData]="item"
2382
+ [cdkDragBoundry]="boundrySelector"
2320
2383
[style.height.px]="item.height"
2321
2384
[style.margin-bottom.px]="item.margin"
2322
2385
style="width: 100%; background: red;">{{item.value}}</div>
@@ -2334,6 +2397,7 @@ class DraggableInDropZone {
2334
2397
{ value : 'Three' , height : ITEM_HEIGHT , margin : 0 }
2335
2398
] ;
2336
2399
dropZoneId = 'items' ;
2400
+ boundrySelector : string ;
2337
2401
sortedSpy = jasmine . createSpy ( 'sorted spy' ) ;
2338
2402
droppedSpy = jasmine . createSpy ( 'dropped spy' ) . and . callFake ( ( event : CdkDragDrop < string [ ] > ) => {
2339
2403
moveItemInArray ( this . items , event . previousIndex , event . currentIndex ) ;
@@ -2396,10 +2460,16 @@ class DraggableInHorizontalDropZone {
2396
2460
@Component ( {
2397
2461
template : `
2398
2462
<div cdkDropList style="width: 100px; background: pink;">
2399
- <div *ngFor="let item of items" cdkDrag
2463
+ <div
2464
+ *ngFor="let item of items"
2465
+ cdkDrag
2466
+ [cdkDragBoundry]="boundrySelector"
2400
2467
style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">
2401
2468
{{item}}
2402
- <div class="custom-preview" *cdkDragPreview>Custom preview</div>
2469
+ <div
2470
+ class="custom-preview"
2471
+ style="width: 50px; height: 50px; background: purple;"
2472
+ *cdkDragPreview>Custom preview</div>
2403
2473
</div>
2404
2474
</div>
2405
2475
`
@@ -2408,6 +2478,7 @@ class DraggableInDropZoneWithCustomPreview {
2408
2478
@ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
2409
2479
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
2410
2480
items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2481
+ boundrySelector : string ;
2411
2482
}
2412
2483
2413
2484
0 commit comments