14
14
* limitations under the License.
15
15
*/
16
16
17
- /**
18
- * Construct a GrabToPan instance for a given HTML element.
19
- * @param options.element {Element}
20
- * @param options.ignoreTarget {function} optional. See `ignoreTarget(node)`
21
- * @param options.onActiveChanged {function(boolean)} optional. Called
22
- * when grab-to-pan is (de)activated. The first argument is a boolean that
23
- * shows whether grab-to-pan is activated.
24
- */
25
- function GrabToPan ( options ) {
26
- this . element = options . element ;
27
- this . document = options . element . ownerDocument ;
28
- if ( typeof options . ignoreTarget === "function" ) {
29
- this . ignoreTarget = options . ignoreTarget ;
30
- }
31
- this . onActiveChanged = options . onActiveChanged ;
32
-
33
- // Bind the contexts to ensure that `this` always points to
34
- // the GrabToPan instance.
35
- this . activate = this . activate . bind ( this ) ;
36
- this . deactivate = this . deactivate . bind ( this ) ;
37
- this . toggle = this . toggle . bind ( this ) ;
38
- this . _onmousedown = this . _onmousedown . bind ( this ) ;
39
- this . _onmousemove = this . _onmousemove . bind ( this ) ;
40
- this . _endPan = this . _endPan . bind ( this ) ;
41
-
42
- // This overlay will be inserted in the document when the mouse moves during
43
- // a grab operation, to ensure that the cursor has the desired appearance.
44
- const overlay = ( this . overlay = document . createElement ( "div" ) ) ;
45
- overlay . className = "grab-to-pan-grabbing" ;
46
- }
47
- GrabToPan . prototype = {
17
+ // Class name of element which can be grabbed.
18
+ const CSS_CLASS_GRAB = "grab-to-pan-grab" ;
19
+
20
+ class GrabToPan {
48
21
/**
49
- * Class name of element which can be grabbed
22
+ * Construct a GrabToPan instance for a given HTML element.
23
+ * @param {Element } options.element
24
+ * @param {function } [options.ignoreTarget] - See `ignoreTarget(node)`.
25
+ * @param {function(boolean) } [options.onActiveChanged } - Called when
26
+ * grab-to-pan is (de)activated. The first argument is a boolean that
27
+ * shows whether grab-to-pan is activated.
50
28
*/
51
- CSS_CLASS_GRAB : "grab-to-pan-grab" ,
29
+ constructor ( options ) {
30
+ this . element = options . element ;
31
+ this . document = options . element . ownerDocument ;
32
+ if ( typeof options . ignoreTarget === "function" ) {
33
+ this . ignoreTarget = options . ignoreTarget ;
34
+ }
35
+ this . onActiveChanged = options . onActiveChanged ;
36
+
37
+ // Bind the contexts to ensure that `this` always points to
38
+ // the GrabToPan instance.
39
+ this . activate = this . activate . bind ( this ) ;
40
+ this . deactivate = this . deactivate . bind ( this ) ;
41
+ this . toggle = this . toggle . bind ( this ) ;
42
+ this . _onmousedown = this . #onmousedown. bind ( this ) ;
43
+ this . _onmousemove = this . #onmousemove. bind ( this ) ;
44
+ this . _endPan = this . #endPan. bind ( this ) ;
45
+
46
+ // This overlay will be inserted in the document when the mouse moves during
47
+ // a grab operation, to ensure that the cursor has the desired appearance.
48
+ const overlay = ( this . overlay = document . createElement ( "div" ) ) ;
49
+ overlay . className = "grab-to-pan-grabbing" ;
50
+ }
52
51
53
52
/**
54
53
* Bind a mousedown event to the element to enable grab-detection.
55
54
*/
56
- activate : function GrabToPan_activate ( ) {
55
+ activate ( ) {
57
56
if ( ! this . active ) {
58
57
this . active = true ;
59
58
this . element . addEventListener ( "mousedown" , this . _onmousedown , true ) ;
60
- this . element . classList . add ( this . CSS_CLASS_GRAB ) ;
61
- if ( this . onActiveChanged ) {
62
- this . onActiveChanged ( true ) ;
63
- }
59
+ this . element . classList . add ( CSS_CLASS_GRAB ) ;
60
+
61
+ this . onActiveChanged ?. ( true ) ;
64
62
}
65
- } ,
63
+ }
66
64
67
65
/**
68
66
* Removes all events. Any pending pan session is immediately stopped.
69
67
*/
70
- deactivate : function GrabToPan_deactivate ( ) {
68
+ deactivate ( ) {
71
69
if ( this . active ) {
72
70
this . active = false ;
73
71
this . element . removeEventListener ( "mousedown" , this . _onmousedown , true ) ;
74
72
this . _endPan ( ) ;
75
- this . element . classList . remove ( this . CSS_CLASS_GRAB ) ;
76
- if ( this . onActiveChanged ) {
77
- this . onActiveChanged ( false ) ;
78
- }
73
+ this . element . classList . remove ( CSS_CLASS_GRAB ) ;
74
+
75
+ this . onActiveChanged ?. ( false ) ;
79
76
}
80
- } ,
77
+ }
81
78
82
- toggle : function GrabToPan_toggle ( ) {
79
+ toggle ( ) {
83
80
if ( this . active ) {
84
81
this . deactivate ( ) ;
85
82
} else {
86
83
this . activate ( ) ;
87
84
}
88
- } ,
85
+ }
89
86
90
87
/**
91
88
* Whether to not pan if the target element is clicked.
92
89
* Override this method to change the default behaviour.
93
90
*
94
- * @param node {Element} The target of the event
91
+ * @param {Element } node - The target of the event.
95
92
* @returns {boolean } Whether to not react to the click event.
96
93
*/
97
- ignoreTarget : function GrabToPan_ignoreTarget ( node ) {
94
+ ignoreTarget ( node ) {
98
95
// Check whether the clicked element is, a child of, an input element/link.
99
96
return node . matches (
100
97
"a[href], a[href] *, input, textarea, button, button *, select, option"
101
98
) ;
102
- } ,
99
+ }
103
100
104
- /**
105
- * @private
106
- */
107
- _onmousedown : function GrabToPan__onmousedown ( event ) {
101
+ #onmousedown( event ) {
108
102
if ( event . button !== 0 || this . ignoreTarget ( event . target ) ) {
109
103
return ;
110
104
}
@@ -135,12 +129,9 @@ GrabToPan.prototype = {
135
129
if ( focusedElement && ! focusedElement . contains ( event . target ) ) {
136
130
focusedElement . blur ( ) ;
137
131
}
138
- } ,
132
+ }
139
133
140
- /**
141
- * @private
142
- */
143
- _onmousemove : function GrabToPan__onmousemove ( event ) {
134
+ #onmousemove( event ) {
144
135
this . element . removeEventListener ( "scroll" , this . _endPan , true ) ;
145
136
if ( ! ( event . buttons & 1 ) ) {
146
137
// The left mouse button is released.
@@ -164,18 +155,15 @@ GrabToPan.prototype = {
164
155
if ( ! this . overlay . parentNode ) {
165
156
document . body . appendChild ( this . overlay ) ;
166
157
}
167
- } ,
158
+ }
168
159
169
- /**
170
- * @private
171
- */
172
- _endPan : function GrabToPan__endPan ( ) {
160
+ #endPan( ) {
173
161
this . element . removeEventListener ( "scroll" , this . _endPan , true ) ;
174
162
this . document . removeEventListener ( "mousemove" , this . _onmousemove , true ) ;
175
163
this . document . removeEventListener ( "mouseup" , this . _endPan , true ) ;
176
164
// Note: ChildNode.remove doesn't throw if the parentNode is undefined.
177
165
this . overlay . remove ( ) ;
178
- } ,
179
- } ;
166
+ }
167
+ }
180
168
181
169
export { GrabToPan } ;
0 commit comments