@@ -33,7 +33,7 @@ export class TabCardsComponent extends TabContentComponent implements AfterViewI
33
33
// it most of the time since this.bindings.undoHistory returns the same instance).
34
34
}
35
35
36
- ngAfterViewInit ( ) {
36
+ public ngAfterViewInit ( ) : void {
37
37
// This binder creates the command that allows the user to move a card from one list to another
38
38
this . bindings . dndBinder ( true )
39
39
. on ( window . document . body )
@@ -43,55 +43,35 @@ export class TabCardsComponent extends TabContentComponent implements AfterViewI
43
43
return new TransferArrayItem < CardData > ( [ ] , [ ] , - 1 , 0 , 'Drag card' ) ;
44
44
} )
45
45
// Checks if the user picked a valid card, and a new list for the card as a destination
46
- . when ( i => {
47
- // A valid card has to be selected in order to create the command
48
- const card = ( i . src . target as Element ) . closest ( 'mat-card' ) ;
49
- return card !== null ;
50
- } )
46
+ . when ( i => ( i . src . target as Element ) . classList . contains ( 'mat-card' ) )
51
47
. first ( ( _ , i ) => {
52
- this . card = ( i . src . target as Element ) . closest ( 'mat-card' ) as HTMLElement ;
48
+ this . card = ( i . src . target as HTMLElement ) ;
53
49
this . sourceIndex = Array . prototype . indexOf . call ( this . card ! . parentNode ?. children ?? [ ] , this . card ) ;
54
50
// Saves the initial state of the card's style to be able to restore it if the command can't be executed
55
51
this . mementoX = this . card . style . left ;
56
52
this . mementoY = this . card . style . top ;
57
53
this . mementoCSSPosition = this . card . style . position ;
58
- // Edits the card's style to make it movable visually
59
- this . card . style . width = String ( this . card . clientWidth - 32 ) + 'px' ;
60
- this . card . style . position = 'absolute' ;
54
+
55
+ this . card . style . position = 'relative' ;
61
56
this . card . style . zIndex = '999' ;
57
+
62
58
} )
63
59
. then ( ( c , i ) => {
64
- // Retrieves the position of the mouse on the page (substract the sidebar size)
65
- let x = i . tgt . pageX - 220 ;
66
- let y = i . tgt . pageY ;
67
- // Prevents parts of the card from going outside of the document
68
- if ( i . tgt . pageX > window . innerWidth - this . card ! . clientWidth - 10 ) {
69
- x = x - this . card ! . clientWidth - 5 ;
70
- }
71
- if ( i . tgt . pageY > window . innerHeight - this . card ! . clientHeight - 15 ) {
72
- y = y - this . card ! . clientHeight - 5 ;
73
- }
74
-
75
- // Moves the card visually
76
- this . card ! . style . left = String ( x ) + 'px' ;
77
- this . card ! . style . top = String ( y ) + 'px' ;
60
+ this . card ! . style . left = `${ i . diffClientX } px` ;
61
+ this . card ! . style . top = `${ i . diffClientY } px` ;
62
+ c . srcIndex = this . sourceIndex ;
78
63
79
- // Checks if the target selected is valid for the current card and makes the command executable if it is
80
- const isCardPositionValid = ( this . card ! . parentNode === this . cards1 . nativeElement ?
81
- i . tgt . target === this . cards2 . nativeElement : i . tgt . target === this . cards1 . nativeElement ) ;
82
- if ( ! isCardPositionValid ) {
83
- c . srcIndex = - 1 ;
64
+ if ( this . insideRectangle ( this . cards2 . nativeElement . getBoundingClientRect ( ) , i . tgt . clientX , i . tgt . clientY ) &&
65
+ ! this . insideRectangle ( this . cards2 . nativeElement . getBoundingClientRect ( ) , i . src . clientX , i . src . clientY ) ) {
66
+ c . srcArray = this . dataService . cards1 ;
67
+ c . tgtArray = this . dataService . cards2 ;
84
68
} else {
85
- c . srcIndex = this . sourceIndex ;
86
-
87
- // Defines which array is the source and which one is the target
88
- const fromSrcToTgt = i . tgt . target === this . cards2 . nativeElement && i . src . target !== this . cards1 . nativeElement ;
89
- if ( fromSrcToTgt ) {
90
- c . srcArray = this . dataService . cards1 ;
91
- c . tgtArray = this . dataService . cards2 ;
92
- } else {
69
+ if ( this . insideRectangle ( this . cards1 . nativeElement . getBoundingClientRect ( ) , i . tgt . clientX , i . tgt . clientY ) &&
70
+ ! this . insideRectangle ( this . cards1 . nativeElement . getBoundingClientRect ( ) , i . src . clientX , i . src . clientY ) ) {
93
71
c . srcArray = this . dataService . cards2 ;
94
72
c . tgtArray = this . dataService . cards1 ;
73
+ } else {
74
+ c . srcIndex = - 1 ;
95
75
}
96
76
}
97
77
} )
@@ -108,4 +88,8 @@ export class TabCardsComponent extends TabContentComponent implements AfterViewI
108
88
} )
109
89
. bind ( ) ;
110
90
}
91
+
92
+ private insideRectangle ( rec : DOMRect , x : number , y : number ) : boolean {
93
+ return x >= rec . x && y >= rec . y && x <= ( rec . x + rec . width ) && y <= ( rec . y + rec . height ) ;
94
+ }
111
95
}
0 commit comments