fix select all

This commit is contained in:
rlx 2011-12-19 17:19:54 +00:00
parent c1c4441b10
commit 609754fbad

View file

@ -220,36 +220,29 @@ Ox.List = function(options, self) {
} }
function addAllToSelection(pos) { function addAllToSelection(pos) {
var arr, var arr, i, len = self.$items.length;
len = self.$items.length;
if (!isSelected(pos)) { if (!isSelected(pos)) {
if (self.selected.length == 0) { if (self.selected.length == 0) {
addToSelection(pos); addToSelection(pos);
} else { } else {
arr = [pos];
if (Ox.min(self.selected) < pos) { if (Ox.min(self.selected) < pos) {
var arr = [pos]; for (i = pos - 1; i >= 0; i--) {
for (var i = pos - 1; i >= 0; i--) {
if (isSelected(i)) { if (isSelected(i)) {
arr.forEach(function(v) {
addToSelection(v);
});
break; break;
} }
arr.push(i); arr.push(i);
} }
} }
if (Ox.max(self.selected) > pos) { if (Ox.max(self.selected) > pos) {
var arr = [pos]; for (i = pos + 1; i < len; i++) {
for (var i = pos + 1; i < len; i++) {
if (isSelected(i)) { if (isSelected(i)) {
arr.forEach(function(v) {
addToSelection(v);
});
break; break;
} }
arr.push(i); arr.push(i);
} }
} }
addToSelection(arr);
} }
} }
} }
@ -285,16 +278,20 @@ Ox.List = function(options, self) {
} }
function addToSelection(pos) { function addToSelection(pos) {
if (!isSelected(pos)) { var triggerEvent = false;
self.selected.push(pos); Ox.toArray(pos).forEach(function(pos) {
!Ox.isUndefined(self.$items[pos]) && if (!isSelected(pos)) {
self.$items[pos].addClass('OxSelected'); self.selected.push(pos);
triggerSelectEvent(); !Ox.isUndefined(self.$items[pos])
} else { && self.$items[pos].addClass('OxSelected');
// allow for 'cursor navigation' if orientation == 'both' triggerEvent = true;
self.selected.splice(self.selected.indexOf(pos), 1); } else {
self.selected.push(pos); // allow for 'cursor navigation' if orientation == 'both'
} self.selected.splice(self.selected.indexOf(pos), 1);
self.selected.push(pos);
}
});
triggerEvent && triggerSelectEvent();
} }
function clear() { function clear() {
@ -348,12 +345,16 @@ Ox.List = function(options, self) {
} }
function deselect(pos) { function deselect(pos) {
if (isSelected(pos)) { var triggerEvent = false;
self.selected.splice(self.selected.indexOf(pos), 1); Ox.toArray(pos).forEach(function(pos) {
!Ox.isUndefined(self.$items[pos]) && if (isSelected(pos)) {
self.$items[pos].removeClass('OxSelected'); self.selected.splice(self.selected.indexOf(pos), 1);
triggerSelectEvent(); !Ox.isUndefined(self.$items[pos])
} && self.$items[pos].removeClass('OxSelected');
triggerEvent = true;
}
});
triggerEvent && triggerSelectEvent();
} }
function dragstart(data) { function dragstart(data) {
@ -671,13 +672,40 @@ Ox.List = function(options, self) {
) * self.rowLength; ) * self.rowLength;
} }
function getSelectedIds() { function getSelectedIds(callback) {
var ids = [], notFound = false;
if (self.$items.length == 0) { if (self.$items.length == 0) {
return self.options.selected; callback(self.options.selected);
} else { } else {
return self.selected.map(function(pos) { Ox.forEach(self.selected, function(pos) {
return self.$items[pos].options('data')[self.options.unique]; if (self.$items[pos]) {
ids.push(self.$items[pos].options('data')[self.options.unique]);
} else {
notFound = true;
return false;
}
}); });
if (notFound) {
// selection across items that are not in the DOM
self.options.items({
keys: [self.options.unique],
range: [0, self.listLength],
sort: self.options.sort
}, function(result) {
var ids = [], rest = [],
useRest = self.selected.length > self.listLength / 2;
result.data.items.forEach(function(item, i) {
if (self.selected.indexOf(i) > -1) {
ids.push(item[self.options.unique]);
} else if (useRest) {
rest.push(item[self.options.unique]);
}
});
useRest ? callback(ids, rest) : callback(ids);
});
} else {
callback(ids);
}
} }
} }
@ -687,9 +715,11 @@ Ox.List = function(options, self) {
} }
function invertSelection() { function invertSelection() {
Ox.range(self.listLength).forEach(function(v) { var arr = Ox.range(self.listLength).filter(function(pos) {
toggleSelection(v); return !isSelected(pos);
}); });
selectNone();
addToSelection(arr);
} }
function isSelected(pos) { function isSelected(pos) {
@ -1155,9 +1185,7 @@ Ox.List = function(options, self) {
} }
function selectAll() { function selectAll() {
Ox.range(self.listLength).forEach(function(pos) { addToSelection(Ox.range(self.listLength));
addToSelection(pos);
});
} }
function selectBelow() { function selectBelow() {
@ -1177,9 +1205,7 @@ Ox.List = function(options, self) {
} }
function selectNone() { function selectNone() {
self.$items.forEach(function(v, i) { deselect(Ox.range(self.listLength));
deselect(i);
});
} }
function selectPrevious() { function selectPrevious() {
@ -1238,6 +1264,7 @@ Ox.List = function(options, self) {
} }
function toggleSelection(pos) { function toggleSelection(pos) {
// FIXME: unused
if (!isSelected(pos)) { if (!isSelected(pos)) {
addToSelection(pos); addToSelection(pos);
} else { } else {
@ -1255,21 +1282,24 @@ Ox.List = function(options, self) {
} }
function triggerSelectEvent() { function triggerSelectEvent() {
var ids = self.options.selected = getSelectedIds(); getSelectedIds(function(ids) {
setTimeout(function() { self.options.selected = ids;
var ids_ = self.options.selected = getSelectedIds(); setTimeout(function() {
// Ox.Log('List', 'ids', ids, 'ids after 100 msec', ids_, Ox.isEqual(ids, ids_)) getSelectedIds(function(ids_, rest) {
if (Ox.isEqual(ids, ids_)) { self.options.selected = ids_;
that.triggerEvent('select', { if (Ox.isEqual(ids, ids_)) {
ids: ids that.triggerEvent('select', Ox.extend({
ids: ids
}, rest ? {
rest: rest
} : {}));
self.preview && that.triggerEvent('openpreview', {
ids: ids
});
}
}); });
self.preview && that.triggerEvent('openpreview', { }, 100);
ids: ids })
});
} else {
// Ox.Log('List', 'select event not triggered after timeout');
}
}, 100);
} }
function triggerToggleEvent(expanded) { function triggerToggleEvent(expanded) {
@ -1360,15 +1390,16 @@ Ox.List = function(options, self) {
function updateSelected() { function updateSelected() {
//Ox.Log('List', 'updateSelected') //Ox.Log('List', 'updateSelected')
var oldSelectedIds = getSelectedIds(), getSelectedIds(function(oldIds) {
newSelectedIds = []; var newIds = [];
Ox.forEach(self.options.items, function(item) { Ox.forEach(self.options.items, function(item) {
if (oldSelectedIds.indexOf(item.id) > -1) { if (oldIds.indexOf(item.id) > -1) {
newSelectedIds.push(item.id); newIds.push(item.id);
} }
return newSelectedIds.length < oldSelectedIds.length; return newIds.length < oldIds.length;
});
setSelected(newIds);
}); });
setSelected(newSelectedIds);
} }
function updateSort() { function updateSort() {
@ -1379,30 +1410,31 @@ Ox.List = function(options, self) {
sort = {}; sort = {};
//if (self.listLength > 1) { //if (self.listLength > 1) {
if (!self.isAsync) { if (!self.isAsync) {
selectedIds = getSelectedIds(); getSelectedIds(function(selectedIds) {
self.options.items.forEach(function(item) { self.options.items.forEach(function(item) {
sort[item.id] = map ? map(item[key], item) : item[key]; sort[item.id] = map ? map(item[key], item) : item[key];
});
self.options.items.sort(function(a, b) {
var aValue = sort[a.id],
bValue = sort[b.id],
ret = 0;
if (aValue < bValue) {
ret = operator == '+' ? -1 : 1;
} else if (aValue > bValue) {
ret = operator == '+' ? 1 : -1;
}
return ret;
});
if (selectedIds.length) {
self.selected = [];
self.options.items.forEach(function(item, i) {
if (selectedIds.indexOf(item.id) > -1) {
self.selected.push(i);
}
}); });
} self.options.items.sort(function(a, b) {
loadItems(); var aValue = sort[a.id],
bValue = sort[b.id],
ret = 0;
if (aValue < bValue) {
ret = operator == '+' ? -1 : 1;
} else if (aValue > bValue) {
ret = operator == '+' ? 1 : -1;
}
return ret;
});
if (selectedIds.length) {
self.selected = [];
self.options.items.forEach(function(item, i) {
if (selectedIds.indexOf(item.id) > -1) {
self.selected.push(i);
}
});
}
loadItems();
});
} else { } else {
clear(); // fixme: bad function name clear(); // fixme: bad function name
getPositions(); getPositions();