2015-06-29 04:31:12 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-01-28 05:21:48 +00:00
|
|
|
const MenuView = require('./menu_view.js').MenuView;
|
|
|
|
const strUtil = require('./string_util.js');
|
|
|
|
const formatString = require('./string_format');
|
|
|
|
const { pipeToAnsi } = require('./color_codes.js');
|
|
|
|
const { goto } = require('./ansi_term.js');
|
2015-06-29 04:31:12 +00:00
|
|
|
|
2018-01-28 05:21:48 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const _ = 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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.cursor = options.cursor || 'hide';
|
|
|
|
|
|
|
|
if(!_.isNumber(options.itemSpacing)) {
|
|
|
|
options.itemSpacing = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuView.call(this, options);
|
|
|
|
|
|
|
|
this.dimens.height = 1; // always the case
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.getSpacer = function() {
|
|
|
|
return new Array(self.itemSpacing + 1).join(' ');
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
self.items[i].col = col;
|
|
|
|
col += spacer.length + self.items[i].text.length + spacer.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.positionCacheExpired = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.drawItem = function(index) {
|
|
|
|
assert(!this.positionCacheExpired);
|
|
|
|
|
|
|
|
const item = self.items[index];
|
|
|
|
if(!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let text;
|
|
|
|
let sgr;
|
|
|
|
if(item.focused && self.hasFocusItems()) {
|
|
|
|
const focusItem = self.focusItems[index];
|
|
|
|
text = focusItem ? focusItem.text : item.text;
|
|
|
|
sgr = '';
|
|
|
|
} else if(this.complexItems) {
|
|
|
|
text = pipeToAnsi(formatString(item.focused && this.focusItemFormat ? this.focusItemFormat : this.itemFormat, item));
|
|
|
|
sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
|
|
|
|
} else {
|
|
|
|
text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);
|
|
|
|
sgr = (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
|
|
|
|
}
|
|
|
|
|
|
|
|
const drawWidth = strUtil.renderStringLength(text) + (self.getSpacer().length * 2);
|
|
|
|
|
|
|
|
self.client.term.write(
|
|
|
|
`${goto(self.position.row, item.col)}${sgr}${strUtil.pad(text, drawWidth, self.fillChar, 'center')}`
|
|
|
|
);
|
|
|
|
};
|
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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
height = parseInt(height, 10);
|
|
|
|
assert(1 === height); // nothing else allowed here
|
|
|
|
HorizontalMenuView.super_.prototype.setHeight(this, height);
|
2015-07-01 04:45:27 +00:00
|
|
|
};
|
|
|
|
|
2015-06-29 04:31:12 +00:00
|
|
|
HorizontalMenuView.prototype.redraw = function() {
|
2018-06-22 05:15:04 +00:00
|
|
|
HorizontalMenuView.super_.prototype.redraw.call(this);
|
2015-07-01 04:45:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.cachePositions();
|
2015-07-01 04:45:27 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
HorizontalMenuView.super_.prototype.setPosition.call(this, pos);
|
2015-06-29 04:31:12 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.positionCacheExpired = true;
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.setFocus = function(focused) {
|
2018-06-22 05:15:04 +00:00
|
|
|
HorizontalMenuView.super_.prototype.setFocus.call(this, focused);
|
2015-06-29 04:31:12 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.redraw();
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.setItems = function(items) {
|
2018-06-22 05:15:04 +00:00
|
|
|
HorizontalMenuView.super_.prototype.setItems.call(this, items);
|
2015-06-29 04:31:12 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.positionCacheExpired = true;
|
2015-07-02 02:18:34 +00:00
|
|
|
};
|
|
|
|
|
2016-10-13 04:07:22 +00:00
|
|
|
HorizontalMenuView.prototype.focusNext = function() {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(this.items.length - 1 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = 0;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex++;
|
|
|
|
}
|
2016-10-13 04:07:22 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
// :TODO: Optimize this in cases where we only need to redraw two items. Always the case now, somtimes
|
|
|
|
this.redraw();
|
2016-10-13 04:07:22 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
HorizontalMenuView.super_.prototype.focusNext.call(this);
|
2016-10-13 04:07:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.focusPrevious = function() {
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(0 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = this.items.length - 1;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex--;
|
|
|
|
}
|
2016-10-13 04:07:22 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
// :TODO: Optimize this in cases where we only need to redraw two items. Always the case now, somtimes
|
|
|
|
this.redraw();
|
2016-10-13 04:07:22 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
HorizontalMenuView.super_.prototype.focusPrevious.call(this);
|
2016-10-13 04:07:22 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 02:18:34 +00:00
|
|
|
HorizontalMenuView.prototype.onKeyPress = function(ch, key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(key) {
|
|
|
|
if(this.isKeyMapped('left', key.name)) {
|
|
|
|
this.focusPrevious();
|
|
|
|
} else if(this.isKeyMapped('right', key.name)) {
|
|
|
|
this.focusNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HorizontalMenuView.super_.prototype.onKeyPress.call(this, ch, key);
|
2015-07-02 02:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HorizontalMenuView.prototype.getData = function() {
|
2018-06-22 05:15:04 +00:00
|
|
|
const item = this.getItem(this.focusedItemIndex);
|
|
|
|
return _.isString(item.data) ? item.data : this.focusedItemIndex;
|
2015-06-29 04:31:12 +00:00
|
|
|
};
|