@@ -20,10 +20,8 @@ export class DDResizableHandle {
20
20
private option : DDResizableHandleOpt ;
21
21
/** @internal */
22
22
private dir : string ;
23
- /** @internal */
24
- private mouseMoving = false ;
25
- /** @internal */
26
- private started = false ;
23
+ /** @internal true after we've moved enough pixels to start a resize */
24
+ private moving = false ;
27
25
/** @internal */
28
26
private mouseDownEvent : MouseEvent ;
29
27
/** @internal */
@@ -38,10 +36,11 @@ export class DDResizableHandle {
38
36
this . _mouseMove = this . _mouseMove . bind ( this ) ;
39
37
this . _mouseUp = this . _mouseUp . bind ( this ) ;
40
38
41
- this . init ( ) ;
39
+ this . _init ( ) ;
42
40
}
43
41
44
- public init ( ) : DDResizableHandle {
42
+ /** @internal */
43
+ private _init ( ) : DDResizableHandle {
45
44
const el = document . createElement ( 'div' ) ;
46
45
el . classList . add ( 'ui-resizable-handle' ) ;
47
46
el . classList . add ( `${ DDResizableHandle . prefix } ${ this . dir } ` ) ;
@@ -53,68 +52,49 @@ export class DDResizableHandle {
53
52
return this ;
54
53
}
55
54
55
+ /** call this when resize handle needs to be removed and cleaned up */
56
56
public destroy ( ) : DDResizableHandle {
57
+ if ( this . moving ) this . _mouseUp ( this . mouseDownEvent ) ;
58
+ this . el . removeEventListener ( 'mousedown' , this . _mouseDown ) ;
57
59
this . host . removeChild ( this . el ) ;
60
+ delete this . el ;
61
+ delete this . host ;
58
62
return this ;
59
63
}
60
64
61
- /** @internal */
62
- private _mouseDown ( event : MouseEvent ) : void {
63
- this . mouseDownEvent = event ;
64
- setTimeout ( ( ) => {
65
- document . addEventListener ( 'mousemove' , this . _mouseMove , true ) ;
66
- document . addEventListener ( 'mouseup' , this . _mouseUp ) ;
67
- setTimeout ( ( ) => {
68
- if ( ! this . mouseMoving ) {
69
- document . removeEventListener ( 'mousemove' , this . _mouseMove , true ) ;
70
- document . removeEventListener ( 'mouseup' , this . _mouseUp ) ;
71
- delete this . mouseDownEvent ;
72
- }
73
- } , 300 ) ;
74
- } , 100 ) ;
65
+ /** @internal called on mouse down on us: capture move on the entire document (mouse might not stay on us) until we release the mouse */
66
+ private _mouseDown ( e : MouseEvent ) : void {
67
+ this . mouseDownEvent = e ;
68
+ document . addEventListener ( 'mousemove' , this . _mouseMove , true ) ; // capture, not bubble
69
+ document . addEventListener ( 'mouseup' , this . _mouseUp ) ;
75
70
}
76
71
77
72
/** @internal */
78
- private _mouseMove ( event : MouseEvent ) : void {
79
- if ( ! this . started && ! this . mouseMoving ) {
80
- if ( this . _hasMoved ( event , this . mouseDownEvent ) ) {
81
- this . mouseMoving = true ;
82
- this . _triggerEvent ( 'start' , this . mouseDownEvent ) ;
83
- this . started = true ;
84
- }
85
- }
86
- if ( this . started ) {
87
- this . _triggerEvent ( 'move' , event ) ;
73
+ private _mouseMove ( e : MouseEvent ) : void {
74
+ let s = this . mouseDownEvent ;
75
+ // don't start unless we've moved at least 3 pixels
76
+ if ( ! this . moving && Math . abs ( e . x - s . x ) + Math . abs ( e . y - s . y ) > 2 ) {
77
+ this . moving = true ;
78
+ this . _triggerEvent ( 'start' , this . mouseDownEvent ) ;
79
+ } else if ( this . moving ) {
80
+ this . _triggerEvent ( 'move' , e ) ;
88
81
}
89
82
}
90
83
91
84
/** @internal */
92
- private _mouseUp ( event : MouseEvent ) : void {
93
- if ( this . mouseMoving ) {
94
- this . _triggerEvent ( 'stop' , event ) ;
85
+ private _mouseUp ( e : MouseEvent ) : void {
86
+ if ( this . moving ) {
87
+ this . _triggerEvent ( 'stop' , e ) ;
95
88
}
96
89
document . removeEventListener ( 'mousemove' , this . _mouseMove , true ) ;
97
90
document . removeEventListener ( 'mouseup' , this . _mouseUp ) ;
98
- this . mouseMoving = false ;
99
- this . started = false ;
91
+ delete this . moving ;
100
92
delete this . mouseDownEvent ;
101
93
}
102
94
103
- /** @internal */
104
- private _hasMoved ( event : MouseEvent , oEvent : MouseEvent ) : boolean {
105
- const { clientX, clientY } = event ;
106
- const { clientX : oClientX , clientY : oClientY } = oEvent ;
107
- return (
108
- Math . abs ( clientX - oClientX ) > 1
109
- || Math . abs ( clientY - oClientY ) > 1
110
- ) ;
111
- }
112
-
113
95
/** @internal */
114
96
private _triggerEvent ( name : string , event : MouseEvent ) : DDResizableHandle {
115
- if ( this . option [ name ] ) {
116
- this . option [ name ] ( event ) ;
117
- }
97
+ if ( this . option [ name ] ) this . option [ name ] ( event ) ;
118
98
return this ;
119
99
}
120
100
}
0 commit comments