@@ -1068,6 +1068,92 @@ describe('CdkDrag', () => {
1068
1068
flush ( ) ;
1069
1069
} ) ) ;
1070
1070
1071
+ it ( 'should lay out the elements correctly, when swapping down with a taller element' ,
1072
+ fakeAsync ( ( ) => {
1073
+ const fixture = createComponent ( DraggableInDropZone ) ;
1074
+ fixture . detectChanges ( ) ;
1075
+
1076
+ const items = fixture . componentInstance . dragItems . map ( i => i . element . nativeElement ) ;
1077
+ const { top, left} = items [ 0 ] . getBoundingClientRect ( ) ;
1078
+
1079
+ fixture . componentInstance . items [ 0 ] . height = ITEM_HEIGHT * 2 ;
1080
+ fixture . detectChanges ( ) ;
1081
+
1082
+ startDraggingViaMouse ( fixture , items [ 0 ] , left , top ) ;
1083
+
1084
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1085
+ const target = items [ 1 ] ;
1086
+ const targetRect = target . getBoundingClientRect ( ) ;
1087
+
1088
+ // Add a few pixels to the top offset so we get some overlap.
1089
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . left , targetRect . top + 5 ) ;
1090
+ fixture . detectChanges ( ) ;
1091
+
1092
+ expect ( placeholder . style . transform ) . toBe ( `translate3d(0px, ${ ITEM_HEIGHT } px, 0px)` ) ;
1093
+ expect ( target . style . transform ) . toBe ( `translate3d(0px, ${ - ITEM_HEIGHT * 2 } px, 0px)` ) ;
1094
+
1095
+ dispatchMouseEvent ( document , 'mouseup' ) ;
1096
+ fixture . detectChanges ( ) ;
1097
+ flush ( ) ;
1098
+ } ) ) ;
1099
+
1100
+ it ( 'should lay out the elements correctly, when swapping up with a taller element' ,
1101
+ fakeAsync ( ( ) => {
1102
+ const fixture = createComponent ( DraggableInDropZone ) ;
1103
+ fixture . detectChanges ( ) ;
1104
+
1105
+ const items = fixture . componentInstance . dragItems . map ( i => i . element . nativeElement ) ;
1106
+ const { top, left} = items [ 1 ] . getBoundingClientRect ( ) ;
1107
+
1108
+ fixture . componentInstance . items [ 1 ] . height = ITEM_HEIGHT * 2 ;
1109
+ fixture . detectChanges ( ) ;
1110
+
1111
+ startDraggingViaMouse ( fixture , items [ 1 ] , left , top ) ;
1112
+
1113
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1114
+ const target = items [ 0 ] ;
1115
+ const targetRect = target . getBoundingClientRect ( ) ;
1116
+
1117
+ // Add a few pixels to the top offset so we get some overlap.
1118
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . left , targetRect . bottom - 5 ) ;
1119
+ fixture . detectChanges ( ) ;
1120
+
1121
+ expect ( placeholder . style . transform ) . toBe ( `translate3d(0px, ${ - ITEM_HEIGHT } px, 0px)` ) ;
1122
+ expect ( target . style . transform ) . toBe ( `translate3d(0px, ${ ITEM_HEIGHT * 2 } px, 0px)` ) ;
1123
+
1124
+ dispatchMouseEvent ( document , 'mouseup' ) ;
1125
+ fixture . detectChanges ( ) ;
1126
+ flush ( ) ;
1127
+ } ) ) ;
1128
+
1129
+ it ( 'should lay out elements correctly, when swapping an item with margin' , fakeAsync ( ( ) => {
1130
+ const fixture = createComponent ( DraggableInDropZone ) ;
1131
+ fixture . detectChanges ( ) ;
1132
+
1133
+ const items = fixture . componentInstance . dragItems . map ( i => i . element . nativeElement ) ;
1134
+ const { top, left} = items [ 0 ] . getBoundingClientRect ( ) ;
1135
+
1136
+ fixture . componentInstance . items [ 0 ] . margin = 12 ;
1137
+ fixture . detectChanges ( ) ;
1138
+
1139
+ startDraggingViaMouse ( fixture , items [ 0 ] , left , top ) ;
1140
+
1141
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1142
+ const target = items [ 1 ] ;
1143
+ const targetRect = target . getBoundingClientRect ( ) ;
1144
+
1145
+ // Add a few pixels to the top offset so we get some overlap.
1146
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . left , targetRect . top + 5 ) ;
1147
+ fixture . detectChanges ( ) ;
1148
+
1149
+ expect ( placeholder . style . transform ) . toBe ( `translate3d(0px, ${ ITEM_HEIGHT + 12 } px, 0px)` ) ;
1150
+ expect ( target . style . transform ) . toBe ( `translate3d(0px, ${ - ITEM_HEIGHT - 12 } px, 0px)` ) ;
1151
+
1152
+ dispatchMouseEvent ( document , 'mouseup' ) ;
1153
+ fixture . detectChanges ( ) ;
1154
+ flush ( ) ;
1155
+ } ) ) ;
1156
+
1071
1157
it ( 'should lay out the elements correctly, if an element skips multiple positions when ' +
1072
1158
'sorting horizontally' , fakeAsync ( ( ) => {
1073
1159
const fixture = createComponent ( DraggableInHorizontalDropZone ) ;
@@ -1094,6 +1180,90 @@ describe('CdkDrag', () => {
1094
1180
flush ( ) ;
1095
1181
} ) ) ;
1096
1182
1183
+ it ( 'should lay out the elements correctly, when swapping to the right with a wider element' ,
1184
+ fakeAsync ( ( ) => {
1185
+ const fixture = createComponent ( DraggableInHorizontalDropZone ) ;
1186
+ fixture . detectChanges ( ) ;
1187
+
1188
+ const items = fixture . componentInstance . dragItems . map ( i => i . element . nativeElement ) ;
1189
+
1190
+ fixture . componentInstance . items [ 0 ] . width = ITEM_WIDTH * 2 ;
1191
+ fixture . detectChanges ( ) ;
1192
+
1193
+ const { top, left} = items [ 0 ] . getBoundingClientRect ( ) ;
1194
+ startDraggingViaMouse ( fixture , items [ 0 ] , left , top ) ;
1195
+
1196
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1197
+ const target = items [ 1 ] ;
1198
+ const targetRect = target . getBoundingClientRect ( ) ;
1199
+
1200
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . right - 5 , targetRect . top ) ;
1201
+ fixture . detectChanges ( ) ;
1202
+
1203
+ expect ( placeholder . style . transform ) . toBe ( `translate3d(${ ITEM_WIDTH } px, 0px, 0px)` ) ;
1204
+ expect ( target . style . transform ) . toBe ( `translate3d(${ - ITEM_WIDTH * 2 } px, 0px, 0px)` ) ;
1205
+
1206
+ dispatchMouseEvent ( document , 'mouseup' ) ;
1207
+ fixture . detectChanges ( ) ;
1208
+ flush ( ) ;
1209
+ } ) ) ;
1210
+
1211
+ it ( 'should lay out the elements correctly, when swapping left with a wider element' ,
1212
+ fakeAsync ( ( ) => {
1213
+ const fixture = createComponent ( DraggableInHorizontalDropZone ) ;
1214
+ fixture . detectChanges ( ) ;
1215
+
1216
+ const items = fixture . componentInstance . dragItems . map ( i => i . element . nativeElement ) ;
1217
+ const { top, left} = items [ 1 ] . getBoundingClientRect ( ) ;
1218
+
1219
+ fixture . componentInstance . items [ 1 ] . width = ITEM_WIDTH * 2 ;
1220
+ fixture . detectChanges ( ) ;
1221
+
1222
+ startDraggingViaMouse ( fixture , items [ 1 ] , left , top ) ;
1223
+
1224
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1225
+ const target = items [ 0 ] ;
1226
+ const targetRect = target . getBoundingClientRect ( ) ;
1227
+
1228
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . right - 5 , targetRect . top ) ;
1229
+ fixture . detectChanges ( ) ;
1230
+
1231
+ expect ( placeholder . style . transform ) . toBe ( `translate3d(${ - ITEM_WIDTH } px, 0px, 0px)` ) ;
1232
+ expect ( target . style . transform ) . toBe ( `translate3d(${ ITEM_WIDTH * 2 } px, 0px, 0px)` ) ;
1233
+
1234
+ dispatchMouseEvent ( document , 'mouseup' ) ;
1235
+ fixture . detectChanges ( ) ;
1236
+ flush ( ) ;
1237
+ } ) ) ;
1238
+
1239
+ it ( 'should lay out elements correctly, when horizontally swapping an item with margin' ,
1240
+ fakeAsync ( ( ) => {
1241
+ const fixture = createComponent ( DraggableInHorizontalDropZone ) ;
1242
+ fixture . detectChanges ( ) ;
1243
+
1244
+ const items = fixture . componentInstance . dragItems . map ( i => i . element . nativeElement ) ;
1245
+ const { top, left} = items [ 0 ] . getBoundingClientRect ( ) ;
1246
+
1247
+ fixture . componentInstance . items [ 0 ] . margin = 12 ;
1248
+ fixture . detectChanges ( ) ;
1249
+
1250
+ startDraggingViaMouse ( fixture , items [ 0 ] , left , top ) ;
1251
+
1252
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) ! as HTMLElement ;
1253
+ const target = items [ 1 ] ;
1254
+ const targetRect = target . getBoundingClientRect ( ) ;
1255
+
1256
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . right - 5 , targetRect . top ) ;
1257
+ fixture . detectChanges ( ) ;
1258
+
1259
+ expect ( placeholder . style . transform ) . toBe ( `translate3d(${ ITEM_WIDTH + 12 } px, 0px, 0px)` ) ;
1260
+ expect ( target . style . transform ) . toBe ( `translate3d(${ - ITEM_WIDTH - 12 } px, 0px, 0px)` ) ;
1261
+
1262
+ dispatchMouseEvent ( document , 'mouseup' ) ;
1263
+ fixture . detectChanges ( ) ;
1264
+ flush ( ) ;
1265
+ } ) ) ;
1266
+
1097
1267
it ( 'should not swap position for tiny pointer movements' , fakeAsync ( ( ) => {
1098
1268
const fixture = createComponent ( DraggableInDropZone ) ;
1099
1269
fixture . detectChanges ( ) ;
@@ -1819,14 +1989,21 @@ class StandaloneDraggableWithMultipleHandles {
1819
1989
*ngFor="let item of items"
1820
1990
cdkDrag
1821
1991
[cdkDragData]="item"
1822
- style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">{{item}}</div>
1992
+ [style.height.px]="item.height"
1993
+ [style.margin-bottom.px]="item.margin"
1994
+ style="width: 100%; background: red;">{{item.value}}</div>
1823
1995
</div>
1824
1996
`
1825
1997
} )
1826
1998
class DraggableInDropZone {
1827
1999
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
1828
2000
@ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
1829
- items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2001
+ items = [
2002
+ { value : 'Zero' , height : ITEM_HEIGHT , margin : 0 } ,
2003
+ { value : 'One' , height : ITEM_HEIGHT , margin : 0 } ,
2004
+ { value : 'Two' , height : ITEM_HEIGHT , margin : 0 } ,
2005
+ { value : 'Three' , height : ITEM_HEIGHT , margin : 0 }
2006
+ ] ;
1830
2007
dropZoneId = 'items' ;
1831
2008
droppedSpy = jasmine . createSpy ( 'dropped spy' ) . and . callFake ( ( event : CdkDragDrop < string [ ] > ) => {
1832
2009
moveItemInArray ( this . items , event . previousIndex , event . currentIndex ) ;
@@ -1841,13 +2018,12 @@ class DraggableInDropZone {
1841
2018
`
1842
2019
.cdk-drop-list {
1843
2020
display: block;
1844
- width: 300px ;
2021
+ width: 500px ;
1845
2022
background: pink;
1846
2023
font-size: 0;
1847
2024
}
1848
2025
1849
2026
.cdk-drag {
1850
- width: ${ ITEM_WIDTH } px;
1851
2027
height: ${ ITEM_HEIGHT } px;
1852
2028
background: red;
1853
2029
display: inline-block;
@@ -1859,14 +2035,23 @@ class DraggableInDropZone {
1859
2035
cdkDropListOrientation="horizontal"
1860
2036
[cdkDropListData]="items"
1861
2037
(cdkDropListDropped)="droppedSpy($event)">
1862
- <div *ngFor="let item of items" cdkDrag>{{item}}</div>
2038
+ <div
2039
+ *ngFor="let item of items"
2040
+ [style.width.px]="item.width"
2041
+ [style.margin-right.px]="item.margin"
2042
+ cdkDrag>{{item.value}}</div>
1863
2043
</div>
1864
2044
`
1865
2045
} )
1866
2046
class DraggableInHorizontalDropZone {
1867
2047
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
1868
2048
@ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
1869
- items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
2049
+ items = [
2050
+ { value : 'Zero' , width : ITEM_WIDTH , margin : 0 } ,
2051
+ { value : 'One' , width : ITEM_WIDTH , margin : 0 } ,
2052
+ { value : 'Two' , width : ITEM_WIDTH , margin : 0 } ,
2053
+ { value : 'Three' , width : ITEM_WIDTH , margin : 0 }
2054
+ ] ;
1870
2055
droppedSpy = jasmine . createSpy ( 'dropped spy' ) . and . callFake ( ( event : CdkDragDrop < string [ ] > ) => {
1871
2056
moveItemInArray ( this . items , event . previousIndex , event . currentIndex ) ;
1872
2057
} ) ;
0 commit comments