sortable: fixed a items issue caused the items of the container itself to be doubled if the connected query also match on it. Implemented the option connected (bool), for function serialize to also serialize connected lists.

This commit is contained in:
Paul Bakaus 2008-07-19 15:05:37 +00:00
parent 5ec53d2c2c
commit 3ddb7477e2

View File

@ -58,32 +58,36 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
sender: inst ? inst.element : null sender: inst ? inst.element : null
}; };
}, },
propagate: function(n,e,inst, noPropagation) { propagate: function(n,e,inst, noPropagation) {
$.ui.plugin.call(this, n, [e, this.ui(inst)]); $.ui.plugin.call(this, n, [e, this.ui(inst)]);
if(!noPropagation) this.element.triggerHandler(n == "sort" ? n : "sort"+n, [e, this.ui(inst)], this.options[n]); if(!noPropagation) this.element.triggerHandler(n == "sort" ? n : "sort"+n, [e, this.ui(inst)], this.options[n]);
}, },
serialize: function(o) { serialize: function(o) {
var items = ($.isFunction(this.options.items) ? this.options.items.call(this.element) : $(this.options.items, this.element)).not('.ui-sortable-helper'); //Only the items of the sortable itself var items = this.getItemsAsjQuery(o && o.connected);
var str = []; o = o || {}; var str = []; o = o || {};
items.each(function() { $(items).each(function() {
var res = ($(this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/)); var res = ($(this.item || this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/));
if(res) str.push((o.key || res[1])+'[]='+(o.key && o.expression ? res[1] : res[2])); if(res) str.push((o.key || res[1])+'[]='+(o.key && o.expression ? res[1] : res[2]));
}); });
return str.join('&'); return str.join('&');
}, },
toArray: function(attr) { toArray: function(attr) {
var items = ($.isFunction(this.options.items) ? this.options.items.call(this.element) : $(this.options.items, this.element)).not('.ui-sortable-helper'); //Only the items of the sortable itself var items = this.getItemsAsjQuery(o && o.connected);
var ret = []; var ret = [];
items.each(function() { ret.push($(this).attr(attr || 'id')); }); items.each(function() { ret.push($(this).attr(attr || 'id')); });
return ret; return ret;
}, },
/* Be careful with the following core functions */ /* Be careful with the following core functions */
intersectsWith: function(item) { intersectsWith: function(item) {
var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width, var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width,
@ -105,6 +109,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
} }
}, },
intersectsWithEdge: function(item) { intersectsWithEdge: function(item) {
var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width, var x1 = this.positionAbs.left, x2 = x1 + this.helperProportions.width,
y1 = this.positionAbs.top, y2 = y1 + this.helperProportions.height; y1 = this.positionAbs.top, y2 = y1 + this.helperProportions.height;
@ -147,10 +152,42 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
return false; return false;
}, },
refresh: function() { refresh: function() {
this.refreshItems(); this.refreshItems();
this.refreshPositions(); this.refreshPositions();
}, },
getItemsAsjQuery: function(connected) {
var self = this;
var items = [];
var queries = [];
if(this.options.connectWith && connected) {
for (var i = this.options.connectWith.length - 1; i >= 0; i--){
var cur = $(this.options.connectWith[i]);
for (var j = cur.length - 1; j >= 0; j--){
var inst = $.data(cur[j], 'sortable');
if(inst && inst != this && !inst.options.disabled) {
queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element).not(".ui-sortable-helper"), inst]);
}
};
};
}
queries.push([$.isFunction(this.options.items) ? this.options.items.call(this.element, null, { options: this.options, item: this.currentItem }) : $(this.options.items, this.element).not(".ui-sortable-helper"), this]);
for (var i = queries.length - 1; i >= 0; i--){
queries[i][0].each(function() {
items.push(this);
});
};
return $(items);
},
refreshItems: function() { refreshItems: function() {
this.items = []; this.items = [];
@ -164,7 +201,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
var cur = $(this.options.connectWith[i]); var cur = $(this.options.connectWith[i]);
for (var j = cur.length - 1; j >= 0; j--){ for (var j = cur.length - 1; j >= 0; j--){
var inst = $.data(cur[j], 'sortable'); var inst = $.data(cur[j], 'sortable');
if(inst && !inst.options.disabled) { if(inst && inst != this && !inst.options.disabled) {
queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element), inst]); queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element), inst]);
this.containers.push(inst); this.containers.push(inst);
} }
@ -185,6 +222,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
}; };
}, },
refreshPositions: function(fast) { refreshPositions: function(fast) {
//This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
@ -225,6 +263,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
} }
}, },
destroy: function() { destroy: function() {
this.element this.element
.removeClass("ui-sortable ui-sortable-disabled") .removeClass("ui-sortable ui-sortable-disabled")
@ -235,6 +274,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
for ( var i = this.items.length - 1; i >= 0; i-- ) for ( var i = this.items.length - 1; i >= 0; i-- )
this.items[i].item.removeData("sortable-item"); this.items[i].item.removeData("sortable-item");
}, },
createPlaceholder: function(that) { createPlaceholder: function(that) {
var self = that || this, o = self.options; var self = that || this, o = self.options;
@ -260,6 +300,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
o.placeholder.update(self, self.placeholder); o.placeholder.update(self, self.placeholder);
}, },
contactContainers: function(e) { contactContainers: function(e) {
for (var i = this.containers.length - 1; i >= 0; i--){ for (var i = this.containers.length - 1; i >= 0; i--){
@ -304,6 +345,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
}; };
}, },
mouseCapture: function(e, overrideHandle) { mouseCapture: function(e, overrideHandle) {
if(this.options.disabled || this.options.type == 'static') return false; if(this.options.disabled || this.options.type == 'static') return false;
@ -332,6 +374,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
return true; return true;
}, },
mouseStart: function(e, overrideHandle, noActivation) { mouseStart: function(e, overrideHandle, noActivation) {
var o = this.options; var o = this.options;
@ -446,6 +489,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
}, },
convertPositionTo: function(d, pos) { convertPositionTo: function(d, pos) {
if(!pos) pos = this.position; if(!pos) pos = this.position;
var mod = d == "absolute" ? 1 : -1; var mod = d == "absolute" ? 1 : -1;
@ -464,6 +508,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
) )
}; };
}, },
generatePosition: function(e) { generatePosition: function(e) {
var o = this.options; var o = this.options;
@ -505,6 +550,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
return position; return position;
}, },
mouseDrag: function(e) { mouseDrag: function(e) {
//Compute the helpers position //Compute the helpers position
@ -553,6 +599,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
return false; return false;
}, },
rearrange: function(e, i, a, hardRefresh) { rearrange: function(e, i, a, hardRefresh) {
a ? a[0].appendChild(this.placeholder[0]) : i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction == 'down' ? i.item[0] : i.item[0].nextSibling)); a ? a[0].appendChild(this.placeholder[0]) : i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction == 'down' ? i.item[0] : i.item[0].nextSibling));
@ -570,6 +617,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
},0); },0);
}, },
mouseStop: function(e, noPropagation) { mouseStop: function(e, noPropagation) {
//If we are using droppables, inform the manager about the drop //If we are using droppables, inform the manager about the drop
@ -593,6 +641,7 @@ $.widget("ui.sortable", $.extend({}, $.ui.mouse, {
return false; return false;
}, },
clear: function(e, noPropagation) { clear: function(e, noPropagation) {
//We first have to update the dom position of the actual currentItem //We first have to update the dom position of the actual currentItem