2015-06-29 04:31:12 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var MenuView = require('./menu_view.js').MenuView;
|
|
|
|
var ansi = require('./ansi_term.js');
|
|
|
|
var strUtil = require('./string_util.js');
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
var assert = require('assert');
|
2015-07-01 05:09:58 +00:00
|
|
|
var _ = require('lodash');
|
2015-07-01 04:45:27 +00:00
|
|
|
|
|
|
|
exports.HorizontalMenuView = HorizontalMenuView;
|
|
|
|
|
|
|
|
function HorizontalMenuView(options) {
|
2015-06-29 04:31:12 +00:00
|
|
|
options.cursor = options.cursor || 'hide';
|
|
|
|
|
2015-07-01 05:09:58 +00:00
|
|
|
if(!_.isNumber(options.itemSpacing)) {
|
|
|
|
options.itemSpacing = 1;
|
|
|
|
}
|
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
MenuView.call(this, options);
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
this.dimens.height = 1; // always the case
|
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
var self = this;
|
2015-07-01 04:45:27 +00:00
|
|
|
|
|
|
|
this.getSpacer = function() {
|
2015-07-01 05:09:58 +00:00
|
|
|
return new Array(self.itemSpacing + 1).join(' ');
|
2015-07-01 04:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.performAutoScale = function() {
|
|
|
|
if(self.autoScale.width) {
|
|
|
|
var spacer = self.getSpacer();
|
|
|
|
var width = self.items.join(spacer).length + (spacer.length * 2);
|
|
|
|
assert(width <= self.client.term.termWidth - self.position.col);
|
|
|
|
self.dimens.width = width;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.performAutoScale();
|
|
|
|
|
|
|
|
this.cachePositions = function() {
|
|
|
|
if(this.positionCacheExpired) {
|
|
|
|
var col = self.position.col;
|
|
|
|
var spacer = self.getSpacer();
|
|
|
|
|
|
|
|
for(var i = 0; i < self.items.length; ++i) {
|
2015-07-01 22:43:19 +00:00
|
|
|
self.items[i].col = col;
|
2015-07-01 04:45:27 +00:00
|
|
|
col += spacer.length + self.items[i].length + spacer.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.positionCacheExpired = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.drawItem = function(index) {
|
2015-07-01 22:43:19 +00:00
|
|
|
assert(!this.positionCacheExpired);
|
2015-07-01 04:45:27 +00:00
|
|
|
|
|
|
|
var item = self.items[index];
|
|
|
|
if(!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);
|
|
|
|
|
|
|
|
var extraPad = self.getSpacer().length * 2;
|
2015-07-01 05:09:58 +00:00
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
self.client.term.write(
|
2015-07-01 22:43:19 +00:00
|
|
|
ansi.goto(self.position.row, self.items[index].col) +
|
2015-07-01 05:09:58 +00:00
|
|
|
(index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()) +
|
|
|
|
strUtil.pad(text, text.length + extraPad, self.fillChar, 'center')
|
|
|
|
);
|
2015-07-01 04:45:27 +00:00
|
|
|
};
|
2015-06-29 04:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(HorizontalMenuView, MenuView);
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
HorizontalMenuView.prototype.setHeight = function(height) {
|
|
|
|
height = parseInt(height, 10);
|
|
|
|
assert(1 === height); // nothing else allowed here
|
|
|
|
HorizontalMenuView.super_.prototype.setHeight(this, height);
|
|
|
|
};
|
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
HorizontalMenuView.prototype.redraw = function() {
|
|
|
|
HorizontalMenuView.super_.prototype.redraw.call(this);
|
2015-07-01 04:45:27 +00:00
|
|
|
|
|
|
|
this.cachePositions();
|
|
|
|
|
|
|
|
for(var i = 0; i < this.items.length; ++i) {
|
|
|
|
this.items[i].focused = this.focusedItemIndex === i;
|
|
|
|
this.drawItem(i);
|
|
|
|
}
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.setPosition = function(pos) {
|
|
|
|
HorizontalMenuView.super_.prototype.setPosition.call(this, pos);
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
this.positionCacheExpired = true;
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.setFocus = function(focused) {
|
|
|
|
HorizontalMenuView.super_.prototype.setFocus.call(this, focused);
|
|
|
|
|
|
|
|
this.redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.setItems = function(items) {
|
|
|
|
HorizontalMenuView.super_.prototype.setItems.call(this, items);
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
this.positionCacheExpired = true;
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|