From 45dec1039cd919aa7d6d8a6d68bb6168b4536805 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 2 Jun 2013 22:29:26 -0500 Subject: [PATCH] first working scrollable box. --- lib/high-test.js | 20 +++++------ lib/high.js | 88 ++++++++++++++++-------------------------------- 2 files changed, 39 insertions(+), 69 deletions(-) diff --git a/lib/high-test.js b/lib/high-test.js index 4dfc837..01309de 100644 --- a/lib/high-test.js +++ b/lib/high-test.js @@ -65,16 +65,16 @@ screen.append(new blessed.List({ left: 'center', selectedBg: 2, items: [ - { content: 'one' }, - { content: 'two' }, - { content: 'three' }, - { content: 'four' }, - { content: 'five' }, - { content: 'six' }, - { content: 'seven' }, - { content: 'eight' }, - { content: 'nine' }, - { content: 'ten' } + 'one', + 'two', + 'three', + 'four', + 'five', + 'six', + 'seven', + 'eight', + 'nine', + 'ten' ] })); diff --git a/lib/high.js b/lib/high.js index 2830938..f4b3b03 100644 --- a/lib/high.js +++ b/lib/high.js @@ -533,7 +533,8 @@ Text.prototype.render = function() { , attr , ch , ci = 0 - , cl = this.content.length; + , cl = this.content.length + , ended = -1; if (this.position.width) { xl = xi + this.width; @@ -547,11 +548,19 @@ Text.prototype.render = function() { xl = xi + this.parent.width - (this.parent.border ? 2 : 0) - this.rleft - this.rright; } - //if (this.parent.childOffset) { - // yi -= this.parent.childOffset; - //} + if (this.parent.childBase != null && ~this.parent.items.indexOf(this)) { + var rtop = this.rtop - (this.parent.border ? 1 : 0) + , visible = this.parent.height - (this.parent.border ? 2 : 0); - var ended = -1; + yi -= this.parent.childBase; + + if (rtop - this.parent.childBase < 0) { + return; + } + if (rtop - this.parent.childBase >= visible) { + return; + } + } for (; yi < yl; yi++) { for (xi = this.left; xi < xl; xi++) { @@ -586,32 +595,11 @@ Text.prototype.render = function() { function ScrollableBox(options) { Box.call(this, options); this.childOffset = 0; + this.childBase = 0; } ScrollableBox.prototype.__proto__ = Box.prototype; -ScrollableBox.prototype.scroll = function(offset) { - if (typeof offset === 'object') { - this.childOffset = this.children.indexOf(offset); - return; - } - this.childOffset += offset; - if (this.childOffset < 0) { - this.childOffset = 0; - } else if (this.childOffset > this.children.length - 1) { - this.childOffset = this.children.length - 1; - } -}; - -ScrollableBox.prototype._render = ScrollableBox.prototype.render; -ScrollableBox.prototype.render = function() { - var children = this.children; - this.children = this.children.slice(this.childOffset, - this.childOffset + this.height - (this.border ? 1 : 0)); - this._render(); - this.children = children; -}; - /** * List */ @@ -647,8 +635,8 @@ List.prototype.add = function(item) { parent: this, fg: this.fg, bg: this.bg, - content: item.content, - top: this.children.length + (this.border ? 1 : 0), + content: item.content || item, + top: this.items.length + (this.border ? 1 : 0), left: (this.border ? 1 : 0) + 1, full: true, height: 1 @@ -700,24 +688,20 @@ List.prototype.select = function(index) { this.items[index].underline = this.selectedUnderline; } + var diff = index - this.selected; + this.childOffset += diff; + this.selected = index; - // Find the number of non-item children preceding the items. - var self = this; - var sawNon = false; - var children = this.children.filter(function(c, i) { - if (sawNon) return; - if (~self.items.indexOf(c)) { - sawNon = true; - return; - } - return !self.border || c.position.top > 0; - }).length; - - //var height = this.height - (this.children.length - this.items.length); - var height = this.height - children; - if (this.selected >= height) { - this.scroll(this.selected - height); + var visible = this.height - (this.border ? 2 : 0); + if (this.childOffset > visible - 1) { + var d = this.childOffset - (visible - 1); + this.childOffset -= d; + this.childBase += d; + } else if (this.childOffset < 0) { + var d = this.childOffset; + this.childOffset += -d; + this.childBase += d; } }; @@ -733,20 +717,6 @@ List.prototype.down = function(offset) { this.move(offset || 1); }; -//List.prototype.remove = function(index) { -// if (typeof index === 'object') { -// index = this.children.indexOf(index); -// } -// this.children.splice(index, 1); -//}; - -/* -List.prototype._render = List.prototype.render; -List.prototype.render = function() { - this._render(); -}; -*/ - /** * Input */