use Ox.debounce to fix instant list selection, fixes #2142

This commit is contained in:
j 2014-02-02 01:17:20 +05:30
parent 92a122eb0a
commit d039d0c781
2 changed files with 32 additions and 45 deletions

View file

@ -351,17 +351,11 @@ Ox.ArrayEditable = function(options, self) {
} }
} }
function triggerSelectEvent() { var triggerSelectEvent = Ox.debounce(function() {
if (!self.triggered) { that.triggerEvent('select', {
that.triggerEvent('select', { id: self.options.selected
id: self.options.selected });
}); }, true);
self.triggered = true;
setTimeout(function() {
self.triggered = false;
}, 250);
}
}
/*@ /*@
addItem <f> addItem addItem <f> addItem

View file

@ -1149,8 +1149,8 @@ Ox.List = function(options, self) {
function select(pos) { function select(pos) {
if (!isSelected(pos) || self.selected.length > 1) { if (!isSelected(pos) || self.selected.length > 1) {
selectNone(); setSelected([pos]);
addToSelection(pos); triggerSelectEvent();
self.options.centered && scrollToPosition(pos); self.options.centered && scrollToPosition(pos);
} }
} }
@ -1205,7 +1205,7 @@ Ox.List = function(options, self) {
} }
function selectNone() { function selectNone() {
deselect(Ox.range(self.listLength)); deselect(Ox.clone(self.selected));
} }
function selectPrevious() { function selectPrevious() {
@ -1229,20 +1229,21 @@ Ox.List = function(options, self) {
} }
function setSelected(ids, callback) { function setSelected(ids, callback) {
// ids can be positions
// fixme: callback is never used // fixme: callback is never used
// note: can't use selectNone here, // note: can't use selectNone here,
// since it'd trigger a select event // since it'd trigger a select event
var counter = 0; var counter = 0;
self.$items.forEach(function($item, pos) { self.$items.forEach(function($item, pos) {
if (isSelected(pos)) { if (isSelected(pos)) {
self.selected.splice(self.selected.indexOf(pos), 1);
if (!Ox.isUndefined(self.$items[pos])) { if (!Ox.isUndefined(self.$items[pos])) {
self.$items[pos].removeClass('OxSelected'); self.$items[pos].removeClass('OxSelected');
} }
} }
}); });
self.selected = [];
ids.forEach(function(id, i) { ids.forEach(function(id, i) {
var pos = getPositionById(id); var pos = Ox.isString(id) ? getPositionById(id) : id;
if (pos > -1) { if (pos > -1) {
select(pos, i); select(pos, i);
} else if (self.isAsync) { } else if (self.isAsync) {
@ -1314,34 +1315,26 @@ Ox.List = function(options, self) {
}, key ? {key: key} : {})); }, key ? {key: key} : {}));
} }
function triggerSelectEvent() { var triggerSelectEvent = Ox.debounce(function() {
// FIXME: the first select event should fire immediately // throttle in case shift+cursor is pressed
// see ArrayEditable getSelectedIds(function(ids, rest) {
getSelectedIds(function(ids) {
self.options.selected = ids; self.options.selected = ids;
setTimeout(function() { that.triggerEvent('select', Ox.extend({
getSelectedIds(function(ids_, rest) { ids: ids
self.options.selected = ids_; }, rest ? {
if (Ox.isEqual(ids, ids_)) { rest: rest
that.triggerEvent('select', Ox.extend({ } : {}));
ids: ids if (self.preview) {
}, rest ? { if (ids.length) {
rest: rest that.triggerEvent('openpreview', {
} : {})); ids: ids
if (self.preview) { });
if (ids.length) { } else {
that.triggerEvent('openpreview', { that.triggerEvent('closepreview');
ids: ids }
}); }
} else { });
that.triggerEvent('closepreview'); }, true);
}
}
}
});
}, 100);
})
}
function triggerToggleEvent(expanded) { function triggerToggleEvent(expanded) {
that.triggerEvent('toggle', { that.triggerEvent('toggle', {
@ -1612,8 +1605,8 @@ Ox.List = function(options, self) {
var arr = Ox.range(self.listLength).filter(function(pos) { var arr = Ox.range(self.listLength).filter(function(pos) {
return !isSelected(pos); return !isSelected(pos);
}); });
selectNone(); setSelected(arr);
addToSelection(arr); triggerSelectEvent();
return that; return that;
}; };