add "interactive" option for lists. fixes #103.

This commit is contained in:
Christopher Jeffrey 2015-02-09 20:11:02 -08:00
parent d69ed5c294
commit 00a0f800f9
2 changed files with 18 additions and 3 deletions

View File

@ -569,7 +569,12 @@ A scrollable list which can display selectable items.
- **keys** - use predefined keys for navigating the list. - **keys** - use predefined keys for navigating the list.
- **vi** - use vi keys with the `keys` option. - **vi** - use vi keys with the `keys` option.
- **items** - an array of strings which become the list's items. - **items** - an array of strings which become the list's items.
- **search** - a function that is called when `vi` mode is enabled and the key `/` is pressed. This function accepts a callback function which should be called with the search string. The search string is then used to jump to an item that is found in `items`. - **search** - a function that is called when `vi` mode is enabled and the key
`/` is pressed. This function accepts a callback function which should be
called with the search string. The search string is then used to jump to an
item that is found in `items`.
- **interactive** - whether the list is interactive or can have items selected
(default: true).
##### Properties: ##### Properties:

View File

@ -3384,7 +3384,7 @@ Element.prototype.render = function() {
ci += c[0].length - 1; ci += c[0].length - 1;
attr = this.screen.attrCode(c[0], attr, dattr); attr = this.screen.attrCode(c[0], attr, dattr);
// Ignore foreground changes for selected items. // Ignore foreground changes for selected items.
if (this.parent._isList if (this.parent._isList && this.parent.interactive
&& this.parent.items[this.parent.selected] === this) { && this.parent.items[this.parent.selected] === this) {
attr = (attr & ~(0x1ff << 9)) | (dattr & (0x1ff << 9)); attr = (attr & ~(0x1ff << 9)) | (dattr & (0x1ff << 9));
} }
@ -4229,6 +4229,8 @@ function List(options) {
this.style.item.focus = this.options.itemFocusEffects; this.style.item.focus = this.options.itemFocusEffects;
} }
this.interactive = options.interactive !== false;
this.mouse = options.mouse || false; this.mouse = options.mouse || false;
if (options.items) { if (options.items) {
@ -4399,7 +4401,7 @@ List.prototype.appendItem = function(item) {
['bg', 'fg', 'bold', 'underline', ['bg', 'fg', 'bold', 'underline',
'blink', 'inverse', 'invisible'].forEach(function(name) { 'blink', 'inverse', 'invisible'].forEach(function(name) {
options[name] = function() { options[name] = function() {
var attr = self.items[self.selected] === item var attr = self.items[self.selected] === item && self.interactive
? self.style.selected[name] ? self.style.selected[name]
: self.style.item[name]; : self.style.item[name];
if (typeof attr === 'function') attr = attr(item); if (typeof attr === 'function') attr = attr(item);
@ -4506,6 +4508,10 @@ List.prototype.setItems = function(items) {
}; };
List.prototype.select = function(index) { List.prototype.select = function(index) {
if (!this.interactive) {
return;
}
if (!this.items.length) { if (!this.items.length) {
this.selected = 0; this.selected = 0;
this.value = ''; this.value = '';
@ -4549,6 +4555,10 @@ List.prototype.pick = function(label, callback) {
label = null; label = null;
} }
if (!this.interactive) {
return callback();
}
var self = this; var self = this;
var focused = this.screen.focused; var focused = this.screen.focused;
if (focused && focused._done) focused._done('stop'); if (focused && focused._done) focused._done('stop');