New menu sorting, fix up default SGR

This commit is contained in:
Bryan Ashby 2018-01-28 13:03:11 -07:00
parent b6317e0541
commit 999033ec15
4 changed files with 33 additions and 4 deletions

View File

@ -71,7 +71,7 @@ function HorizontalMenuView(options) {
text = focusItem ? focusItem.text : item.text; text = focusItem ? focusItem.text : item.text;
sgr = ''; sgr = '';
} else if(this.complexItems) { } else if(this.complexItems) {
text = pipeToAnsi(formatString(item.focused ? this.focusItemFormat : this.itemFormat, item)); text = pipeToAnsi(formatString(item.focused && this.focusItemFormat ? this.focusItemFormat : this.itemFormat, item));
sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()); sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
} else { } else {
text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle); text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);

View File

@ -64,6 +64,8 @@ util.inherits(MenuView, View);
MenuView.prototype.setItems = function(items) { MenuView.prototype.setItems = function(items) {
if(Array.isArray(items)) { if(Array.isArray(items)) {
this.sorted = false;
// //
// Items can be an array of strings or an array of objects. // Items can be an array of strings or an array of objects.
// //
@ -92,12 +94,37 @@ MenuView.prototype.setItems = function(items) {
if(this.complexItems) { if(this.complexItems) {
this.itemFormat = this.itemFormat || '{text}'; this.itemFormat = this.itemFormat || '{text}';
this.focusItemFormat = this.focusItemFormat || this.itemFormat;
} }
} }
}; };
MenuView.prototype.setSort = function(sort) {
if(this.sorted || !Array.isArray(this.items) || 0 === this.items.length) {
return;
}
const key = true === sort ? 'text' : sort;
if('text' !== sort && !this.complexItems) {
return; // need a valid sort key
}
this.items.sort( (a, b) => {
const a1 = a[key];
const b1 = b[key];
if(!a1) {
return -1;
}
if(!b1) {
return 1;
}
return a1.localeCompare( b1, { sensitivity : false, numeric : true } );
});
this.sorted = true;
};
MenuView.prototype.removeItem = function(index) { MenuView.prototype.removeItem = function(index) {
this.sorted = false;
this.items.splice(index, 1); this.items.splice(index, 1);
if(this.focusItems) { if(this.focusItems) {
@ -203,6 +230,8 @@ MenuView.prototype.setPropertyValue = function(propName, value) {
case 'focusItemFormat' : case 'focusItemFormat' :
this[propName] = value; this[propName] = value;
break; break;
case 'sort' : this.setSort(value); break;
} }
MenuView.super_.prototype.setPropertyValue.call(this, propName, value); MenuView.super_.prototype.setPropertyValue.call(this, propName, value);

View File

@ -71,7 +71,7 @@ function VerticalMenuView(options) {
text = focusItem ? focusItem.text : item.text; text = focusItem ? focusItem.text : item.text;
sgr = ''; sgr = '';
} else if(this.complexItems) { } else if(this.complexItems) {
text = pipeToAnsi(formatString(item.focused ? this.focusItemFormat : this.itemFormat, item)); text = pipeToAnsi(formatString(item.focused && this.focusItemFormat ? this.focusItemFormat : this.itemFormat, item));
sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()); sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
} else { } else {
text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle); text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);