From 22469380e2d51d58e1e5b608f9825fe2d4ed47ca Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 30 Jul 2015 17:55:40 -0700 Subject: [PATCH] fix focus error. fix table setData error. fixes #160. --- lib/widgets/listtable.js | 10 +++++++++- lib/widgets/screen.js | 2 ++ lib/widgets/table.js | 11 +++++++++++ test/widget-listtable.js | 5 ++++- test/widget-table.js | 3 ++- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/widgets/listtable.js b/lib/widgets/listtable.js index 3d9ebfd..311f9fd 100644 --- a/lib/widgets/listtable.js +++ b/lib/widgets/listtable.js @@ -65,6 +65,10 @@ function ListTable(options) { this.setData(options.rows || options.data); + this.on('attach', function() { + self.setData(self.rows); + }); + this.on('resize', function() { var selected = self.selected; self.setData(self.rows); @@ -84,7 +88,9 @@ ListTable.prototype.setData = function(rows) { var self = this , align = this.__align; - this.clearPos(); + if (this.visible && this.lpos) { + this.clearPos(); + } this.clearItems(); @@ -92,6 +98,8 @@ ListTable.prototype.setData = function(rows) { this._calculateMaxes(); + if (!this._maxes) return; + this.addItem(''); this.rows.forEach(function(row, i) { diff --git a/lib/widgets/screen.js b/lib/widgets/screen.js index a18db24..775344a 100644 --- a/lib/widgets/screen.js +++ b/lib/widgets/screen.js @@ -1623,6 +1623,8 @@ Screen.prototype._focus = function(self, old) { if (el.scrollable) break; } + if (el && el.detached) return; + // If we're in a scrollable element, // automatically scroll to the focused element. if (el) { diff --git a/lib/widgets/table.js b/lib/widgets/table.js index daca678..618cb5a 100644 --- a/lib/widgets/table.js +++ b/lib/widgets/table.js @@ -44,6 +44,11 @@ function Table(options) { this.setData(options.rows || options.data); + this.on('attach', function() { + self.setContent(''); + self.setData(self.rows); + }); + this.on('resize', function() { self.setContent(''); self.setData(self.rows); @@ -59,6 +64,10 @@ Table.prototype._calculateMaxes = function() { var self = this; var maxes = []; + if (this.detached) return; + + this.rows = this.rows || []; + this.rows.forEach(function(row) { row.forEach(function(cell, i) { var clen = self.strWidth(cell); @@ -110,6 +119,8 @@ Table.prototype.setData = function(rows) { this._calculateMaxes(); + if (!this._maxes) return; + this.rows.forEach(function(row, i) { var isHeader = i === 0; var isFooter = i === self.rows.length - 1; diff --git a/test/widget-listtable.js b/test/widget-listtable.js index d581dfb..a9a0be1 100644 --- a/test/widget-listtable.js +++ b/test/widget-listtable.js @@ -30,7 +30,7 @@ var box = blessed.box({ */ var table = blessed.listtable({ - parent: screen, + //parent: screen, top: 'center', left: 'center', data: null, @@ -89,6 +89,9 @@ screen.key('q', function() { table.focus(); table.setData(data2); + +screen.append(table); + screen.render(); setTimeout(function() { diff --git a/test/widget-table.js b/test/widget-table.js index e4a60ba..8f6786e 100644 --- a/test/widget-table.js +++ b/test/widget-table.js @@ -12,7 +12,7 @@ var DU = '杜'; var JUAN = '鹃'; var table = blessed.table({ - parent: screen, + //parent: screen, top: 'center', left: 'center', data: null, @@ -62,6 +62,7 @@ screen.key('q', function() { }); table.setData(data2); +screen.append(table); screen.render(); setTimeout(function() {