diff --git a/README.md b/README.md index f07f91d..4e90077 100644 --- a/README.md +++ b/README.md @@ -391,7 +391,7 @@ A scrollable list which can display selectable items. ##### Options: - inherits all from ScrollableBox. -- **selectFg, selectedBg** - foreground and background for selected item, +- **selectedFg, selectedBg** - foreground and background for selected item, treated like fg and bg. - **selectedBold, selectedUnderline** - character attributes for selected item, treated like bold and underline. diff --git a/lib/widget.js b/lib/widget.js index 77ebf99..9a58ac8 100644 --- a/lib/widget.js +++ b/lib/widget.js @@ -2575,10 +2575,8 @@ List.prototype.type = 'list'; List.prototype.add = function(item) { var self = this; - var item = new Box({ + var options = { screen: this.screen, - fg: this.fg, - bg: this.bg, content: item, align: this.align || 'left', top: this.items.length + (this.border ? 1 : 0) + this.padding, @@ -2586,9 +2584,23 @@ List.prototype.add = function(item) { right: (this.border ? 1 : 0) + this.padding + 1, tags: this.parseTags, height: 1, - hoverBg: this.mouse ? this.options.itemHoverBg : null + hoverBg: this.mouse ? this.options.itemHoverBg : null, + hoverEffects: this.mouse ? this.options.itemHoverEffects : null + }; + + ['bg', 'fg', 'bold', 'underline', + 'blink', 'inverse', 'invisible'].forEach(function(name) { + // TODO: Move all specific options to their own object namespaces. + var sname = 'selected' + name[0].toUpperCase() + name.substring(1); + options[name] = function() { + return self.items[self.selected] === item + ? self[sname] + : self[name]; + }; }); + var item = new Box(options); + this.append(item); this.items.push(item); @@ -2656,17 +2668,6 @@ List.prototype.select = function(index) { if (this.selected === index && this._listInitialized) return; this._listInitialized = true; - // TODO: Handle this a less stupid way. - ['bg', 'fg', 'bold', 'underline', - 'blink', 'inverse', 'invisible'].forEach(function(name) { - if (this.items[this.selected]) { - this.items[this.selected][name] = this[name]; - } - this.items[index][name] = this['selected' - + name.substring(0, 1).toUpperCase() - + name.substring(1)]; - }, this); - var diff = index - this.selected; this.selected = index; this.scroll(diff); @@ -4200,11 +4201,28 @@ function hsort(obj) { } function sattr(obj, fg, bg) { - return ((((obj.invisible ? 16 : 0) << 18) - | ((obj.inverse ? 8 : 0) << 18) - | ((obj.blink ? 4 : 0) << 18) - | ((obj.underline ? 2 : 0) << 18)) - | ((obj.bold ? 1 : 0) << 18) + var bold = obj.bold + , underline = obj.underline + , blink = obj.blink + , inverse = obj.inverse + , invisible = obj.invisible; + + // This used to be a loop, but I decided + // to unroll it for performance's sake. + if (typeof bold === 'function') bold = bold(); + if (typeof underline === 'function') underline = underline(); + if (typeof blink === 'function') blink = blink(); + if (typeof inverse === 'function') inverse = inverse(); + if (typeof invisible === 'function') invisible = invisible(); + + if (typeof fg === 'function') fg = fg(); + if (typeof bg === 'function') bg = bg(); + + return ((((invisible ? 16 : 0) << 18) + | ((inverse ? 8 : 0) << 18) + | ((blink ? 4 : 0) << 18) + | ((underline ? 2 : 0) << 18)) + | ((bold ? 1 : 0) << 18) | (colors.convert(fg) << 9)) | colors.convert(bg); }