even more listbar work.

This commit is contained in:
Christopher Jeffrey 2014-01-11 18:54:26 -06:00
parent 66933f63b6
commit 2b33e34d3a
2 changed files with 96 additions and 30 deletions

View File

@ -634,6 +634,8 @@ Screen.prototype.render = function() {
this.draw(0, this.rows - 1);
// XXX Workaround to deal with cursor pos before the screen has rendered and
// lpos is not reliable (stale).
if (this.focused && this.focused._updateCursor) {
this.focused._updateCursor(true);
}
@ -5696,15 +5698,21 @@ function Listbar(options) {
options = options || {};
// XXX Workaround to make sure buttons don't
// overlap border on the right.
options.scrollable = true;
this.items = [];
this.ritems = [];
this.commands = [];
this.leftBase = 0;
this.leftOffset = 0;
this.mouse = options.mouse || false;
Box.call(this, options);
/*
this._.debug = new Box({
parent: this.screen,
top: 0,
@ -5712,7 +5720,6 @@ function Listbar(options) {
shrink: true,
content: '...'
});
*/
// XXX Make consistent with vertical lists.
//if (this.style.selected) {
@ -5726,14 +5733,13 @@ function Listbar(options) {
if (options.keys) {
this.on('keypress', function(ch, key) {
//this.on('element keypress', function(el, ch, key) {
if (key.name === 'left' || (options.vi && key.name === 'h')) {
self.sel(-1);
self.moveLeft();
self.screen.render();
return;
}
if (key.name === 'right' || (options.vi && key.name === 'l')) {
self.sel(1);
self.moveRight();
self.screen.render();
return;
}
@ -5743,7 +5749,9 @@ function Listbar(options) {
self.emit('select', self.items[self.selected], self.selected);
var item = self.items[self.selected];
//item.press();
if (item._.cmd.callback) item._.cmd.callback();
if (item._.cmd.callback) {
item._.cmd.callback();
}
self.screen.render();
return;
}
@ -5755,7 +5763,7 @@ function Listbar(options) {
});
}
if (options.globalKeys) {
if (options.autoCommandKeys) {
this.screen.on('keypress', function(ch, key) {
if (/^[0-9]$/.test(ch)) {
var i = +ch - 1;
@ -5763,8 +5771,9 @@ function Listbar(options) {
var item = self.items[i];
if (item) {
//item.press();
//item.focus();
if (item._.cmd.callback) item._.cmd.callback();
if (item._.cmd.callback) {
item._.cmd.callback();
}
self.select(i);
self.screen.render();
}
@ -5773,10 +5782,7 @@ function Listbar(options) {
}
this.on('focus', function() {
if (self.items[self.selected]) {
//self.items[self.selected].focus();
self.select(self.selected);
}
self.select(self.selected);
});
}
@ -5823,10 +5829,12 @@ Listbar.prototype.setItems = function(commands) {
this.commands = [];
commands.forEach(function(cmd) {
self.appendItem(cmd);
self.add(cmd);
});
};
Listbar.prototype.add =
Listbar.prototype.addItem =
Listbar.prototype.appendItem = function(item, callback) {
var self = this
, prev = this.items[this.items.length - 1]
@ -5840,11 +5848,19 @@ Listbar.prototype.appendItem = function(item, callback) {
}
if (typeof item === 'string') {
cmd = { prefix: (this.items.length + 1) + '', text: item, callback: callback };
cmd = {
prefix: (this.items.length + 1) + '',
text: item,
callback: callback
};
}
if (typeof item === 'function') {
cmd = { prefix: (this.items.length + 1) + '', text: item.name, callback: item };
cmd = {
prefix: (this.items.length + 1) + '',
text: item.name,
callback: item
};
}
title = (cmd.prefix != null ? '{light-black-fg}'
@ -5869,9 +5885,7 @@ Listbar.prototype.appendItem = function(item, callback) {
style: merge({}, this.style.item)
};
if (this.screen.autoPadding) {
;
} else {
if (!this.screen.autoPadding) {
options.top += this.itop;
options.left += this.ileft;
}
@ -5887,7 +5901,8 @@ Listbar.prototype.appendItem = function(item, callback) {
};
});
var el = new Button(options);
var el = new Box(options);
//var el = new Button(options);
this._[cmd.text] = el;
cmd.element = el;
@ -5899,7 +5914,7 @@ Listbar.prototype.appendItem = function(item, callback) {
this.append(el);
if (cmd.callback) {
el.on('press', cmd.callback);
//el.on('press', cmd.callback);
if (cmd.keys) {
this.screen.key(cmd.keys, cmd.callback);
}
@ -5909,16 +5924,36 @@ Listbar.prototype.appendItem = function(item, callback) {
this.select(0);
}
if (0) if (this.mouse) {
if (this.mouse) {
el.on('click', function(data) {
if (self.items[self.selected] === el) {
self.emit('action', el, self.selected);
self.emit('select', el, self.selected);
return;
self.emit('action', el, self.selected);
self.emit('select', el, self.selected);
//el.press();
if (el._.cmd.callback) {
el._.cmd.callback();
}
self.select(el);
self.screen.render();
});
/*
el.on('click', function(data) {
if (self.items[self.selected] === el) {
self._.debug.setContent('execute');
self.emit('action', el, self.selected);
self.emit('select', el, self.selected);
var item = self.items[self.selected];
//item.press();
if (item._.cmd.callback) {
item._.cmd.callback();
}
self.screen.render();
return;
}
self._.debug.setContent('select');
self.select(el);
self.screen.render();
});
*/
}
};
@ -5940,6 +5975,10 @@ Listbar.prototype.render = function() {
};
Listbar.prototype.select = function(offset) {
if (typeof offset !== 'number') {
offset = this.items.indexOf(offset);
}
var lpos = this._getCoords();
if (!lpos) return;
@ -5998,8 +6037,32 @@ Listbar.prototype.select = function(offset) {
//el.focus();
};
Listbar.prototype.sel = function(i) {
this.select(this.leftBase + this.leftOffset + i);
Listbar.prototype.removeItem = function(child) {
var i = typeof child !== 'number'
? this.items.indexOf(child)
: child;
if (~i && this.items[i]) {
child = this.items.splice(i, 1)[0];
this.ritems.splice(i, 1);
this.commands.splice(i, 1);
this.remove(child);
if (i === this.selected) {
this.select(i - 1);
}
}
};
Listbar.prototype.move = function(offset) {
this.select(this.selected + offset);
};
Listbar.prototype.moveLeft = function(offset) {
this.move(-(offset || 1));
};
Listbar.prototype.moveRight = function(offset) {
this.move(offset || 1);
};
/**

View File

@ -18,10 +18,13 @@ var bar = blessed.listbar({
parent: screen,
bottom: 0,
left: 0,
height: 1,
height: 3,
mouse: true,
keys: true,
globalKeys: true,
autoCommandKeys: true,
border: {
type: 'line'
},
vi: true,
style: {
bg: 'green',