2014-10-31 04:59:21 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var MenuView = require('./menu_view.js').MenuView;
|
|
|
|
var ansi = require('./ansi_term.js');
|
2014-10-31 22:25:11 +00:00
|
|
|
var strUtil = require('./string_util.js');
|
2014-10-31 04:59:21 +00:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2014-10-31 22:25:11 +00:00
|
|
|
exports.VerticalMenuView = VerticalMenuView;
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
function VerticalMenuView(client, options) {
|
|
|
|
MenuView.call(this, client, options);
|
2014-10-31 22:25:11 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.cacheXPositions = function() {
|
|
|
|
if(self.xPositionCacheExpired) {
|
|
|
|
var count = this.items.length;
|
|
|
|
var x = self.position.x;
|
|
|
|
for(var i = 0; i < count; ++i) {
|
|
|
|
if(i > 0) {
|
|
|
|
x += self.itemSpacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.items[i].xPosition = x;
|
|
|
|
}
|
|
|
|
self.xPositionCacheExpired = false;
|
|
|
|
}
|
|
|
|
};
|
2014-11-01 15:50:11 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(VerticalMenuView, MenuView);
|
|
|
|
|
2014-10-31 22:25:11 +00:00
|
|
|
VerticalMenuView.prototype.setPosition = function(pos) {
|
|
|
|
VerticalMenuView.super_.prototype.setPosition.call(this, pos);
|
|
|
|
|
|
|
|
this.xPositionCacheExpired = true;
|
|
|
|
};
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
VerticalMenuView.prototype.redraw = function() {
|
|
|
|
VerticalMenuView.super_.prototype.redraw.call(this);
|
2014-10-31 22:25:11 +00:00
|
|
|
|
|
|
|
this.cacheXPositions();
|
|
|
|
|
2014-11-01 15:50:11 +00:00
|
|
|
var count = this.items.length;
|
2014-10-31 22:25:11 +00:00
|
|
|
for(var i = 0; i < count; ++i) {
|
2014-11-01 15:50:11 +00:00
|
|
|
this.drawItem(i);
|
2014-10-31 22:25:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
VerticalMenuView.prototype.setFocus = function(focused) {
|
|
|
|
VerticalMenuView.super_.prototype.setFocus.call(this, focused);
|
|
|
|
|
|
|
|
this.redraw();
|
2014-11-01 15:50:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2014-10-31 04:59:21 +00:00
|
|
|
};
|