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;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function VerticalMenuView(options) {
|
|
|
|
options.cursor = options.cursor || 'hide';
|
|
|
|
|
|
|
|
MenuView.call(this, options);
|
2014-10-31 22:25:11 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
this.itemSpacing = 3;
|
|
|
|
|
2014-11-02 19:07:17 +00:00
|
|
|
this.calculateDimens = function() {
|
|
|
|
if(!self.dimens || !self.dimens.width) {
|
|
|
|
var l = 0;
|
|
|
|
self.items.forEach(function onItem(item) {
|
|
|
|
if(item.text.length > l) {
|
|
|
|
l = item.text.length;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.dimens = self.dimens || {};
|
|
|
|
self.dimens.width = l;
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
if(this.items.length > 0) {
|
|
|
|
this.dimens.height = (self.items.length * self.itemSpacing) - (self.itemSpacing - 1);
|
|
|
|
} else {
|
|
|
|
this.dimens.height = 0;
|
2014-11-02 19:07:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.calculateDimens();
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
this.cachePositions = function() {
|
|
|
|
if(self.positionCacheExpired) {
|
2014-10-31 22:25:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-11-03 23:49:15 +00:00
|
|
|
self.positionCacheExpired = false;
|
2014-10-31 22:25:11 +00:00
|
|
|
}
|
|
|
|
};
|
2014-11-01 15:50:11 +00:00
|
|
|
|
|
|
|
this.drawItem = function(index) {
|
2014-11-03 23:49:15 +00:00
|
|
|
assert(!this.positionCacheExpired);
|
2014-11-01 15:50:11 +00:00
|
|
|
|
|
|
|
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()));
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);
|
2014-11-02 19:07:17 +00:00
|
|
|
|
|
|
|
self.client.term.write(
|
|
|
|
strUtil.pad(text, this.dimens.width, this.fillChar, this.justify));
|
2014-11-01 15:50:11 +00:00
|
|
|
};
|
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);
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
this.positionCacheExpired = true;
|
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-11-02 19:07:17 +00:00
|
|
|
};
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
VerticalMenuView.prototype.getData = function() {
|
2014-11-02 19:07:17 +00:00
|
|
|
return this.focusedItemIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
VerticalMenuView.prototype.setItems = function(items) {
|
|
|
|
VerticalMenuView.super_.prototype.setItems.call(this, items);
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
this.positionCacheExpired = true;
|
|
|
|
this.cachePositions();
|
2014-11-02 19:07:17 +00:00
|
|
|
this.calculateDimens();
|
2014-10-31 04:59:21 +00:00
|
|
|
};
|