Skip to content

Commit

Permalink
improve listtable.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Dec 22, 2015
1 parent 04ffa28 commit 1c20b8b
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/widgets/listtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function ListTable(options) {
}

options = options || {};
options.shrink = true;

// options.shrink = true;
options.normalShrink = true;
options.style = options.style || {};
options.style.border = options.style.border || {};
Expand All @@ -37,8 +38,19 @@ function ListTable(options) {
options.style.selected = options.style.cell.selected;
options.style.item = options.style.cell;

var border = options.border;
if (border
&& border.top === false
&& border.bottom === false
&& border.left === false
&& border.right === false) {
delete options.border;
}

List.call(this, options);

options.border = border;

this._header = new Box({
parent: this,
left: this.screen.autoPadding ? 0 : this.ileft,
Expand Down Expand Up @@ -84,7 +96,10 @@ ListTable.prototype._calculateMaxes = Table.prototype._calculateMaxes;
ListTable.prototype.setRows =
ListTable.prototype.setData = function(rows) {
var self = this
, align = this.__align;
, align = this.__align
, selected = this.selected
, original = this.items.slice()
, sel = this.ritems[this.selected];

if (this.visible && this.lpos) {
this.clearPos();
Expand Down Expand Up @@ -148,7 +163,15 @@ ListTable.prototype.setData = function(rows) {

this._header.setFront();

this.select(0);
// Try to find our old item if it still exists.
sel = this.ritems.indexOf(sel);
if (~sel) {
this.select(sel);
} else if (this.items.length === original.length) {
this.select(selected);
} else {
this.select(Math.min(selected, this.items.length - 1));
}
};

ListTable.prototype._select = ListTable.prototype.select;
Expand Down Expand Up @@ -183,7 +206,12 @@ ListTable.prototype.render = function() {

var height = coords.yl - coords.yi - this.ibottom;

if (!this.border || this.options.noCellBorders) return coords;
var border = this.border;
if (!this.border && this.options.border) {
border = this.options.border;
}

if (!border || this.options.noCellBorders) return coords;

// Draw border with correct angles.
ry = 0;
Expand All @@ -200,7 +228,7 @@ ListTable.prototype.render = function() {
lines[yi + ry][xi + rx][0] = battr;
lines[yi + ry][xi + rx][1] = '\u252c'; // '┬'
// XXX If we alter iheight and itop for no borders - nothing should be written here
if (!self.border.top) {
if (!border.top) {
lines[yi + ry][xi + rx][1] = '\u2502'; // '│'
}
lines[yi + ry].dirty = true;
Expand All @@ -210,7 +238,7 @@ ListTable.prototype.render = function() {
lines[yi + ry][xi + rx][0] = battr;
lines[yi + ry][xi + rx][1] = '\u2534'; // '┴'
// XXX If we alter iheight and ibottom for no borders - nothing should be written here
if (!self.border.bottom) {
if (!border.bottom) {
lines[yi + ry][xi + rx][1] = '\u2502'; // '│'
}
lines[yi + ry].dirty = true;
Expand Down

0 comments on commit 1c20b8b

Please sign in to comment.