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) {
var triggerEvent = false;
Ox.toArray(pos).forEach(function(pos) {
if (!isSelected(pos)) { if (!isSelected(pos)) {
self.selected.push(pos); self.selected.push(pos);
!Ox.isUndefined(self.$items[pos]) && !Ox.isUndefined(self.$items[pos])
self.$items[pos].addClass('OxSelected'); && self.$items[pos].addClass('OxSelected');
triggerSelectEvent(); triggerEvent = true;
} else { } else {
// allow for 'cursor navigation' if orientation == 'both' // allow for 'cursor navigation' if orientation == 'both'
self.selected.splice(self.selected.indexOf(pos), 1); self.selected.splice(self.selected.indexOf(pos), 1);
self.selected.push(pos); 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) {
var triggerEvent = false;
Ox.toArray(pos).forEach(function(pos) {
if (isSelected(pos)) { if (isSelected(pos)) {
self.selected.splice(self.selected.indexOf(pos), 1); self.selected.splice(self.selected.indexOf(pos), 1);
!Ox.isUndefined(self.$items[pos]) && !Ox.isUndefined(self.$items[pos])
self.$items[pos].removeClass('OxSelected'); && self.$items[pos].removeClass('OxSelected');
triggerSelectEvent(); 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) {
self.options.selected = ids;
setTimeout(function() { setTimeout(function() {
var ids_ = self.options.selected = getSelectedIds(); getSelectedIds(function(ids_, rest) {
// Ox.Log('List', 'ids', ids, 'ids after 100 msec', ids_, Ox.isEqual(ids, ids_)) self.options.selected = ids_;
if (Ox.isEqual(ids, ids_)) { if (Ox.isEqual(ids, ids_)) {
that.triggerEvent('select', { that.triggerEvent('select', Ox.extend({
ids: ids ids: ids
}); }, rest ? {
rest: rest
} : {}));
self.preview && that.triggerEvent('openpreview', { self.preview && that.triggerEvent('openpreview', {
ids: ids ids: ids
}); });
} else {
// Ox.Log('List', 'select event not triggered after timeout');
} }
});
}, 100); }, 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,7 +1410,7 @@ 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];
}); });
@ -1403,6 +1434,7 @@ Ox.List = function(options, self) {
}); });
} }
loadItems(); loadItems();
});
} else { } else {
clear(); // fixme: bad function name clear(); // fixme: bad function name
getPositions(); getPositions();