Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6bd80b

Browse files
committed
Fixed disable method, it didn't work well for multiple instances.
Previously, disabling gridster caused all the instances of Gridster running on the pages was disabled. Because Draggable class uses event delegation from the body to manage drag events.
1 parent c503a4f commit c6bd80b

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/jquery.draggable.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,28 @@
6868
fn.init = function() {
6969
this.calculate_positions();
7070
this.$container.css('position', 'relative');
71-
this.enable();
71+
this.disabled = false;
72+
this.events();
7273

7374
$(window).bind('resize',
7475
throttle($.proxy(this.calculate_positions, this), 200));
7576
};
7677

78+
fn.events = function() {
79+
this.$container.on('selectstart', this.on_select_start);
80+
81+
this.$container.on(pointer_events.start, this.options.items, $.proxy(
82+
this.drag_handler, this));
83+
84+
this.$body.on(pointer_events.end, $.proxy(function(e) {
85+
this.is_dragging = false;
86+
if (this.disabled) { return; }
87+
this.$body.off(pointer_events.move);
88+
if (this.drag_start) {
89+
this.on_dragstop(e);
90+
}
91+
}, this));
92+
};
7793

7894
fn.get_actual_pos = function($el) {
7995
var pos = $el.position();
@@ -85,7 +101,7 @@
85101
if (isTouch) {
86102
var oe = e.originalEvent;
87103
e = oe.touches.length ? oe.touches[0] : oe.changedTouches[0];
88-
};
104+
}
89105

90106
return {
91107
left: e.clientX,
@@ -144,34 +160,33 @@
144160
$window.scrollTop(nextScrollTop);
145161
this.scrollOffset = this.scrollOffset + 30;
146162
}
147-
};
163+
}
148164

149165
if (abs_mouse_top <= mouse_up_zone) {
150166
nextScrollTop = scrollTop - 30;
151167
if (nextScrollTop > 0) {
152168
$window.scrollTop(nextScrollTop);
153169
this.scrollOffset = this.scrollOffset - 30;
154170
}
155-
};
156-
}
171+
}
172+
};
157173

158174

159175
fn.calculate_positions = function(e) {
160176
this.window_height = $window.height();
161-
}
177+
};
162178

163179

164180
fn.drag_handler = function(e) {
165181
var node = e.target.nodeName;
166-
167-
if (e.which !== 1 && !isTouch) {
182+
if (this.disabled || e.which !== 1 && !isTouch) {
168183
return;
169184
}
170185

171186
if (node === 'INPUT' || node === 'TEXTAREA' || node === 'SELECT' ||
172187
node === 'BUTTON') {
173188
return;
174-
};
189+
}
175190

176191
var self = this;
177192
var first = true;
@@ -199,7 +214,7 @@
199214
return false;
200215
}
201216

202-
if (self.is_dragging == true) {
217+
if (self.is_dragging === true) {
203218
self.on_dragmove.call(self, mme);
204219
}
205220

@@ -290,30 +305,16 @@
290305
};
291306

292307
fn.on_select_start = function(e) {
308+
if (this.disabled) { return; }
293309
return false;
294-
}
295-
296-
297-
fn.enable = function(){
298-
this.$container.on('selectstart', this.on_select_start);
299-
300-
this.$container.on(pointer_events.start, this.options.items, $.proxy(
301-
this.drag_handler, this));
302-
303-
this.$body.on(pointer_events.end, $.proxy(function(e) {
304-
this.is_dragging = false;
305-
this.$body.off(pointer_events.move);
306-
if (this.drag_start) {
307-
this.on_dragstop(e);
308-
}
309-
}, this));
310310
};
311311

312+
fn.enable = function() {
313+
this.disabled = false;
314+
};
312315

313-
fn.disable = function(){
314-
this.$container.off(pointer_events.start);
315-
this.$body.off(pointer_events.end);
316-
this.$container.off('selectstart', this.on_select_start);
316+
fn.disable = function() {
317+
this.disabled = true;
317318
};
318319

319320

@@ -322,7 +323,6 @@
322323
$.removeData(this.$container, 'drag');
323324
};
324325

325-
326326
//jQuery adapter
327327
$.fn.drag = function ( options ) {
328328
return this.each(function () {

0 commit comments

Comments
 (0)