Skip to content

Commit ff06fe5

Browse files
author
Lawrence Goldstien
committed
Add internal state awareness to dragpan to ensure addition and removal of correct events
1 parent e6a989f commit ff06fe5

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

src/dragpan.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ $.widget( "oml.dragpan", {
4646
_this.vars.lastPosX = 0;
4747
_this.vars.lastPosY = 0;
4848

49+
// Set up a var to store the state of dragpan
50+
_this.vars.state = 'off';
51+
4952
// Find out if text selection was enabled before
5053
_this.vars.selection = false;
5154
if ( _this.vars.$parent.css( "-webkit-touch-callout" ) !== 'none' ){
@@ -125,42 +128,58 @@ $.widget( "oml.dragpan", {
125128
});
126129
},
127130
_removeMouseBinding: function () {
128-
_this.vars.$parent.off();
131+
_this.vars.$parent.off('mousemove');
132+
_this.vars.$parent.off('mousedown');
133+
_this.vars.$parent.off('mouseup');
134+
_this.vars.$parent.off('mouseleave');
129135
_this._dragging( 'off' );
130136
},
137+
_state: function () {
138+
return _this.vars.state;
139+
},
131140

132141
// The public functions
133142
on: function () {
134-
// Set the cursor
135-
_this.options.$parent.css( "cursor", _this.options.cursor );
136-
137-
// Disable selection dragging
138-
_this.vars.$parent.css( "-webkit-touch-callout", "none" );
139-
_this.vars.$parent.css( "-webkit-user-select", "none" );
140-
_this.vars.$parent.css( "-khtml-user-select", "none" );
141-
_this.vars.$parent.css( "-moz-user-select", "-moz-none" );
142-
_this.vars.$parent.css( "-ms-user-select", "none" );
143-
_this.vars.$parent.css( "user-select", "none" );
144-
145-
// Add the mouse binding
146-
this._addMouseBinding();
143+
if (this._state() === 'off') {
144+
// Set the cursor
145+
_this.options.$parent.css( "cursor", _this.options.cursor );
146+
147+
// Disable selection dragging
148+
_this.vars.$parent.css( "-webkit-touch-callout", "none" );
149+
_this.vars.$parent.css( "-webkit-user-select", "none" );
150+
_this.vars.$parent.css( "-khtml-user-select", "none" );
151+
_this.vars.$parent.css( "-moz-user-select", "-moz-none" );
152+
_this.vars.$parent.css( "-ms-user-select", "none" );
153+
_this.vars.$parent.css( "user-select", "none" );
154+
155+
// Add the mouse binding
156+
this._addMouseBinding();
157+
158+
// Set the state to on
159+
_this.vars.state = 'on';
160+
}
147161
},
148162
off: function () {
149-
// Enable selection dragging if it was enabled to begin with
150-
if (_this.vars.selection === true) {
151-
_this.vars.$parent.css( "-webkit-touch-callout", "all" );
152-
_this.vars.$parent.css( "-webkit-user-select", "all" );
153-
_this.vars.$parent.css( "-khtml-user-select", "all" );
154-
_this.vars.$parent.css( "-moz-user-select", "all" );
155-
_this.vars.$parent.css( "-ms-user-select", "all" );
156-
_this.vars.$parent.css( "user-select", "all" );
163+
if (this._state() === 'on') {
164+
// Enable selection dragging if it was enabled to begin with
165+
if (_this.vars.selection === true) {
166+
_this.vars.$parent.css( "-webkit-touch-callout", "all" );
167+
_this.vars.$parent.css( "-webkit-user-select", "all" );
168+
_this.vars.$parent.css( "-khtml-user-select", "all" );
169+
_this.vars.$parent.css( "-moz-user-select", "all" );
170+
_this.vars.$parent.css( "-ms-user-select", "all" );
171+
_this.vars.$parent.css( "user-select", "all" );
172+
}
173+
174+
// Set back to default cursor
175+
_this.options.$parent.css( "cursor", "default" );
176+
177+
// Remove the mouse binding
178+
this._removeMouseBinding();
179+
180+
// Set the state to off
181+
_this.vars.state = 'off';
157182
}
158-
159-
// Set back to default cursor
160-
_this.options.$parent.css( "cursor", "default" );
161-
162-
// Remove the mouse binding
163-
this._removeMouseBinding();
164183
}
165184

166185
});

0 commit comments

Comments
 (0)