listbar work.

This commit is contained in:
Christopher Jeffrey 2014-01-11 18:23:38 -06:00
parent 045f8d6dc1
commit 6ed2451e36
1 changed files with 184 additions and 22 deletions

View File

@ -5697,7 +5697,8 @@ function Listbar(options) {
options = options || {};
this.items = [];
this.commands = options.commands;
this.ritems = [];
this.commands = [];
this.leftBase = 0;
this.leftOffset = 0;
@ -5714,17 +5715,18 @@ function Listbar(options) {
*/
// XXX Make consistent with vertical lists.
if (this.style.selected) {
this.style.item = this.style.item || merge({}, this.style);
this.style.item.focus = this.style.selected;
}
//if (this.style.selected) {
// this.style.item = this.style.item || merge({}, this.style);
// this.style.item.focus = this.style.selected;
//}
if (options.commands || options.items) {
this.setItems(options.commands || options.items);
}
if (options.keys) {
this.on('element keypress', function(el, ch, key) {
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.screen.render();
@ -5739,6 +5741,10 @@ function Listbar(options) {
|| (options.vi && key.name === 'k' && !key.shift)) {
self.emit('action', self.items[self.selected], self.selected);
self.emit('select', self.items[self.selected], self.selected);
var item = self.items[self.selected];
//item.press();
if (item._.cmd.callback) item._.cmd.callback();
self.screen.render();
return;
}
if (key.name === 'escape' || (options.vi && key.name === 'q')) {
@ -5754,10 +5760,13 @@ function Listbar(options) {
if (/^[0-9]$/.test(ch)) {
var i = +ch - 1;
if (!~i) i = 9;
var button = self.items[i];
if (button) {
button.press();
button.focus();
var item = self.items[i];
if (item) {
//item.press();
//item.focus();
if (item._.cmd.callback) item._.cmd.callback();
self.select(i);
self.screen.render();
}
}
});
@ -5765,7 +5774,8 @@ function Listbar(options) {
this.on('focus', function() {
if (self.items[self.selected]) {
self.items[self.selected].focus();
//self.items[self.selected].focus();
self.select(self.selected);
}
});
}
@ -5778,8 +5788,6 @@ Listbar.prototype.__defineGetter__('selected', function() {
return this.leftBase + this.leftOffset;
});
Listbar.prototype.setOptions =
Listbar.prototype.setCommands =
Listbar.prototype.setItems = function(commands) {
if (Array.isArray(commands)) {
commands = commands.reduce(function(obj, text, i) {
@ -5795,6 +5803,8 @@ Listbar.prototype.setItems = function(commands) {
}
});
this.ritems = Object.keys(commands);
this.items.forEach(function(el) {
el.detach();
});
@ -5809,7 +5819,7 @@ Listbar.prototype.setItems = function(commands) {
var cmd = commands[name]
, title
, len
, button;
, item;
title = (cmd.prefix != null ? '{light-black-fg}'
+ cmd.prefix
@ -5819,7 +5829,7 @@ Listbar.prototype.setItems = function(commands) {
len = ((cmd.prefix != null ? cmd.prefix + ':' : '') + name).length;
button = new Button({
var options = {
parent: self,
top: 0,
left: drawn + 1,
@ -5831,17 +5841,35 @@ Listbar.prototype.setItems = function(commands) {
tags: true,
mouse: true,
style: merge({}, self.style.item)
//style: {}
};
if (self.screen.autoPadding) {
;
}
['bg', 'fg', 'bold', 'underline',
'blink', 'inverse', 'invisible'].forEach(function(name) {
options.style[name] = function() {
var attr = self.items[self.selected] === item
? self.style.selected[name]
: self.style.item[name];
if (typeof attr === 'function') attr = attr(item);
return attr;
};
});
self._[name] = button;
cmd.element = button;
self.items.push(button);
button._.cmd = cmd;
item = new Button(options);
self._[name] = item;
cmd.element = item;
self.items.push(item);
item._.cmd = cmd;
if (cmd.callback) {
button.on('press', cmd.callback);
item.on('press', cmd.callback);
if (cmd.keys) {
screen.key(cmd.keys, cmd.callback);
self.screen.key(cmd.keys, cmd.callback);
}
}
@ -5851,6 +5879,140 @@ Listbar.prototype.setItems = function(commands) {
this.select(0);
};
Listbar.prototype.setItems = function(commands) {
var self = this;
if (!Array.isArray(commands)) {
commands = Object.keys(commands).reduce(function(obj, key, i) {
var cmd = commands[key]
, cb;
if (typeof cmd === 'function') {
cb = cmd;
cmd = { callback: cb };
}
if (cmd.text == null) cmd.text = key;
if (cmd.prefix == null) cmd.prefix = ++i + '';
if (cmd.text == null && cmd.callback) {
cmd.text = cmd.callback.name;
}
obj.push(cmd);
return obj;
}, []);
}
this.items.forEach(function(el) {
el.detach();
});
this.items = [];
this.ritems = [];
this.commands = [];
commands.forEach(function(cmd) {
self.appendItem(cmd);
});
};
Listbar.prototype.appendItem = function(item, callback) {
var self = this
, prev = this.items[this.items.length - 1]
, drawn = prev ? prev.left + prev.width : 0
, cmd
, title
, len;
if (typeof item === 'object') {
cmd = item;
}
if (typeof item === 'string') {
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 };
}
title = (cmd.prefix != null ? '{light-black-fg}'
+ cmd.prefix
+ '{/light-black-fg}'
+ ':' : '')
+ cmd.text;
len = ((cmd.prefix != null ? cmd.prefix + ':' : '') + cmd.text).length;
var options = {
screen: this.screen,
top: 0,
left: drawn + 1,
height: 1,
content: title,
width: len + 2,
align: 'center',
autoFocus: false,
tags: true,
mouse: true,
style: merge({}, this.style.item)
};
if (this.screen.autoPadding) {
;
} else {
options.top += this.itop;
options.left += this.ileft;
}
['bg', 'fg', 'bold', 'underline',
'blink', 'inverse', 'invisible'].forEach(function(name) {
options.style[name] = function() {
var attr = self.items[self.selected] === el
? self.style.selected[name]
: self.style.item[name];
if (typeof attr === 'function') attr = attr(el);
return attr;
};
});
var el = new Button(options);
this._[cmd.text] = el;
cmd.element = el;
el._.cmd = cmd;
this.ritems.push(cmd.text);
this.items.push(el);
this.commands.push(cmd);
this.append(el);
if (cmd.callback) {
el.on('press', cmd.callback);
if (cmd.keys) {
this.screen.key(cmd.keys, cmd.callback);
}
}
if (this.items.length === 1) {
this.select(0);
}
if (0) 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.select(el);
self.screen.render();
});
}
};
Listbar.prototype.render = function() {
var self = this
, drawn = 0;
@ -5924,7 +6086,7 @@ Listbar.prototype.select = function(offset) {
}, null, 2));
*/
el.focus();
//el.focus();
};
Listbar.prototype.sel = function(i) {