* Fix some handling of height, itemSpacing, etc. in VerticalMenuView
* Change auto scale to be height/width dependent
This commit is contained in:
parent
7fc4858959
commit
37adeb5f90
|
@ -119,7 +119,7 @@ function pad(s, len, padChar, dir, stringSGR, padSGR) {
|
|||
stringSGR = miscUtil.valueWithDefault(stringSGR, '');
|
||||
padSGR = miscUtil.valueWithDefault(padSGR, '');
|
||||
|
||||
var padlen = len - s.length;
|
||||
var padlen = len >= s.length ? len - s.length : 0;
|
||||
|
||||
switch(dir) {
|
||||
case 'L' :
|
||||
|
|
|
@ -14,60 +14,32 @@ exports.VerticalMenuView = VerticalMenuView;
|
|||
|
||||
function VerticalMenuView(options) {
|
||||
options.cursor = options.cursor || 'hide';
|
||||
options.justify = options.justify || 'center';
|
||||
options.justify = options.justify || 'right';
|
||||
|
||||
MenuView.call(this, options);
|
||||
|
||||
var self = this;
|
||||
|
||||
this.performAutoScale = function() {
|
||||
if(this.autoScale) {
|
||||
if(this.autoScale.height) {
|
||||
this.dimens.height = (self.items.length * (self.itemSpacing + 1)) - (self.itemSpacing);
|
||||
this.dimens.height = Math.min(this.dimens.height, self.client.term.termHeight - self.position.x);
|
||||
}
|
||||
|
||||
if(this.autoScale.width) {
|
||||
var l = 0;
|
||||
self.items.forEach(function item(i) {
|
||||
if(i.text.length > l) {
|
||||
l = Math.min(i.text.length, self.client.term.termWidth - self.position.y);
|
||||
}
|
||||
});
|
||||
self.dimens.width = l;
|
||||
self.dimens.width = l + 1;
|
||||
}
|
||||
};
|
||||
|
||||
this.performAutoScale();
|
||||
|
||||
this.cachePositions = function() {
|
||||
if(self.positionCacheExpired) {
|
||||
var count = this.items.length;
|
||||
var x = self.position.x;
|
||||
for(var i = 0; i < count; ++i) {
|
||||
if(i > 0) {
|
||||
x += self.itemSpacing + 1;
|
||||
}
|
||||
|
||||
self.items[i].xPosition = x;
|
||||
}
|
||||
self.positionCacheExpired = false;
|
||||
}
|
||||
};
|
||||
|
||||
this.changeSelection = function(fromIndex, toIndex) {
|
||||
assert(!self.positionCacheExpired);
|
||||
assert(fromIndex >= 0 && fromIndex <= self.items.length);
|
||||
assert(toIndex >= 0 && toIndex <= self.items.length);
|
||||
|
||||
self.items[fromIndex].focused = false;
|
||||
self.drawItem(fromIndex);
|
||||
|
||||
self.items[toIndex].focused = true;
|
||||
self.focusedItemIndex = toIndex;
|
||||
self.drawItem(toIndex);
|
||||
};
|
||||
|
||||
this.drawItem = function(index) {
|
||||
assert(self.positionCacheExpired === false);
|
||||
|
||||
var item = self.items[index];
|
||||
if(!item) {
|
||||
return;
|
||||
|
@ -161,11 +133,9 @@ VerticalMenuView.prototype.getData = function() {
|
|||
VerticalMenuView.prototype.setItems = function(items) {
|
||||
VerticalMenuView.super_.prototype.setItems.call(this, items);
|
||||
|
||||
this.positionCacheExpired = true;
|
||||
this.cachePositions();
|
||||
this.performAutoScale();
|
||||
|
||||
this.maxVisibleItems = this.dimens.height / (this.itemSpacing + 1);
|
||||
this.maxVisibleItems = Math.ceil(this.dimens.height / (this.itemSpacing + 1));
|
||||
|
||||
this.viewWindow = {
|
||||
top : this.focusedItemIndex,
|
||||
|
|
16
core/view.js
16
core/view.js
|
@ -55,11 +55,15 @@ function View(options) {
|
|||
this.setPosition(options.position);
|
||||
}
|
||||
|
||||
this.autoScale = _.isBoolean(options.autoScale) ? options.autoScale : true;
|
||||
if(_.isObject(options.autoScale)) {
|
||||
this.autoScale = options.autoScale;
|
||||
} else {
|
||||
this.autoScale = { height : true, width : true };
|
||||
}
|
||||
|
||||
if(options.dimens) {
|
||||
this.setDimension(options.dimens);
|
||||
this.autoScale = false; // no auto scaling dimens supplied
|
||||
this.autoScale = { height : false, width : false };
|
||||
} else {
|
||||
this.dimens = { width : 0, height : 0 };
|
||||
}
|
||||
|
@ -137,15 +141,17 @@ View.prototype.setDimension = function(dimens) {
|
|||
assert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width));
|
||||
|
||||
this.dimens = dimens;
|
||||
this.autoScale = false;
|
||||
this.autoScale = { height : false, width : false };
|
||||
};
|
||||
|
||||
View.prototype.setHeight = function(height) {
|
||||
this.setDimension( { height : height, width : this.dimens.width } );
|
||||
this.dimens.height = height;
|
||||
this.autoScale.height = false;
|
||||
};
|
||||
|
||||
View.prototype.setWidth = function(width) {
|
||||
this.setDimension( { height : this.dimens.height, width : width } );
|
||||
this.dimens.width = width;
|
||||
this.autoScale.width = false;
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
|
@ -233,8 +233,7 @@
|
|||
"Some More Stuff",
|
||||
"Other"
|
||||
],
|
||||
"height" : 8,
|
||||
"width" : 33,
|
||||
"height" : 10,
|
||||
"itemSpacing" : 1,
|
||||
// :TODO: justify not working??
|
||||
"focusTextStyle" : "small i"
|
||||
|
|
Loading…
Reference in New Issue