1
- import { Directive , ElementRef , Inject , Input , OnDestroy , OnInit , Renderer2 } from '@angular/core' ;
2
- import { NodeDraggableService } from './node-draggable.service' ;
3
- import { CapturedNode } from './captured-node' ;
4
- import { Tree } from '../tree' ;
1
+ import { Directive , ElementRef , Inject , Input , OnDestroy , OnInit , Renderer2 } from '@angular/core' ;
2
+ import { NodeDraggableService } from './node-draggable.service' ;
3
+ import { CapturedNode } from './captured-node' ;
4
+ import { Tree } from '../tree' ;
5
5
6
6
@Directive ( {
7
7
selector : '[nodeDraggable]'
8
8
} )
9
9
export class NodeDraggableDirective implements OnDestroy , OnInit {
10
10
public static DATA_TRANSFER_STUB_DATA = 'some browsers enable drag-n-drop only when dataTransfer has data' ;
11
11
12
- @Input ( )
13
- public nodeDraggable : ElementRef ;
12
+ @Input ( ) public nodeDraggable : ElementRef ;
14
13
15
- @Input ( )
16
- public tree : Tree ;
14
+ @Input ( ) public tree : Tree ;
17
15
18
16
private nodeNativeElement : HTMLElement ;
19
17
private disposersForDragListeners : Function [ ] = [ ] ;
20
18
21
- public constructor ( @Inject ( ElementRef ) public element : ElementRef ,
22
- @Inject ( NodeDraggableService ) private nodeDraggableService : NodeDraggableService ,
23
- @Inject ( Renderer2 ) private renderer : Renderer2 ) {
19
+ public constructor (
20
+ @Inject ( ElementRef ) public element : ElementRef ,
21
+ @Inject ( NodeDraggableService ) private nodeDraggableService : NodeDraggableService ,
22
+ @Inject ( Renderer2 ) private renderer : Renderer2
23
+ ) {
24
24
this . nodeNativeElement = element . nativeElement ;
25
25
}
26
26
27
27
public ngOnInit ( ) : void {
28
28
if ( ! this . tree . isStatic ( ) ) {
29
29
this . renderer . setAttribute ( this . nodeNativeElement , 'draggable' , 'true' ) ;
30
- this . disposersForDragListeners . push ( this . renderer . listen ( this . nodeNativeElement , 'dragenter' , this . handleDragEnter . bind ( this ) ) ) ;
31
- this . disposersForDragListeners . push ( this . renderer . listen ( this . nodeNativeElement , 'dragover' , this . handleDragOver . bind ( this ) ) ) ;
32
- this . disposersForDragListeners . push ( this . renderer . listen ( this . nodeNativeElement , 'dragstart' , this . handleDragStart . bind ( this ) ) ) ;
33
- this . disposersForDragListeners . push ( this . renderer . listen ( this . nodeNativeElement , 'dragleave' , this . handleDragLeave . bind ( this ) ) ) ;
34
- this . disposersForDragListeners . push ( this . renderer . listen ( this . nodeNativeElement , 'drop' , this . handleDrop . bind ( this ) ) ) ;
35
- this . disposersForDragListeners . push ( this . renderer . listen ( this . nodeNativeElement , 'dragend' , this . handleDragEnd . bind ( this ) ) ) ;
30
+ this . disposersForDragListeners . push (
31
+ this . renderer . listen ( this . nodeNativeElement , 'dragenter' , this . handleDragEnter . bind ( this ) )
32
+ ) ;
33
+ this . disposersForDragListeners . push (
34
+ this . renderer . listen ( this . nodeNativeElement , 'dragover' , this . handleDragOver . bind ( this ) )
35
+ ) ;
36
+ this . disposersForDragListeners . push (
37
+ this . renderer . listen ( this . nodeNativeElement , 'dragstart' , this . handleDragStart . bind ( this ) )
38
+ ) ;
39
+ this . disposersForDragListeners . push (
40
+ this . renderer . listen ( this . nodeNativeElement , 'dragleave' , this . handleDragLeave . bind ( this ) )
41
+ ) ;
42
+ this . disposersForDragListeners . push (
43
+ this . renderer . listen ( this . nodeNativeElement , 'drop' , this . handleDrop . bind ( this ) )
44
+ ) ;
45
+ this . disposersForDragListeners . push (
46
+ this . renderer . listen ( this . nodeNativeElement , 'dragend' , this . handleDragEnd . bind ( this ) )
47
+ ) ;
36
48
}
37
49
}
38
50
@@ -90,9 +102,7 @@ export class NodeDraggableDirective implements OnDestroy, OnInit {
90
102
91
103
private isDropPossible ( e : DragEvent ) : boolean {
92
104
const capturedNode = this . nodeDraggableService . getCapturedNode ( ) ;
93
- return capturedNode
94
- && capturedNode . canBeDroppedAt ( this . nodeDraggable )
95
- && this . containsElementAt ( e ) ;
105
+ return capturedNode && capturedNode . canBeDroppedAt ( this . nodeDraggable ) && this . containsElementAt ( e ) ;
96
106
}
97
107
98
108
private handleDragEnd ( e : DragEvent ) : any {
@@ -101,7 +111,7 @@ export class NodeDraggableDirective implements OnDestroy, OnInit {
101
111
}
102
112
103
113
private containsElementAt ( e : DragEvent ) : boolean {
104
- const { x = e . clientX , y = e . clientY } = e ;
114
+ const { x = e . clientX , y = e . clientY } = e ;
105
115
return this . nodeNativeElement . contains ( document . elementFromPoint ( x , y ) ) ;
106
116
}
107
117
0 commit comments