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;
|
|
|
|
|
2015-07-02 02:18:34 +00:00
|
|
|
// :TODO: Update this to allow scrolling if number of items cannot fit in width (similar to VerticalMenuView)
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
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-12 02:12:07 +00:00
|
|
|
};
|
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-02 04:53:01 +00:00
|
|
|
col += spacer.length + self.items[i].text.length + spacer.length;
|
2015-07-01 04:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-02 04:53:01 +00:00
|
|
|
var text = strUtil.stylizeString(
|
|
|
|
item.text,
|
|
|
|
this.hasFocus && item.focused ? self.focusTextStyle : self.textStyle);
|
2015-07-01 04:45:27 +00:00
|
|
|
|
2015-07-02 04:53:01 +00:00
|
|
|
var drawWidth = text.length + self.getSpacer().length * 2; // * 2 = sides
|
2015-07-01 05:09:58 +00:00
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
self.client.term.write(
|
2015-07-02 04:53:01 +00:00
|
|
|
ansi.goto(self.position.row, item.col) +
|
2015-07-01 05:09:58 +00:00
|
|
|
(index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()) +
|
2015-07-02 04:53:01 +00:00
|
|
|
strUtil.pad(text, drawWidth, self.fillChar, 'center')
|
2015-07-01 05:09:58 +00:00
|
|
|
);
|
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-07-02 02:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.onKeyPress = function(ch, key) {
|
|
|
|
if(key) {
|
|
|
|
var prevFocusedItemIndex = this.focusedItemIndex;
|
|
|
|
|
|
|
|
if(this.isKeyMapped('left', key.name)) {
|
|
|
|
if(0 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = this.items.length - 1;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex--;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(this.isKeyMapped('right', key.name)) {
|
|
|
|
if(this.items.length - 1 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = 0;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(prevFocusedItemIndex !== this.focusedItemIndex) {
|
|
|
|
// :TODO: Optimize this in cases where we only need to redraw two items. Always the case now, somtimes
|
|
|
|
// if this is changed to allow scrolling
|
|
|
|
this.redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ch && this.hotKeys) {
|
|
|
|
var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch];
|
|
|
|
if(_.isNumber(keyIndex)) {
|
|
|
|
this.focusedItemIndex = keyIndex;
|
|
|
|
this.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HorizontalMenuView.super_.prototype.onKeyPress.call(this, ch, key);
|
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.getData = function() {
|
|
|
|
return this.focusedItemIndex;
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|