8000 sortable,draggable: fixed order and propagation of events (fixes #365… · ainformatico/jquery-ui@c8305ae · GitHub
Skip to content

Commit c8305ae

Browse files
author
Paul Bakaus
committed
sortable,draggable: fixed order and propagation of events (fixes #3658, #3730 and #3726)
1 parent 72f7945 commit c8305ae

2 files changed

Lines changed: 39 additions & 36 deletions

File tree

ui/ui.draggable.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, {
104104
this._setContainment();
105105

106106
//Call plugins and callbacks
107-
this._propagate("start", event);
107+
this._trigger("start", event);
108108

109109
//Recache the helper size
110110
this._cacheHelperProportions();
@@ -130,7 +130,7 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, {
130130
*/
131131

132132
//Call plugins and callbacks and use the resulting position if something is returned
133-
if(!noPropagation) this.position = this._propagate("drag", event) || this.position;
133+
if(!noPropagation) this.position = this._trigger("drag", event) || this.position;
134134

135135
if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
136136
if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
@@ -155,11 +155,11 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, {
155155
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))) {
156156
var self = this;
157157
$(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
158-
self._propagate("stop", event);
158+
self._trigger("stop", event);
159159
self._clear();
160160
});
161161
} else {
162-
this._propagate("stop", event);
162+
this._trigger("stop", event);
163163
this._clear();
164164
}
165165

@@ -373,10 +373,10 @@ $.widget("ui.draggable", $.extend({}, $.ui.mouse, {
373373

374374
// From now on bulk stuff - mainly helpers
375375

376-
_propagate: function(n, event) {
377-
$.ui.plugin.call(this, n, [event, this._uiHash()]);
378-
if(n == "drag") this.positionAbs = this._convertPositionTo("absolute"); //The absolute position has to be recalculated after plugins
379-
this._trigger(n, event, this._uiHash());
376+
_trigger: function(type, event) {
377+
$.ui.plugin.call(this, type, [event, this._uiHash()]);
378+
if(type == "drag") this.positionAbs = this._convertPositionTo("absolute"); //The absolute position has to be recalculated after plugins
379+
$.widget.prototype._trigger.call(this, type, event, this._uiHash());
380380
return event.returnValue;
381381
},
382382

@@ -443,7 +443,7 @@ $.ui.plugin.add("draggable", "connectToSortable", {
443443
shouldRevert: sortable.options.revert
444444
});
445445
sortable._refreshItems(); //Do a one-time refresh at start to refresh the containerCache
446-
sortable._propagate("activate", event, inst);
446+
sortable._trigger("activate", event, inst);
447447
}
448448
});
449449
});
@@ -479,7 +479,7 @@ $.ui.plugin.add("draggable", "connectToSortable", {
479479

480480
} else {
481481
this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
482-
this.instance._propagate("deactivate", event, inst);
482+
this.instance._trigger("deactivate", event, inst);
483483
}
484484

485485
});
@@ -522,8 +522,9 @@ $.ui.plugin.add("draggable", "connectToSortable", {
522522
this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
523523
this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
524524

525-
inst._propagate("toSortable", event);
525+
inst._trigger("toSortable", event);
526526
inst.dropped = this.instance.element; //draggable revert needs that
527+
this.instance.fromOutside = true; //Little hack so receive/update callbacks work
527528

528529
}
529530

@@ -545,7 +546,7 @@ $.ui.plugin.add("draggable", "connectToSortable", {
545546
this.instance.currentItem.remove();
546547
if(this.instance.placeholder) this.instance.placeholder.remove();
547548

548-
inst._propagate("fromSortable", event);
549+
inst._trigger("fromSortable", event);
549550
inst.dropped = false; //draggable revert needs that
550551
}
551552

ui/ui.sortable.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
148148
this._setContainment();
149149

150150
//Call plugins and callbacks
151-
this._propagate("start", event);
151+
this._trigger("start", event);
152152

153153
//Recache the helper size
154154
if(!this._preserveHelperProportions)
@@ -157,7 +157,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
157157

158158
//Post 'activate' events to possible containers
159159
if(!noActivation) {
160-
for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i]._propagate("activate", event, this); }
160+
for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i]._trigger("activate", event, this); }
161161
}
162162

163163
//Prepare possible droppables
@@ -216,7 +216,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
216216
break;
217217
}
218218

219-
this._propagate("change", event); //Call plugins and callbacks
219+
this._trigger("change", event); //Call plugins and callbacks
220220
break;
221221
}
222222
}
@@ -276,9 +276,9 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
276276

277277
//Post deactivating events to containers
278278
for (var i = this.containers.length - 1; i >= 0; i--){
279-
this.containers[i]._propagate("deactivate", null, this);
279+
this.containers[i]._trigger("deactivate", null, this);
280280
if(this.containers[i].containerCache.over) {
281-
this.containers[i]._propagate("out", null, this);
281+
this.containers[i]._trigger("out", null, this);
282282
this.containers[i].containerCache.over = 0;
283283
}
284284
}
@@ -608,20 +608,20 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
608608

609609
this.currentContainer = this.containers[i];
610610
itemWithLeastDistance ? this.options.sortIndicator.call(this, event, itemWithLeastDistance, null, true) : this.options.sortIndicator.call(this, event, null, this.containers[i].element, true);
611-
this._propagate("change", event); //Call plugins and callbacks
612-
this.containers[i]._propagate("change", event, this); //Call plugins and callbacks
611+
this._trigger("change", event); //Call plugins and callbacks
612+
this.containers[i]._trigger("change", event, this); //Call plugins and callbacks
613613

614614
//Update the placeholder
615615
this.options.placeholder.update(this.currentContainer, this.placeholder);
616616

617617
}
618618

619-
this.containers[i]._propagate("over", event, this);
619+
this.containers[i]._trigger("over", event, this);
620620
this.containers[i].containerCache.over = 1;
621621
}
622622
} else {
623623
if(this.containers[i].containerCache.over) {
624-
this.containers[i]._propagate("out", event, this);
624+
this.containers[i]._trigger("out", event, this);
625625
this.containers[i].containerCache.over = 0;
626626
}
627627
}
@@ -828,49 +828,51 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
828828
this.currentItem.show();
829829
}
830830

831-
if(this.domPosition.prev != this.currentItem.prev().not("."+this.options.cssNamespace+"-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) this._propagate("update", event, null, noPropagation); //Trigger update callback if the DOM position has changed
831+
if(this.fromOutside) this._trigger("receive", event, this, noPropagation);
832+
if(this.fromOutside || this.domPosition.prev != this.currentItem.prev().not("."+this.options.cssNamespace+"-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) this._trigger("update", event, null, noPropagation); //Trigger update callback if the DOM position has changed
832833
if(!$.ui.contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element
833-
this._propagate("remove", event, null, noPropagation);
834+
this._trigger("remove", event, null, noPropagation);
834835
for (var i = this.containers.length - 1; i >= 0; i--){
835836
if($.ui.contains(this.containers[i].element[0], this.currentItem[0])) {
836-
this.containers[i]._propagate("update", event, this, noPropagation);
837-
this.containers[i]._propagate("receive", event, this, noPropagation);
837+
this.containers[i]._trigger("receive", event, this, noPropagation);
838+
this.containers[i]._trigger("update", event, this, noPropagation);
838839
}
839840
};
840841
};
841842

842843
//Post events to containers
843844
for (var i = this.containers.length - 1; i >= 0; i--){
844-
this.containers[i]._propagate("deactivate", event, this, noPropagation);
845+
this.containers[i]._trigger("deactivate", event, this, noPropagation);
845846
if(this.containers[i].containerCache.over) {
846-
this.containers[i]._propagate("out", event, this);
847+
this.containers[i]._trigger("out", event, this);
847848
this.containers[i].containerCache.over = 0;
848849
}
849850
}
850851

851852
this.dragging = false;
852853
if(this.cancelHelperRemoval) {
853-
this._propagate("beforeStop", event, null, noPropagation);
854-
this._propagate("stop", event, null, noPropagation);
854+
this._trigger("beforeStop", event, null, noPropagation);
855+
this._trigger("stop", event, null, noPropagation);
855856
return false;
856857
}
857858

858-
this._propagate("beforeStop", event, null, noPropagation);
859+
this._trigger("beforeStop", event, null, noPropagation);
859860

860861
//$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node!
861862
this.placeholder[0].parentNode.removeChild(this.placeholder[0]);
862863

863-
if(this.options.helper != "original") this.helper.remove(); this.helper = null;
864-
this._propagate("stop", event, null, noPropagation);
864+
if(this.helper[0] != this.currentItem[0]) this.helper.remove(); this.helper = null;
865+
this._trigger("stop", event, null, noPropagation);
865866

867+
this.fromOutside = false;
866868
return true;
867869

868870
},
869871

870-
_propagate: function(n, event, inst, noPropagation) {
871-
$.ui.plugin.call(this, n, [event, this._ui(inst)]);
872-
var dontCancel = !noPropagation ? this.element.triggerHandler(n == "sort" ? n : "sort"+n, [event, this._ui(inst)], this.options[n]) : true;
873-
if(dontCancel === false) this.cancel();
872+
_trigger: function(type, event, inst, noPropagation) {
873+
$.ui.plugin.call(this, type, [event, this._ui(inst)]);
874+
if(!noPropagation) $.widget.prototype._trigger.call(this, type, event, this._ui(inst));
875+
if(event.returnValue === false) this.cancel();
874876
},
875877

876878
plugins: {},

0 commit comments

Comments
 (0)