11import {
22 Directive , Output , EventEmitter , ContentChildren ,
3- QueryList , KeyValueDiffers , AfterContentInit , OnDestroy
3+ QueryList , KeyValueDiffers , AfterContentInit , OnDestroy , Inject
44} from '@angular/core' ;
5- import { DraggableDirective } from './draggable.directive' ;
5+ import { DraggableDirective } from './draggable.directive' ;
6+ import { DOCUMENT } from '@angular/platform-browser' ;
67
7- @Directive ( { selector : '[orderable]' } )
8+ @Directive ( { selector : '[orderable]' } )
89export class OrderableDirective implements AfterContentInit , OnDestroy {
910
1011 @Output ( ) reorder : EventEmitter < any > = new EventEmitter ( ) ;
1112
12- @ContentChildren ( DraggableDirective , { descendants : true } )
13+ @ContentChildren ( DraggableDirective , { descendants : true } )
1314 draggables : QueryList < DraggableDirective > ;
1415
1516 positions : any ;
1617 differ : any ;
1718
18- constructor ( differs : KeyValueDiffers ) {
19+ constructor ( differs : KeyValueDiffers , @ Inject ( DOCUMENT ) private document : any ) {
1920 this . differ = differs . find ( { } ) . create ( null ) ;
2021 }
2122
@@ -37,16 +38,16 @@ export class OrderableDirective implements AfterContentInit, OnDestroy {
3738 const diffs = this . differ . diff ( this . createMapDiffs ( ) ) ;
3839
3940 if ( diffs ) {
40- const subscribe = ( { currentValue, previousValue} : any ) => {
41- unsubscribe ( { previousValue} ) ;
41+ const subscribe = ( { currentValue, previousValue } : any ) => {
42+ unsubscribe ( { previousValue } ) ;
4243
4344 if ( currentValue ) {
4445 currentValue . dragStart . subscribe ( this . onDragStart . bind ( this ) ) ;
4546 currentValue . dragEnd . subscribe ( this . onDragEnd . bind ( this ) ) ;
4647 }
4748 } ;
4849
49- const unsubscribe = ( { previousValue} : any ) => {
50+ const unsubscribe = ( { previousValue } : any ) => {
5051 if ( previousValue ) {
5152 previousValue . dragStart . unsubscribe ( ) ;
5253 previousValue . dragEnd . unsubscribe ( ) ;
@@ -66,20 +67,19 @@ export class OrderableDirective implements AfterContentInit, OnDestroy {
6667 for ( const dragger of this . draggables . toArray ( ) ) {
6768 const elm = dragger . element ;
6869 let left = parseInt ( elm . offsetLeft . toString ( ) , 0 ) ;
69- this . positions [ dragger . dragModel . prop ] = {
70+ this . positions [ dragger . dragModel . prop ] = {
7071 left : left ,
7172 right : left + parseInt ( elm . offsetWidth . toString ( ) , 0 ) ,
72- index : i ++
73+ index : i ++ ,
74+ element : elm
7375 } ;
7476 }
7577 }
7678
77- onDragEnd ( { element, model, event} : any ) {
78- const newPos = parseInt ( element . offsetLeft . toString ( ) , 0 ) ;
79- const prevPos = this . positions [ model . prop ] ;
79+ onDragEnd ( { element, model, event } : any ) {
80+ const prevPos = this . positions [ model . prop ] ;
8081
81- // add the mouseX to be closer to the cursor position when checking the target.
82- let target = this . isTarget ( newPos + event . layerX , model ) ;
82+ let target = this . isTarget ( model , event ) ;
8383 if ( target ) {
8484 this . reorder . emit ( {
8585 prevIndex : prevPos . index ,
@@ -91,15 +91,16 @@ export class OrderableDirective implements AfterContentInit, OnDestroy {
9191 element . style . left = 'auto' ;
9292 }
9393
94- isTarget ( newPos , model ) {
94+ isTarget ( model , event ) {
9595 let i = 0 ;
96+ let targets = this . document . elementsFromPoint ( event . x , event . y ) ;
97+
9698 for ( const prop in this . positions ) {
9799 // current column position which throws event.
98- const pos = this . positions [ prop ] ;
100+ const pos = this . positions [ prop ] ;
99101
100- // current column is not the dragging model
101- // && newPos in bounds
102- if ( model . prop != prop && ( newPos >= pos . left && newPos <= pos . right ) ) {
102+ // since we drag the inner span, we need to find it in the elements at the cursor
103+ if ( model . prop != prop && targets . find ( ( el ) => el === pos . element ) ) {
103104 return {
104105 pos : pos ,
105106 i : i
@@ -113,7 +114,7 @@ export class OrderableDirective implements AfterContentInit, OnDestroy {
113114 private createMapDiffs ( ) : { [ key : string ] : DraggableDirective } {
114115 return this . draggables . toArray ( )
115116 . reduce ( ( acc , curr ) => {
116- acc [ curr . dragModel . $$id ] = curr ;
117+ acc [ curr . dragModel . $$id ] = curr ;
117118 return acc ;
118119 } , { } ) ;
119120 }
0 commit comments