@@ -567,6 +567,17 @@ describe('CdkDrag', () => {
567
567
expect ( dragElement . style . transform ) . toBe ( 'translate3d(13px, 37px, 0px)' ) ;
568
568
} ) ) ;
569
569
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
+
570
581
} ) ;
571
582
572
583
describe ( 'draggable with a handle' , ( ) => {
@@ -1057,6 +1068,29 @@ describe('CdkDrag', () => {
1057
1068
expect ( preview . parentNode ) . toBeFalsy ( 'Expected preview to be removed from the DOM' ) ;
1058
1069
} ) ) ;
1059
1070
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
+
1060
1094
it ( 'should clear the id from the preview' , fakeAsync ( ( ) => {
1061
1095
const fixture = createComponent ( DraggableInDropZone ) ;
1062
1096
fixture . detectChanges ( ) ;
@@ -1108,7 +1142,7 @@ describe('CdkDrag', () => {
1108
1142
preview . style . transitionDuration = '500ms' ;
1109
1143
1110
1144
// Move somewhere so the draggable doesn't exit immediately.
1111
- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1145
+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
1112
1146
fixture . detectChanges ( ) ;
1113
1147
1114
1148
dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1166,7 +1200,7 @@ describe('CdkDrag', () => {
1166
1200
const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1167
1201
preview . style . transition = 'opacity 500ms ease' ;
1168
1202
1169
- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1203
+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
1170
1204
fixture . detectChanges ( ) ;
1171
1205
1172
1206
dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1188,7 +1222,7 @@ describe('CdkDrag', () => {
1188
1222
const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
1189
1223
preview . style . transition = 'opacity 500ms ease, transform 1000ms ease' ;
1190
1224
1191
- dispatchTouchEvent ( document , 'mousemove' , 50 , 50 ) ;
1225
+ dispatchMouseEvent ( document , 'mousemove' , 50 , 50 ) ;
1192
1226
fixture . detectChanges ( ) ;
1193
1227
1194
1228
dispatchMouseEvent ( document , 'mouseup' ) ;
@@ -1679,6 +1713,29 @@ describe('CdkDrag', () => {
1679
1713
expect ( preview . textContent ! . trim ( ) ) . toContain ( 'Custom preview' ) ;
1680
1714
} ) ) ;
1681
1715
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
+
1682
1739
it ( 'should revert the element back to its parent after dragging with a custom ' +
1683
1740
'preview has stopped' , fakeAsync ( ( ) => {
1684
1741
const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
@@ -2298,19 +2355,23 @@ describe('CdkDrag', () => {
2298
2355
2299
2356
@Component ( {
2300
2357
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>
2307
2367
`
2308
2368
} )
2309
2369
class StandaloneDraggable {
2310
2370
@ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
2311
2371
@ViewChild ( CdkDrag ) dragInstance : CdkDrag ;
2312
2372
startedSpy = jasmine . createSpy ( 'started spy' ) ;
2313
2373
endedSpy = jasmine . createSpy ( 'ended spy' ) ;
2374
+ boundarySelector : string ;
2314
2375
}
2315
2376
2316
2377
@Component ( {
@@ -2414,6 +2475,7 @@ const DROP_ZONE_FIXTURE_TEMPLATE = `
2414
2475
*ngFor="let item of items"
2415
2476
cdkDrag
2416
2477
[cdkDragData]="item"
2478
+ [cdkDragBoundary]="boundarySelector"
2417
2479
[style.height.px]="item.height"
2418
2480
[style.margin-bottom.px]="item.margin"
2419
2481
style="width: 100%; background: red;">{{item.value}}</div>
@@ -2431,6 +2493,7 @@ class DraggableInDropZone {
2431
2493
{ value : 'Three' , height : ITEM_HEIGHT , margin : 0 }
2432
2494
] ;
2433
2495
dropZoneId = 'items' ;
2496
+ boundarySelector : string ;
2434
2497
sortedSpy = jasmine . createSpy ( 'sorted spy' ) ;
2435
2498
droppedSpy = jasmine . createSpy ( 'dropped spy' ) . and . callFake ( ( event : CdkDragDrop < string [ ] > ) => {
2436
2499
moveItemInArray ( this . items , event . previousIndex , event . currentIndex ) ;
@@ -2493,10 +2556,16 @@ class DraggableInHorizontalDropZone {
2493
2556
@Component ( {
2494
2557
template : `
2495
2558
<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"
2497
2563
style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">
2498
2564
{{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>
2500
2569
</div>
2501
2570
</div>
2502
2571
`
@@ -2505,6 +2574,7 @@ class DraggableInDropZoneWithCustomPreview {
2505
2574
@ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
2506
2575
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
2507
2576
items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2577
+ boundarySelector : string ;
2508
2578
}
2509
2579
2510
2580
0 commit comments