Lists: make most recently selected item selected[0]; add selectSelected method to change this item from outside

This commit is contained in:
rolux 2014-02-02 12:34:12 +00:00
parent 120a9eda41
commit e069f11c9a
5 changed files with 66 additions and 16 deletions

View file

@ -134,6 +134,11 @@ Ox.CustomList = function(options, self) {
return that; return that;
}; };
that.selectSelected = function(offset) {
that.$list.selectSelected(offset);
return that;
};
/*@ /*@
size <f> size size <f> size
@*/ @*/

View file

@ -220,6 +220,16 @@ Ox.IconList = function(options, self) {
return that; return that;
}; };
that.selectPosition = function(pos) {
that.$element.selectPosition(pos);
return that;
};
that.selectSelected = function(offset) {
that.$element.selectSelected(offset);
return that;
};
/*@ /*@
size <f> get size of list size <f> get size of list
() -> <n> size () -> <n> size

View file

@ -239,6 +239,16 @@ Ox.InfoList = function(options, self) {
return that; return that;
}; };
that.selectPosition = function(pos) {
that.$element.selectPosition(pos);
return that;
};
that.selectSelected = function(offset) {
that.$element.selectSelected(offset);
return that;
};
/*@ /*@
size <f> size size <f> size
@*/ @*/

View file

@ -355,21 +355,19 @@ Ox.List = function(options, self) {
} }
function addToSelection(pos) { function addToSelection(pos) {
var triggerEvent = false; Ox.makeArray(pos).reverse().forEach(function(pos) {
Ox.makeArray(pos).forEach(function(pos) {
if (!isSelected(pos)) { if (!isSelected(pos)) {
self.selected.push(pos); self.selected.unshift(pos);
if (!Ox.isUndefined(self.$items[pos])) { if (!Ox.isUndefined(self.$items[pos])) {
self.$items[pos].addClass('OxSelected'); self.$items[pos].addClass('OxSelected');
} }
triggerEvent = true;
} else { } else {
// allow for 'cursor navigation' if orientation == 'both' // allow for 'cursor navigation' if orientation is 'both'
self.selected.splice(self.selected.indexOf(pos), 1); self.selected.splice(self.selected.indexOf(pos), 1);
self.selected.push(pos); self.selected.unshift(pos);
} }
}); });
triggerEvent && triggerSelectEvent(); triggerSelectEvent();
} }
function clear() { function clear() {
@ -573,7 +571,7 @@ Ox.List = function(options, self) {
function getAbove() { function getAbove() {
var pos = -1; var pos = -1;
if (self.selected.length) { if (self.selected.length) {
pos = self.selected[self.selected.length - 1] - self.rowLength; pos = self.selected[0] - self.rowLength;
if (pos < 0) { if (pos < 0) {
pos = -1; pos = -1;
} }
@ -584,7 +582,7 @@ Ox.List = function(options, self) {
function getBelow() { function getBelow() {
var pos = -1; var pos = -1;
if (self.selected.length) { if (self.selected.length) {
pos = self.selected[self.selected.length - 1] + self.rowLength; pos = self.selected[0] + self.rowLength;
if (pos >= self.$items.length) { if (pos >= self.$items.length) {
pos = -1; pos = -1;
} }
@ -609,9 +607,11 @@ Ox.List = function(options, self) {
function getNext() { function getNext() {
var pos = -1; var pos = -1;
if (self.selected.length) { if (self.selected.length) {
pos = (self.options.orientation == 'both' pos = (
? self.selected[self.selected.length - 1] self.options.orientation == 'both'
: Ox.max(self.selected)) + 1; ? self.selected[0]
: Ox.max(self.selected)
) + 1;
if (pos == self.$items.length) { if (pos == self.$items.length) {
pos = -1; pos = -1;
} }
@ -721,9 +721,11 @@ Ox.List = function(options, self) {
function getPrevious() { function getPrevious() {
var pos = -1; var pos = -1;
if (self.selected.length) { if (self.selected.length) {
pos = (self.options.orientation == 'both' pos = (
? self.selected[self.selected.length - 1] self.options.orientation == 'both'
: Ox.min(self.selected)) - 1; ? self.selected[0]
: Ox.min(self.selected)
) - 1;
} }
return pos; return pos;
} }
@ -1316,7 +1318,7 @@ Ox.List = function(options, self) {
} }
var triggerSelectEvent = Ox.debounce(function() { var triggerSelectEvent = Ox.debounce(function() {
// throttle in case shift+cursor is pressed // throttle in case shift+arrow is pressed
getSelectedIds(function(ids, rest) { getSelectedIds(function(ids, rest) {
self.options.selected = ids; self.options.selected = ids;
that.triggerEvent('select', Ox.extend({ that.triggerEvent('select', Ox.extend({
@ -1721,6 +1723,24 @@ Ox.List = function(options, self) {
return that; return that;
}; };
/*@
selectSelected <f> Change the first selected item within the selection
(offset) -> <o> List Object
offset <n> Offset (`-1` for previous, `1` for next)
@*/
that.selectSelected = function(offset) {
var pos, positions;
if (self.selected.length > 1) {
positions = Ox.sort(Ox.clone(self.selected));
pos = positions[
Ox.mod(positions.indexOf(self.selected[0]) + offset, positions.length)
];
addToSelection(pos);
scrollToPosition(pos);
}
return that;
};
/*@ /*@
size <f> fixme: not a good function name size <f> fixme: not a good function name
() -> <o> List Object () -> <o> List Object

View file

@ -1133,6 +1133,11 @@ Ox.TableList = function(options, self) {
return that; return that;
}; };
that.selectSelected = function(offset) {
that.$body.selectSelected(offset);
return that;
};
that.setColumnTitle = function(id, title) { that.setColumnTitle = function(id, title) {
var index = getColumnIndexById(id); var index = getColumnIndexById(id);
self.options.columns[index].title = title; self.options.columns[index].title = title;