* VerticalMenuView up/down working

This commit is contained in:
Bryan Ashby 2014-11-01 09:50:11 -06:00
parent edc8266310
commit 4f3a8ac500
3 changed files with 63 additions and 21 deletions

View File

@ -11,12 +11,11 @@ exports.MenuView = MenuView;
function MenuView(client, options) { function MenuView(client, options) {
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true); options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
var self = this; options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
View.call(this, client, options); View.call(this, client, options);
this.focusedItemIndex = 0; var self = this;
//// --- TESTING //// --- TESTING
options.items = [ 'Login', 'Apply', 'Logout' ]; options.items = [ 'Login', 'Apply', 'Logout' ];
@ -33,6 +32,9 @@ function MenuView(client, options) {
}); });
} }
this.focusedItemIndex = this.options.focusedItemIndex || 0;
this.focusedItemIndex = this.items.length >= this.focusedItemIndex ? this.focusedItemIndex : 0;
this.itemSpacing = this.options.itemSpacing || 1; this.itemSpacing = this.options.itemSpacing || 1;
this.itemSpacing = parseInt(this.itemSpacing, 10); this.itemSpacing = parseInt(this.itemSpacing, 10);

View File

@ -28,6 +28,35 @@ function VerticalMenuView(client, options) {
self.xPositionCacheExpired = false; self.xPositionCacheExpired = false;
} }
}; };
this.drawItem = function(index) {
assert(!this.xPositionCacheExpired);
var item = self.items[index];
if(!item) {
return;
}
self.client.term.write(ansi.goto(item.xPosition, self.position.y));
this.client.term.write(self.getANSIColor(
index === self.focusedItemIndex || item.selected ? self.getFocusColor() : self.getColor()));
var text = strUtil.stylizeString(item.text, item.hasFocus ? self.focusTextStyle : self.textStyle);
self.client.term.write(text); // :TODO: apply justify
};
this.moveSelection = function(fromIndex, toIndex) {
assert(!self.xPositionCacheExpired);
assert(fromIndex >= 0 && fromIndex <= self.items.length);
assert(toIndex >= 0 && toIndex <= self.items.length);
self.items[fromIndex].focused = false;
self.drawItem(fromIndex);
self.items[toIndex].focused = true;
self.focusedItemIndex = toIndex;
self.drawItem(toIndex);
};
} }
util.inherits(VerticalMenuView, MenuView); util.inherits(VerticalMenuView, MenuView);
@ -41,27 +70,11 @@ VerticalMenuView.prototype.setPosition = function(pos) {
VerticalMenuView.prototype.redraw = function() { VerticalMenuView.prototype.redraw = function() {
VerticalMenuView.super_.prototype.redraw.call(this); VerticalMenuView.super_.prototype.redraw.call(this);
var color = this.getColor();
var focusColor = this.getFocusColor();
console.log(focusColor);
var x = this.position.x;
var y = this.position.y;
var count = this.items.length;
var item;
var text;
this.cacheXPositions(); this.cacheXPositions();
var count = this.items.length;
for(var i = 0; i < count; ++i) { for(var i = 0; i < count; ++i) {
item = this.items[i]; this.drawItem(i);
this.client.term.write(ansi.goto(item.xPosition, y));
this.client.term.write(this.getANSIColor(i === this.focusedItemIndex || item.selected ? focusColor : color));
text = strUtil.stylizeString(item.text, item.hasFocus ? this.focusTextStyle : this.textStyle);
this.client.term.write(text); // :TODO: apply justify, and style
} }
}; };
@ -70,3 +83,28 @@ VerticalMenuView.prototype.setFocus = function(focused) {
this.redraw(); this.redraw();
}; };
VerticalMenuView.prototype.onSpecialKeyPress = function(keyName) {
var prevFocusedItemIndex = this.focusedItemIndex;
if(this.isSpecialKeyMapped('up', keyName)) {
if(0 === this.focusedItemIndex) {
this.focusedItemIndex = this.items.length - 1;
} else {
this.focusedItemIndex--;
}
} else if(this.isSpecialKeyMapped('down', keyName)) {
if(this.items.length - 1 === this.focusedItemIndex) {
this.focusedItemIndex = 0;
} else {
this.focusedItemIndex++;
}
}
if(prevFocusedItemIndex !== this.focusedItemIndex) {
this.moveSelection(prevFocusedItemIndex, this.focusedItemIndex);
}
VerticalMenuView.super_.prototype.onSpecialKeyPress.call(this, keyName);
};

View File

@ -15,6 +15,8 @@ var VIEW_SPECIAL_KEY_MAP_DEFAULT = {
backspace : [ 'backspace' ], backspace : [ 'backspace' ],
del : [ 'del' ], del : [ 'del' ],
next : [ 'tab' ], next : [ 'tab' ],
up : [ 'up arrow' ],
down : [ 'down arrow' ],
}; };
function View(client, options) { function View(client, options) {