draggable: implemented #4145 - start,drag and stop events should be preventable

This commit is contained in:
Paul Bakaus
2010-01-28 16:35:59 +00:00
parent b72caa0f3d
commit 9617d15054
3 changed files with 103 additions and 10 deletions

View File

@@ -24,4 +24,58 @@ test("callbacks occurance count", function() {
});
test("stopping the start callback", function() {
expect(3);
var start = 0, stop = 0, dragc = 0;
el = $("#draggable2").draggable({
start: function() { start++; return false; },
drag: function() { dragc++; },
stop: function() { stop++; }
});
drag(el, 10, 10);
equals(start, 1, "start callback should happen exactly once");
equals(dragc, 0, "drag callback should not happen at all");
equals(stop, 0, "stop callback should not happen if there wasnt even a start");
});
test("stopping the drag callback", function() {
expect(3);
var start = 0, stop = 0, dragc = 0;
el = $("#draggable2").draggable({
start: function() { start++;},
drag: function() { dragc++; return false; },
stop: function() { stop++; }
});
drag(el, 10, 10);
equals(start, 1, "start callback should happen exactly once");
equals(dragc, 1, "drag callback should happen exactly once");
equals(stop, 1, "stop callback should happen, as we need to actively stop the drag");
});
test("stopping the stop callback", function() {
expect(1);
el = $("#draggable2").draggable({
helper: 'clone',
stop: function() { return false; }
});
drag(el, 10, 10);
ok($("#draggable2").data('draggable').helper, "the clone should not be deleted if the stop callback is stopped");
});
})(jQuery);

View File

@@ -42,15 +42,34 @@ test("{ addClasses: false }", function() {
test("{ appendTo: 'parent' }, default", function() {
equals(draggable_defaults.appendTo, "parent");
ok(false, 'missing test - untested code is broken code');
el = $("#draggable2").draggable({ appendTo: 'parent' });
drag(el, 50, 50);
moved(50, 50);
el = $("#draggable1").draggable({ appendTo: 'parent' });
drag(el, 50, 50);
moved(50, 50);
});
test("{ appendTo: Element }", function() {
ok(false, 'missing test - untested code is broken code');
el = $("#draggable2").draggable({ appendTo: $("#draggable2").parent()[0] });
drag(el, 50, 50);
moved(50, 50);
el = $("#draggable1").draggable({ appendTo: $("#draggable2").parent()[0] });
drag(el, 50, 50);
moved(50, 50);
});
test("{ appendTo: Selector }", function() {
ok(false, 'missing test - untested code is broken code');
el = $("#draggable2").draggable({ appendTo: "#main" });
drag(el, 50, 50);
moved(50, 50);
el = $("#draggable1").draggable({ appendTo: "#main" });
drag(el, 50, 50);
moved(50, 50);
});
test("{ axis: false }, default", function() {

View File

@@ -137,8 +137,11 @@ $.widget("ui.draggable", $.ui.mouse, {
if(o.containment)
this._setContainment();
//Call plugins and callbacks
this._trigger("start", event);
//Trigger event + callbacks
if(this._trigger("start", event) === false) {
this._clear();
return false;
}
//Recache the helper size
this._cacheHelperProportions();
@@ -161,7 +164,10 @@ $.widget("ui.draggable", $.ui.mouse, {
//Call plugins and callbacks and use the resulting position if something is returned
if (!noPropagation) {
var ui = this._uiHash();
this._trigger('drag', event, ui);
if(this._trigger('drag', event, ui) === false) {
this._mouseUp({});
return false;
}
this.position = ui.position;
}
@@ -192,16 +198,30 @@ $.widget("ui.draggable", $.ui.mouse, {
if((this.options.revert == "invalid" && !dropped) || (this.options.revert == "valid" && dropped) || this.options.revert === true || ($.isFunction(this.options.revert) && this.options.revert.call(this.element, dropped))) {
var self = this;
$(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
self._trigger("stop", event);
self._clear();
if(self._trigger("stop", event) !== false) {
self._clear();
}
});
} else {
this._trigger("stop", event);
this._clear();
if(this._trigger("stop", event) !== false) {
this._clear();
}
}
return false;
},
cancel: function() {
if(this.helper.is(".ui-draggable-dragging")) {
this._mouseUp({});
} else {
this._clear();
}
return this;
},
_getHandle: function(event) {