From 5e5116b3d4ce615baaa62c8cf2a45a5dd3523ffa Mon Sep 17 00:00:00 2001 From: Paul Bakaus Date: Mon, 3 Nov 2008 21:41:59 +0000 Subject: [PATCH] sortable: implemented ui.cancel(), a function that can be called to completely cancel the sortable attempt, and revert to the original state (implements #3283, #3402) --- ui/ui.sortable.js | 53 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/ui/ui.sortable.js b/ui/ui.sortable.js index 4c165d342..ed9f4198c 100644 --- a/ui/ui.sortable.js +++ b/ui/ui.sortable.js @@ -46,17 +46,58 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, { }, plugins: {}, ui: function(inst) { + var self = inst || this; return { - helper: (inst || this)["helper"], - placeholder: (inst || this)["placeholder"] || $([]), - position: (inst || this)["position"], - absolutePosition: (inst || this)["positionAbs"], + helper: self.helper, + placeholder: self.placeholder || $([]), + position: self.position, + absolutePosition: self.positionAbs, options: this.options, element: this.element, - item: (inst || this)["currentItem"], - sender: inst ? inst.element : null + item: self.currentItem, + sender: inst ? inst.element : null, + cancel: function() { self.cancel(); } }; }, + + cancel: function() { + + if(this.dragging) { + if(this.options.helper == "original") + this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper"); + else + this.currentItem.show(); + + //Post deactivating events to containers + for (var i = this.containers.length - 1; i >= 0; i--){ + this.containers[i]._propagate("deactivate", null, this); + if(this.containers[i].containerCache.over) { + this.containers[i]._propagate("out", null, this); + this.containers[i].containerCache.over = 0; + } + } + } + + //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node! + if(this.placeholder[0].parentNode) this.placeholder[0].parentNode.removeChild(this.placeholder[0]); + if(this.options.helper != "original" && this.helper && this.helper[0].parentNode) this.helper.remove(); + + $.extend(this, { + helper: null, + dragging: false, + reverting: false, + _noFinalSort: null + }); + + if(this.domPosition.prev) { + $(this.domPosition.prev).after(this.currentItem); + } else { + $(this.domPosition.parent).prepend(this.currentItem); + } + + return true; + + }, _propagate: function(n,e,inst, noPropagation) { $.ui.plugin.call(this, n, [e, this.ui(inst)]);