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');
|
2015-05-04 21:39:48 +00:00
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
2015-05-05 04:04:36 +00:00
|
|
|
var _ = require('lodash');
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2014-10-31 22:25:11 +00:00
|
|
|
exports.VerticalMenuView = VerticalMenuView;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function VerticalMenuView(options) {
|
2015-05-07 03:19:24 +00:00
|
|
|
options.cursor = options.cursor || 'hide';
|
2015-06-29 04:31:12 +00:00
|
|
|
options.justify = options.justify || 'right'; // :TODO: default to center
|
2015-04-09 04:54:13 +00:00
|
|
|
|
|
|
|
MenuView.call(this, options);
|
2014-10-31 22:25:11 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-05-07 03:19:24 +00:00
|
|
|
this.performAutoScale = function() {
|
2015-05-07 22:14:16 +00:00
|
|
|
if(this.autoScale.height) {
|
2015-05-06 04:19:21 +00:00
|
|
|
this.dimens.height = (self.items.length * (self.itemSpacing + 1)) - (self.itemSpacing);
|
2015-06-30 05:14:17 +00:00
|
|
|
this.dimens.height = Math.min(self.dimens.height, self.client.term.termHeight - self.position.row);
|
2015-05-07 22:14:16 +00:00
|
|
|
}
|
2015-05-05 04:04:36 +00:00
|
|
|
|
2015-05-07 22:14:16 +00:00
|
|
|
if(this.autoScale.width) {
|
2015-05-05 04:04:36 +00:00
|
|
|
var l = 0;
|
|
|
|
self.items.forEach(function item(i) {
|
|
|
|
if(i.text.length > l) {
|
2015-05-18 17:31:35 +00:00
|
|
|
l = Math.min(i.text.length, self.client.term.termWidth - self.position.col);
|
2015-05-05 04:04:36 +00:00
|
|
|
}
|
|
|
|
});
|
2015-05-07 22:14:16 +00:00
|
|
|
self.dimens.width = l + 1;
|
2015-05-05 04:04:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-07 03:19:24 +00:00
|
|
|
this.performAutoScale();
|
2014-11-02 19:07:17 +00:00
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
this.updateViewVisibleItems = function() {
|
|
|
|
self.maxVisibleItems = Math.ceil(self.dimens.height / (self.itemSpacing + 1));
|
|
|
|
|
|
|
|
self.viewWindow = {
|
|
|
|
top : self.focusedItemIndex,
|
|
|
|
bottom : Math.min(self.focusedItemIndex + self.maxVisibleItems, self.items.length) - 1
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-11-01 15:50:11 +00:00
|
|
|
this.drawItem = function(index) {
|
|
|
|
var item = self.items[index];
|
|
|
|
if(!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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(
|
2015-07-06 01:05:55 +00:00
|
|
|
ansi.goto(item.row, self.position.col) +
|
|
|
|
(index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()) +
|
|
|
|
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);
|
|
|
|
|
2015-04-27 03:57:23 +00:00
|
|
|
VerticalMenuView.prototype.redraw = function() {
|
2015-05-06 04:19:21 +00:00
|
|
|
VerticalMenuView.super_.prototype.redraw.call(this);
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
// :TODO: rename positionCacheExpired to something that makese sense; combine methods for such
|
2015-06-30 05:14:17 +00:00
|
|
|
if(this.positionCacheExpired) {
|
|
|
|
this.performAutoScale();
|
|
|
|
this.updateViewVisibleItems();
|
|
|
|
|
|
|
|
this.positionCacheExpired = false;
|
|
|
|
}
|
|
|
|
|
2015-05-20 19:24:39 +00:00
|
|
|
var row = this.position.row;
|
2015-05-07 03:19:24 +00:00
|
|
|
for(var i = this.viewWindow.top; i <= this.viewWindow.bottom; ++i) {
|
2015-05-20 19:24:39 +00:00
|
|
|
this.items[i].row = row;
|
|
|
|
row += this.itemSpacing + 1;
|
2015-05-06 04:19:21 +00:00
|
|
|
this.items[i].focused = this.focusedItemIndex === i;
|
|
|
|
this.drawItem(i);
|
|
|
|
}
|
2015-04-27 03:57:23 +00:00
|
|
|
};
|
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
VerticalMenuView.prototype.setHeight = function(height) {
|
|
|
|
VerticalMenuView.super_.prototype.setHeight.call(this, height);
|
|
|
|
|
|
|
|
this.positionCacheExpired = true;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
VerticalMenuView.prototype.onKeyPress = function(ch, key) {
|
|
|
|
|
|
|
|
if(key) {
|
|
|
|
var prevFocusedItemIndex = this.focusedItemIndex;
|
|
|
|
|
2015-07-02 02:18:34 +00:00
|
|
|
if(this.isKeyMapped('up', key.name)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
if(0 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = this.items.length - 1;
|
|
|
|
|
|
|
|
this.viewWindow = {
|
|
|
|
top : this.items.length - this.maxVisibleItems,
|
|
|
|
bottom : this.items.length - 1
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex--;
|
|
|
|
|
|
|
|
if(this.focusedItemIndex < this.viewWindow.top) {
|
|
|
|
this.viewWindow.top--;
|
|
|
|
this.viewWindow.bottom--;
|
|
|
|
}
|
2015-05-07 03:19:24 +00:00
|
|
|
}
|
2015-07-02 02:18:34 +00:00
|
|
|
} else if(this.isKeyMapped('down', key.name)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
if(this.items.length - 1 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = 0;
|
|
|
|
|
|
|
|
this.viewWindow = {
|
|
|
|
top : 0,
|
|
|
|
bottom : Math.min(this.focusedItemIndex + this.maxVisibleItems, this.items.length) - 1
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex++;
|
|
|
|
|
|
|
|
if(this.focusedItemIndex > this.viewWindow.bottom) {
|
|
|
|
this.viewWindow.top++;
|
|
|
|
this.viewWindow.bottom++;
|
|
|
|
}
|
2015-05-07 03:19:24 +00:00
|
|
|
}
|
2014-11-01 15:50:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
if(prevFocusedItemIndex !== this.focusedItemIndex) {
|
2015-07-02 02:18:34 +00:00
|
|
|
// :TODO: Optimize this for cases where no scrolling occured & only two items need updated
|
2015-06-05 22:20:26 +00:00
|
|
|
this.redraw();
|
|
|
|
}
|
2014-11-01 15:50:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
VerticalMenuView.super_.prototype.onKeyPress.call(this, ch, key);
|
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);
|
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
this.positionCacheExpired = true;
|
|
|
|
};
|
2015-05-06 04:19:21 +00:00
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
VerticalMenuView.prototype.setItemSpacing = function(itemSpacing) {
|
|
|
|
VerticalMenuView.super_.prototype.setItemSpacing.call(this, itemSpacing);
|
2015-05-06 22:43:49 +00:00
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
this.positionCacheExpired = true;
|
2014-10-31 04:59:21 +00:00
|
|
|
};
|