6
6
ViewChildren ,
7
7
QueryList ,
8
8
AfterViewInit ,
9
+ ViewEncapsulation ,
9
10
} from '@angular/core' ;
10
11
import { TestBed , ComponentFixture , fakeAsync , flush } from '@angular/core/testing' ;
11
12
import { DragDropModule } from './drag-drop-module' ;
@@ -14,6 +15,7 @@ import {CdkDrag} from './drag';
14
15
import { CdkDragDrop } from './drag-events' ;
15
16
import { moveItemInArray , transferArrayItem } from './drag-utils' ;
16
17
import { CdkDrop } from './drop' ;
18
+ import { CdkDragHandle } from './drag-handle' ;
17
19
18
20
const ITEM_HEIGHT = 25 ;
19
21
@@ -136,9 +138,40 @@ describe('CdkDrag', () => {
136
138
const handle = fixture . componentInstance . handleElement . nativeElement ;
137
139
138
140
expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
139
- dragElementViaMouse ( fixture , handle , 50 , 100 ) ;
141
+ dragElementViaMouse ( fixture , dragElement , 50 , 100 , handle ) ;
140
142
expect ( dragElement . style . transform ) . toBe ( 'translate3d(50px, 100px, 0px)' ) ;
141
143
} ) ) ;
144
+
145
+ it ( 'should be able to use a handle that was added after init' , fakeAsync ( ( ) => {
146
+ const fixture = createComponent ( StandaloneDraggableWithDelayedHandle ) ;
147
+
148
+ fixture . detectChanges ( ) ;
149
+ fixture . componentInstance . showHandle = true ;
150
+ fixture . detectChanges ( ) ;
151
+
152
+ const dragElement = fixture . componentInstance . dragElement . nativeElement ;
153
+ const handle = fixture . componentInstance . handleElement . nativeElement ;
154
+
155
+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
156
+ dragElementViaMouse ( fixture , dragElement , 50 , 100 , handle ) ;
157
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(50px, 100px, 0px)' ) ;
158
+ } ) ) ;
159
+
160
+ it ( 'should be able to use more than one handle to drag the element' , fakeAsync ( ( ) => {
161
+ const fixture = createComponent ( StandaloneDraggableWithMultipleHandles ) ;
162
+ fixture . detectChanges ( ) ;
163
+
164
+ const dragElement = fixture . componentInstance . dragElement . nativeElement ;
165
+ const handles = fixture . componentInstance . handles . map ( handle => handle . element . nativeElement ) ;
166
+
167
+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
168
+ dragElementViaMouse ( fixture , dragElement , 50 , 100 , handles [ 1 ] ) ;
169
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(50px, 100px, 0px)' ) ;
170
+
171
+ dragElementViaMouse ( fixture , dragElement , 100 , 200 , handles [ 0 ] ) ;
172
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(150px, 300px, 0px)' ) ;
173
+ } ) ) ;
174
+
142
175
} ) ;
143
176
144
177
describe ( 'in a drop container' , ( ) => {
@@ -415,9 +448,48 @@ export class StandaloneDraggable {
415
448
export class StandaloneDraggableWithHandle {
416
449
@ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
417
450
@ViewChild ( 'handleElement' ) handleElement : ElementRef < HTMLElement > ;
418
- @ViewChild ( CdkDrag ) dragInstance : CdkDrag ;
419
451
}
420
452
453
+ @Component ( {
454
+ template : `
455
+ <div #dragElement cdkDrag
456
+ style="width: 100px; height: 100px; background: red; position: relative">
457
+ <div
458
+ #handleElement
459
+ *ngIf="showHandle"
460
+ cdkDragHandle style="width: 10px; height: 10px; background: green;"></div>
461
+ </div>
462
+ `
463
+ } )
464
+ export class StandaloneDraggableWithDelayedHandle {
465
+ @ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
466
+ @ViewChild ( 'handleElement' ) handleElement : ElementRef < HTMLElement > ;
467
+ showHandle = false ;
468
+ }
469
+
470
+ @Component ( {
471
+ encapsulation : ViewEncapsulation . None ,
472
+ styles : [ `
473
+ .cdk-drag-handle {
474
+ position: absolute;
475
+ top: 0;
476
+ background: green;
477
+ width: 10px;
478
+ height: 10px;
479
+ }
480
+ ` ] ,
481
+ template : `
482
+ <div #dragElement cdkDrag
483
+ style="width: 100px; height: 100px; background: red; position: relative">
484
+ <div cdkDragHandle style="left: 0;"></div>
485
+ <div cdkDragHandle style="right: 0;"></div>
486
+ </div>
487
+ `
488
+ } )
489
+ export class StandaloneDraggableWithMultipleHandles {
490
+ @ViewChild ( 'dragElement' ) dragElement : ElementRef < HTMLElement > ;
491
+ @ViewChildren ( CdkDragHandle ) handles : QueryList < CdkDragHandle > ;
492
+ }
421
493
422
494
@Component ( {
423
495
template : `
@@ -542,11 +614,12 @@ export class ConnectedDropZones implements AfterViewInit {
542
614
* @param element Element which is being dragged.
543
615
* @param x Position along the x axis to which to drag the element.
544
616
* @param y Position along the y axis to which to drag the element.
617
+ * @param eventTarget Event to be passed as the target to the event handler.
545
618
*/
546
619
function dragElementViaMouse ( fixture : ComponentFixture < any > ,
547
- element : HTMLElement , x : number , y : number ) {
620
+ element : HTMLElement , x : number , y : number , eventTarget = element ) {
548
621
549
- dispatchMouseEvent ( element , 'mousedown' ) ;
622
+ dispatchMouseEvent ( element , 'mousedown' , undefined , undefined , eventTarget ) ;
550
623
fixture . detectChanges ( ) ;
551
624
552
625
dispatchMouseEvent ( document , 'mousemove' , x , y ) ;
@@ -562,11 +635,12 @@ function dragElementViaMouse(fixture: ComponentFixture<any>,
562
635
* @param element Element which is being dragged.
563
636
* @param x Position along the x axis to which to drag the element.
564
637
* @param y Position along the y axis to which to drag the element.
638
+ * @param eventTarget Event to be passed as the target to the event handler.
565
639
*/
566
640
function dragElementViaTouch ( fixture : ComponentFixture < any > ,
567
- element : HTMLElement , x : number , y : number ) {
641
+ element : HTMLElement , x : number , y : number , eventTarget = element ) {
568
642
569
- dispatchTouchEvent ( element , 'touchstart' ) ;
643
+ dispatchTouchEvent ( element , 'touchstart' , undefined , undefined , eventTarget ) ;
570
644
fixture . detectChanges ( ) ;
571
645
572
646
dispatchTouchEvent ( document , 'touchmove' , x , y ) ;
0 commit comments