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() {
if (!self.triggered) {
that.triggerEvent('select', {
id: self.options.selected
});
self.triggered = true;
setTimeout(function() {
self.triggered = false;
}, 250);
}
}
var triggerSelectEvent = Ox.debounce(function() {
that.triggerEvent('select', {
id: self.options.selected
});
}, true);
/*@
addItem <f> addItem

View file

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